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