]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/SiteMap.php
renamed global $Theme to $WikiTheme (gforge nameclash)
[SourceForge/phpwiki.git] / lib / plugin / SiteMap.php
1 <?php // -*-php-*-
2 rcs_id('$Id: SiteMap.php,v 1.11 2004-03-24 19:39:03 rurban Exp $');
3 /**
4  Copyright 1999, 2000, 2001, 2002 $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
19  along with PhpWiki; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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     var $_pagename;
49
50     function getName () {
51         return _("SiteMap");
52     }
53
54     function getDescription () {
55         return _("Recursively get BackLinks or links");
56     }
57
58     function getVersion() {
59         return preg_replace("/[Revision: $]/", '',
60                             "\$Revision: 1.11 $");
61     }
62
63     function getDefaultArguments() {
64         return array('exclude'        => '',
65                      'include_self'   => 0,
66                      'noheader'       => 0,
67                      'page'           => '[pagename]',
68                      'description'    => $this->getDescription(),
69                      'reclimit'       => 4,
70                      'info'           => false,
71                      'direction'      => 'back',
72                      'firstreversed'  => false,
73                      'excludeunknown' => true,
74                      'includepages'   => '' // to be used only from the IncludeSiteMap plugin
75                      );
76     }
77     // info arg allows multiple columns
78     // info=mtime,hits,summary,version,author,locked,minor
79     // exclude arg allows multiple pagenames
80     // exclude=HomePage,RecentChanges
81     
82     // Fixme: overcome limitation if two SiteMap plugins are in the same page!
83     // static $VisitedPages still holds it
84     function recursivelyGetBackLinks($startpage, $pagearr, $level = '*',
85                                      $reclimit = '***') {
86         static $VisitedPages = array();
87
88         $startpagename = $startpage->getName();
89         //trigger_error("DEBUG: recursivelyGetBackLinks( $startpagename , $level )");
90         if ($level == $reclimit)
91             return $pagearr;
92         if (in_array($startpagename, $VisitedPages))
93             return $pagearr;
94         array_push($VisitedPages, $startpagename);
95         $pagelinks = $startpage->getLinks();
96         while ($link = $pagelinks->next()) {
97             $linkpagename = $link->getName();
98             if (($linkpagename != $startpagename)
99                 && !preg_match("/$this->ExcludedPages/", $linkpagename)) {
100                 $pagearr[$level . " [$linkpagename]"] = $link;
101                 $pagearr = $this->recursivelyGetBackLinks($link, $pagearr,
102                                                           $level . '*',
103                                                           $reclimit);
104             }
105         }
106         return $pagearr;
107     }
108
109     function recursivelyGetLinks($startpage, $pagearr, $level = '*',
110                                  $reclimit = '***') {
111         static $VisitedPages = array();
112
113         $startpagename = $startpage->getName();
114         //trigger_error("DEBUG: recursivelyGetLinks( $startpagename , $level )");
115         if ($level == $reclimit)
116             return $pagearr;
117         if (in_array($startpagename, $VisitedPages))
118             return $pagearr;
119         array_push($VisitedPages, $startpagename);
120         $reversed = (($this->firstreversed)
121                      && ($startpagename == $this->initialpage));
122         //trigger_error("DEBUG: \$reversed = $reversed");
123         $pagelinks = $startpage->getLinks($reversed);
124         while ($link = $pagelinks->next()) {
125             $linkpagename = $link->getName();
126             if (($linkpagename != $startpagename)
127                 && !preg_match("/$this->ExcludedPages/", $linkpagename)) {
128                 if (!$this->excludeunknown
129                     || $this->dbi->isWikiPage($linkpagename)) {
130                     $pagearr[$level . " [$linkpagename]"] = $link;
131                     $pagearr = $this->recursivelyGetLinks($link, $pagearr,
132                                                           $level . '*',
133                                                           $reclimit);
134                 }
135             }
136         }
137         return $pagearr;
138     }
139
140
141     function run($dbi, $argstr, &$request, $basepage) {
142         include_once('lib/BlockParser.php');
143         
144         $args = $this->getArgs($argstr, $request, false);
145         extract($args);
146         if (!$page)
147             return '';
148         $this->_pagename = $page;
149         $out = ''; // get rid of this
150         $html = HTML();
151         $exclude = $exclude ? explode(",", $exclude) : array();
152         if (!$include_self)
153             $exclude[] = $page;
154         $this->ExcludedPages = "^(?:" . join("|", $exclude) . ")";
155         $this->_default_limit = str_pad('', 3, '*');
156         if (is_numeric($reclimit)) {
157             if ($reclimit < 0)
158                 $reclimit = 0;
159             if ($reclimit > 10)
160                 $reclimit = 10;
161             $limit = str_pad('', $reclimit + 2, '*');
162         } else {
163             $limit = '***';
164         }
165         //Fixme:  override given arg
166         $description = $this->getDescription();
167         if (! $noheader) {
168             $out = $this->getDescription() ." ". sprintf(_("(max. recursion level: %d)"),
169                                                          $reclimit) . ":\n\n";
170             $html->pushContent(TransformText($out, 1.0, $page));
171         }
172         $pagelist = new PageList($info, $exclude);
173         $p = $dbi->getPage($page);
174
175         $pagearr = array();
176         if ($direction == 'back') {
177             $pagearr = $this->recursivelyGetBackLinks($p, $pagearr, "*",
178                                                       $limit);
179         }
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"))
192                 $includepages = '';
193             if (!is_string($includepages))
194                 $includepages = ' '; // avoid plugin loader problems
195             $loader = new WikiPluginLoader();
196             $plugin = $loader->getPlugin('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                 $plugin_args = 'page=' . $link->getName() . ' ' . $includepages;
206                 $pagehtml = $plugin->run($dbi, $plugin_args, $request, $basepage);
207                 $html->pushContent($pagehtml); 
208                 //$html->pushContent( HTML(TransformText($indenter, 1.0, $page), $pagehtml)); 
209                 //$out .= $indenter . $pagehtml . "\n";
210             }
211             else {
212                 $out .= $key . "\n";
213             }
214         }
215         if (empty($includepages)) {
216             return TransformText($out, 1.0, $page); 
217         } else {
218             return $html; 
219         }
220     }
221 };
222
223 // $Log: not supported by cvs2svn $
224 // Revision 1.10  2004/02/17 12:11:36  rurban
225 // added missing 4th basepage arg at plugin->run() to almost all plugins. This caused no harm so far, because it was silently dropped on normal usage. However on plugin internal ->run invocations it failed. (InterWikiSearch, IncludeSiteMap, ...)
226 //
227 // Revision 1.9  2004/02/12 13:05:50  rurban
228 // Rename functional for PearDB backend
229 // some other minor changes
230 // SiteMap comes with a not yet functional feature request: includepages (tbd)
231 //
232 // Revision 1.8  2004/01/24 23:24:07  rurban
233 // Patch by Alec Thomas, allows Perl regular expressions in SiteMap exclude lists.
234 //   exclude=WikiWikiWeb,(?:Category|Topic).*
235 // It is backwards compatible unless old exclude lists, and therefore Wiki
236 // page names, contain regular expression characters.
237 //
238 // Revision 1.7  2003/02/21 04:12:06  dairiki
239 // Minor fixes for new cached markup.
240 //
241 // Revision 1.6  2003/01/18 22:08:01  carstenklapp
242 // Code cleanup:
243 // Reformatting & tabs to spaces;
244 // Added copyleft, getVersion, getDescription, rcs_id.
245 //
246
247 // For emacs users
248 // Local Variables:
249 // mode: php
250 // tab-width: 8
251 // c-basic-offset: 4
252 // c-hanging-comment-ender-p: nil
253 // indent-tabs-mode: nil
254 // End:
255 ?>