<?php

/*  wikiinc.php

	An add-on for PmWiki (see www.pmwiki.org)
	to facilitate nested includes in a sidebar
	
	put in your local directory 
	and include by putting this line in your local/config.php
	
	    include_once("local/wikiinc.php")

	Created for the University Libraries
	of the University of Minnesota (Twin Cities)
	by Eric Celeste <efc@umn.edu>
	
	Based heavily on the original pmwiki.php code.
	
	March 2004
*/

function wikiinctest($pagename,$wikilist=NULL) {
	/* goes through the process of determining which
	   of a list of pages exists, but then returns
	   just the name of the first existing page
	   
	   based on PrintWikiPage, but only prints the
	   page name instead of the page text
	 */
  global $PrintWikiPageNotFoundFmt;
  if (is_null($wikilist)) $wikilist=$pagename;
  $pagelist = preg_split('/\s+/',$wikilist,-1,PREG_SPLIT_NO_EMPTY);
  foreach($pagelist as $p) {
    if (PageExists($p)) {
      $page = RetrieveAuthPage($p,"read",false);
      if ($page['text']) echo $p;
      return;
    }
  }
}

function wikiinc($pagename,$wikilist=NULL) {
	/* does the same thing as the "wiki" directive,
	   except that included pages are also processed for
	   further inclusions, code directly lifted from
	   portions of pmwiki.php, but I'm not sure what
	   dire consequence may be encountered by using it
	   in this way
	   
	   broadly similar to PrintWikiPage, but with
	   code from ProcessTextDirectives thrown in
	*/
  global $Text,$GroupNamePattern,$PageTitlePattern,$MaxIncludes,$SpaceWikiWords,
    $GroupHeaderFmt,$GroupFooterFmt,$BrowseDirectives,$PrintWikiPageNotFoundFmt;
  if (is_null($wikilist)) $wikilist=$pagename;
  $pagelist = preg_split('/\s+/',$wikilist,-1,PREG_SPLIT_NO_EMPTY);
  foreach($pagelist as $p) {
    if (PageExists($p)) {
      $page = RetrieveAuthPage($p,"read",false);
      if ($page['text']) {
		$text = $page['text'];
		/* pick up ProcessTextDirectives here */
		$text = preg_replace("/\\[\\=(.*?)\\=\\]/se",
    "Keep(htmlspecialchars(str_replace('\\\"','\"','$1'),ENT_NOQUOTES))",$text);
		$inclcount=0;
		while ($inclcount<$MaxIncludes &&
	      preg_match("/\\[\\[include:(.*?)\\]\\]/",$text,$match)) {
	    $inclrepl=$match[0]; $inclname=$match[1]; $incltext='';
	    if (!preg_match("/^$GroupNamePattern([\\/.])$PageTitlePattern\$/",
	        $inclname))
	      $inclname = FmtPageName('$Group',$pagename).".$inclname";
	    $inclpage = RetrieveAuthPage($inclname,"read",false);
	    if ($inclpage) $incltext=$inclpage['text'];
	    $text = str_replace($inclrepl,$incltext,$text);
	    $inclcount++;
		}
		if (!strstr($text,"[[nogroupheader]]")) {
		  $hdname = FmtPageName($GroupHeaderFmt,$pagename);
		  if ($hdname != $pagename) 
		    { $hdpage=ReadPage($hdname,""); $text = $hdpage['text'].$text; }
		}
		if (!strstr($text,"[[nogroupfooter]]")) {
		  $hdname = FmtPageName($GroupFooterFmt,$pagename);
		  if ($hdname != $pagename) 
		    { $hdpage=ReadPage($hdname,""); $text = $text.$hdpage['text']; }
		}
		Lock(0);
		foreach($BrowseDirectives as $p=>$s) {
		  if ($p[0]=='/') $text=preg_replace($p,$s,$text);
		  else if (strstr($text,$p)) $text = str_replace($p,eval($s),$text);
		}
		/* end of ProcessTextDirectives code */
	    PrintText($pagename,$text);
	  }
      return;
    }
  }
  if ($PrintWikiPageNotFoundFmt>'') 
    print FmtPageName(@$PrintWikiPageNotFoundFmt,array_pop($pagelist));
}

?>