]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/SiteMap.php
var --> public
[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 getName()
51     {
52         return _("SiteMap");
53     }
54
55     function getDescription()
56     {
57         return _("Recursively get BackLinks or links.");
58     }
59
60     function getDefaultArguments()
61     {
62         return array('exclude' => '',
63             'include_self' => 0,
64             'noheader' => 0,
65             'page' => '[pagename]',
66             'description' => $this->getDescription(),
67             'reclimit' => 4,
68             'info' => false,
69             'direction' => 'back',
70             'firstreversed' => false,
71             'excludeunknown' => true,
72             'includepages' => '', // only for IncludeSiteMap and IncludeTree
73             'category' => '', // optional category filter (comma-delimited)
74             'dtree' => false, // optional for IncludeTree
75         );
76     }
77
78     // info arg allows multiple columns
79     // info=mtime,hits,summary,version,author,locked,minor
80     // exclude arg allows multiple pagenames
81     // exclude=HomePage,RecentChanges
82
83     // Fixme: overcome limitation if two SiteMap plugins are in the same page!
84     // static $VisitedPages still holds it
85     function recursivelyGetBackLinks($startpage, $pagearr, $level = '*',
86                                      $reclimit = '***')
87     {
88         static $VisitedPages = array();
89
90         $startpagename = $startpage->getName();
91         //trigger_error("DEBUG: recursivelyGetBackLinks( $startpagename , $level )");
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         //trigger_error("DEBUG: recursivelyGetLinks( $startpagename , $level )");
119         if ($level == $reclimit)
120             return $pagearr;
121         if (in_array($startpagename, $VisitedPages))
122             return $pagearr;
123         array_push($VisitedPages, $startpagename);
124         $reversed = (($this->firstreversed)
125             && ($startpagename == $this->initialpage));
126         //trigger_error("DEBUG: \$reversed = $reversed");
127         $pagelinks = $startpage->getLinks($reversed);
128         while ($link = $pagelinks->next()) {
129             $linkpagename = $link->getName();
130             if (($linkpagename != $startpagename) and
131                 (!$this->ExcludedPages or !preg_match("/$this->ExcludedPages/", $linkpagename))
132             ) {
133                 if (!$this->excludeunknown or $this->dbi->isWikiPage($linkpagename)) {
134                     $pagearr[$level . " [$linkpagename]"] = $link;
135                     $pagearr = $this->recursivelyGetLinks($link, $pagearr,
136                         $level . '*',
137                         $reclimit);
138                 }
139             }
140         }
141         return $pagearr;
142     }
143
144     function run($dbi, $argstr, &$request, $basepage)
145     {
146         include_once 'lib/BlockParser.php';
147
148         $args = $this->getArgs($argstr, $request, false);
149         extract($args);
150         if (!$page)
151             return '';
152         $this->_pagename = $page;
153         $out = ''; // get rid of this
154         $html = HTML();
155         if (empty($exclude)) $exclude = array();
156         if (!$include_self)
157             $exclude[] = $page;
158         $this->ExcludedPages = empty($exclude) ? "" : ("^(?:" . join("|", $exclude) . ")");
159         $this->_default_limit = str_pad('', 3, '*');
160         if (is_numeric($reclimit)) {
161             if ($reclimit < 0)
162                 $reclimit = 0;
163             if ($reclimit > 10)
164                 $reclimit = 10;
165             $limit = str_pad('', $reclimit + 2, '*');
166         } else {
167             $limit = '***';
168         }
169         //Fixme:  override given arg
170         if (!$noheader) {
171             $out = $this->getDescription() . " " . sprintf(_("(max. recursion level: %d)"),
172                 $reclimit) . ":\n\n";
173             $html->pushContent(TransformText($out, 1.0, $page));
174         }
175         $pagelist = new PageList($info, $exclude);
176         $p = $dbi->getPage($page);
177
178         $pagearr = array();
179         if ($direction == 'back') {
180             $pagearr = $this->recursivelyGetBackLinks($p, $pagearr, "*", $limit);
181         } else {
182             $this->dbi = $dbi;
183             $this->initialpage = $page;
184             $this->firstreversed = $firstreversed;
185             $this->excludeunknown = $excludeunknown;
186             $pagearr = $this->recursivelyGetLinks($p, $pagearr, "*", $limit);
187         }
188
189         reset($pagearr);
190         if (!empty($includepages)) {
191             // disallow direct usage, only via child class IncludeSiteMap
192             if (!isa($this, "WikiPlugin_IncludeSiteMap") and !isa($this, "WikiPlugin_IncludeTree"))
193                 $includepages = '';
194             if (!is_string($includepages))
195                 $includepages = ' '; // avoid plugin loader problems
196             $loader = new WikiPluginLoader();
197             $plugin = $loader->getPlugin(!empty($dtree) ? 'DynamicIncludePage' : 'IncludePage', false);
198             $nothing = '';
199         }
200
201         while (list($key, $link) = each($pagearr)) {
202             if (!empty($includepages)) {
203                 $a = substr_count($key, '*');
204                 $indenter = str_pad($nothing, $a);
205                 //$request->setArg('IncludePage', 1);
206                 // quote linkname, by Stefan Schorn
207                 $plugin_args = 'page=\'' . $link->getName() . '\' ' . $includepages;
208                 $pagehtml = $plugin->run($dbi, $plugin_args, $request, $basepage);
209                 $html->pushContent($pagehtml);
210                 //$html->pushContent( HTML(TransformText($indenter, 1.0, $page), $pagehtml));
211                 //$out .= $indenter . $pagehtml . "\n";
212             } else {
213                 $out .= $key . "\n";
214             }
215         }
216         if (empty($includepages)) {
217             return TransformText($out, 2.0, $page);
218         } else {
219             return $html;
220         }
221     }
222 }
223
224 // Local Variables:
225 // mode: php
226 // tab-width: 8
227 // c-basic-offset: 4
228 // c-hanging-comment-ender-p: nil
229 // indent-tabs-mode: nil
230 // End: