]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/SiteMap.php
Update PHP Doc; type compatibility
[SourceForge/phpwiki.git] / lib / plugin / SiteMap.php
1 <?php
2
3 /**
4  * Copyright 1999,2000,2001,2002,2004 $ThePhpWikiProgrammingTeam
5  *
6  * This file is part of PhpWiki.
7  *
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.
12  *
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.
17  *
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.
21  */
22
23 /**
24  * http://sourceforge.net/tracker/?func=detail&aid=537380&group_id=6121&atid=306121
25  *
26  * Submitted By: Cuthbert Cat (cuthbertcat)
27  *
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.
32  *
33  * The list is as deep as the recursion level.
34  *
35  * direction: Get BackLinks or forward links (links listed on the page)
36  *
37  * firstreversed: If true, get BackLinks for the first page and forward
38  * links for the rest. Only applicable when direction = 'forward'.
39  *
40  * excludeunknown: If true (default) then exclude any mentioned pages
41  * which don't exist yet.  Only applicable when direction = 'forward'.
42  */
43 require_once 'lib/PageList.php';
44
45 class WikiPlugin_SiteMap
46     extends WikiPlugin
47 {
48     public $_pagename;
49
50     function getDescription()
51     {
52         return _("Recursively get BackLinks or links.");
53     }
54
55     function getDefaultArguments()
56     {
57         return array('exclude' => '',
58             'include_self' => 0,
59             'noheader' => 0,
60             'page' => '[pagename]',
61             'description' => $this->getDescription(),
62             'reclimit' => 4,
63             'info' => false,
64             'direction' => 'back',
65             'firstreversed' => false,
66             'excludeunknown' => true,
67             'includepages' => '', // only for IncludeSiteMap and IncludeTree
68             'category' => '', // optional category filter (comma-delimited)
69             'dtree' => false, // optional for IncludeTree
70         );
71     }
72
73     // info arg allows multiple columns
74     // info=mtime,hits,summary,version,author,locked,minor
75     // exclude arg allows multiple pagenames
76     // exclude=HomePage,RecentChanges
77
78     // Fixme: overcome limitation if two SiteMap plugins are in the same page!
79     // static $VisitedPages still holds it
80     function recursivelyGetBackLinks($startpage, $pagearr, $level = '*',
81                                      $reclimit = '***')
82     {
83         static $VisitedPages = array();
84
85         $startpagename = $startpage->getName();
86         if ($level == $reclimit)
87             return $pagearr;
88         if (in_array($startpagename, $VisitedPages))
89             return $pagearr;
90         array_push($VisitedPages, $startpagename);
91         $pagelinks = $startpage->getLinks();
92         while ($link = $pagelinks->next()) {
93             $linkpagename = $link->getName();
94             if (($linkpagename != $startpagename)
95                 and (!$this->ExcludedPages or !preg_match("/" . $this->ExcludedPages . "/", $linkpagename))
96             ) {
97                 $pagearr[$level . " [$linkpagename]"] = $link;
98                 $pagearr = $this->recursivelyGetBackLinks($link, $pagearr,
99                     $level . '*',
100                     $reclimit);
101             }
102         }
103         return $pagearr;
104     }
105
106     function recursivelyGetLinks($startpage, $pagearr, $level = '*',
107                                  $reclimit = '***')
108     {
109         static $VisitedPages = array();
110
111         $startpagename = $startpage->getName();
112         if ($level == $reclimit)
113             return $pagearr;
114         if (in_array($startpagename, $VisitedPages))
115             return $pagearr;
116         array_push($VisitedPages, $startpagename);
117         $reversed = (($this->firstreversed)
118             && ($startpagename == $this->initialpage));
119         $pagelinks = $startpage->getLinks($reversed);
120         while ($link = $pagelinks->next()) {
121             $linkpagename = $link->getName();
122             if (($linkpagename != $startpagename) and
123                 (!$this->ExcludedPages or !preg_match("/$this->ExcludedPages/", $linkpagename))
124             ) {
125                 if (!$this->excludeunknown or $this->dbi->isWikiPage($linkpagename)) {
126                     $pagearr[$level . " [$linkpagename]"] = $link;
127                     $pagearr = $this->recursivelyGetLinks($link, $pagearr,
128                         $level . '*',
129                         $reclimit);
130                 }
131             }
132         }
133         return $pagearr;
134     }
135
136     /**
137      * @param WikiDB $dbi
138      * @param string $argstr
139      * @param WikiRequest $request
140      * @param string $basepage
141      * @return mixed
142      */
143     function run($dbi, $argstr, &$request, $basepage)
144     {
145         include_once 'lib/BlockParser.php';
146
147         $args = $this->getArgs($argstr, $request, array());
148         extract($args);
149         if (!$page)
150             return '';
151         $this->_pagename = $page;
152         $out = ''; // get rid of this
153         $html = HTML();
154         if (empty($exclude)) $exclude = array();
155         if (!$include_self)
156             $exclude[] = $page;
157         $this->ExcludedPages = empty($exclude) ? "" : ("^(?:" . join("|", $exclude) . ")");
158         $this->_default_limit = str_pad('', 3, '*');
159         if (is_numeric($reclimit)) {
160             if ($reclimit < 0)
161                 $reclimit = 0;
162             if ($reclimit > 10)
163                 $reclimit = 10;
164             $limit = str_pad('', $reclimit + 2, '*');
165         } else {
166             $limit = '***';
167         }
168         //Fixme:  override given arg
169         if (!$noheader) {
170             $out = $this->getDescription() . " " . sprintf(_("(max. recursion level: %d)"),
171                 $reclimit) . ":\n\n";
172             $html->pushContent(TransformText($out, $page));
173         }
174         $pagelist = new PageList($info, $exclude);
175         $p = $dbi->getPage($page);
176
177         $pagearr = array();
178         if ($direction == 'back') {
179             $pagearr = $this->recursivelyGetBackLinks($p, $pagearr, "*", $limit);
180         } else {
181             $this->dbi = $dbi;
182             $this->initialpage = $page;
183             $this->firstreversed = $firstreversed;
184             $this->excludeunknown = $excludeunknown;
185             $pagearr = $this->recursivelyGetLinks($p, $pagearr, "*", $limit);
186         }
187
188         reset($pagearr);
189         if (!empty($includepages)) {
190             // disallow direct usage, only via child class IncludeSiteMap
191             if (!isa($this, "WikiPlugin_IncludeSiteMap") and !isa($this, "WikiPlugin_IncludeTree"))
192                 $includepages = '';
193             if (!is_string($includepages))
194                 $includepages = ' '; // avoid plugin loader problems
195             $loader = new WikiPluginLoader();
196             $plugin = $loader->getPlugin(!empty($dtree) ? 'DynamicIncludePage' : 'IncludePage', false);
197             $nothing = '';
198         }
199
200         while (list($key, $link) = each($pagearr)) {
201             if (!empty($includepages)) {
202                 $a = substr_count($key, '*');
203                 $indenter = str_pad($nothing, $a);
204                 //$request->setArg('IncludePage', 1);
205                 // quote linkname, by Stefan Schorn
206                 $plugin_args = 'page=\'' . $link->getName() . '\' ' . $includepages;
207                 $pagehtml = $plugin->run($dbi, $plugin_args, $request, $basepage);
208                 $html->pushContent($pagehtml);
209             } else {
210                 $out .= $key . "\n";
211             }
212         }
213         if (empty($includepages)) {
214             return TransformText($out, $page);
215         } else {
216             return $html;
217         }
218     }
219 }
220
221 // Local Variables:
222 // mode: php
223 // tab-width: 8
224 // c-basic-offset: 4
225 // c-hanging-comment-ender-p: nil
226 // indent-tabs-mode: nil
227 // End: