]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/BackLinks.php
getName should not translate
[SourceForge/phpwiki.git] / lib / plugin / BackLinks.php
1 <?php
2
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 along
19  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 require_once 'lib/PageList.php';
24
25 class WikiPlugin_BackLinks
26     extends WikiPlugin
27 {
28     function getDescription()
29     {
30         return sprintf(_("List all pages which link to %s."), '[pagename]');
31     }
32
33     function getDefaultArguments()
34     {
35         return array_merge
36         (
37             PageList::supportedArgs(),
38             array('include_self' => false,
39                 'noheader' => 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.
43             ));
44     }
45
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)
52     {
53         $args = $this->getArgs($argstr, $request);
54
55         extract($args);
56         if (empty($page) and $page != '0')
57             return '';
58         // exclude is now already expanded in WikiPlugin::getArgs()
59         if (empty($exclude)) $exclude = array();
60         if (!$include_self)
61             $exclude[] = $page;
62         if ($info) {
63             $info = explode(",", $info);
64             if (in_array('count', $info))
65                 $args['types']['count'] =
66                     new _PageList_Column_BackLinks_count('count', _("#"), 'center');
67         }
68         if (!empty($limit))
69             $args['limit'] = $limit;
70         // $args['dosort'] = !empty($args['sortby']); // override DB sort (??)
71         $pagelist = new PageList($info, $exclude, $args);
72
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
78             $bl = array();
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]))
85                         $bl[$name]++;
86                     else
87                         $bl[$name] = 1;
88                 }
89             }
90             foreach ($bl as $b => $v)
91                 if ($v == $count)
92                     $pagelist->addPage($b);
93         } else {
94             $p = $dbi->getPage($page);
95             $pagelist->addPages($p->getBackLinks(false, $sortby, 0, $exclude));
96         }
97         $total = $pagelist->getTotal();
98
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. :)
105         if (!$noheader) {
106             if ($page == $request->getArg('pagename')
107                 and !$dbi->isWikiPage($page)
108             ) {
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.
112                 //
113                 // Don't bother displaying a WikiLink 'unknown', just
114                 // the Un~WikiLink~ified (plain) name of the uncreated
115                 // page currently being viewed.
116                 $pagelink = $page;
117
118                 if ($pagelist->isEmpty())
119                     return HTML::p(fmt("No other page links to %s yet.", $pagelink));
120
121                 if ($total == 1)
122                     $pagelist->setCaption(fmt("One page would link to %s:",
123                         $pagelink));
124                 // Some future localizations will actually require
125                 // this... (BelieveItOrNot, English-only-speakers!(:)
126                 //
127                 // else if ($pagelist->getTotal() == 2)
128                 //     $pagelist->setCaption(fmt("Two pages would link to %s:",
129                 //                               $pagelink));
130                 else
131                     $pagelist->setCaption(fmt("%s pages would link to %s:",
132                         $total, $pagelink));
133             } else {
134                 if ($count) {
135                     $tmp_pages = $pages;
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'));
140                 } else
141                     // BackLinks plugin is being displayed on a normal page.
142                     $pagelink = WikiLink($page, 'auto');
143
144                 if ($pagelist->isEmpty())
145                     return HTML::p(fmt("No page links to %s.", $pagelink));
146
147                 //trigger_error("DEBUG: " . $pagelist->getTotal());
148
149                 if ($total == 1)
150                     $pagelist->setCaption(fmt("One page links to %s:",
151                         $pagelink));
152                 // Some future localizations will actually require
153                 // this... (BelieveItOrNot, English-only-speakers!(:)
154                 //
155                 // else if ($pagelist->getTotal() == 2)
156                 //     $pagelist->setCaption(fmt("Two pages link to %s:",
157                 //                               $pagelink));
158                 else
159                     $pagelist->setCaption(fmt("%s pages link to %s:",
160                         $limit > 0 ? $total : _("Those"),
161                         $pagelink));
162             }
163         }
164         if (!empty($args['linkmore'])
165             and $dbi->isWikiPage($args['linkmore'])
166                 and $limit > 0 and $total > $limit
167         )
168             $pagelist->addCaption(WikiLink($args['linkmore'], "auto", _("More...")));
169         return $pagelist;
170     }
171
172 }
173
174 // how many links from this backLink to other pages
175 class _PageList_Column_BackLinks_count extends _PageList_Column
176 {
177     function _getValue($page, &$revision_handle)
178     {
179         $iter = $page->getPageLinks();
180         $count = $iter->count();
181         return $count;
182     }
183 }
184
185 // Local Variables:
186 // mode: php
187 // tab-width: 8
188 // c-basic-offset: 4
189 // c-hanging-comment-ender-p: nil
190 // indent-tabs-mode: nil
191 // End: