[pmwiki-users] Recipes you use/need to be updated for PHP 5.5

John Rankin john.rankin at affinity.co.nz
Thu Jan 9 19:12:09 CST 2014


On 10/01/14 12:53 PM, Petko Yotov wrote:
> John Rankin writes:
>> On 9/01/14 6:35 PM, Petko Yotov wrote:
>>> John Rankin writes:
>>>> > John Rankin writes:
>>>> >> On 8/01/14 12:31 PM, Petko Yotov wrote:
>>> ...
>>>> > 2. $pagename is not in the scope of the callback function,
>>> ...
>>>
>>>> Sorry, I over-simplified. In practice, the $LinkTidy array is 
>>>> defined once
>>>> at the start and then referenced as a global variable several times in
>>>> different places to do the actual tidying. So at the time $LinkTidy is
>>>> defined, the code may not know the $pagename. Potentially, the 
>>>> tidying can
>>>> apply to multiple different $pagename values as it assembles 
>>>> several wiki
>>>> pages into one output.
>>>>
>>>> Do I use \$pagename instead?
>>>
>>> Once again, $pagename is not automatically in the scope of the 
>>> callback function. If you want to use $pagename in the callback, it 
>>> must somehow get into that callback. If it does, yes, when defining 
>>> the callback, use \$pagename.
>> Of course; you said that before. Stupid me. Apologies.
>
> John,
>
> I'm sorry if I wrote in a way that offended you. I didn't mean to. 
> English is not my best language, or my second best language, but my 
> third language. Anyone can imagine participating in such a discussion 
> in their third best language.
Not at all -- I was embarrassed, not offended :-)

I have done some experiments and the following code illustrates an 
approach which looks interesting for my needs (note the use (&$pagename) 
in the anonymous function definition, which avoids making it a global 
variable):

<?php if (!defined('PmWiki')) exit();

if ($action=='testcallback') {
   @session_start();
   header('Content-type: text/plain');

   function isClosure($v) {
     return is_object($v) && ($v instanceof Closure);
   }

   SDVA($TestCleaner, array(
     '/`\..*?$/' => '...',
     "/\\[\\[([^|\\]]+)\\|\\s*(.*?)\\]\\]($SuffixPattern)/" =>
       function ($m) use (&$pagename) { return 
MakeLink($pagename,$m[1],$m[2],$m[3],'$LinkText'); },
"/\\[\\[([^\\]]+?)\\s*-+>\\s*(.*?)\\]\\]($SuffixPattern)/" =>
       function ($m) use (&$pagename) { return 
MakeLink($pagename,$m[2],$m[1],$m[3],'$LinkText'); },
     '/\\[\\[#([A-Za-z][-.:\\w]*)\\]\\]/' => "",
     "/\\[\\[(.*?)\\]\\]($SuffixPattern)/" =>
       function ($m) use (&$pagename) { return 
MakeLink($pagename,$m[1],NULL,$m[2],'$LinkText'); },
     "/`(($GroupPattern([\\/.]))?($WikiWordPattern))/" => '$1'
             ));

   $testString = "[[#label]]this is [[page name | link text]] to clean, 
plus [[another link -> another page]], and finally some [[test]]s and 
a `WikiWord `.to truncate";
   $pagename = 'Harvard.TestPage';
   foreach ($TestCleaner as $k => $v) {
     $testString = isClosure($v) ? preg_replace_callback($k,$v,$testString)
                                 : preg_replace($k,$v,$testString);
   }
   echo $pagename."\n".$testString;
   exit();
}

The output looks right to me, but I haven't tested it on real pages.

It may be useful to have a PCAF($code) function that returns an 
anonymous function using $code, but I can't get this to work.
>
> Sometimes I write too fast and I miss an important point. For example, 
> now I see that in my messages I was documenting the usage of the new 
> core functions. As a very experienced programmer, you certainly know 
> all this just by reading the functions, and you don't have to use them 
> if you don't want to. There are different ways to perform search and 
> replace in a text string - use those that work when you test them and 
> that suit you best.
>
> Sorry again,
Thanks. The use of callback is new to me and it has taken a while to get 
my head around it. To me, the anonymous function approach looks cleaner 
and tidier than the create_function approach, but it requires php 5.3 or 
above.

JR

-- 
John Rankin




More information about the pmwiki-users mailing list