Recent Changes - Search:

Cookbook

PmWiki

pmwiki.org

IncludeAble

Summary: How to include the current revision of a pmwiki page in another php page
Version:
Prerequisites: Last tested on PmWiki version: 2.0.6
Status:
Maintainer: Wesley Tanaka
Categories: Includes

Question

How do you include the current revision of a pmwiki page in another php page?

Answer

Let's say I want to include the current content of a pmwiki page in a php page called viewplace.php

1) Patch pmwiki.php in one place, namely:

changing the exit; on line 279 (version 2.0.6) to return;

See PITS.00548 fixed in pmwiki v 2.0.12!

2) Add this php code to viewplace.php:

<?php

$_REQUEST['pagename'] = 'pmwiki page';

$cwd = getcwd();

chdir('wiki');

include 'pmwiki.php';

chdir($cwd);

?>

3) To your config.php, add something like:

if (strpos($_SERVER['REQUEST_URI'], 'viewplace') !== FALSE

      || strpos($_SERVER['REQUEST_URI'], 'handleplacecomment') !== FALSE)

{

   // Switch skin.  This has to go first because stdconfig loads
   // scripts/skins which loads the skin.
   if (strpos($_SERVER['REQUEST_URI'], 'viewplace') !== FALSE)
      $Skin = 'embed';
   else if (strpos($_SERVER['REQUEST_URI'], 'handleplacecomment') !== FALSE)
      $Skin = 'blank';
   // process stdconfig.  This has to go before clearing the
   // HTTPHeaders because some headers are set from stdconfig itself.
   include_once("$FarmD/scripts/stdconfig.php");
   // Disable stdconfig so that it doesn't get processed again.
   $EnableStdConfig = FALSE;
   // stdconfig adds some stuff to that specified in the template file
   // (<div id='wikitext'> </div> and some blank lines), even though
   // our template is completely blank, so we try even harder to clear
   // out the template format.
   if (strpos($_SERVER['REQUEST_URI'], 'handleplacecomment') !== FALSE)
   {
      $TmplFmt=array();
   }
   // Disable headers, since some output may have already been created
   // before pmwiki was included.
   $HTTPHeaders = array();
   // Disable automated redirect for the same reason.
   $EnableFixedUrlRedirect = FALSE;
   // Disable links, because they link back to the wiki URIs, which
   // may be confusing.  This function could be rewritten to rewrite
   // the links to other pages instead, if that made sense.
   function Nothing($pagename,$imap,$path,$title,$txt,$fmt=NULL)
   {
      return $txt;
   }
   $LinkFunctions = array('<:page>' => 'Nothing');
   $InterMapFiles = array();
   // Don't display any text if the pmwiki page is blank or doesn't exist.
   // Again, the default text might be confusing.
   $DefaultPageTextFmt = '';

}

This code needs to be added after any other HTTPHeaders or Skin assignments in config.php.

4) Create an 'embed' skin with:

<!--PageText-->

in it.

Notes

Releases

Comments

It's possible to add items to a pmwiki page without going to a pm wiki page by doing something like:

if ($datathere) {

   // Necessary because if the pmwiki page exists, there's no way to make
   // pmwiki.php not output anything.   HandleBrowse has a hardcoded
   // $PageText in HandleBrowseFmt.
   $_REQUEST['pagename'] = 'Some.Pmwiki.Page.Or.Other.Which.Does.Not.Exist';
   $cwd = getcwd();
   chdir('../wiki');
   include 'pmwiki.php';
   $_REQUEST['pagename'] = 'PmwikiPage.To.Change';
   $oldpage = RetrieveAuthPage($_REQUEST['pagename'], 'read');
   $newpage = $oldpage;
   $newpage['text'] .= "\n!!!" . 'comments go here';
   PostPage($_REQUEST['pagename'], $oldpage, $newpage);
   chdir($cwd);
   header('Location: http://some.success.page/(approve links)');

} else {

   header("Location: http://some.error.page/(approve links)");

}

By pointing _REQUEST['pagename'] to a pmwiki page that doesn't exist, and by applying the above patches as well, pmwiki.php doesn't generate any output, which allows you to set headers (e.g. redirect) after the pmwiki page has been updated. Be careful, because pmwiki appears to clear variables you may have set before it was included. I got around this by storing things in $_REQUEST.


I'm also interested in plugging in the authentication mechanism for the rest of my website so that comments by logged in users will be treated and logged differently than comments from anonymous guests. I saw some reference to that on another page, but nothing specific yet.


This seems like it could be used as a recipe to create includes which execute with the 'context' of the included page. This would mean that vars like {$Name} and {$Group} would get replaced with the values of the included page instead of the including page. - Martin Fick November 07, 2005, at 04:48 PM

See Also

Contributors

Wesley Tanaka

Edit - History - Print - Recent Changes - Search
Page last modified on March 23, 2007, at 05:36 PM