]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/SiteMap.php
Replace tabs by spaces; remove EOL spaces
[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'   => '', // only for IncludeSiteMap and IncludeTree
75                      'category'       => '', // optional category filter (comma-delimited)
76                      'dtree'          => false, // optional for IncludeTree
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         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         static $VisitedPages = array();
115
116         $startpagename = $startpage->getName();
117         //trigger_error("DEBUG: recursivelyGetLinks( $startpagename , $level )");
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         //trigger_error("DEBUG: \$reversed = $reversed");
126         $pagelinks = $startpage->getLinks($reversed);
127         while ($link = $pagelinks->next()) {
128             $linkpagename = $link->getName();
129             if (($linkpagename != $startpagename) and
130                 (!$this->ExcludedPages or !preg_match("/$this->ExcludedPages/", $linkpagename)))
131             {
132                 if (!$this->excludeunknown or $this->dbi->isWikiPage($linkpagename)) {
133                     $pagearr[$level . " [$linkpagename]"] = $link;
134                     $pagearr = $this->recursivelyGetLinks($link, $pagearr,
135                                                           $level . '*',
136                                                           $reclimit);
137                 }
138             }
139         }
140         return $pagearr;
141     }
142
143
144     function run($dbi, $argstr, &$request, $basepage) {
145         include_once('lib/BlockParser.php');
146
147         $args = $this->getArgs($argstr, $request, false);
148         extract($args);
149         if (!$page)
150             return '';
151         $this->_pagename = $page;
152         $out = ''; // get rid of this
153         $html = HTML();
154         if (empty($exclude)) $exclude = array();
155         if (!$include_self)
156             $exclude[] = $page;
157         $this->ExcludedPages = empty($exclude) ? "" : ("^(?:" . join("|", $exclude) . ")");
158         $this->_default_limit = str_pad('', 3, '*');
159         if (is_numeric($reclimit)) {
160             if ($reclimit < 0)
161                 $reclimit = 0;
162             if ($reclimit > 10)
163                 $reclimit = 10;
164             $limit = str_pad('', $reclimit + 2, '*');
165         } else {
166             $limit = '***';
167         }
168         //Fixme:  override given arg
169         $description = $this->getDescription();
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         }
182         else {
183             $this->dbi = $dbi;
184             $this->initialpage = $page;
185             $this->firstreversed = $firstreversed;
186             $this->excludeunknown = $excludeunknown;
187             $pagearr = $this->recursivelyGetLinks($p, $pagearr, "*", $limit);
188         }
189
190         reset($pagearr);
191         if (!empty($includepages)) {
192             // disallow direct usage, only via child class IncludeSiteMap
193             if (!isa($this,"WikiPlugin_IncludeSiteMap") and !isa($this,"WikiPlugin_IncludeTree"))
194                 $includepages = '';
195             if (!is_string($includepages))
196                 $includepages = ' '; // avoid plugin loader problems
197             $loader = new WikiPluginLoader();
198             $plugin = $loader->getPlugin($dtree ? 'DynamicIncludePage' : 'IncludePage', false);
199             $nothing = '';
200         }
201
202         while (list($key, $link) = each($pagearr)) {
203             if (!empty($includepages)) {
204                 $a = substr_count($key, '*');
205                 $indenter = str_pad($nothing, $a);
206                 //$request->setArg('IncludePage', 1);
207                 // quote linkname, by Stefan Schorn
208                 $plugin_args = 'page=\'' . $link->getName() . '\' ' . $includepages;
209                 $pagehtml = $plugin->run($dbi, $plugin_args, $request, $basepage);
210                 $html->pushContent($pagehtml);
211                 //$html->pushContent( HTML(TransformText($indenter, 1.0, $page), $pagehtml));
212                 //$out .= $indenter . $pagehtml . "\n";
213             }
214             else {
215                 $out .= $key . "\n";
216             }
217         }
218         if (empty($includepages)) {
219             return TransformText($out, 2.0, $page);
220         } else {
221             return $html;
222         }
223     }
224 };
225
226 // For emacs users
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:
234 ?>