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