]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WantedPages.php
function run: @return mixed
[SourceForge/phpwiki.git] / lib / plugin / WantedPages.php
1 <?php
2
3 /*
4  * Copyright (C) 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 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 /**
24  * Rewrite of WantedPages, which uses PageList and prints the references, not just the count.
25  * It disables r1.6 but is more explicit, and of comparable convenience.
26  *
27  * A plugin which returns a list of referenced pages which do not exist yet.
28  * All empty pages which are linked from any page - with an ending question mark,
29  * or for just a single page, when the page argument is present.
30  *
31  * TODO: sort pagename col: disable backend fallback
32  **/
33 include_once 'lib/PageList.php';
34
35 class WikiPlugin_WantedPages
36     extends WikiPlugin
37 {
38     function getDescription()
39     {
40         return _("List referenced page names which do not exist yet.");
41     }
42
43     function getDefaultArguments()
44     {
45         return array_merge
46         (
47             PageList::supportedArgs(),
48             array('page' => '[pagename]', // just for a single page.
49                 'withlinks' => 0,
50                 'noheader' => false,
51                 'exclude_from' => __("PgsrcTranslation") . ',' . __("InterWikiMap"),
52                 'limit' => '100',
53                 'paging' => 'auto'));
54     }
55
56     // info arg allows multiple columns
57     // info=mtime,hits,summary,version,author,locked,minor,markup or all
58     // exclude arg allows multiple pagenames exclude=HomePage,RecentChanges
59     /**
60      * @param WikiDB $dbi
61      * @param string $argstr
62      * @param WikiRequest $request
63      * @param string $basepage
64      * @return mixed
65      */
66     function run($dbi, $argstr, &$request, $basepage)
67     {
68         $args = $this->getArgs($argstr, $request);
69         if (!empty($args['exclude_from']))
70             $args['exclude_from'] = is_string($args['exclude_from'])
71                 ? explodePageList($args['exclude_from'])
72                 : $args['exclude_from']; // <! plugin-list !>
73         extract($args);
74         if ($page == _("WantedPages")) $page = "";
75
76         // There's probably a more memory-efficient way to do this (eg
77         // a tailored SQL query via the backend, but this gets the job
78         // done.
79         // TODO: Move this to backend/dumb/WantedPagesIter.php
80
81         if (!$page and $withlinks) {
82             $GLOBALS['WikiTheme']->addPageListColumn(
83                 array('wanted' => array('_PageList_Column_WantedPages_wanted', 'custom:wanted', _("Wanted From"), 'left')));
84             $info = "pagename,wanted";
85         } elseif ($page) {
86             //only get WantedPages links for one page
87             $info = "";
88         } else {
89             // just link to links
90             $GLOBALS['WikiTheme']->addPageListColumn(
91                 array('links' => array('_PageList_Column_WantedPages_links', 'custom:links', _("Links"), 'left')));
92             $info = "pagename,links";
93         }
94         $pagelist = new PageList($info, $exclude, $args); // search button?
95         $pagelist->_wpagelist = array();
96
97         if (!$page) {
98             list($offset, $maxcount) = $pagelist->limit($limit);
99             $wanted_iter = $dbi->wantedPages($exclude_from, $exclude, $sortby, $limit);
100             while ($row = $wanted_iter->next()) {
101                 $wantedfrom = $row['pagename'];
102                 $wanted = $row['wantedfrom'];
103                 // ignore duplicates:
104                 if (empty($pagelist->_wpagelist[$wanted]))
105                     $pagelist->addPage($wanted);
106                 if (!isset($pagelist->_wpagelist[$wanted]))
107                     $pagelist->_wpagelist[$wanted][] = $wantedfrom;
108                 elseif (!in_array($wantedfrom, $pagelist->_wpagelist[$wanted]))
109                     $pagelist->_wpagelist[$wanted][] = $wantedfrom;
110             }
111             $wanted_iter->free();
112             unset($wanted_iter);
113             // update limit, but it's still a hack.
114             $pagelist->_options['limit'] = "$offset," . min($pagelist->getTotal(), $maxcount);
115         } elseif ($dbi->isWikiPage($page)) {
116             //only get WantedPages links for one page
117             $page_handle = $dbi->getPage($page);
118             $links = $page_handle->getPageLinks(true); // include_empty
119             while ($link_handle = $links->next()) {
120                 $linkname = $link_handle->getName();
121                 if (!$dbi->isWikiPage($linkname)) {
122                     $pagelist->addPage($linkname);
123                     //if (!array_key_exists($linkname, $this->_wpagelist))
124                     $pagelist->_wpagelist[$linkname][] = 1;
125                 }
126             }
127         }
128         /*
129         if ($sortby) {
130             ksort($this->_wpagelist);
131             arsort($this->_wpagelist);
132         }*/
133         if (!$noheader) {
134             if ($page) {
135                 $pagelist->setCaption(sprintf(_("Wanted Pages for %s:"), $page));
136             } else {
137                 $pagelist->setCaption(_("Wanted Pages in this wiki:"));
138             }
139         }
140         // reference obviously doesn't work, so force an update to add _wpagelist to parentobj
141         if (isset($pagelist->_columns[1])
142             and in_array($pagelist->_columns[1]->_field, array('wanted', 'links'))
143         )
144             $pagelist->_columns[1]->parentobj =& $pagelist;
145         return $pagelist;
146     }
147 }
148
149 // which links to the missing page
150 class _PageList_Column_WantedPages_wanted extends _PageList_Column
151 {
152     function _PageList_Column_WantedPages_wanted(&$params)
153     {
154         $this->parentobj =& $params[3];
155         $this->_PageList_Column($params[0], $params[1], $params[2]);
156     }
157
158     function _getValue(&$page, $revision_handle)
159     {
160         $html = false;
161         $pagename = $page->getName();
162         foreach ($this->parentobj->_wpagelist[$pagename] as $page) {
163             if ($html)
164                 $html->pushContent(', ', WikiLink($page));
165             else
166                 $html = HTML(WikiLink($page));
167         }
168         return $html;
169     }
170 }
171
172 /*
173  * List of Links and link to ListLinks
174  */
175 class _PageList_Column_WantedPages_links extends _PageList_Column
176 {
177     function _PageList_Column_WantedPages_links(&$params)
178     {
179         $this->parentobj =& $params[3];
180         $this->_PageList_Column($params[0], $params[1], $params[2]);
181     }
182
183     function _getValue(&$page, $revision_handle)
184     {
185         $pagename = $page->getName();
186         $count = count($this->parentobj->_wpagelist[$pagename]);
187         return LinkURL(WikiURL($page, array('action' => 'BackLinks'), false),
188             fmt("(%d Links)", $count));
189     }
190 }
191
192 // Local Variables:
193 // mode: php
194 // tab-width: 8
195 // c-basic-offset: 4
196 // c-hanging-comment-ender-p: nil
197 // indent-tabs-mode: nil
198 // End: