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
25 require_once('lib/PageList.php');
27 class WikiPlugin_BackLinks
31 return _("BackLinks");
34 function getDescription() {
35 return sprintf(_("List all pages which link to %s."), '[pagename]');
38 function getVersion() {
39 return preg_replace("/[Revision: $]/", '',
43 function getDefaultArguments() {
46 PageList::supportedArgs(),
47 array('include_self' => 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.
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);
64 if (empty($page) and $page != '0')
66 // exclude is now already expanded in WikiPlugin::getArgs()
67 if (empty($exclude)) $exclude = array();
71 $info = explode(",", $info);
72 if (in_array('count',$info))
73 $args['types']['count'] =
74 new _PageList_Column_BackLinks_count('count', _("#"), 'center');
77 $args['limit'] = $limit;
78 $args['dosort'] = !empty($args['sortby']); // override DB sort (??)
79 $pagelist = new PageList($info, $exclude, $args);
81 // support logical AND: page1,page2
82 $pages = explodePageList($page);
83 $count = count($pages);
84 if (count($pages) > 1) {
85 // AND: the intersection of all these pages
87 foreach ($pages as $p) {
88 $dp = $dbi->getPage($p);
89 $bi = $dp->getBackLinks(false, $sortby, 0, $exclude);
90 while ($b = $bi->next()) {
91 $name = $b->getName();
92 if (isset($bl[$name]))
98 foreach ($bl as $b => $v)
100 $pagelist->addPage($b);
102 $p = $dbi->getPage($page);
103 $pagelist->addPages($p->getBackLinks(false, $sortby, 0, $exclude));
105 $total = $pagelist->getTotal();
107 // Localization note: In English, the differences between the
108 // various phrases spit out here may seem subtle or negligible
109 // enough to tempt you to combine/normalize some of these
110 // strings together, but the grammar employed most by other
111 // languages does not always end up with so subtle a
112 // distinction as it does with English in this case. :)
114 if ($page == $request->getArg('pagename')
115 and !$dbi->isWikiPage($page))
117 // BackLinks plugin is more than likely being called
118 // upon for an empty page on said page, while either
119 // 'browse'ing, 'create'ing or 'edit'ing.
121 // Don't bother displaying a WikiLink 'unknown', just
122 // the Un~WikiLink~ified (plain) name of the uncreated
123 // page currently being viewed.
126 if ($pagelist->isEmpty())
127 return HTML::p(fmt("No other page links to %s yet.", $pagelink));
130 $pagelist->setCaption(fmt("One page would link to %s:",
132 // Some future localizations will actually require
133 // this... (BelieveItOrNot, English-only-speakers!(:)
135 // else if ($pagelist->getTotal() == 2)
136 // $pagelist->setCaption(fmt("Two pages would link to %s:",
139 $pagelist->setCaption(fmt("%s pages would link to %s:",
145 $p = array_shift($tmp_pages);
146 $pagelink = HTML(WikiLink($p, 'auto'));
147 foreach ($tmp_pages as $p)
148 $pagelink->pushContent(" ",_("AND")," ",WikiLink($p, 'auto'));
150 // BackLinks plugin is being displayed on a normal page.
151 $pagelink = WikiLink($page, 'auto');
153 if ($pagelist->isEmpty())
154 return HTML::p(fmt("No page links to %s.", $pagelink));
156 //trigger_error("DEBUG: " . $pagelist->getTotal());
159 $pagelist->setCaption(fmt("One page links to %s:",
161 // Some future localizations will actually require
162 // this... (BelieveItOrNot, English-only-speakers!(:)
164 // else if ($pagelist->getTotal() == 2)
165 // $pagelist->setCaption(fmt("Two pages link to %s:",
168 $pagelist->setCaption(fmt("%s pages link to %s:",
169 $limit > 0 ? $total : _("Those"),
173 if (!empty($args['linkmore'])
174 and $dbi->isWikiPage($args['linkmore'])
175 and $limit > 0 and $total > $limit
177 $pagelist->addCaption(WikiLink($args['linkmore'], "auto", _("More...")));
183 // how many links from this backLink to other pages
184 class _PageList_Column_BackLinks_count extends _PageList_Column {
185 function _getValue($page, &$revision_handle) {
186 $iter = $page->getPageLinks();
187 $count = $iter->count();
197 // c-hanging-comment-ender-p: nil
198 // indent-tabs-mode: nil