NacoWiki PHP API

Util
in package

Utility methods

Class providing utility methods that are not strictly tied to NacoWiki

Table of Contents

Constants

VALID_CHARS  = '-A-Za-z0-9_\/\.'
Set of valid characters

Properties

$cache  : array<string|int, string>
content cache
$logmsg  : array<string|int, string>
used to keep logged strings

Methods

defaultMeta()  : array<string|int, mixed>|null
Generate default meta-data from file system
dumpLog()  : void
dump log messages
fileContents()  : string|null
cached file_get_contents
fileMeta()  : array<string|int, mixed>|null
Generat file-system level meta data
is_program_in_path()  : bool
Check if executable is in path
log()  : void
log message
recurse_copy()  : void
Copy files recursively
runPath()  : string
resolves a file or URL path
sanitize()  : string
make sure input path is sane
sendFile()  : void
Send $file_path
stackTrace()  : string
Return a simplified stack trace
vdump()  : string
var_dump to string
walkTree()  : array<string|int, mixed>
get directory contents

Constants

VALID_CHARS

Set of valid characters

public string VALID_CHARS = '-A-Za-z0-9_\/\.'

Follows POSIX portable filename character set.

If changing this, try to keep valid chars compatible with Windows. (See: https://stackoverflow.com/questions/1976007/what-characters-are-forbidden-in-windows-and-linux-directory-names)

Also one must reserve ';' to use for alternative streams for example:

.prop;page.md

Properties

$cache

content cache

public static array<string|int, string> $cache = ['content' => []]

Caches file contents

$logmsg

used to keep logged strings

public static array<string|int, string> $logmsg = []

Methods

defaultMeta()

Generate default meta-data from file system

public static defaultMeta(string $fn[, mixed $mtime = NULL ]) : array<string|int, mixed>|null

Create default metadata from filesystem meta-data containing:

  • title - based on the filename
  • date - YYYY-MM-DD
Parameters
$fn : string

name of file to use

$mtime : mixed = NULL

if provided, it will not get timestamps from the file system.

Return values
array<string|int, mixed>|null

contains default metadata or NULL on error

dumpLog()

dump log messages

public static dumpLog([bool $hr = false ]) : void

Output HTML with the contents of the message log

Parameters
$hr : bool = false

if true, a <hr> will be shown first.

fileContents()

cached file_get_contents

public static fileContents(string $fn) : string|null

It wraps file_get_contents with a simple cache.

Parameters
$fn : string

name of file to read

Return values
string|null

file context or NULL on error.

fileMeta()

Generat file-system level meta data

public static fileMeta(string $fn[, mixed $mtime = NULL ]) : array<string|int, mixed>|null

Will create file level metadata containing:

  • datetime - YYYY-MM-DD HR:MN:SC
  • year - 4 digit number
  • mtime - time stamp in Linux Epoch.
Parameters
$fn : string

name of file to use

$mtime : mixed = NULL

if provided, it will not get timestamps from the file system.

Return values
array<string|int, mixed>|null

contains file-system related metadata, NULL on error.

is_program_in_path()

Check if executable is in path

public static is_program_in_path(string $program) : bool

This function checks if a given program/command is in the Linux executable path

Parameters
$program : string

: program to check for

Tags
todo

Eventhough it uses PATH_SEPARATOR this is still very much Linux specific!

Return values
bool

: true if found, false if it does not exist

log()

log message

public static log([string $msg = '' ]) : void

Writes the message to stderr and also saves it to the logmsg static property. The log messages can then be retrieved later with dumpLog.

Log entries are tagged with the file and line location of calling scope.

Parameters
$msg : string = ''

text to log

recurse_copy()

Copy files recursively

public static recurse_copy(string $src, string $dst) : void

From copy doc in php.net

Parameters
$src : string

: source directory

$dst : string

: target directory

runPath()

resolves a file or URL path

public static runPath(string $path[, bool $nodots = false ]) : string

It will examine the $path and resolves . and .. directory entries.

Optionally, will remove path components that start with . (dot).

Resolved path will never contain any . or .. path components, so, resulting paths will never go up outside the current directory tree.

Parameters
$path : string

file path to resulve

$nodots : bool = false

defaults to false, if true path componets that start with . (dot) will be removed.

Return values
string

resolved path

sanitize()

make sure input path is sane

public static sanitize(string $url[, string $rdoc = '' ]) : string

Will use runPath and VALID_CHARS to make sure that the given $url does not contain invalid characters (as defined by VALID_CHARS and does not contain . or .. path components.

Parameters
$url : string

URL to sanitize

$rdoc : string = ''

optional realtive doc to use when sanitizing relative paths

Return values
string

sanitized url path

sendFile()

Send $file_path

public static sendFile(string $file_path[, string $mime = NULL ]) : void

Sends the given file. It supports byte ranges for download resume.

Parameters
$file_path : string

path of the file to send

$mime : string = NULL

content-type header

stackTrace()

Return a simplified stack trace

public static stackTrace() : string
Return values
string

dumped strack trace suitable for Util::log()

vdump()

var_dump to string

public static vdump(mixed $val) : string

Returns a string representation of the given value

Parameters
$val : mixed

value to dump

Return values
string

dumped value

walkTree()

get directory contents

public static walkTree(string $basedir[, bool $hide_dots = true ][, bool $vacuum = false ]) : array<string|int, mixed>

Create a list of files and folders in the $basedir directory.

Parameters
$basedir : string

directory to read.

$hide_dots : bool = true

if True, files beginning with dot(.) or comma(,) are hidden. Pass $false to include these files.

$vacuum : bool = false

if True, will delete dangling metadata files...

Return values
array<string|int, mixed>

[ $array-of-dirs, $array-of-files ]


        
On this page

Search results