01402: Let ROSPatterns functions know what page is being edited (Open)

Summary: Let ROSPatterns functions know what page is being edited
Created: 2017-01-31 12:47
Status: Open
Category: Feature
From: Peter Kay
Assigned:
Priority: 5
Version: 2.2.93
OS:

Description: The Replace on Save functionality allows calling a function for the replacement:

  $ROSPatterns['/sometext/']="MyFunction()";

However, MyFunction has no way of knowing what page is currently being saved. The $pagename variable is not reliable for recipe writers: custom actions and even markup can change various pages through UpdatePage().

Proposed Change 1

In the function UpdatePage(), add:

  global $UpdatingPageName; #remains after call is finished
  $UpdatePageName=$pagename;

Benefits: minimal processing and memory needed; any of the $EditFunctions can access the information as needed.

While it would be possible to clear the variable after UpdatePage is finished,

  unset($GLOBALS['UpdatingPageName']);

there does not seem to be any compelling reason to do so. It leaves a record of the last edited page, and if anyone is calling any of the $EditFunction() directly, they can set the variable as needed themselves.

Doing it this way means that any other of the $EditFunctions will also have access to the page name if needed.

Drawback: If someone calls any of the $EditFunctions directly, they will need to manually set the variable.

Proposed Change 2

In the function ReplaceOnSave() add:

  global $UpdatingPageName; # or some such
  $UpdatingPageName=$pagename;

Benefits: as above. In addition, if any cookbook recipes call ReplaceOnSave directly, any other cookbook recipes that rely on knowing the page name will work. This increases cookbook flexibility, which is one of the things that makes PmWiki so great.

Drawbacks: Only affects ReplaceOnSave(). Does not affect any other $EditFunctions.

Peter Kay January 31, 2017, at 12:51 PM

Thanks for this proposition, I'll think about it for the core. In the meantime, you could do it in your recipe something like this:

  array_unshift($EditFunctions, "MySetUpdatedPagename");
  function MySetUpdatedPagename($pagename, $p, $n) {
    global $UpdatingPageName;
    $UpdatingPageName = $pagename;
  }

Then get this variable in you ROE function.

About the core, at first sight, I think this may hit two problems: (1) some edit function changes $pagename, see draft.php; (2) some edit function called from UpdatePage() creates or changes a few other pages via UpdatePage(), in that case your global variable will be overwritten. This needs some thought. --Petko January 31, 2017, at 03:07 PM

That's a fantastic workaround that easily meets my needs and can probably be adapted to whatever a given author has in mind. I think it may be sufficient to leave it as is. Thanks! Peter Kay February 19, 2017, at 01:25 AM