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