]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/BackLinks.php
function _getValue is not private
[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 getName()
29     {
30         return _("BackLinks");
31     }
32
33     function getDescription()
34     {
35         return sprintf(_("List all pages which link to %s."), '[pagename]');
36     }
37
38     function getDefaultArguments()
39     {
40         return array_merge
41         (
42             PageList::supportedArgs(),
43             array('include_self' => false,
44                 'noheader' => false,
45                 'page' => '[pagename]',
46                 'linkmore' => '', // If count>0 and limit>0 display a link with
47                 // the number of all results, linked to the given pagename.
48             ));
49     }
50
51     // info arg allows multiple columns
52     // info=mtime,hits,summary,version,author,locked,minor
53     // exclude arg allows multiple pagenames exclude=HomePage,RecentChanges
54     // NEW: info=count : number of links
55     // page=foo,bar : backlinks to both pages
56     function run($dbi, $argstr, &$request, $basepage)
57     {
58         $args = $this->getArgs($argstr, $request);
59
60         extract($args);
61         if (empty($page) and $page != '0')
62             return '';
63         // exclude is now already expanded in WikiPlugin::getArgs()
64         if (empty($exclude)) $exclude = array();
65         if (!$include_self)
66             $exclude[] = $page;
67         if ($info) {
68             $info = explode(",", $info);
69             if (in_array('count', $info))
70                 $args['types']['count'] =
71                     new _PageList_Column_BackLinks_count('count', _("#"), 'center');
72         }
73         if (!empty($limit))
74             $args['limit'] = $limit;
75         // $args['dosort'] = !empty($args['sortby']); // override DB sort (??)
76         $pagelist = new PageList($info, $exclude, $args);
77
78         // support logical AND: page1,page2
79         $pages = explodePageList($page);
80         $count = count($pages);
81         if (count($pages) > 1) {
82             // AND: the intersection of all these pages
83             $bl = array();
84             foreach ($pages as $p) {
85                 $dp = $dbi->getPage($p);
86                 $bi = $dp->getBackLinks(false, $sortby, 0, $exclude);
87                 while ($b = $bi->next()) {
88                     $name = $b->getName();
89                     if (isset($bl[$name]))
90                         $bl[$name]++;
91                     else
92                         $bl[$name] = 1;
93                 }
94             }
95             foreach ($bl as $b => $v)
96                 if ($v == $count)
97                     $pagelist->addPage($b);
98         } else {
99             $p = $dbi->getPage($page);
100             $pagelist->addPages($p->getBackLinks(false, $sortby, 0, $exclude));
101         }
102         $total = $pagelist->getTotal();
103
104         // Localization note: In English, the differences between the
105         // various phrases spit out here may seem subtle or negligible
106         // enough to tempt you to combine/normalize some of these
107         // strings together, but the grammar employed most by other
108         // languages does not always end up with so subtle a
109         // distinction as it does with English in this case. :)
110         if (!$noheader) {
111             if ($page == $request->getArg('pagename')
112                 and !$dbi->isWikiPage($page)
113             ) {
114                 // BackLinks plugin is more than likely being called
115                 // upon for an empty page on said page, while either
116                 // 'browse'ing, 'create'ing or 'edit'ing.
117                 //
118                 // Don't bother displaying a WikiLink 'unknown', just
119                 // the Un~WikiLink~ified (plain) name of the uncreated
120                 // page currently being viewed.
121                 $pagelink = $page;
122
123                 if ($pagelist->isEmpty())
124                     return HTML::p(fmt("No other page links to %s yet.", $pagelink));
125
126                 if ($total == 1)
127                     $pagelist->setCaption(fmt("One page would link to %s:",
128                         $pagelink));
129                 // Some future localizations will actually require
130                 // this... (BelieveItOrNot, English-only-speakers!(:)
131                 //
132                 // else if ($pagelist->getTotal() == 2)
133                 //     $pagelist->setCaption(fmt("Two pages would link to %s:",
134                 //                               $pagelink));
135                 else
136                     $pagelist->setCaption(fmt("%s pages would link to %s:",
137                         $total, $pagelink));
138             } else {
139                 if ($count) {
140                     $tmp_pages = $pages;
141                     $p = array_shift($tmp_pages);
142                     $pagelink = HTML(WikiLink($p, 'auto'));
143                     foreach ($tmp_pages as $p)
144                         $pagelink->pushContent(" ", _("AND"), " ", WikiLink($p, 'auto'));
145                 } else
146                     // BackLinks plugin is being displayed on a normal page.
147                     $pagelink = WikiLink($page, 'auto');
148
149                 if ($pagelist->isEmpty())
150                     return HTML::p(fmt("No page links to %s.", $pagelink));
151
152                 //trigger_error("DEBUG: " . $pagelist->getTotal());
153
154                 if ($total == 1)
155                     $pagelist->setCaption(fmt("One page links to %s:",
156                         $pagelink));
157                 // Some future localizations will actually require
158                 // this... (BelieveItOrNot, English-only-speakers!(:)
159                 //
160                 // else if ($pagelist->getTotal() == 2)
161                 //     $pagelist->setCaption(fmt("Two pages link to %s:",
162                 //                               $pagelink));
163                 else
164                     $pagelist->setCaption(fmt("%s pages link to %s:",
165                         $limit > 0 ? $total : _("Those"),
166                         $pagelink));
167             }
168         }
169         if (!empty($args['linkmore'])
170             and $dbi->isWikiPage($args['linkmore'])
171                 and $limit > 0 and $total > $limit
172         )
173             $pagelist->addCaption(WikiLink($args['linkmore'], "auto", _("More...")));
174         return $pagelist;
175     }
176
177 }
178
179 // how many links from this backLink to other pages
180 class _PageList_Column_BackLinks_count extends _PageList_Column
181 {
182     function _getValue($page, &$revision_handle)
183     {
184         $iter = $page->getPageLinks();
185         $count = $iter->count();
186         return $count;
187     }
188 }
189
190 // Local Variables:
191 // mode: php
192 // tab-width: 8
193 // c-basic-offset: 4
194 // c-hanging-comment-ender-p: nil
195 // indent-tabs-mode: nil
196 // End: