]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/specials/SpecialWhatlinkshere.php
MediaWiki 1.17.1
[autoinstalls/mediawiki.git] / includes / specials / SpecialWhatlinkshere.php
1 <?php
2 /**
3  * Implements Special:Whatlinkshere
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  * http://www.gnu.org/copyleft/gpl.html
19  *
20  * @file
21  * @todo Use some variant of Pager or something; the pagination here is lousy.
22  */
23
24 /**
25  * Implements Special:Whatlinkshere
26  *
27  * @ingroup SpecialPage
28  */
29 class SpecialWhatLinksHere extends SpecialPage {
30
31         // Stored objects
32         protected $opts, $target, $selfTitle;
33
34         // Stored globals
35         protected $skin;
36
37         protected $limits = array( 20, 50, 100, 250, 500 );
38
39         public function __construct() {
40                 parent::__construct( 'Whatlinkshere' );
41                 global $wgUser;
42                 $this->skin = $wgUser->getSkin();
43         }
44
45         function execute( $par ) {
46                 global $wgOut, $wgRequest;
47
48                 $this->setHeaders();
49
50                 $opts = new FormOptions();
51
52                 $opts->add( 'target', '' );
53                 $opts->add( 'namespace', '', FormOptions::INTNULL );
54                 $opts->add( 'limit', 50 );
55                 $opts->add( 'from', 0 );
56                 $opts->add( 'back', 0 );
57                 $opts->add( 'hideredirs', false );
58                 $opts->add( 'hidetrans', false );
59                 $opts->add( 'hidelinks', false );
60                 $opts->add( 'hideimages', false );
61
62                 $opts->fetchValuesFromRequest( $wgRequest );
63                 $opts->validateIntBounds( 'limit', 0, 5000 );
64
65                 // Give precedence to subpage syntax
66                 if ( isset($par) ) {
67                         $opts->setValue( 'target', $par );
68                 }
69
70                 // Bind to member variable
71                 $this->opts = $opts;
72
73                 $this->target = Title::newFromURL( $opts->getValue( 'target' ) );
74                 if( !$this->target ) {
75                         $wgOut->addHTML( $this->whatlinkshereForm() );
76                         return;
77                 }
78
79                 $this->selfTitle = $this->getTitle( $this->target->getPrefixedDBkey() );
80
81                 $wgOut->setPageTitle( wfMsg( 'whatlinkshere-title', $this->target->getPrefixedText() ) );
82                 $wgOut->setSubtitle( wfMsg( 'whatlinkshere-backlink', $this->skin->link( $this->target, $this->target->getPrefixedText(), array(), array( 'redirect' => 'no'  ) ) ) );
83
84                 $this->showIndirectLinks( 0, $this->target, $opts->getValue( 'limit' ),
85                         $opts->getValue( 'from' ), $opts->getValue( 'back' ) );
86         }
87
88         /**
89          * @param $level  int     Recursion level
90          * @param $target Title   Target title
91          * @param $limit  int     Number of entries to display
92          * @param $from   Title   Display from this article ID
93          * @param $back   Title   Display from this article ID at backwards scrolling
94          * @private
95          */
96         function showIndirectLinks( $level, $target, $limit, $from = 0, $back = 0 ) {
97                 global $wgOut, $wgMaxRedirectLinksRetrieved;
98                 $dbr = wfGetDB( DB_SLAVE );
99                 $options = array();
100
101                 $hidelinks = $this->opts->getValue( 'hidelinks' );
102                 $hideredirs = $this->opts->getValue( 'hideredirs' );
103                 $hidetrans = $this->opts->getValue( 'hidetrans' );
104                 $hideimages = $target->getNamespace() != NS_FILE || $this->opts->getValue( 'hideimages' );
105
106                 $fetchlinks = (!$hidelinks || !$hideredirs);
107
108                 // Make the query
109                 $plConds = array(
110                         'page_id=pl_from',
111                         'pl_namespace' => $target->getNamespace(),
112                         'pl_title' => $target->getDBkey(),
113                 );
114                 if( $hideredirs ) {
115                         $plConds['page_is_redirect'] = 0;
116                 } elseif( $hidelinks ) {
117                         $plConds['page_is_redirect'] = 1;
118                 }
119
120                 $tlConds = array(
121                         'page_id=tl_from',
122                         'tl_namespace' => $target->getNamespace(),
123                         'tl_title' => $target->getDBkey(),
124                 );
125
126                 $ilConds = array(
127                         'page_id=il_from',
128                         'il_to' => $target->getDBkey(),
129                 );
130
131                 $namespace = $this->opts->getValue( 'namespace' );
132                 if ( is_int($namespace) ) {
133                         $plConds['page_namespace'] = $namespace;
134                         $tlConds['page_namespace'] = $namespace;
135                         $ilConds['page_namespace'] = $namespace;
136                 }
137
138                 if ( $from ) {
139                         $tlConds[] = "tl_from >= $from";
140                         $plConds[] = "pl_from >= $from";
141                         $ilConds[] = "il_from >= $from";
142                 }
143
144                 // Read an extra row as an at-end check
145                 $queryLimit = $limit + 1;
146
147                 // Enforce join order, sometimes namespace selector may
148                 // trigger filesorts which are far less efficient than scanning many entries
149                 $options[] = 'STRAIGHT_JOIN';
150
151                 $options['LIMIT'] = $queryLimit;
152                 $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect' );
153
154                 if( $fetchlinks ) {
155                         $options['ORDER BY'] = 'pl_from';
156                         $plRes = $dbr->select( array( 'pagelinks', 'page' ), $fields,
157                                 $plConds, __METHOD__, $options );
158                 }
159
160                 if( !$hidetrans ) {
161                         $options['ORDER BY'] = 'tl_from';
162                         $tlRes = $dbr->select( array( 'templatelinks', 'page' ), $fields,
163                                 $tlConds, __METHOD__, $options );
164                 }
165
166                 if( !$hideimages ) {
167                         $options['ORDER BY'] = 'il_from';
168                         $ilRes = $dbr->select( array( 'imagelinks', 'page' ), $fields,
169                                 $ilConds, __METHOD__, $options );
170                 }
171
172                 if( ( !$fetchlinks || !$dbr->numRows($plRes) ) && ( $hidetrans || !$dbr->numRows($tlRes) ) && ( $hideimages || !$dbr->numRows($ilRes) ) ) {
173                         if ( 0 == $level ) {
174                                 $wgOut->addHTML( $this->whatlinkshereForm() );
175
176                                 // Show filters only if there are links
177                                 if( $hidelinks || $hidetrans || $hideredirs || $hideimages )
178                                         $wgOut->addHTML( $this->getFilterPanel() );
179
180                                 $errMsg = is_int($namespace) ? 'nolinkshere-ns' : 'nolinkshere';
181                                 $wgOut->addWikiMsg( $errMsg, $this->target->getPrefixedText() );
182                         }
183                         return;
184                 }
185
186                 // Read the rows into an array and remove duplicates
187                 // templatelinks comes second so that the templatelinks row overwrites the
188                 // pagelinks row, so we get (inclusion) rather than nothing
189                 if( $fetchlinks ) {
190                         foreach ( $plRes as $row ) {
191                                 $row->is_template = 0;
192                                 $row->is_image = 0;
193                                 $rows[$row->page_id] = $row;
194                         }
195                 }
196                 if( !$hidetrans ) {
197                         foreach ( $tlRes as $row ) {
198                                 $row->is_template = 1;
199                                 $row->is_image = 0;
200                                 $rows[$row->page_id] = $row;
201                         }
202                 }
203                 if( !$hideimages ) {
204                         foreach ( $ilRes as $row ) {
205                                 $row->is_template = 0;
206                                 $row->is_image = 1;
207                                 $rows[$row->page_id] = $row;
208                         }
209                 }
210
211                 // Sort by key and then change the keys to 0-based indices
212                 ksort( $rows );
213                 $rows = array_values( $rows );
214
215                 $numRows = count( $rows );
216
217                 // Work out the start and end IDs, for prev/next links
218                 if ( $numRows > $limit ) {
219                         // More rows available after these ones
220                         // Get the ID from the last row in the result set
221                         $nextId = $rows[$limit]->page_id;
222                         // Remove undisplayed rows
223                         $rows = array_slice( $rows, 0, $limit );
224                 } else {
225                         // No more rows after
226                         $nextId = false;
227                 }
228                 $prevId = $from;
229
230                 if ( $level == 0 ) {
231                         $wgOut->addHTML( $this->whatlinkshereForm() );
232                         $wgOut->addHTML( $this->getFilterPanel() );
233                         $wgOut->addWikiMsg( 'linkshere', $this->target->getPrefixedText() );
234
235                         $prevnext = $this->getPrevNext( $prevId, $nextId );
236                         $wgOut->addHTML( $prevnext );
237                 }
238
239                 $wgOut->addHTML( $this->listStart( $level ) );
240                 foreach ( $rows as $row ) {
241                         $nt = Title::makeTitle( $row->page_namespace, $row->page_title );
242
243                         if ( $row->page_is_redirect && $level < 2 ) {
244                                 $wgOut->addHTML( $this->listItem( $row, $nt, true ) );
245                                 $this->showIndirectLinks( $level + 1, $nt, $wgMaxRedirectLinksRetrieved );
246                                 $wgOut->addHTML( Xml::closeElement( 'li' ) );
247                         } else {
248                                 $wgOut->addHTML( $this->listItem( $row, $nt ) );
249                         }
250                 }
251
252                 $wgOut->addHTML( $this->listEnd() );
253
254                 if( $level == 0 ) {
255                         $wgOut->addHTML( $prevnext );
256                 }
257         }
258
259         protected function listStart( $level ) {
260                 return Xml::openElement( 'ul', ( $level ? array() : array( 'id' => 'mw-whatlinkshere-list' ) ) );
261         }
262
263         protected function listItem( $row, $nt, $notClose = false ) {
264                 # local message cache
265                 static $msgcache = null;
266                 if ( $msgcache === null ) {
267                         static $msgs = array( 'isredirect', 'istemplate', 'semicolon-separator',
268                                 'whatlinkshere-links', 'isimage' );
269                         $msgcache = array();
270                         foreach ( $msgs as $msg ) {
271                                 $msgcache[$msg] = wfMsgExt( $msg, array( 'escapenoentities' ) );
272                         }
273                 }
274
275                 if( $row->page_is_redirect ) {
276                         $query = array( 'redirect' => 'no' );
277                 } else {
278                         $query = array();
279                 }
280
281                 $link = $this->skin->linkKnown(
282                         $nt,
283                         null,
284                         array(),
285                         $query
286                 );
287
288                 // Display properties (redirect or template)
289                 $propsText = '';
290                 $props = array();
291                 if ( $row->page_is_redirect )
292                         $props[] = $msgcache['isredirect'];
293                 if ( $row->is_template )
294                         $props[] = $msgcache['istemplate'];
295                 if( $row->is_image )
296                         $props[] = $msgcache['isimage'];
297
298                 if ( count( $props ) ) {
299                         $propsText = '(' . implode( $msgcache['semicolon-separator'], $props ) . ')';
300                 }
301
302                 # Space for utilities links, with a what-links-here link provided
303                 $wlhLink = $this->wlhLink( $nt, $msgcache['whatlinkshere-links'] );
304                 $wlh = Xml::wrapClass( "($wlhLink)", 'mw-whatlinkshere-tools' );
305
306                 return $notClose ?
307                         Xml::openElement( 'li' ) . "$link $propsText $wlh\n" :
308                         Xml::tags( 'li', null, "$link $propsText $wlh" ) . "\n";
309         }
310
311         protected function listEnd() {
312                 return Xml::closeElement( 'ul' );
313         }
314
315         protected function wlhLink( Title $target, $text ) {
316                 static $title = null;
317                 if ( $title === null )
318                         $title = $this->getTitle();
319
320                 return $this->skin->linkKnown(
321                         $title,
322                         $text,
323                         array(),
324                         array( 'target' => $target->getPrefixedText() )
325                 );
326         }
327
328         function makeSelfLink( $text, $query ) {
329                 return $this->skin->linkKnown(
330                         $this->selfTitle,
331                         $text,
332                         array(),
333                         $query
334                 );
335         }
336
337         function getPrevNext( $prevId, $nextId ) {
338                 global $wgLang;
339                 $currentLimit = $this->opts->getValue( 'limit' );
340                 $fmtLimit = $wgLang->formatNum( $currentLimit );
341                 $prev = wfMsgExt( 'whatlinkshere-prev', array( 'parsemag', 'escape' ), $fmtLimit );
342                 $next = wfMsgExt( 'whatlinkshere-next', array( 'parsemag', 'escape' ), $fmtLimit );
343
344                 $changed = $this->opts->getChangedValues();
345                 unset($changed['target']); // Already in the request title
346
347                 if ( 0 != $prevId ) {
348                         $overrides = array( 'from' => $this->opts->getValue( 'back' ) );
349                         $prev = $this->makeSelfLink( $prev, array_merge( $changed, $overrides ) );
350                 }
351                 if ( 0 != $nextId ) {
352                         $overrides = array( 'from' => $nextId, 'back' => $prevId );
353                         $next = $this->makeSelfLink( $next, array_merge( $changed, $overrides ) );
354                 }
355
356                 $limitLinks = array();
357                 foreach ( $this->limits as $limit ) {
358                         $prettyLimit = $wgLang->formatNum( $limit );
359                         $overrides = array( 'limit' => $limit );
360                         $limitLinks[] = $this->makeSelfLink( $prettyLimit, array_merge( $changed, $overrides ) );
361                 }
362
363                 $nums = $wgLang->pipeList( $limitLinks );
364
365                 return wfMsgHtml( 'viewprevnext', $prev, $next, $nums );
366         }
367
368         function whatlinkshereForm() {
369                 global $wgScript;
370
371                 // We get nicer value from the title object
372                 $this->opts->consumeValue( 'target' );
373                 // Reset these for new requests
374                 $this->opts->consumeValues( array( 'back', 'from' ) );
375
376                 $target = $this->target ? $this->target->getPrefixedText() : '';
377                 $namespace = $this->opts->consumeValue( 'namespace' );
378
379                 # Build up the form
380                 $f = Xml::openElement( 'form', array( 'action' => $wgScript ) );
381                 
382                 # Values that should not be forgotten
383                 $f .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() );
384                 foreach ( $this->opts->getUnconsumedValues() as $name => $value ) {
385                         $f .= Html::hidden( $name, $value );
386                 }
387
388                 $f .= Xml::fieldset( wfMsg( 'whatlinkshere' ) );
389
390                 # Target input
391                 $f .= Xml::inputLabel( wfMsg( 'whatlinkshere-page' ), 'target',
392                                 'mw-whatlinkshere-target', 40, $target );
393
394                 $f .= ' ';
395
396                 # Namespace selector
397                 $f .= Xml::label( wfMsg( 'namespace' ), 'namespace' ) . '&#160;' .
398                         Xml::namespaceSelector( $namespace, '' );
399
400                 $f .= ' ';
401
402                 # Submit
403                 $f .= Xml::submitButton( wfMsg( 'allpagessubmit' ) );
404
405                 # Close
406                 $f .= Xml::closeElement( 'fieldset' ) . Xml::closeElement( 'form' ) . "\n";
407
408                 return $f;
409         }
410
411         /**
412          * Create filter panel
413          * 
414          * @return string HTML fieldset and filter panel with the show/hide links
415          */
416         function getFilterPanel() {
417                 global $wgLang;
418                 $show = wfMsgHtml( 'show' );
419                 $hide = wfMsgHtml( 'hide' );
420
421                 $changed = $this->opts->getChangedValues();
422                 unset($changed['target']); // Already in the request title
423
424                 $links = array();
425                 $types = array( 'hidetrans', 'hidelinks', 'hideredirs' );
426                 if( $this->target->getNamespace() == NS_FILE )
427                         $types[] = 'hideimages';
428
429                 // Combined message keys: 'whatlinkshere-hideredirs', 'whatlinkshere-hidetrans', 'whatlinkshere-hidelinks', 'whatlinkshere-hideimages'
430                 // To be sure they will be find by grep
431                 foreach( $types as $type ) {
432                         $chosen = $this->opts->getValue( $type );
433                         $msg = $chosen ? $show : $hide;
434                         $overrides = array( $type => !$chosen );
435                         $links[] =  wfMsgHtml( "whatlinkshere-{$type}", $this->makeSelfLink( $msg, array_merge( $changed, $overrides ) ) );
436                 }
437                 return Xml::fieldset( wfMsg( 'whatlinkshere-filters' ), $wgLang->pipeList( $links ) );
438         }
439 }