]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/LinkDatabase.php
Test 'limit' argument is numeric to avoid SQL injection
[SourceForge/phpwiki.git] / lib / plugin / LinkDatabase.php
1 <?php // -*-php-*-
2 rcs_id('$Id$');
3 /**
4  Copyright 2004,2007 $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 require_once('lib/PageList.php');
24 require_once('lib/WikiPluginCached.php');
25
26 /**
27  * - To be used by WikiBrowser at http://touchgraph.sourceforge.net/
28  *   Only via a static text file yet. (format=text)
29  * - Or the Hypergraph applet (format=xml)
30  *   http://hypergraph.sourceforge.net/
31  *   So far also only for a static xml file, but I'll fix the applet and test 
32  *   the RPC2 interface.
33  *
34  * TODO: Currently the meta-head tags disturb the touchgraph java browser a bit. 
35  * Maybe add a theme without that much header tags.
36  * DONE: Convert " " to %20
37  */
38 class WikiPlugin_LinkDatabase
39 extends WikiPluginCached
40 {
41     function getName () {
42         return _("LinkDatabase");
43     }
44     function getPluginType() {
45         return PLUGIN_CACHED_HTML;
46     }
47     function getDescription () {
48         return _("List all pages with all links in various formats for some Java Visualization tools");
49     }
50     function getVersion() {
51         return preg_replace("/[Revision: $]/", '',
52                             "\$Revision$");
53     }
54     function getExpire($dbi, $argarray, $request) {
55         return '+900'; // 15 minutes
56     }
57
58     function getDefaultArguments() {
59         return array_merge
60             (
61              PageList::supportedArgs(),
62              array(
63                    'format'        => 'html', // 'html', 'text', 'xml'
64                    'noheader'      => false,
65                    'include_empty' => false,
66                    'exclude_from'  => false,
67                    'info'          => '',
68                    ));
69     }
70
71     function getHtml($dbi, $argarray, $request, $basepage) {
72         $this->run($dbi, WikiPluginCached::glueArgs($argarray), $request, $basepage);
73     }
74     
75     function run($dbi, $argstr, $request, $basepage) {
76         global $WikiTheme;
77         $args = $this->getArgs($argstr, $request);
78
79         if (!empty($args['limit']) && !is_numeric($args['limit'])) {
80             return $this->error(_("Illegal 'limit' argument: must be numeric"));
81         }
82
83         $caption = _("All pages with all links in this wiki (%d total):");
84         
85         if ( !empty($args['owner']) ) {
86             $pages = PageList::allPagesByOwner($args['owner'],$args['include_empty'],
87                                                $args['sortby'],$args['limit']);
88             if ($args['owner'])
89                 $caption = fmt("List of pages owned by [%s] (%d total):", 
90                                WikiLink($args['owner'], 'if_known'),
91                                count($pages));
92         } elseif ( !empty($args['author']) ) {
93             $pages = PageList::allPagesByAuthor($args['author'],$args['include_empty'],
94                                                 $args['sortby'],$args['limit']);
95             if ($args['author'])
96                 $caption = fmt("List of pages last edited by [%s] (%d total):", 
97                                WikiLink($args['author'], 'if_known'), 
98                                count($pages));
99         } elseif ( !empty($args['creator']) ) {
100             $pages = PageList::allPagesByCreator($args['creator'],$args['include_empty'],
101                                                  $args['sortby'],$args['limit']);
102             if ($args['creator'])
103                 $caption = fmt("List of pages created by [%s] (%d total):", 
104                                WikiLink($args['creator'], 'if_known'), 
105                                count($pages));
106         } else {
107             if (! $request->getArg('count'))  
108                 $args['count'] = $dbi->numPages($args['include_empty'], $args['exclude_from']);
109             else 
110                 $args['count'] = $request->getArg('count');
111             $pages = $dbi->getAllPages($args['include_empty'], $args['sortby'], 
112                                        $args['limit'], $args['exclude_from']);
113         }
114         if ($args['format'] == 'html') {
115             $args['types']['links'] = 
116                 new _PageList_Column_LinkDatabase_links('links', _("Links"), 'left');
117             $pagelist = new PageList($args['info'], $args['exclude_from'], $args);
118             //$pagelist->_addColumn("links");
119             if (!$args['noheader']) $pagelist->setCaption($caption);
120             $pagelist->addPages($pages);
121             return $pagelist;
122         } elseif ($args['format'] == 'text') {
123             $request->discardOutput();
124             $request->buffer_output(false);
125             if (!headers_sent())
126                 header("Content-Type: text/plain");
127             $request->checkValidators();
128             while ($page = $pages->next()) {
129                 echo preg_replace("/ /","%20",$page->getName());
130                 $links = $page->getPageLinks(false, $args['sortby'], $args['limit'], 
131                                              $args['exclude']);
132                 while ($link = $links->next()) {
133                     echo " ", preg_replace("/ /","%20",$link->getName());
134                 }
135                 echo "\n";
136             }
137             flush();
138             if (empty($WikiTheme->DUMP_MODE))
139                 $request->finish();
140
141         } elseif ($args['format'] == 'xml') {
142             // For hypergraph.jar. Best dump it to a local sitemap.xml periodically
143             global $WikiTheme, $charset;
144             $currpage = $request->getArg('pagename');
145             $request->discardOutput();
146             $request->buffer_output(false);
147             if (!headers_sent())
148                 header("Content-Type: text/xml");
149             $request->checkValidators();
150             echo "<?xml version=\"1.0\" encoding=\"$charset\"?>";
151             // As applet it prefers only "GraphXML.dtd", but then we must copy it to the webroot.
152             $dtd = $WikiTheme->_findData("GraphXML.dtd");
153             echo "<!DOCTYPE GraphXML SYSTEM \"$dtd\">\n";
154             echo "<GraphXML xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n";
155             echo "<graph id=\"",MangleXmlIdentifier(WIKI_NAME),"\">\n";
156             echo '<style><line tag="node" class="main" colour="#ffffff"/><line tag="node" class="child" colour="blue"/><line tag="node" class="relation" colour="green"/></style>',"\n\n";
157             while ($page = $pages->next()) {
158                 $pageid = MangleXmlIdentifier($page->getName());
159                 $pagename = $page->getName();
160                 echo "<node name=\"$pageid\"";
161                 if ($pagename == $currpage) echo " class=\"main\"";
162                 echo "><label>$pagename</label>";
163                 echo "<dataref><ref xlink:href=\"",WikiURL($pagename,'',true),"\"/></dataref></node>\n";
164                 $links = $page->getPageLinks(false, $args['sortby'], $args['limit'], $args['exclude']);
165                 while ($link = $links->next()) {
166                     $edge = MangleXmlIdentifier($link->getName());
167                     echo "<edge source=\"$pageid\" target=\"$edge\" />\n";
168                 }
169                 echo "\n";
170             }
171             echo "</graph>\n";
172             echo "</GraphXML>\n";
173             if (empty($WikiTheme->DUMP_MODE)) {
174                 unset($GLOBALS['ErrorManager']->_postponed_errors);
175                 $request->finish();
176             }
177         } else {
178             return $this->error(fmt("Unsupported format argument %s", $args['format']));
179         }
180     }
181 };
182
183 class _PageList_Column_LinkDatabase_links extends _PageList_Column {
184     function _getValue($page, &$revision_handle) {
185         $out = HTML();
186         $links = $page->getPageLinks();
187         while ($link = $links->next()) {
188             $out->pushContent(" ", WikiLink($link));
189         }
190         return $out;
191     }
192 }
193
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 ?>