[pmwiki-users] Deprecated preg_replace() eval feature in PHP 5.5
Martin Rüegg
martin.rueegg at metaworx.ch
Thu May 8 09:08:49 CDT 2014
Petko Yotov <5ko <at> 5ko.fr> writes:
>
> I have committed to the Subversion repository the latest code with the new
> formats. There are many small changes on many lines so a diff is hardly
> usable.
>
> A few new functions were added:
> PCCF() PmWiki Create Callback Function
> PPRE() PmWiki preg_replace eval
> PPRA() PmWiki preg_replace arrays
> Markup_e() like Markup() but with replacement evaluated in a Callback
>
> We'll document these functions once we're sure they work like we want them
> to work. But the Markup_e() function should not change a lot in the future,
> so I'll give an example:
>
> if until now a recipe used:
>
> Markup('id', 'direcives', '/p(a)tte(rn)/e', "ReplFunc('$1', '$2')");
>
> now it can alternatively use:
>
> Markup_e('id', 'direcives', '/p(a)tte(rn)/', "ReplFunc(\$m[1], \$m[2])");
>
> so the changes are:
> Markup_e() instead of Markup()
> the pattern without the "e" modifier after the last /
> the replacement with $m[0], $m[1], $m[2], $m[3] instead of '$0', '$1' etc.
> (this last one is not automatic, we should be careful there)
>
> Note that the old interface with Markup() will not be removed so there is no
> urgency to migrate the recipes, except for hostings with PHP 5.5.
>
> If anyone would like to help, please get the latest code either via
> Subversion, or by downloading the file pmwiki-latest-svn.zip linked from
> this page: http://www.pmwiki.org/wiki/PmWiki/Subversion .
>
> (maybe don't install it yet on production wikis, and always have backups of
> your last working version
>
> This development pre-release should be tested and should work like 2.2.55
> even over older installations, and in all versions of PHP > 4.1, with and
> without UTF-8 enabled, etc. - should be as good as the stable. Additionally,
> it should also work in PHP 5.5 without too many warnings about the
> deprecated feature (when we're done, there shouldn't be any warnings).
>
> There may be some bugs, for example if some chunk of text is missing, a
> variable scope may have been forgotten: just show us how to reproduce the
> bug, for example on the pmwiki.org Test group, and we'll fix it.
>
> If you notice a problem or have questions or ideas for improvements, please
> tell us.
>
> Petko
>
> Petko Yotov writes:
> > Hello. Patrick and I have started addressing the issue of a deprecated
> > function in future versions of PHP.
> >
> > Partick found the time to look into the problem and suggested a solution
> > which I couldn't see myself in the last 3-4 months. I have worked on it
> > yesterday and today and the migration appears possible, without breaking
> > existing recipes or dropping support for PHP 4!!
> >
> > At this moment, most of the core Markup() rules are rewritten to the new
> > format, and we still need to decide how to deal with different replacement
> > patterns like $ROSPatterns or $MakePageNamePatterns.
> >
> > The next PmWiki version should be mostly done and compatible with PHP 5.5.
> >
> > Our plan is to first remove the deprecated feature from the core program,
> > and then advise recipe authors to use the new Markup definition format
> > (which will be relatively easy).
> >
> > In a recipe, the old format will continue to work with the new PmWiki
> > versions so an existing wiki with local customisation shouldn't break
after
> > a PmWiki upgrade. But if your hosting provider forces you to use PHP 5.5
or
> > just disables the deprecated feature, you may have to upgrade both PmWiki
> > and your recipes.
>
i'm running v2.2.63 on phph 5.5 and had a lot of warnings in my php-log.
so i extended the changes to respect the /e modifier in the pattern (as
opposed the the is_callable() of the replacment).
additionally the old "'$x'" are being replaced with "\$m[x]".
here is the patch i used:
---------------------8<------------------------------
Index: pmwiki.php
===================================================================
--- pmwiki.php (revision 2601)
+++ pmwiki.php (working copy)
@@ -465,11 +465,23 @@
function PPRA($array, $x) {
foreach($array as $pat => $rep) {
$fmt = $x; # for $FmtP
- if(is_callable($rep)) $x = preg_replace_callback($pat,$rep,$x);
- else $x = preg_replace($pat,$rep,$x);
+ $x = PPRX($pat,$rep,$x);
}
return $x;
}
+function PPRX($p, $r, $x) {
+ if(is_callable($r)) return preg_replace_callback($p,$r,$x);
+ $delim = substr($p, 0, 1);
+ $pos = strrpos($p, $delim);
+ $mod = substr($p, $pos+1);
+ if(strpos($mod, 'e') === false)
+ return preg_replace($p,$r,$x);
+ else {
+ $p = substr($p, 0, $pos+1).str_replace('e', '', $mod);
+ $r = preg_replace("/[']\\\$(\\d)[']/", '\\$m[\\1]', $r);
+ return PPRE($p,$r,$x);
+ }
+}
function StopWatch($x) {
global $StopWatch, $EnableStopWatch;
@@ -641,7 +653,7 @@
## FixGlob changes wildcard patterns without '.' to things like
## '*.foo' (name matches) or 'foo.*' (group matches).
function FixGlob($x, $rep = '$1*.$2') {
- return preg_replace('/([\\s,][-!]?)([^\\/.\\s,]+)(?=[\\s,])/', $rep, ",$x,");
+ return PPRX('/([\\s,][-!]?)([^\\/.\\s,]+)(?=[\\s,])/', $rep, ",$x,");
}
## MatchPageNames reduces $pagelist to those pages with names
@@ -1658,10 +1670,7 @@
$RedoMarkupLine=0;
$markrules = BuildMarkupRules();
foreach($markrules as $p=>$r) {
- if ($p{0} == '/') {
- if(is_callable($r)) $x = preg_replace_callback($p,$r,$x);
- else $x=preg_replace($p,$r,$x);
- }
+ if ($p{0} == '/') $x=PPRX($p,$r,$x);
elseif (strstr($x,$p)!==false) $x=eval($r);
if (isset($php_errormsg))
{ echo "ERROR: pat=$p $php_errormsg"; unset($php_errormsg); }
@@ -1674,7 +1683,7 @@
StopWatch('MarkupToHTML end');
return $out;
}
-
+
function HandleBrowse($pagename, $auth = 'read') {
# handle display of a page
global $DefaultPageTextFmt, $PageNotFoundHeaderFmt, $HTTPHeaders,
---------------------8<------------------------------
unfortunately i was not able to check out or export the latest trunk form
the subversion repository. i got:
---------------------8<------------------------------
svn export svn://pmwiki.org/pmwiki/trunk pmwiki --force
svn: E000111: Unable to connect to a repository at URL
'svn://pmwiki.org/pmwiki/trunk'
svn: E000111: Can't connect to host 'pmwiki.org': Connection refused
---------------------8<------------------------------
best regards,
martin.
More information about the pmwiki-users
mailing list