SharedPages

Summary: Share selected pages among several wikis on a common server, as in WikiFarms
Status: Stable
Prerequisites: pmwiki-2.1.beta10
Maintainer: Pm
Version: 2005-12-05

Description

The general approach to sharing pages is to create a special shared.d/ directory to hold the shared pages, and then configure the $WikiLibDirs variable to search the shared directory for pages. Then, move any pages to be shared from their normal wiki.d directory into the shared.d/ directory.

Normally the shared.d/ directory will go in the same directory of the main PmWiki installation, but any directory will work for this.

Sharing pages "read only"

To have shared pages that cannot be directly modified by any of the wikis, use a configuration such as:

    $WikiLibDirs = array(
      &$WikiDir,
      new PageStore('$FarmD/shared.d/$FullName'), # read only page store
      new PageStore('$FarmD/wikilib.d/$FullName'));

The above can be placed in a wiki's local/config.php file, or it can go into a farm's local/farmconfig.php to cause all fields of the farm to use the shared pages.

Then, move any pages that are to be shared from a wiki.d/ directory into the shared.d/ directory. Any pages in the shared directory will then be visible to all of the wikis configured to use that directory.

However, when the shared.d/ directory is read-only as in the above, then any (field) wiki that modifies (edits) the shared page will place the page in its local wiki.d/ directory and use the local copy in preference to the shared copy.

Sharing writable pages

If we want a wiki to be able to write into the shared pages, then the configuration is slightly more complex:

    $LockFile = "$FarmD/shared.d/.flock";
    $WikiLibDirs = array(
      &$WikiDir,
      new PageStore('$FarmD/shared.d/$FullName', 1), # writeable page store
      new PageStore('$FarmD/wikilib.d/$FullName'));

We also have to make sure the shared.d/ directory is writable by the webserver -- usually chmod 777 shared.d/ will do it. The $LockFile setting is needed so that the different wikis sharing writable pages can coordinate edits. The "1" in the new PageStore(...) line tells PmWiki that this is a shared directory, and that PmWiki should go ahead and write the page into this directory if it already exists here.

As before, the above lines go into a local/config.php, or into local/farmconfig.php if the pages are to be shared by all fields in a farm.

Any pages to be shared should be moved from wiki.d/ into the shared.d/ directory.

You can have more than one writable shared directory. Notice that there is still only 1 $LockFile.

    $LockFile = "$FarmD/shared.d/.flock";
    $WikiLibDirs = array(
      &$WikiDir,
      new PageStore('$FarmD/shared.d/$FullName', 1),
      new PageStore('$FarmD/shared2.d/$FullName', 1),
      new PageStore('$FarmD/shared3.d/$FullName', 1),
      new PageStore('$FarmD/wikilib.d/$FullName'));

Sharing writable groups

To share groups of pages that can be updated and added to (ie new pages) from both websites, where

  • new pages in the shared groups are added to shared group page store
  • existing pages in the shared groups are updated in their existing local or shared page store
  • pages not in shared groups are updated in the local page store (this happens now)

Re-define the $WikiLibDirs array in the local/SharedGroup.php files in both wikis:

<?php
## only alter the directories when a page is posted in this group
if (@$_REQUEST['action']=='edit'
 && preg_grep('/^post/', array_keys($_REQUEST) ) ) {
  $LockFile = "$FarmD/shared.d/.flock"; # set a shared lock file
  $WikiDir = new PageStore('$FarmD/shared.d/$FullName'); # shared writeable page store
  $WikiLibDirs = array(  &$WikiDir,
    new PageStore('$FarmD/wiki.d/$FullName', 1), # writable page store
    new PageStore('$FarmD/wikilib.d/$FullName')  ); # default PmWiki pages
}

Sharing local.css files on the farm

You can use the farm's pub/css/ directory to share local.css files by redefining the $PageCSSListFmt variable in a field's config.php or the farm's farmconfig.php (for all fields).

Currently the pmwiki distribution of skins.php uses $PubDirUrl rather than $FarmPubDirUrl. You can locally configure it to look in FarmPubDir as well with:

 $PageCSSListFmt = array(
  '$FarmD/pub/css/local.css' => '$FarmPubDirUrl/css/local.css',
  '$FarmD/pub/css/$Group.css' => '$FarmPubDirUrl/css/$Group.css',
  '$FarmD/pub/css/$FullName.css' => '$FarmPubDirUrl/css/$FullName.css',
  'pub/css/local.css' => '$PubDirUrl/css/local.css',
  'pub/css/$Group.css' => '$PubDirUrl/css/$Group.css',
  'pub/css/$FullName.css' => '$PubDirUrl/css/$FullName.css');

Notes

Note that any passwords on shared pages are evaluated in the context of the wiki being accessed; i.e., as if the shared pages were stored locally in the wiki.

Comments

  • How do you set up a shared uploads/ dir? At the moment, shared pages that have attached images only retain the images in the local wiki. In the other wiki that shares the page the images are missing.
There's not really a way to do this yet, sorry. You might try using a custom InterMap link for files in a shared uploads directory. --Pm
  • Thanks. Ed Chadwick 2006-01-26.
    Alternatively, you can remove the uploads directory from one wiki and replace it with a symbolic link to the uploads directory of a second wiki. —Eemeli Aro July 23, 2009, at 09:18 AM
  • Ed, if you don't care where the images are physically but want to still use them in pages in other wikis in your wikifarm, includefieldpage might work for you. 2006-06-01 --BJayaram
  • Is there any way to have multiple shared directories? I want one for my farm and one specifically just for a set of three wikis in the same farm. --GNUZoo
  • How can I use one shared page to show slightly different text depending on which field it's accessed from?
     ## (:if field1:) is true when hostname contains "field1"
     $Conditions['field1'] = (strpos($_SERVER['HTTP_HOST'], 'field1') !== false);

     ## (:if field2:) is true when hostname contains "field2"
     $Conditions['field2'] = (strpos($_SERVER['HTTP_HOST'], 'field2') !== false);
Then you can have conditionals like
     (:if field1:)
     This content is visible only to people accessing field1.mydomain.com.
     (:if field2:)
     This content is visible only to people accessing field2.mydomain.com
     (:if:)
     Everybody sees this.
  • Is there an easy way to automatically indicate on shared pages that they are shared? e.g. define a new conditional mark-up along the lines of the one above and put it in an all group header? Francis
  • I found that I needed to use double quotes and a backslash in front of $FullName in the PageStore() calls, as demonstrated in WikiFarmAlternative - I combined this with the share-unshare recipe above into sharedpages.phpΔ shi

See Also

Contributors

  • Pm, original text, 2005-12-05

User notes +2: If you use, used or reviewed this recipe, you can add your name. These statistics appear in the Cookbook listings and will help newcomers browsing through the wiki.