]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/SiteMap.php
Merge OldTextFormattingRules into TextFormattingRules; rename underscore plugins
[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         //trigger_error("DEBUG: recursivelyGetBackLinks( $startpagename , $level )");
87         if ($level == $reclimit)
88             return $pagearr;
89         if (in_array($startpagename, $VisitedPages))
90             return $pagearr;
91         array_push($VisitedPages, $startpagename);
92         $pagelinks = $startpage->getLinks();
93         while ($link = $pagelinks->next()) {
94             $linkpagename = $link->getName();
95             if (($linkpagename != $startpagename)
96                 and (!$this->ExcludedPages or !preg_match("/" . $this->ExcludedPages . "/", $linkpagename))
97             ) {
98                 $pagearr[$level . " [$linkpagename]"] = $link;
99                 $pagearr = $this->recursivelyGetBackLinks($link, $pagearr,
100                     $level . '*',
101                     $reclimit);
102             }
103         }
104         return $pagearr;
105     }
106
107     function recursivelyGetLinks($startpage, $pagearr, $level = '*',
108                                  $reclimit = '***')
109     {
110         static $VisitedPages = array();
111
112         $startpagename = $startpage->getName();
113         //trigger_error("DEBUG: recursivelyGetLinks( $startpagename , $level )");
114         if ($level == $reclimit)
115             return $pagearr;
116         if (in_array($startpagename, $VisitedPages))
117             return $pagearr;
118         array_push($VisitedPages, $startpagename);
119         $reversed = (($this->firstreversed)
120             && ($startpagename == $this->initialpage));
121         //trigger_error("DEBUG: \$reversed = $reversed");
122         $pagelinks = $startpage->getLinks($reversed);
123         while ($link = $pagelinks->next()) {
124             $linkpagename = $link->getName();
125             if (($linkpagename != $startpagename) and
126                 (!$this->ExcludedPages or !preg_match("/$this->ExcludedPages/", $linkpagename))
127             ) {
128                 if (!$this->excludeunknown or $this->dbi->isWikiPage($linkpagename)) {
129                     $pagearr[$level . " [$linkpagename]"] = $link;
130                     $pagearr = $this->recursivelyGetLinks($link, $pagearr,
131                         $level . '*',
132                         $reclimit);
133                 }
134             }
135         }
136         return $pagearr;
137     }
138
139     function run($dbi, $argstr, &$request, $basepage)
140     {
141         include_once 'lib/BlockParser.php';
142
143         $args = $this->getArgs($argstr, $request, false);
144         extract($args);
145         if (!$page)
146             return '';
147         $this->_pagename = $page;
148         $out = ''; // get rid of this
149         $html = HTML();
150         if (empty($exclude)) $exclude = array();
151         if (!$include_self)
152             $exclude[] = $page;
153         $this->ExcludedPages = empty($exclude) ? "" : ("^(?:" . join("|", $exclude) . ")");
154         $this->_default_limit = str_pad('', 3, '*');
155         if (is_numeric($reclimit)) {
156             if ($reclimit < 0)
157                 $reclimit = 0;
158             if ($reclimit > 10)
159                 $reclimit = 10;
160             $limit = str_pad('', $reclimit + 2, '*');
161         } else {
162             $limit = '***';
163         }
164         //Fixme:  override given arg
165         if (!$noheader) {
166             $out = $this->getDescription() . " " . sprintf(_("(max. recursion level: %d)"),
167                 $reclimit) . ":\n\n";
168             $html->pushContent(TransformText($out, $page));
169         }
170         $pagelist = new PageList($info, $exclude);
171         $p = $dbi->getPage($page);
172
173         $pagearr = array();
174         if ($direction == 'back') {
175             $pagearr = $this->recursivelyGetBackLinks($p, $pagearr, "*", $limit);
176         } else {
177             $this->dbi = $dbi;
178             $this->initialpage = $page;
179             $this->firstreversed = $firstreversed;
180             $this->excludeunknown = $excludeunknown;
181             $pagearr = $this->recursivelyGetLinks($p, $pagearr, "*", $limit);
182         }
183
184         reset($pagearr);
185         if (!empty($includepages)) {
186             // disallow direct usage, only via child class IncludeSiteMap
187             if (!isa($this, "WikiPlugin_IncludeSiteMap") and !isa($this, "WikiPlugin_IncludeTree"))
188                 $includepages = '';
189             if (!is_string($includepages))
190                 $includepages = ' '; // avoid plugin loader problems
191             $loader = new WikiPluginLoader();
192             $plugin = $loader->getPlugin(!empty($dtree) ? 'DynamicIncludePage' : 'IncludePage', false);
193             $nothing = '';
194         }
195
196         while (list($key, $link) = each($pagearr)) {
197             if (!empty($includepages)) {
198                 $a = substr_count($key, '*');
199                 $indenter = str_pad($nothing, $a);
200                 //$request->setArg('IncludePage', 1);
201                 // quote linkname, by Stefan Schorn
202                 $plugin_args = 'page=\'' . $link->getName() . '\' ' . $includepages;
203                 $pagehtml = $plugin->run($dbi, $plugin_args, $request, $basepage);
204                 $html->pushContent($pagehtml);
205             } else {
206                 $out .= $key . "\n";
207             }
208         }
209         if (empty($includepages)) {
210             return TransformText($out, $page);
211         } else {
212             return $html;
213         }
214     }
215 }
216
217 // Local Variables:
218 // mode: php
219 // tab-width: 8
220 // c-basic-offset: 4
221 // c-hanging-comment-ender-p: nil
222 // indent-tabs-mode: nil
223 // End: