[pmwiki-devel] modifying $pagename upon first Save - FREQ

Patrick R. Michaud pmichaud at pobox.com
Thu Nov 30 18:22:09 CST 2006


On Thu, Nov 30, 2006 at 10:34:24PM +0000, J. Meijer wrote:
> 
> What I want is to start of with a pagename like this:
>  
>   Event.2006-11-30___xx
>  
> and have it transformed into 
>  
>   Event.2006-11-30_01
>  
> when it is saved. I can't fix the pagename before a Save because 
> multiple users would end up competing for the same pagename. 
>
> I found I can't do it decently. I have to resolve to hacks. These 
> hacks don't guarantee that that all page actions that result in 
> page creation have the pagename fixed. 
>  
> What I really want is a hook to HandleEdit(), so actions that do 
> some preprocessing and then revert to standard HandleEdit() 
> processing have a single point where $pagename can be set/changed, 
> just before the page is created first time. This hook may go into 
> UpdatePage() instead. The point is I can't get things done properly. 
> Replacing HandleEdit in $EditFunctions with my own version just 
> makes sure that any recipy that reverts to HandleEdit won't call mine. 

Your description misstates the purpose and function of $EditFunctions.  
$EditFunctions doesn't contain HandleEdit, $HandleActions does.  
$EditFunctions is a list of functions to be called whenever the 
page is being edited or updated.

And this thus answers your question -- what you really want
to do is to modify pagename just before the page gets saved.
For that you just need to insert a special function in
$EditFunctions that will correct the pagename just before
the page gets saved.  Something like:

    function TransformPageName(&$pagename, &$page, &$new) {
      global $EnablePost;
      ## adjust $pagename if page is about to be saved
      if ($EnablePost) {
        $pagename = preg_replace('/___.*$/', '_01', $pagename);
      }
    }

    ## Insert 'TransformPageName' immediately before 'PostPage'
    array_splice($EditFunctions,
                 array_search('PostPage', $EditFunctions), 
                 0, 'TransformPageName');

So, the TransformPageName function will be called *immediately*
before PostPage, and set value of $pagename (which PostPage and
subsequent functions will use to actually save the page).

Also, there's no race condition, because one of the first
things that HandleEdit does is to call Lock(2) to lock out
any other processes that might be entering an edit/update
transaction.

Note that as described here, HandleEdit will still use the
permissions of the original $pagename to decide if the
author has permissions to edit the page.  If you want to
verify that the author has write permissions under the
new pagename, make an additional call to CondAuth()
after changing the pagename, and set $EnablePost to zero
if the page should not be saved.

Pm




More information about the pmwiki-devel mailing list