]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/SiteMap.php
Remove history
[SourceForge/phpwiki.git] / lib / plugin / SiteMap.php
1 <?php // -*-php-*-
2 rcs_id('$Id$');
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
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$");
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                 and (!$this->ExcludedPages or !preg_match("/".$this->ExcludedPages."/", $linkpagename)))
100             {
101                 $pagearr[$level . " [$linkpagename]"] = $link;
102                 $pagearr = $this->recursivelyGetBackLinks($link, $pagearr,
103                                                           $level . '*',
104                                                           $reclimit);
105             }
106         }
107         return $pagearr;
108     }
109
110     function recursivelyGetLinks($startpage, $pagearr, $level = '*',
111                                  $reclimit = '***') {
112         static $VisitedPages = array();
113
114         $startpagename = $startpage->getName();
115         //trigger_error("DEBUG: recursivelyGetLinks( $startpagename , $level )");
116         if ($level == $reclimit)
117             return $pagearr;
118         if (in_array($startpagename, $VisitedPages))
119             return $pagearr;
120         array_push($VisitedPages, $startpagename);
121         $reversed = (($this->firstreversed)
122                      && ($startpagename == $this->initialpage));
123         //trigger_error("DEBUG: \$reversed = $reversed");
124         $pagelinks = $startpage->getLinks($reversed);
125         while ($link = $pagelinks->next()) {
126             $linkpagename = $link->getName();
127             if (($linkpagename != $startpagename) and 
128                 (!$this->ExcludedPages or !preg_match("/$this->ExcludedPages/", $linkpagename)))
129             {
130                 if (!$this->excludeunknown or $this->dbi->isWikiPage($linkpagename)) {
131                     $pagearr[$level . " [$linkpagename]"] = $link;
132                     $pagearr = $this->recursivelyGetLinks($link, $pagearr,
133                                                           $level . '*',
134                                                           $reclimit);
135                 }
136             }
137         }
138         return $pagearr;
139     }
140
141
142     function run($dbi, $argstr, &$request, $basepage) {
143         include_once('lib/BlockParser.php');
144         
145         $args = $this->getArgs($argstr, $request, false);
146         extract($args);
147         if (!$page)
148             return '';
149         $this->_pagename = $page;
150         $out = ''; // get rid of this
151         $html = HTML();
152         if (empty($exclude)) $exclude = array();
153         if (!$include_self)
154             $exclude[] = $page;
155         $this->ExcludedPages = empty($exclude) ? "" : ("^(?:" . join("|", $exclude) . ")");
156         $this->_default_limit = str_pad('', 3, '*');
157         if (is_numeric($reclimit)) {
158             if ($reclimit < 0)
159                 $reclimit = 0;
160             if ($reclimit > 10)
161                 $reclimit = 10;
162             $limit = str_pad('', $reclimit + 2, '*');
163         } else {
164             $limit = '***';
165         }
166         //Fixme:  override given arg
167         $description = $this->getDescription();
168         if (! $noheader) {
169             $out = $this->getDescription() ." ". sprintf(_("(max. recursion level: %d)"),
170                                                          $reclimit) . ":\n\n";
171             $html->pushContent(TransformText($out, 1.0, $page));
172         }
173         $pagelist = new PageList($info, $exclude);
174         $p = $dbi->getPage($page);
175
176         $pagearr = array();
177         if ($direction == 'back') {
178             $pagearr = $this->recursivelyGetBackLinks($p, $pagearr, "*",
179                                                       $limit);
180         }
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"))
193                 $includepages = '';
194             if (!is_string($includepages))
195                 $includepages = ' '; // avoid plugin loader problems
196             $loader = new WikiPluginLoader();
197             $plugin = $loader->getPlugin('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             }
213             else {
214                 $out .= $key . "\n";
215             }
216         }
217         if (empty($includepages)) {
218             return TransformText($out, 2.0, $page); 
219         } else {
220             return $html; 
221         }
222     }
223 };
224
225 // For emacs users
226 // Local Variables:
227 // mode: php
228 // tab-width: 8
229 // c-basic-offset: 4
230 // c-hanging-comment-ender-p: nil
231 // indent-tabs-mode: nil
232 // End:
233 ?>