]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/LinkDatabase.php
Let us put some abstraction
[SourceForge/phpwiki.git] / lib / plugin / LinkDatabase.php
1 <?php
2
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 along
19  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 getPluginType()
42     {
43         return PLUGIN_CACHED_HTML;
44     }
45
46     function getDescription()
47     {
48         return _("List all pages with all links in various formats for some Java Visualization tools.");
49     }
50
51     function getExpire($dbi, $argarray, $request)
52     {
53         return '+900'; // 15 minutes
54     }
55
56     function getDefaultArguments()
57     {
58         return array_merge
59         (
60             PageList::supportedArgs(),
61             array(
62                 'format' => 'html', // 'html', 'text', 'xml'
63                 'noheader' => false,
64                 'include_empty' => false,
65                 'exclude_from' => false,
66                 'info' => '',
67             ));
68     }
69
70     /**
71      * @param WikiDB $dbi
72      * @param string $argstr
73      * @param WikiRequest $request
74      * @param string $basepage
75      * @return mixed
76      */
77     protected function getHtml($dbi, $argarray, $request, $basepage)
78     {
79         $this->run($dbi, WikiPluginCached::glueArgs($argarray), $request, $basepage);
80     }
81
82     protected function getImage($dbi, $argarray, $request)
83     {
84         trigger_error('pure virtual', E_USER_ERROR);
85     }
86
87     protected function getMap($dbi, $argarray, $request)
88     {
89         trigger_error('pure virtual', E_USER_ERROR);
90     }
91
92     /**
93      * @param WikiDB $dbi
94      * @param string $argstr
95      * @param WikiRequest $request
96      * @param string $basepage
97      * @return mixed
98      */
99     function run($dbi, $argstr, &$request, $basepage)
100     {
101         global $WikiTheme;
102         $args = $this->getArgs($argstr, $request);
103
104         $caption = _("All pages with all links in this wiki (%d total):");
105
106         if (!empty($args['owner'])) {
107             $pages = PageList::allPagesByOwner($args['owner'], $args['include_empty'],
108                 $args['sortby'], $args['limit']);
109             if ($args['owner'])
110                 $caption = fmt("List of pages owned by %s (%d total):",
111                     WikiLink($args['owner'], 'if_known'),
112                     count($pages));
113         } elseif (!empty($args['author'])) {
114             $pages = PageList::allPagesByAuthor($args['author'], $args['include_empty'],
115                 $args['sortby'], $args['limit']);
116             if ($args['author'])
117                 $caption = fmt("List of pages last edited by %s (%d total):",
118                     WikiLink($args['author'], 'if_known'),
119                     count($pages));
120         } elseif (!empty($args['creator'])) {
121             $pages = PageList::allPagesByCreator($args['creator'], $args['include_empty'],
122                 $args['sortby'], $args['limit']);
123             if ($args['creator'])
124                 $caption = fmt("List of pages created by %s (%d total):",
125                     WikiLink($args['creator'], 'if_known'),
126                     count($pages));
127         } else {
128             if (!$request->getArg('count'))
129                 $args['count'] = $dbi->numPages($args['include_empty'], $args['exclude_from']);
130             else
131                 $args['count'] = $request->getArg('count');
132             $pages = $dbi->getAllPages($args['include_empty'], $args['sortby'],
133                 $args['limit'], $args['exclude_from']);
134         }
135         if ($args['format'] == 'html') {
136             $args['types']['links'] =
137                 new _PageList_Column_LinkDatabase_links('links', _("Links"), 'left');
138             $pagelist = new PageList($args['info'], $args['exclude_from'], $args);
139             //$pagelist->_addColumn("links");
140             if (!$args['noheader']) $pagelist->setCaption($caption);
141             $pagelist->addPages($pages);
142             return $pagelist;
143         } elseif ($args['format'] == 'text') {
144             $request->discardOutput();
145             $request->buffer_output(false);
146             if (!headers_sent())
147                 header("Content-Type: text/plain");
148             $request->checkValidators();
149             while ($page = $pages->next()) {
150                 echo preg_replace("/ /", "%20", $page->getName());
151                 $links = $page->getPageLinks(false, $args['sortby'], $args['limit'],
152                     $args['exclude']);
153                 while ($link = $links->next()) {
154                     echo " ", preg_replace("/ /", "%20", $link->getName());
155                 }
156                 echo "\n";
157             }
158             flush();
159             if (empty($WikiTheme->DUMP_MODE))
160                 $request->finish();
161
162         } elseif ($args['format'] == 'xml') {
163             // For hypergraph.jar. Best dump it to a local sitemap.xml periodically
164             global $WikiTheme;
165             $currpage = $request->getArg('pagename');
166             $request->discardOutput();
167             $request->buffer_output(false);
168             if (!headers_sent())
169                 header("Content-Type: text/xml");
170             $request->checkValidators();
171             echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
172             // As applet it prefers only "GraphXML.dtd", but then we must copy it to the webroot.
173             $dtd = $WikiTheme->_findData("GraphXML.dtd");
174             echo "<!DOCTYPE GraphXML SYSTEM \"$dtd\">\n";
175             echo "<GraphXML xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n";
176             echo "<graph id=\"", MangleXmlIdentifier(WIKI_NAME), "\">\n";
177             echo '<style>';
178             echo '<line tag="node" class="main" colour="#ffffff"/>';
179             echo '<line tag="node" class="child" colour="blue"/>';
180             echo '<line tag="node" class="relation" colour="green"/>';
181             echo "</style>\n\n";
182             while ($page = $pages->next()) {
183                 $pageid = MangleXmlIdentifier($page->getName());
184                 $pagename = $page->getName();
185                 echo "<node name=\"$pageid\"";
186                 if ($pagename == $currpage) echo " class=\"main\"";
187                 echo "><label>$pagename</label>";
188                 echo "<dataref><ref xlink:href=\"", WikiURL($pagename, '', true), "\"/></dataref></node>\n";
189                 $links = $page->getPageLinks(false, $args['sortby'], $args['limit'], $args['exclude']);
190                 while ($link = $links->next()) {
191                     $edge = MangleXmlIdentifier($link->getName());
192                     echo "<edge source=\"$pageid\" target=\"$edge\" />\n";
193                 }
194                 echo "\n";
195             }
196             echo "</graph>\n";
197             echo "</GraphXML>\n";
198             if (empty($WikiTheme->DUMP_MODE)) {
199                 unset($GLOBALS['ErrorManager']->_postponed_errors);
200                 $request->finish();
201             }
202         } else {
203             return $this->error(fmt("Unsupported format argument %s", $args['format']));
204         }
205         return '';
206     }
207 }
208
209 class _PageList_Column_LinkDatabase_links extends _PageList_Column
210 {
211     function _getValue($page, &$revision_handle)
212     {
213         $out = HTML();
214         $links = $page->getPageLinks();
215         while ($link = $links->next()) {
216             $out->pushContent(" ", WikiLink($link));
217         }
218         return $out;
219     }
220 }
221
222 // Local Variables:
223 // mode: php
224 // tab-width: 8
225 // c-basic-offset: 4
226 // c-hanging-comment-ender-p: nil
227 // indent-tabs-mode: nil
228 // End: