Summary: How to apply an operation to every page of a wiki
Categories: Administration


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


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') {
    $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.

and insert


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]);
  WritePage($pagename, $page);


This space intentionally left blank for additional ideas.


  • 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




See discussion at GlobalOperations-Talk?