My Web host, 1and1, is running PHP Version 7.2.0RC6. I've updated pmwiki.php to cause fewer "deprecated" warnings about Cookbook recipes, and to provide some diagnostic information. Please see the attached patch "pmwiki-cg.diff".

The first hunk defines two new functions:

1. PCF($args, $code) is a replacement for PHP's create_function(). It creates a lambda function. When enabled by setting $EnablePCF to a true value in a config file, it uses PHP eval() to create the function. If not enabled, it uses create_function().

2. PCFWarn() is a utility function used by PCF and the modified PCCF. When enabled by setting $EnablePCFWarn to a true value in a config file, it adds an entry to the $MessagesFmt array describing the function which was called (Markup_e, PPRE, PCCF or PCF), the name of the file containing the call, the line number in the file and whether a function had to be created.

It also modifies PCCF to call PCF instead of create_function and to call PCFWarn if it gets the lambda from its cache array, $CallbackFunctions.


The second hunk modifies the Markup and Markup_e functions so that, if enabled by setting $EnableMarkupDiag to a true value in a config file, the caller's file name, line number and pattern are saved in the markup table for every call, not just those using the deprecated "e" flag. This makes the ruleset and ruletable actions very instructive.


To see the result of the first hunk, see the page footer at:

* http://p72b.codingmaniac.com/pmwikitestsvn01/apcu/index.php?n=ChuckG.GroupPageListsByNameMe

(all one line).

To see the result of the second hunk, see:

* http://p72b.codingmaniac.com/pmwikitestsvn01/apcu/index.php?n=ChuckG.ChuckG&action=ruletable&rtdiag=1&columns=cmd,pat,rep


Chuck G

--- pmwiki-2.2.106/pmwiki.php   2017-11-08 10:39:13.000000000 -0600
+++ pmwiki-2.2.106/pmwiki-cg.php        2017-12-01 20:21:21.166334652 -0600
@@ -465,29 +465,73 @@
     $z['#'][] = $v;
   }
   return $z;
 }
 function PHSC($x, $flags=ENT_COMPAT, $enc=null, $dbl_enc=true) { # for PHP 5.4
   if (is_null($enc)) $enc = "ISO-8859-1"; # single-byte charset
   if (! is_array($x)) return @htmlspecialchars($x, $flags, $enc, $dbl_enc);
   foreach($x as $k=>$v) $x[$k] = PHSC($v, $flags, $enc, $dbl_enc);
   return $x;
 }
+function PCF($args, $code) {
+  global $EnablePCF;
+  global $MessagesFmt;
+  if (IsEnabled($EnablePCF, 0)) {
+    static $plambdaCnt = 0;
+    $fn = "plambda_" . ++$plambdaCnt;
+    eval ("function {$fn} ({$args}) {{$code}}");
+  } else {
+    $fn = create_function($args, $code);
+   #$msg = "PCF:  create_function args='{$args}', code='{$code}'.";
+   #$MessagesFmt[] = "<div class='wikimessage'>{$msg}</div>";
+  }
+  PCFWarn(' (function created)');
+  return $fn;
+}
+function PCFWarn($sfx = '') {
+  global $EnablePCFWarn, $PCFRootPat;
+  global $MessagesFmt;
+  if (IsEnabled($EnablePCFWarn, 0)) {
+    if (function_exists('debug_backtrace')) {
+      $dbg = debug_backtrace();
+      for ($i=1, $ilen=count($dbg); $i<$ilen; $i++) {
+        $func = $dbg[$i]['function'];
+        if (! ($func == 'PCF' || $func == 'PCCF' || $func == 'PPRE' || $func 
== 'Markup_e')) {
+          break;
+        }
+      }
+      $dbginfo = $dbg[$i-1];
+      $file = $dbginfo['file'];
+      if (isset($PCFRootPat)) {
+        $file = preg_replace($PCFRootPat, '$1', $file);
+      }
+      $msgkey = "PCF_" . $dbginfo['function'] . '_' . $file . "_" . 
$dbginfo['line'];
+      if (! isset($MessagesFmt[$msgkey])) {
+        $msg = "PCF: {$dbginfo['function']} was called from {$file} line 
{$dbginfo['line']}!";
+        $MessagesFmt[$msgkey] = "<div class='wikimessage'>{$msg}{$sfx}</div>";
+      }
+    }
+    else 
+      $MessagesFmt['PCF_unknown'] = "<div class='wikimessage'>PCF was 
Called!</div>";
+  }
+}
 function PCCF($code, $template = 'default', $args = '$m') {
   global $CallbackFnTemplates, $CallbackFunctions;
   if (!isset($CallbackFnTemplates[$template]))
     Abort("No \$CallbackFnTemplates[$template]).");
   $code = sprintf($CallbackFnTemplates[$template], $code);
   if (!isset($CallbackFunctions[$code])) {
-    $fn = create_function($args, $code);
+    $fn = PCF($args, $code);
     if ($fn) $CallbackFunctions[$code] = $fn;
     else StopWatch("Failed to create callback function: ".PHSC($code));
+  } else {
+    PCFWarn();
   }
   return $CallbackFunctions[$code];
 }
 function PPRE($pat, $rep, $x) {
   $lambda = PCCF("return $rep;");
   return preg_replace_callback($pat, $lambda, $x);
 }
 function PPRA($array, $x) {
   foreach($array as $pat => $rep) {
     $fmt = $x; # for $FmtP
@@ -1676,53 +1720,55 @@
     if ($m[1]=='<:page>' && trim($txt) == '+' && $suffix>'') { # PITS:01392
       $txt = array(trim($txt), $suffix);
     }
     else $txt .= $suffix;
   }
   if (@$LinkTitleFunction) $m[4] = $LinkTitleFunction($pagename,$m,$txt);
   $out = $LinkFunctions[$m[1]]($pagename,$m[1],$m[2],@$m[4],$txt,$fmt);
   return preg_replace('/(<[^>]+) title=(""|\'\')/', '$1', $out);
 }
 
-function Markup($id, $when, $pat=NULL, $rep=NULL) {
+function Markup($id, $when, $pat=NULL, $rep=NULL, $tracelev=0) {
+  global $EnableMarkupDiag;
   global $MarkupTable;
   unset($GLOBALS['MarkupRules']);
   if (preg_match('/^([<>])?(.+)$/', $when, $m)) {
     $MarkupTable[$id]['cmd'] = $when;
     $MarkupTable[$m[2]]['dep'][$id] = $m[1];
     if (!$m[1]) $m[1] = '=';
     if (@$MarkupTable[$m[2]]['seq']) {
       $MarkupTable[$id]['seq'] = $MarkupTable[$m[2]]['seq'].$m[1];
       foreach((array)@$MarkupTable[$id]['dep'] as $i=>$m)
         Markup($i,"$m$id");
       unset($GLOBALS['MarkupTable'][$id]['dep']);
     }
   }
   if ($pat && !isset($MarkupTable[$id]['pat'])) {
     $MarkupTable[$id]['pat'] = $pat;
     $MarkupTable[$id]['rep'] = $rep;
     
-    if (preg_match('!/[^/]*e[^/]*$!', $pat)) {
+    if ($EnableMarkupDiag || preg_match('!/[^/]*e[^/]*$!', $pat)) {
       if (function_exists('debug_backtrace')) {
         $dbg = debug_backtrace();
-        $MarkupTable[$id]['dbg'] = "! file: {$dbg['0']['file']}, "
-          . "line: {$dbg['0']['line']}, pat: {$dbg['0']['args'][2]}";
+        $dbginfo = $dbg[$tracelev];
+        $MarkupTable[$id]['dbg'] = "! file: {$dbginfo['file']}, "
+          . "line: {$dbginfo['line']}, pat: {$dbginfo['args'][2]}";
       }
       else 
         $MarkupTable[$id]['dbg'] = "! id: '$id', pat: '$pat'";
     }
   }
 }
 
 function Markup_e($id, $when, $pat, $rep, $template = 'markup_e') {
   if (!is_callable($rep)) $rep = PCCF($rep, $template);
-  Markup($id, $when, $pat, $rep);
+  Markup($id, $when, $pat, $rep, 1);  #CGG
 }
 
 function DisableMarkup() {
   global $MarkupTable;
   $idlist = func_get_args();
   unset($GLOBALS['MarkupRules']);
   while (count($idlist)>0) {
     $id = array_shift($idlist);
     if (is_array($id)) { $idlist = array_merge($idlist, $id); continue; }
     $MarkupTable[$id] = array('cmd' => 'none', 'pat'=>'');
_______________________________________________
pmwiki-devel mailing list
pmwiki-devel@pmichaud.com
http://www.pmichaud.com/mailman/listinfo/pmwiki-devel

Reply via email to