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 along
19 * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 require_once 'lib/PageList.php';
25 class WikiPlugin_BackLinks
28 function getDescription()
30 return sprintf(_("List all pages which link to %s."), '[pagename]');
33 function getDefaultArguments()
37 PageList::supportedArgs(),
38 array('include_self' => false,
40 'page' => '[pagename]',
41 'linkmore' => '', // If count>0 and limit>0 display a link with
42 // the number of all results, linked to the given pagename.
46 // info arg allows multiple columns
47 // info=mtime,hits,summary,version,author,locked,minor
48 // exclude arg allows multiple pagenames exclude=HomePage,RecentChanges
49 // NEW: info=count : number of links
50 // page=foo,bar : backlinks to both pages
51 function run($dbi, $argstr, &$request, $basepage)
53 $args = $this->getArgs($argstr, $request);
56 if (empty($page) and $page != '0')
58 // exclude is now already expanded in WikiPlugin::getArgs()
59 if (empty($exclude)) $exclude = array();
63 $info = explode(",", $info);
64 if (in_array('count', $info))
65 $args['types']['count'] =
66 new _PageList_Column_BackLinks_count('count', _("#"), 'center');
69 $args['limit'] = $limit;
70 // $args['dosort'] = !empty($args['sortby']); // override DB sort (??)
71 $pagelist = new PageList($info, $exclude, $args);
73 // support logical AND: page1,page2
74 $pages = explodePageList($page);
75 $count = count($pages);
76 if (count($pages) > 1) {
77 // AND: the intersection of all these pages
79 foreach ($pages as $p) {
80 $dp = $dbi->getPage($p);
81 $bi = $dp->getBackLinks(false, $sortby, 0, $exclude);
82 while ($b = $bi->next()) {
83 $name = $b->getName();
84 if (isset($bl[$name]))
90 foreach ($bl as $b => $v)
92 $pagelist->addPage($b);
94 $p = $dbi->getPage($page);
95 $pagelist->addPages($p->getBackLinks(false, $sortby, 0, $exclude));
97 $total = $pagelist->getTotal();
99 // Localization note: In English, the differences between the
100 // various phrases spit out here may seem subtle or negligible
101 // enough to tempt you to combine/normalize some of these
102 // strings together, but the grammar employed most by other
103 // languages does not always end up with so subtle a
104 // distinction as it does with English in this case. :)
106 if ($page == $request->getArg('pagename')
107 and !$dbi->isWikiPage($page)
109 // BackLinks plugin is more than likely being called
110 // upon for an empty page on said page, while either
111 // 'browse'ing, 'create'ing or 'edit'ing.
113 // Don't bother displaying a WikiLink 'unknown', just
114 // the Un~WikiLink~ified (plain) name of the uncreated
115 // page currently being viewed.
118 if ($pagelist->isEmpty())
119 return HTML::p(fmt("No other page links to %s yet.", $pagelink));
122 $pagelist->setCaption(fmt("One page would link to %s:",
124 // Some future localizations will actually require
125 // this... (BelieveItOrNot, English-only-speakers!(:)
127 // else if ($pagelist->getTotal() == 2)
128 // $pagelist->setCaption(fmt("Two pages would link to %s:",
131 $pagelist->setCaption(fmt("%s pages would link to %s:",
136 $p = array_shift($tmp_pages);
137 $pagelink = HTML(WikiLink($p, 'auto'));
138 foreach ($tmp_pages as $p)
139 $pagelink->pushContent(" ", _("AND"), " ", WikiLink($p, 'auto'));
141 // BackLinks plugin is being displayed on a normal page.
142 $pagelink = WikiLink($page, 'auto');
144 if ($pagelist->isEmpty())
145 return HTML::p(fmt("No page links to %s.", $pagelink));
147 //trigger_error("DEBUG: " . $pagelist->getTotal());
150 $pagelist->setCaption(fmt("One page links to %s:",
152 // Some future localizations will actually require
153 // this... (BelieveItOrNot, English-only-speakers!(:)
155 // else if ($pagelist->getTotal() == 2)
156 // $pagelist->setCaption(fmt("Two pages link to %s:",
159 $pagelist->setCaption(fmt("%s pages link to %s:",
160 $limit > 0 ? $total : _("Those"),
164 if (!empty($args['linkmore'])
165 and $dbi->isWikiPage($args['linkmore'])
166 and $limit > 0 and $total > $limit
168 $pagelist->addCaption(WikiLink($args['linkmore'], "auto", _("More...")));
174 // how many links from this backLink to other pages
175 class _PageList_Column_BackLinks_count extends _PageList_Column
177 function _getValue($page, &$revision_handle)
179 $iter = $page->getPageLinks();
180 $count = $iter->count();
189 // c-hanging-comment-ender-p: nil
190 // indent-tabs-mode: nil