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