<?php declare(strict_types=1); namespace DebugMessages { # add suffix 'new' for development version if (!defined('PmWiki')) exit(); // Debug function /* Use the dmsg function as follows if (!function_exists('dmsg')) { function dmsg (string $smsgprefix, $smsgdata) { # local instance global $MessagesFmt; $MessagesFmt['functionName'] [] = $smsgprefix . ': ' . \PHSC (strval($smsgdata)); } } */ // This is the Debug message function const DMSGNAME = 'dmsg'; if (substr(__NAMESPACE__, -3) === 'new') { $DMsgNew = 'new'; # empty string for production version, 'new' for development version } const NL = "\n"; const BR = '<br/>' . NL; # set debug flag \SDV($DebugMessagesDebug, false); # set default debug setting if not defined in an configuration file $debugmessages_debugon = boolval ($DebugMessagesDebug); # if on generates a test page directive # Version date $RecipeInfo[DMSGNAME]['Version'] = '2024-01-14' . $DMsgNew; # PmWiki version numbering is done by date # recipe version page variable $FmtPV['$dmsgVersion'] = "'" . __NAMESPACE__ . " version {$RecipeInfo[DMSGNAME]['Version']}'"; // return version as a custom page variable \SDV($HTMLStylesFmt[__NAMESPACE__], NL . '.dmsgprefix {display:inline-block; font-style:italic; min-width:6rem;}' . NL . '.dmsgpre {font-size:smaller;}' . NL); // # Markup is an internal PmWiki function that defines the custom markup for the wiki (see https://www.pmwiki.org/wiki/PmWiki/CustomMarkup) # (:gpxstat optional by syntacically defined parameters:) $MarkupDirectiveFunctions['debugmessages'] = __NAMESPACE__ . '\\DebugMessages_Parse'; // return; # completed setting up the recipe # see https://www.pmwiki.org/wiki/Cookbook/DebuggingForCookbookAuthors ## function DebugMessages_Parse ($pagename, $directive, $args, $content = '') { global $RecipeInfo; $emptyVariable; # unassigned $someHtml = '<strong>some HTML</strong><br><span style="color:blue;">blue text HTML</span>'; $associativeArray = array( "Hua" => array("Ä€poro", "Maika", "Huakiwi"), "Huawhenua" => array("RÄ«wai", "KÄnga", "PÅ«hÄ", "KÅ«mara")); dmsg('<hr>' . __FILE__, $RecipeInfo[DMSGNAME]['Version']); dmsg ('null', null); dmsg ('empty', $emptyVariable); dmsg ('boolean', false); dmsg ('number', 123.45678); dmsg ('string', "strÄ«ng <tag> ♦ \\\\, \\n, ~`@#$%^&*()_-+={}[]|;:<>,.?/"); dmsg ('array', [[1, 2], ['a', 'b', ["#", '*']]]); dmsg ('associative array', $associativeArray); dmsg ('html', $someHtml, true); return ('Debug messages created, use [[Cookbook:MessagesReplacement | [@(:messages:)@]]] to view. '); } ## function sanitise(&$item, $itemkey) { # escape html special characters if (is_string($item)) $item = \PHSC($item, ENT_NOQUOTES); return; } # end sanitise } # end namespace # namespace { // create as a global function outside a namespace // (it's too hard to make it available from with a namespace) function dmsg (string $smsgprefix, $smsgdata, bool $smshtml = false, string $dmsgId = '') { # add debug text to message array from caller global $MessagesFmt; # output array used by PmWiki if (empty ($dmsgId)) { $dmsgKey = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function']; # provide a key if ($dmsgKey == 'include_once') { # not interested in 'include_once' $dmsgKey = basename (debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0]['file'], ''); # leave extension on } else { $dmsgKey .= '()'; } # end if } else {$dmsgKey = $dmsgId;} # end if empty $msgFmt .= '<span class="dmsgprefix">' . $smsgprefix . '</span>: '; switch (true) { case ($smshtml) : # message already formatted as HTML $msgFmt .= $smsgdata; break; case (is_null($smsgdata)) : $msgFmt .= 'null'; break; case (is_bool($smsgdata)) : $msgFmt .= var_export($smsgdata,true); break; case (is_array($smsgdata)) : $msgFmt .= 'ak: ' . implode(', ', array_keys($smsgdata)) . NL; array_walk_recursive ($smsgdata, 'DebugMessages\\sanitise'); $msgFmt .= '<pre class="dmsgpre">' . NL . print_r($smsgdata, true) . '</pre>' . NL; break; case (is_string ($smsgdata)): $msgFmt .= "'" . \PHSC($smsgdata, ENT_NOQUOTES) . "'"; break; default: $msgFmt .= '"' . \PHSC(strval ($smsgdata)) . '"'; break; } # end switch $MessagesFmt[$dmsgKey] [] = $msgFmt; return $msgFmt; # also return HTML in case caller wants to use it } # end dmsg } # end namespace