]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/SiteMap.php
Rename functional for PearDB backend
[SourceForge/phpwiki.git] / lib / plugin / SiteMap.php
1 <?php // -*-php-*-
2 rcs_id('$Id: SiteMap.php,v 1.9 2004-02-12 13:05:50 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     function getName () {
49         return _("SiteMap");
50     }
51
52     function getDescription () {
53         return sprintf(_("Recursively get BackLinks or links for %s"),
54                        '[pagename]');
55     }
56
57     function getVersion() {
58         return preg_replace("/[Revision: $]/", '',
59                             "\$Revision: 1.9 $");
60     }
61
62     function getDefaultArguments() {
63         return array('exclude'        => '',
64                      'include_self'   => 0,
65                      'noheader'       => 0,
66                      'page'           => '[pagename]',
67                      'description'    => $this->getDescription(),
68                      'reclimit'       => 4,
69                      'info'           => false,
70                      'direction'      => 'back',
71                      'firstreversed'  => false,
72                      'excludeunknown' => true,
73                      'includepages'   => ''    // this doesn't yet work as expected
74                      );
75     }
76     // info arg allows multiple columns
77     // info=mtime,hits,summary,version,author,locked,minor
78     // exclude arg allows multiple pagenames
79     // exclude=HomePage,RecentChanges
80
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                 && !preg_match("/$this->ExcludedPages/", $linkpagename)) {
97                 $pagearr[$level . " [$linkpagename]"] = $link;
98                 $pagearr = $this->recursivelyGetBackLinks($link, $pagearr,
99                                                           $level . '*',
100                                                           $reclimit);
101             }
102         }
103         return $pagearr;
104     }
105
106     function recursivelyGetLinks($startpage, $pagearr, $level = '*',
107                                  $reclimit = '***') {
108         static $VisitedPages = array();
109
110         $startpagename = $startpage->getName();
111         //trigger_error("DEBUG: recursivelyGetLinks( $startpagename , $level )");
112         if ($level == $reclimit)
113             return $pagearr;
114         if (in_array($startpagename, $VisitedPages))
115             return $pagearr;
116         array_push($VisitedPages, $startpagename);
117         $reversed = (($this->firstreversed)
118                      && ($startpagename == $this->initialpage));
119         //trigger_error("DEBUG: \$reversed = $reversed");
120         $pagelinks = $startpage->getLinks($reversed);
121         while ($link = $pagelinks->next()) {
122             $linkpagename = $link->getName();
123             if (($linkpagename != $startpagename)
124                 && !preg_match("/$this->ExcludedPages/", $linkpagename)) {
125                 if (!$this->excludeunknown
126                     || $this->dbi->isWikiPage($linkpagename)) {
127                     $pagearr[$level . " [$linkpagename]"] = $link;
128                     $pagearr = $this->recursivelyGetLinks($link, $pagearr,
129                                                           $level . '*',
130                                                           $reclimit);
131                 }
132             }
133         }
134         return $pagearr;
135     }
136
137
138     function run($dbi, $argstr, $request) {
139         include_once('lib/BlockParser.php');
140         
141         $args = $this->getArgs($argstr, $request, false);
142         extract($args);
143         if (!$page)
144             return '';
145         $out = '';
146         $exclude = $exclude ? explode(",", $exclude) : array();
147         if (!$include_self)
148             $exclude[] = $page;
149         $this->ExcludedPages = "^(?:" . join("|", $exclude) . ")";
150         $this->_default_limit = str_pad('', 3, '*');
151         if (is_numeric($reclimit)) {
152             if ($reclimit < 0)
153                 $reclimit = 0;
154             if ($reclimit > 10)
155                 $reclimit = 10;
156             $limit = str_pad('', $reclimit + 2, '*');
157         } else {
158             $limit = '***';
159         }
160         if (! $noheader)
161             $out .= $description ." ". sprintf(_("(max. recursion level: %d)"),
162                                                $reclimit) . ":\n\n";
163         $pagelist = new PageList($info, $exclude);
164         $p = $dbi->getPage($page);
165
166         $pagearr = array();
167         if ($direction == 'back') {
168             $pagearr = $this->recursivelyGetBackLinks($p, $pagearr, "*",
169                                                       $limit);
170         }
171         else {
172             $this->dbi = $dbi;
173             $this->initialpage = $page;
174             $this->firstreversed = $firstreversed;
175             $this->excludeunknown = $excludeunknown;
176             $pagearr = $this->recursivelyGetLinks($p, $pagearr, "*", $limit);
177         }
178
179         reset($pagearr);
180         while (list($key, $link) = each($pagearr)) {
181             if (!empty($includepages)) { // this doesn't work as expected
182                 $a = substr_count($key, '*');
183                 $indenter = str_pad($nothing, $a);
184                 $out .= $indenter  . '<?plugin IncludePage page=' . $link->getName();
185                 if (is_string($includepages))
186                     $out .= " " . $includepages; // args
187                 $out .= " ?>" . "\n";
188             }
189             else {
190                 $out .= $key . "\n";
191             }
192         }
193         return TransformText($out, 1.0, $page /*cached or not cached?...*/); 
194     }
195 };
196
197 // $Log: not supported by cvs2svn $
198 // Revision 1.8  2004/01/24 23:24:07  rurban
199 // Patch by Alec Thomas, allows Perl regular expressions in SiteMap exclude lists.
200 //   exclude=WikiWikiWeb,(?:Category|Topic).*
201 // It is backwards compatible unless old exclude lists, and therefore Wiki
202 // page names, contain regular expression characters.
203 //
204 // Revision 1.7  2003/02/21 04:12:06  dairiki
205 // Minor fixes for new cached markup.
206 //
207 // Revision 1.6  2003/01/18 22:08:01  carstenklapp
208 // Code cleanup:
209 // Reformatting & tabs to spaces;
210 // Added copyleft, getVersion, getDescription, rcs_id.
211 //
212
213 // For emacs users
214 // Local Variables:
215 // mode: php
216 // tab-width: 8
217 // c-basic-offset: 4
218 // c-hanging-comment-ender-p: nil
219 // indent-tabs-mode: nil
220 // End:
221 ?>