GlobalOperations

Summary: How to apply an operation to every page of a wiki
Version:
Prerequisites:
Status:
Maintainer:
Categories: Administration

Question

How do I apply operation XYZ on every page of my Wiki?

Answer

This recipe assumes that you are at least minimally fluent in writing PHP code.

Say if you want to do foo in every page, create a file local/foo_all.php that reads like this (sections in green are those that you'd most likely want to adapt to your purposes):

  if ($action=='foo_all') {
    Lock(2);
    $page = RetrieveAuthPage($pagename, "admin");
    if (!$page) { Abort("?admin password required"); }
    $pagelist = $WikiDir->ls();
    foreach($pagelist as $pagename) {
      echo "$pagename\n";
      $page = ReadPage($pagename);
      ### Do something with $page.
    }
    exit;
  }

and insert

  include_once('local/foo_all.php');

into your local/config.php file.

During each iteration through the loop, $page is an object holding all the information on the wiki page. The next few sections show various things you can do with it.

$page seems to be an array or object (if I read the "Clear History" code correctly). What's missing here is a short overview of the available keys and the data associated with them.

Clear History

This is for expiring page history that's older than N days, as given on the URL as ?keepdays=N:

  
  $keepdays = @$_REQUEST['keepdays'];
  $keepgmt = $Now - $keepdays * 86400;
  $keys = array_keys($page);
  foreach($keys as $k) 
    if (preg_match("/^\\w+:(\\d+)/", $k, $match)) 
      if ($match[1] < $keepgmt) unset($page[$k]);
  $WikiDir->delete($pagename);
  WritePage($pagename, $page);
  

???

This space intentionally left blank for additional ideas.

Notes

  • This recipe was last tested on PmWiki version:
  • This recipe requires at least PmWiki version: and (any other recipes)

Shell and command-line

Below is a command line that extracts the text from a file storing a wiki page. It could be useful in a shell script for doing something with all wiki pages, e.g. extracting a list of all external http://-links.

 
 grep '^text=' wiki.d/Main.WikiSandbox | sed -e 's/^text=//' | tr '\262' '\012'
    

Note that the end-of-line encoding may be different under various circumstances (e.g. Internationalization). You may need to check each page file for the newline character and handle accordingly.

 
 perl -e "while(<>){if(m(newline=(.+))){$n=$1;}if(m(text=(.+))){$t=$1;}}$t=~s($n)(\n)g;print $t;" wiki.d/Main.WikiSandbox

See Also

JJSIterator

Contributors

Comments

See discussion at GlobalOperations-Talk?