]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/BackLinks.php
Test 'limit' argument is numeric to avoid SQL injection
[SourceForge/phpwiki.git] / lib / plugin / BackLinks.php
1 <?php // -*-php-*-
2 rcs_id('$Id$');
3 /**
4  Copyright 1999,2000,2001,2002,2006 $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  */
25 require_once('lib/PageList.php');
26
27 class WikiPlugin_BackLinks
28 extends WikiPlugin
29 {
30     function getName() {
31         return _("BackLinks");
32     }
33     
34     function getDescription() {
35         return sprintf(_("List all pages which link to %s."), '[pagename]');
36     }
37     
38     function getVersion() {
39         return preg_replace("/[Revision: $]/", '',
40                             "\$Revision$");
41     }
42     
43     function getDefaultArguments() {
44         return array_merge
45             (
46              PageList::supportedArgs(),
47              array('include_self' => false,
48                    'noheader'     => false,
49                    'page'         => '[pagename]',
50                    'linkmore'     => '',  // If count>0 and limit>0 display a link with 
51                      // the number of all results, linked to the given pagename.
52                    ));
53     }
54
55     // info arg allows multiple columns
56     // info=mtime,hits,summary,version,author,locked,minor
57     // exclude arg allows multiple pagenames exclude=HomePage,RecentChanges
58     // NEW: info=count : number of links
59     // page=foo,bar : backlinks to both pages
60     function run($dbi, $argstr, &$request, $basepage) {
61         $args = $this->getArgs($argstr, $request);
62
63         if (!empty($args['limit']) && !is_numeric($args['limit'])) {
64             return $this->error(_("Illegal 'limit' argument: must be numeric"));
65         }
66
67         extract($args);
68         if (empty($page) and $page != '0')
69             return '';
70         // exclude is now already expanded in WikiPlugin::getArgs()
71         if (empty($exclude)) $exclude = array();
72         if (!$include_self)
73             $exclude[] = $page;
74         if ($info) {
75             $info = explode(",", $info);
76             if (in_array('count',$info))
77                 $args['types']['count'] = 
78                     new _PageList_Column_BackLinks_count('count', _("#"), 'center');
79         }
80         if (!empty($limit))
81             $args['limit'] = $limit;
82         $args['dosort'] = !empty($args['sortby']); // override DB sort (??)
83         $pagelist = new PageList($info, $exclude, $args);
84
85         // support logical AND: page1,page2
86         $pages = explodePageList($page);
87         $count = count($pages);
88         if (count($pages) > 1) {
89             // AND: the intersection of all these pages
90             $bl = array();
91             foreach ($pages as $p) {
92                 $dp = $dbi->getPage($p);
93                 $bi = $dp->getBackLinks(false, $sortby, 0, $exclude);
94                 while ($b = $bi->next()) {
95                     $name = $b->getName();
96                     if (isset($bl[$name]))
97                         $bl[$name]++;
98                     else 
99                         $bl[$name] = 1;   
100                 } 
101             }
102             foreach ($bl as $b => $v)
103                 if ($v == $count)
104                     $pagelist->addPage($b);
105         } else {
106             $p = $dbi->getPage($page);
107             $pagelist->addPages($p->getBackLinks(false, $sortby, 0, $exclude));
108         }
109         $total = $pagelist->getTotal();
110
111         // Localization note: In English, the differences between the
112         // various phrases spit out here may seem subtle or negligible
113         // enough to tempt you to combine/normalize some of these
114         // strings together, but the grammar employed most by other
115         // languages does not always end up with so subtle a
116         // distinction as it does with English in this case. :)
117         if (!$noheader) {
118             if ($page == $request->getArg('pagename')
119                 and !$dbi->isWikiPage($page)) 
120             {
121                     // BackLinks plugin is more than likely being called
122                     // upon for an empty page on said page, while either
123                     // 'browse'ing, 'create'ing or 'edit'ing.
124                     //
125                     // Don't bother displaying a WikiLink 'unknown', just
126                     // the Un~WikiLink~ified (plain) name of the uncreated
127                     // page currently being viewed.
128                     $pagelink = $page;
129                     
130                     if ($pagelist->isEmpty())
131                         return HTML::p(fmt("No other page links to %s yet.", $pagelink));
132                     
133                     if ($total == 1)
134                         $pagelist->setCaption(fmt("One page would link to %s:",
135                                                   $pagelink));
136                     // Some future localizations will actually require
137                     // this... (BelieveItOrNot, English-only-speakers!(:)
138                     //
139                     // else if ($pagelist->getTotal() == 2)
140                     //     $pagelist->setCaption(fmt("Two pages would link to %s:",
141                     //                               $pagelink));
142                     else
143                         $pagelist->setCaption(fmt("%s pages would link to %s:",
144                                                   $total, $pagelink));
145             }
146             else {
147                 if ($count) {
148                     $tmp_pages = $pages;
149                     $p = array_shift($tmp_pages);
150                     $pagelink = HTML(WikiLink($p, 'auto'));
151                     foreach ($tmp_pages as $p)
152                         $pagelink->pushContent(" ",_("AND")," ",WikiLink($p, 'auto')); 
153                 } else
154                     // BackLinks plugin is being displayed on a normal page.
155                     $pagelink = WikiLink($page, 'auto');
156                 
157                 if ($pagelist->isEmpty())
158                     return HTML::p(fmt("No page links to %s.", $pagelink));
159                 
160                 //trigger_error("DEBUG: " . $pagelist->getTotal());
161                 
162                 if ($total == 1)
163                     $pagelist->setCaption(fmt("One page links to %s:",
164                                               $pagelink));
165                 // Some future localizations will actually require
166                 // this... (BelieveItOrNot, English-only-speakers!(:)
167                 //
168                 // else if ($pagelist->getTotal() == 2)
169                 //     $pagelist->setCaption(fmt("Two pages link to %s:",
170                 //                               $pagelink));
171                 else
172                     $pagelist->setCaption(fmt("%s pages link to %s:",
173                                               $limit > 0 ? $total : _("Those"), 
174                                               $pagelink));
175             }
176         }
177         if (!empty($args['linkmore']) 
178             and $dbi->isWikiPage($args['linkmore'])
179             and $limit > 0 and $total > $limit
180             )
181             $pagelist->addCaption(WikiLink($args['linkmore'], "auto", _("More...")));
182         return $pagelist;
183     }
184     
185 };
186
187 // how many links from this backLink to other pages
188 class _PageList_Column_BackLinks_count extends _PageList_Column {
189     function _getValue($page, &$revision_handle) {
190         $iter = $page->getPageLinks();
191         $count = $iter->count();
192         return $count;
193     }
194 }
195
196 // For emacs users
197 // Local Variables:
198 // mode: php
199 // tab-width: 8
200 // c-basic-offset: 4
201 // c-hanging-comment-ender-p: nil
202 // indent-tabs-mode: nil
203 // End:
204 ?>