4 * Copyright 1999,2000,2001,2002,2004 $ThePhpWikiProgrammingTeam
6 * This file is part of PhpWiki.
8 * PhpWiki is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * PhpWiki is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * http://sourceforge.net/tracker/?func=detail&aid=537380&group_id=6121&atid=306121
26 * Submitted By: Cuthbert Cat (cuthbertcat)
28 * This is a quick mod of BackLinks to do the job recursively. If your
29 * site is categorized correctly, and all the categories are listed in
30 * CategoryCategory, then a RecBackLinks there will produce a contents
31 * page for the entire site.
33 * The list is as deep as the recursion level.
35 * direction: Get BackLinks or forward links (links listed on the page)
37 * firstreversed: If true, get BackLinks for the first page and forward
38 * links for the rest. Only applicable when direction = 'forward'.
40 * excludeunknown: If true (default) then exclude any mentioned pages
41 * which don't exist yet. Only applicable when direction = 'forward'.
43 require_once 'lib/PageList.php';
45 class WikiPlugin_SiteMap
49 public $ExcludedPages;
50 public $firstreversed;
52 public $excludeunknown;
54 public $_default_limit;
56 function getDescription()
58 return _("Recursively get BackLinks or links.");
61 function getDefaultArguments()
63 return array('exclude' => '',
66 'page' => '[pagename]',
67 'description' => $this->getDescription(),
70 'direction' => 'back',
71 'firstreversed' => false,
72 'excludeunknown' => true,
73 'includepages' => '', // only for IncludeSiteMap and IncludeTree
74 'category' => '', // optional category filter (comma-delimited)
75 'dtree' => false, // optional for IncludeTree
79 // info arg allows multiple columns
80 // info=mtime,hits,summary,version,author,locked,minor
81 // exclude arg allows multiple pagenames
82 // exclude=HomePage,RecentChanges
84 // Fixme: overcome limitation if two SiteMap plugins are in the same page!
85 // static $VisitedPages still holds it
86 function recursivelyGetBackLinks($startpage, $pagearr, $level = '*',
89 static $VisitedPages = array();
91 $startpagename = $startpage->getName();
92 if ($level == $reclimit)
94 if (in_array($startpagename, $VisitedPages))
96 array_push($VisitedPages, $startpagename);
97 $pagelinks = $startpage->getLinks();
98 while ($link = $pagelinks->next()) {
99 $linkpagename = $link->getName();
100 if (($linkpagename != $startpagename)
101 and (!$this->ExcludedPages or !preg_match("/" . $this->ExcludedPages . "/", $linkpagename))
103 $pagearr[$level . " [$linkpagename]"] = $link;
104 $pagearr = $this->recursivelyGetBackLinks($link, $pagearr,
112 function recursivelyGetLinks($startpage, $pagearr, $level = '*',
115 static $VisitedPages = array();
117 $startpagename = $startpage->getName();
118 if ($level == $reclimit)
120 if (in_array($startpagename, $VisitedPages))
122 array_push($VisitedPages, $startpagename);
123 $reversed = (($this->firstreversed)
124 && ($startpagename == $this->initialpage));
125 $pagelinks = $startpage->getLinks($reversed);
126 while ($link = $pagelinks->next()) {
127 $linkpagename = $link->getName();
128 if (($linkpagename != $startpagename) and
129 (!$this->ExcludedPages or !preg_match("/$this->ExcludedPages/", $linkpagename))
131 if (!$this->excludeunknown or $this->dbi->isWikiPage($linkpagename)) {
132 $pagearr[$level . " [$linkpagename]"] = $link;
133 $pagearr = $this->recursivelyGetLinks($link, $pagearr,
144 * @param string $argstr
145 * @param WikiRequest $request
146 * @param string $basepage
149 function run($dbi, $argstr, &$request, $basepage)
151 include_once 'lib/BlockParser.php';
153 $args = $this->getArgs($argstr, $request, array());
157 $this->_pagename = $page;
158 $out = ''; // get rid of this
160 if (empty($exclude)) $exclude = array();
163 $this->ExcludedPages = empty($exclude) ? "" : ("^(?:" . join("|", $exclude) . ")");
164 $this->_default_limit = str_pad('', 3, '*');
165 if (is_numeric($reclimit)) {
170 $limit = str_pad('', $reclimit + 2, '*');
174 //Fixme: override given arg
176 $out = $this->getDescription() . " " . sprintf(_("(max. recursion level: %d)"),
177 $reclimit) . ":\n\n";
178 $html->pushContent(TransformText($out, $page));
180 $pagelist = new PageList($info, $exclude);
181 $p = $dbi->getPage($page);
184 if ($direction == 'back') {
185 $pagearr = $this->recursivelyGetBackLinks($p, $pagearr, "*", $limit);
188 $this->initialpage = $page;
189 $this->firstreversed = $firstreversed;
190 $this->excludeunknown = $excludeunknown;
191 $pagearr = $this->recursivelyGetLinks($p, $pagearr, "*", $limit);
195 if (!empty($includepages)) {
196 // disallow direct usage, only via child class IncludeSiteMap
197 if (!is_a($this, "WikiPlugin_IncludeSiteMap") and !is_a($this, "WikiPlugin_IncludeTree"))
199 if (!is_string($includepages))
200 $includepages = ' '; // avoid plugin loader problems
201 $loader = new WikiPluginLoader();
202 $plugin = $loader->getPlugin(!empty($dtree) ? 'DynamicIncludePage' : 'IncludePage', false);
206 while (list($key, $link) = each($pagearr)) {
207 if (!empty($includepages)) {
208 $a = substr_count($key, '*');
209 $indenter = str_pad($nothing, $a);
210 //$request->setArg('IncludePage', 1);
211 // quote linkname, by Stefan Schorn
212 $plugin_args = 'page=\'' . $link->getName() . '\' ' . $includepages;
213 $pagehtml = $plugin->run($dbi, $plugin_args, $request, $basepage);
214 $html->pushContent($pagehtml);
219 if (empty($includepages)) {
220 return TransformText($out, $page);
231 // c-hanging-comment-ender-p: nil
232 // indent-tabs-mode: nil