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