]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/BackLinks.php
Fix AND warnings: de-perlify array access
[SourceForge/phpwiki.git] / lib / plugin / BackLinks.php
1 <?php // -*-php-*-
2 rcs_id('$Id: BackLinks.php,v 1.34 2007-05-19 13:31:20 rurban Exp $');
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: 1.34 $");
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         extract($args);
63         if (empty($page) and $page != '0')
64             return '';
65         // exclude is now already expanded in WikiPlugin::getArgs()
66         if (empty($exclude)) $exclude = array();
67         if (!$include_self)
68             $exclude[] = $page;
69         if ($info) {
70             $info = explode(",", $info);
71             if (in_array('count',$info))
72                 $args['types']['count'] = 
73                     new _PageList_Column_BackLinks_count('count', _("#"), 'center');
74         }
75         if (!empty($limit))
76             $args['limit'] = $limit;
77         $args['dosort'] = !empty($args['sortby']); // override DB sort (??)
78         $pagelist = new PageList($info, $exclude, $args);
79
80         // support logical AND: page1,page2
81         $pages = explodePageList($page);
82         $count = count($pages);
83         if (count($pages) > 1) {
84             // AND: the intersection of all these pages
85             $bl = array();
86             foreach ($pages as $p) {
87                 $dp = $dbi->getPage($p);
88                 $bi = $dp->getBackLinks(false, $sortby, 0, $exclude);
89                 while ($b = $bi->next())
90                     $name = $b->getName();
91                     if (isset($bl[$name]))
92                         $bl[$name]++;
93                     else 
94                         $bl[$name] = 1;    
95             }
96             foreach ($bl as $b => $v)
97                 if ($v == $count)
98                     $pagelist->addPage($b);
99         } else {
100             $p = $dbi->getPage($page);
101             $pagelist->addPages($p->getBackLinks(false, $sortby, 0, $exclude));
102         }
103         $total = $pagelist->getTotal();
104
105         // Localization note: In English, the differences between the
106         // various phrases spit out here may seem subtle or negligible
107         // enough to tempt you to combine/normalize some of these
108         // strings together, but the grammar employed most by other
109         // languages does not always end up with so subtle a
110         // distinction as it does with English in this case. :)
111         if (!$noheader) {
112             if ($page == $request->getArg('pagename')
113                 and !$dbi->isWikiPage($page)) 
114             {
115                     // BackLinks plugin is more than likely being called
116                     // upon for an empty page on said page, while either
117                     // 'browse'ing, 'create'ing or 'edit'ing.
118                     //
119                     // Don't bother displaying a WikiLink 'unknown', just
120                     // the Un~WikiLink~ified (plain) name of the uncreated
121                     // page currently being viewed.
122                     $pagelink = $page;
123                     
124                     if ($pagelist->isEmpty())
125                         return HTML::p(fmt("No other page links to %s yet.", $pagelink));
126                     
127                     if ($total == 1)
128                         $pagelist->setCaption(fmt("One page would link to %s:",
129                                                   $pagelink));
130                     // Some future localizations will actually require
131                     // this... (BelieveItOrNot, English-only-speakers!(:)
132                     //
133                     // else if ($pagelist->getTotal() == 2)
134                     //     $pagelist->setCaption(fmt("Two pages would link to %s:",
135                     //                               $pagelink));
136                     else
137                         $pagelist->setCaption(fmt("%s pages would link to %s:",
138                                                   $total, $pagelink));
139             }
140             else {
141                 if ($count) {
142                     $tmp_pages = $pages;
143                     $p = array_shift($tmp_pages);
144                     $pagelink = HTML(WikiLink($p, 'auto'));
145                     foreach ($tmp_pages as $p)
146                         $pagelink->pushContent(" ",_("AND")," ",WikiLink($p, 'auto')); 
147                 } else
148                     // BackLinks plugin is being displayed on a normal page.
149                     $pagelink = WikiLink($page, 'auto');
150                 
151                 if ($pagelist->isEmpty())
152                     return HTML::p(fmt("No page links to %s.", $pagelink));
153                 
154                 //trigger_error("DEBUG: " . $pagelist->getTotal());
155                 
156                 if ($total == 1)
157                     $pagelist->setCaption(fmt("One page links to %s:",
158                                               $pagelink));
159                 // Some future localizations will actually require
160                 // this... (BelieveItOrNot, English-only-speakers!(:)
161                 //
162                 // else if ($pagelist->getTotal() == 2)
163                 //     $pagelist->setCaption(fmt("Two pages link to %s:",
164                 //                               $pagelink));
165                 else
166                     $pagelist->setCaption(fmt("%s pages link to %s:",
167                                               $limit > 0 ? $total : _("Those"), 
168                                               $pagelink));
169             }
170         }
171         if (!empty($args['linkmore']) 
172             and $dbi->isWikiPage($args['linkmore'])
173             and $limit > 0 and $total > $limit
174             )
175             $pagelist->addCaption(WikiLink($args['linkmore'], "auto", _("More...")));
176         return $pagelist;
177     }
178     
179 };
180
181 // how many links from this backLink to other pages
182 class _PageList_Column_BackLinks_count extends _PageList_Column {
183     function _getValue($page, &$revision_handle) {
184         $iter = $page->getPageLinks();
185         $count = $iter->count();
186         return $count;
187     }
188 }
189
190 // $Log: not supported by cvs2svn $
191 // Revision 1.33  2007/01/04 16:46:22  rurban
192 // Add linkmore. Support multiple pages logically AND them.
193 //
194 // Revision 1.32  2004/12/06 19:50:05  rurban
195 // enable action=remove which is undoable and seeable in RecentChanges: ADODB ony for now.
196 // renamed delete_page to purge_page.
197 // enable action=edit&version=-1 to force creation of a new version.
198 // added BABYCART_PATH config
199 // fixed magiqc in adodb.inc.php
200 // and some more docs
201 //
202 // Revision 1.31  2004/11/26 18:39:02  rurban
203 // new regex search parser and SQL backends (90% complete, glob and pcre backends missing)
204 //
205 // Revision 1.30  2004/11/25 17:20:52  rurban
206 // and again a couple of more native db args: backlinks
207 //
208 // Revision 1.29  2004/11/23 15:17:19  rurban
209 // better support for case_exact search (not caseexact for consistency),
210 // plugin args simplification:
211 //   handle and explode exclude and pages argument in WikiPlugin::getArgs
212 //     and exclude in advance (at the sql level if possible)
213 //   handle sortby and limit from request override in WikiPlugin::getArgs
214 // ListSubpages: renamed pages to maxpages
215 //
216 // Revision 1.28  2004/10/14 17:16:22  rurban
217 // override DB sort: not applicable
218 //
219 // Revision 1.27  2004/09/25 16:33:52  rurban
220 // add support for all PageList options
221 //
222 // Revision 1.26  2004/09/14 10:32:32  rurban
223 // support exclude = plugin-list
224 //
225 // Revision 1.25  2004/09/13 15:00:50  rurban
226 // info=count: number of links at this subpage
227 //
228 // Revision 1.24  2004/04/18 05:42:17  rurban
229 // more fixes for page="0"
230 // better WikiForum support
231 //
232 // Revision 1.23  2004/02/22 23:20:33  rurban
233 // fixed DumpHtmlToDir,
234 // enhanced sortby handling in PageList
235 //   new button_heading th style (enabled),
236 // added sortby and limit support to the db backends and plugins
237 //   for paging support (<<prev, next>> links on long lists)
238 //
239 // Revision 1.22  2004/02/17 12:11:36  rurban
240 // added missing 4th basepage arg at plugin->run() to almost all plugins. This caused no harm so far, because it was silently dropped on normal usage. However on plugin internal ->run invocations it failed. (InterWikiSearch, IncludeSiteMap, ...)
241 //
242 // Revision 1.21  2003/12/22 07:31:57  carstenklapp
243 // Bugfix: commented out debugging code that snuck into the release.
244 //
245 // Revision 1.20  2003/12/14 05:36:31  carstenklapp
246 // Internal changes to prepare for an upcoming feature: Added some
247 // conditions and alternate phrases (alternate wording of text srings
248 // when referring to a non-existant page (i.e. WikiLink 'unknown')) when
249 // calling the BackLinks plugin *within* a non-existant page, such as
250 // from within an editpage or browse template while editing a new page.
251 //
252 // Revision 1.19  2003/01/18 21:19:25  carstenklapp
253 // Code cleanup:
254 // Reformatting; added copyleft, getVersion, getDescription
255 //
256
257 // For emacs users
258 // Local Variables:
259 // mode: php
260 // tab-width: 8
261 // c-basic-offset: 4
262 // c-hanging-comment-ender-p: nil
263 // indent-tabs-mode: nil
264 // End:
265 ?>