4 * Copyright 1999,2000,2001,2002,2006 $ThePhpWikiProgrammingTeam
6 * This file is part of PhpWiki.
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.
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.
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
23 require_once('lib/PageList.php');
25 class WikiPlugin_BackLinks
29 return _("BackLinks");
32 function getDescription() {
33 return sprintf(_("List all pages which link to %s."), '[pagename]');
36 function getDefaultArguments() {
39 PageList::supportedArgs(),
40 array('include_self' => false,
42 'page' => '[pagename]',
43 'linkmore' => '', // If count>0 and limit>0 display a link with
44 // the number of all results, linked to the given pagename.
48 // info arg allows multiple columns
49 // info=mtime,hits,summary,version,author,locked,minor
50 // exclude arg allows multiple pagenames exclude=HomePage,RecentChanges
51 // NEW: info=count : number of links
52 // page=foo,bar : backlinks to both pages
53 function run($dbi, $argstr, &$request, $basepage) {
54 $args = $this->getArgs($argstr, $request);
57 if (empty($page) and $page != '0')
59 // exclude is now already expanded in WikiPlugin::getArgs()
60 if (empty($exclude)) $exclude = array();
64 $info = explode(",", $info);
65 if (in_array('count',$info))
66 $args['types']['count'] =
67 new _PageList_Column_BackLinks_count('count', _("#"), 'center');
70 $args['limit'] = $limit;
71 $args['dosort'] = !empty($args['sortby']); // override DB sort (??)
72 $pagelist = new PageList($info, $exclude, $args);
74 // support logical AND: page1,page2
75 $pages = explodePageList($page);
76 $count = count($pages);
77 if (count($pages) > 1) {
78 // AND: the intersection of all these pages
80 foreach ($pages as $p) {
81 $dp = $dbi->getPage($p);
82 $bi = $dp->getBackLinks(false, $sortby, 0, $exclude);
83 while ($b = $bi->next()) {
84 $name = $b->getName();
85 if (isset($bl[$name]))
91 foreach ($bl as $b => $v)
93 $pagelist->addPage($b);
95 $p = $dbi->getPage($page);
96 $pagelist->addPages($p->getBackLinks(false, $sortby, 0, $exclude));
98 $total = $pagelist->getTotal();
100 // Localization note: In English, the differences between the
101 // various phrases spit out here may seem subtle or negligible
102 // enough to tempt you to combine/normalize some of these
103 // strings together, but the grammar employed most by other
104 // languages does not always end up with so subtle a
105 // distinction as it does with English in this case. :)
107 if ($page == $request->getArg('pagename')
108 and !$dbi->isWikiPage($page))
110 // BackLinks plugin is more than likely being called
111 // upon for an empty page on said page, while either
112 // 'browse'ing, 'create'ing or 'edit'ing.
114 // Don't bother displaying a WikiLink 'unknown', just
115 // the Un~WikiLink~ified (plain) name of the uncreated
116 // page currently being viewed.
119 if ($pagelist->isEmpty())
120 return HTML::p(fmt("No other page links to %s yet.", $pagelink));
123 $pagelist->setCaption(fmt("One page would link to %s:",
125 // Some future localizations will actually require
126 // this... (BelieveItOrNot, English-only-speakers!(:)
128 // else if ($pagelist->getTotal() == 2)
129 // $pagelist->setCaption(fmt("Two pages would link to %s:",
132 $pagelist->setCaption(fmt("%s pages would link to %s:",
138 $p = array_shift($tmp_pages);
139 $pagelink = HTML(WikiLink($p, 'auto'));
140 foreach ($tmp_pages as $p)
141 $pagelink->pushContent(" ",_("AND")," ",WikiLink($p, 'auto'));
143 // BackLinks plugin is being displayed on a normal page.
144 $pagelink = WikiLink($page, 'auto');
146 if ($pagelist->isEmpty())
147 return HTML::p(fmt("No page links to %s.", $pagelink));
149 //trigger_error("DEBUG: " . $pagelist->getTotal());
152 $pagelist->setCaption(fmt("One page links to %s:",
154 // Some future localizations will actually require
155 // this... (BelieveItOrNot, English-only-speakers!(:)
157 // else if ($pagelist->getTotal() == 2)
158 // $pagelist->setCaption(fmt("Two pages link to %s:",
161 $pagelist->setCaption(fmt("%s pages link to %s:",
162 $limit > 0 ? $total : _("Those"),
166 if (!empty($args['linkmore'])
167 and $dbi->isWikiPage($args['linkmore'])
168 and $limit > 0 and $total > $limit
170 $pagelist->addCaption(WikiLink($args['linkmore'], "auto", _("More...")));
176 // how many links from this backLink to other pages
177 class _PageList_Column_BackLinks_count extends _PageList_Column {
178 function _getValue($page, &$revision_handle) {
179 $iter = $page->getPageLinks();
180 $count = $iter->count();
190 // c-hanging-comment-ender-p: nil
191 // indent-tabs-mode: nil