]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/SiteMap.php
Minor fixes for new cached markup.
[SourceForge/phpwiki.git] / lib / plugin / SiteMap.php
1 <?php // -*-php-*-
2 rcs_id('$Id: SiteMap.php,v 1.7 2003-02-21 04:12:06 dairiki 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.7 $");
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                      );
74     }
75     // info arg allows multiple columns
76     // info=mtime,hits,summary,version,author,locked,minor
77     // exclude arg allows multiple pagenames
78     // exclude=HomePage,RecentChanges
79
80     function recursivelyGetBackLinks($startpage, $pagearr, $level = '*',
81                                      $reclimit = '***') {
82         static $VisitedPages = array();
83
84         $startpagename = $startpage->getName();
85         //trigger_error("DEBUG: recursivelyGetBackLinks( $startpagename , $level )");
86         if ($level == $reclimit)
87             return $pagearr;
88         if (in_array($startpagename, $VisitedPages))
89             return $pagearr;
90         array_push($VisitedPages, $startpagename);
91         $pagelinks = $startpage->getLinks();
92         while ($link = $pagelinks->next()) {
93             $linkpagename = $link->getName();
94             if (($linkpagename != $startpagename)
95                 && !in_array($linkpagename, $this->ExcludedPages)) {
96                 $pagearr[$level . " [$linkpagename]"] = $link;
97                 $pagearr = $this->recursivelyGetBackLinks($link, $pagearr,
98                                                           $level . '*',
99                                                           $reclimit);
100             }
101         }
102         return $pagearr;
103     }
104
105     function recursivelyGetLinks($startpage, $pagearr, $level = '*',
106                                  $reclimit = '***') {
107         static $VisitedPages = array();
108
109         $startpagename = $startpage->getName();
110         //trigger_error("DEBUG: recursivelyGetLinks( $startpagename , $level )");
111         if ($level == $reclimit)
112             return $pagearr;
113         if (in_array($startpagename, $VisitedPages))
114             return $pagearr;
115         array_push($VisitedPages, $startpagename);
116         $reversed = (($this->firstreversed)
117                      && ($startpagename == $this->initialpage));
118         //trigger_error("DEBUG: \$reversed = $reversed");
119         $pagelinks = $startpage->getLinks($reversed);
120         while ($link = $pagelinks->next()) {
121             $linkpagename = $link->getName();
122             if (($linkpagename != $startpagename)
123                 && !in_array($linkpagename, $this->ExcludedPages)) {
124                 if (!$this->excludeunknown
125                     || $this->dbi->isWikiPage($linkpagename)) {
126                     $pagearr[$level . " [$linkpagename]"] = $link;
127                     $pagearr = $this->recursivelyGetLinks($link, $pagearr,
128                                                           $level . '*',
129                                                           $reclimit);
130                 }
131             }
132         }
133         return $pagearr;
134     }
135
136
137     function run($dbi, $argstr, $request) {
138         include_once('lib/BlockParser.php');
139         
140         $args = $this->getArgs($argstr, $request, false);
141         extract($args);
142         if (!$page)
143             return '';
144         $out = '';
145         $exclude = $exclude ? explode(",", $exclude) : array();
146         if (!$include_self)
147             $exclude[] = $page;
148         $this->ExcludedPages = $exclude;
149         $this->_default_limit = str_pad('', 3, '*');
150         if (is_numeric($reclimit)) {
151             if ($reclimit < 0)
152                 $reclimit = 0;
153             if ($reclimit > 10)
154                 $reclimit = 10;
155             $limit = str_pad('', $reclimit + 2, '*');
156         } else {
157             $limit = '***';
158         }
159         if (! $noheader)
160             $out .= $description ." ". sprintf(_("(max. recursion level: %d)"),
161                                                $reclimit) . ":\n\n";
162         $pagelist = new PageList($info, $exclude);
163         $p = $dbi->getPage($page);
164
165         $pagearr = array();
166         if ($direction == 'back') {
167             $pagearr = $this->recursivelyGetBackLinks($p, $pagearr, "*",
168                                                       $limit);
169         }
170         else {
171             $this->dbi = $dbi;
172             $this->initialpage = $page;
173             $this->firstreversed = $firstreversed;
174             $this->excludeunknown = $excludeunknown;
175             $pagearr = $this->recursivelyGetLinks($p, $pagearr, "*", $limit);
176         }
177
178         reset($pagearr);
179         while (list($key, $link) = each($pagearr)) {
180             $out .= $key . "\n";
181         }
182         return TransformText($out, 1, $page /*dunno if this last arg is right...*/); 
183     }
184 };
185
186 // $Log: not supported by cvs2svn $
187 // Revision 1.6  2003/01/18 22:08:01  carstenklapp
188 // Code cleanup:
189 // Reformatting & tabs to spaces;
190 // Added copyleft, getVersion, getDescription, rcs_id.
191 //
192
193 // For emacs users
194 // Local Variables:
195 // mode: php
196 // tab-width: 8
197 // c-basic-offset: 4
198 // c-hanging-comment-ender-p: nil
199 // indent-tabs-mode: nil
200 // End:
201 ?>