]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/SiteMap.php
Remove Width and Height preferences modification in Edit Toolbar
[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     public $ExcludedPages;
50     public $firstreversed;
51     public $initialpage;
52     public $excludeunknown;
53     public $dbi;
54     public $_default_limit;
55
56     function getDescription()
57     {
58         return _("Recursively get BackLinks or links.");
59     }
60
61     function getDefaultArguments()
62     {
63         return array('exclude' => '',
64             'include_self' => 0,
65             'noheader' => 0,
66             'page' => '[pagename]',
67             'description' => $this->getDescription(),
68             'reclimit' => 4,
69             'info' => false,
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
76         );
77     }
78
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
83
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 = '*',
87                                      $reclimit = '***')
88     {
89         static $VisitedPages = array();
90
91         $startpagename = $startpage->getName();
92         if ($level == $reclimit)
93             return $pagearr;
94         if (in_array($startpagename, $VisitedPages))
95             return $pagearr;
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))
102             ) {
103                 $pagearr[$level . " [$linkpagename]"] = $link;
104                 $pagearr = $this->recursivelyGetBackLinks($link, $pagearr,
105                     $level . '*',
106                     $reclimit);
107             }
108         }
109         return $pagearr;
110     }
111
112     function recursivelyGetLinks($startpage, $pagearr, $level = '*',
113                                  $reclimit = '***')
114     {
115         static $VisitedPages = array();
116
117         $startpagename = $startpage->getName();
118         if ($level == $reclimit)
119             return $pagearr;
120         if (in_array($startpagename, $VisitedPages))
121             return $pagearr;
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))
130             ) {
131                 if (!$this->excludeunknown or $this->dbi->isWikiPage($linkpagename)) {
132                     $pagearr[$level . " [$linkpagename]"] = $link;
133                     $pagearr = $this->recursivelyGetLinks($link, $pagearr,
134                         $level . '*',
135                         $reclimit);
136                 }
137             }
138         }
139         return $pagearr;
140     }
141
142     /**
143      * @param WikiDB $dbi
144      * @param string $argstr
145      * @param WikiRequest $request
146      * @param string $basepage
147      * @return mixed
148      */
149     function run($dbi, $argstr, &$request, $basepage)
150     {
151         include_once 'lib/BlockParser.php';
152
153         $args = $this->getArgs($argstr, $request, array());
154         extract($args);
155         if (!$page)
156             return '';
157         $this->_pagename = $page;
158         $out = ''; // get rid of this
159         $html = HTML();
160         if (empty($exclude)) $exclude = array();
161         if (!$include_self)
162             $exclude[] = $page;
163         $this->ExcludedPages = empty($exclude) ? "" : ("^(?:" . join("|", $exclude) . ")");
164         $this->_default_limit = str_pad('', 3, '*');
165         if (is_numeric($reclimit)) {
166             if ($reclimit < 0)
167                 $reclimit = 0;
168             if ($reclimit > 10)
169                 $reclimit = 10;
170             $limit = str_pad('', $reclimit + 2, '*');
171         } else {
172             $limit = '***';
173         }
174         //Fixme:  override given arg
175         if (!$noheader) {
176             $out = $this->getDescription() . " " . sprintf(_("(max. recursion level: %d)"),
177                 $reclimit) . ":\n\n";
178             $html->pushContent(TransformText($out, $page));
179         }
180         $pagelist = new PageList($info, $exclude);
181         $p = $dbi->getPage($page);
182
183         $pagearr = array();
184         if ($direction == 'back') {
185             $pagearr = $this->recursivelyGetBackLinks($p, $pagearr, "*", $limit);
186         } else {
187             $this->dbi = $dbi;
188             $this->initialpage = $page;
189             $this->firstreversed = $firstreversed;
190             $this->excludeunknown = $excludeunknown;
191             $pagearr = $this->recursivelyGetLinks($p, $pagearr, "*", $limit);
192         }
193
194         reset($pagearr);
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"))
198                 $includepages = '';
199             if (!is_string($includepages))
200                 $includepages = ' '; // avoid plugin loader problems
201             $loader = new WikiPluginLoader();
202             $plugin = $loader->getPlugin(!empty($dtree) ? 'DynamicIncludePage' : 'IncludePage', false);
203             $nothing = '';
204         }
205
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);
215             } else {
216                 $out .= $key . "\n";
217             }
218         }
219         if (empty($includepages)) {
220             return TransformText($out, $page);
221         } else {
222             return $html;
223         }
224     }
225 }
226
227 // Local Variables:
228 // mode: php
229 // tab-width: 8
230 // c-basic-offset: 4
231 // c-hanging-comment-ender-p: nil
232 // indent-tabs-mode: nil
233 // End: