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