3 * @TODO: Use some variant of Pager or something; the pagination here is lousy.
5 * @addtogroup SpecialPage
10 * @param string $par An article name ??
12 function wfSpecialWhatlinkshere($par = NULL) {
14 $page = new WhatLinksHerePage( $wgRequest, $par );
19 * implements Special:Whatlinkshere
20 * @addtogroup SpecialPage
22 class WhatLinksHerePage
{
24 var $limit, $from, $back, $target;
25 var $selfTitle, $skin;
26 var $hideredirs, $hidetrans, $hidelinks, $fetchlinks;
30 function WhatLinksHerePage( $request, $par = null ) {
32 $this->request
= $request;
33 $this->skin
= $wgUser->getSkin();
40 $this->limit
= min( $this->request
->getInt( 'limit', 50 ), 5000 );
41 if ( $this->limit
<= 0 ) {
44 $this->from
= $this->request
->getInt( 'from' );
45 $this->back
= $this->request
->getInt( 'back' );
47 $this->hideredirs
= $this->request
->getInt( 'hideredirs' );
48 $this->hidetrans
= $this->request
->getInt( 'hidetrans' );
49 $this->hidelinks
= $this->request
->getInt( 'hidelinks' );
50 $this->fetchlinks
= !$this->hidelinks ||
!$this->hideredirs
;
52 $targetString = isset($this->par
) ?
$this->par
: $this->request
->getVal( 'target' );
54 if ( is_null( $targetString ) ) {
55 $wgOut->addHTML( $this->whatlinkshereForm() );
59 $this->target
= Title
::newFromURL( $targetString );
60 if( !$this->target
) {
61 $wgOut->addHTML( $this->whatlinkshereForm() );
64 $this->selfTitle
= Title
::makeTitleSafe( NS_SPECIAL
,
65 'Whatlinkshere/' . $this->target
->getPrefixedDBkey() );
67 $wgOut->setPageTitle( wfMsg( 'whatlinkshere-title', $this->target
->getPrefixedText() ) );
68 $wgOut->setSubtitle( wfMsg( 'linklistsub' ) );
70 $wgOut->addHTML( wfMsgExt( 'whatlinkshere-barrow', array( 'escapenoentities') ) . ' ' .$this->skin
->makeLinkObj($this->target
, '', 'redirect=no' )."<br />\n");
72 $this->showIndirectLinks( 0, $this->target
, $this->limit
, $this->from
, $this->back
);
76 * @param int $level Recursion level
77 * @param Title $target Target title
78 * @param int $limit Number of entries to display
79 * @param Title $from Display from this article ID
80 * @param Title $back Display from this article ID at backwards scrolling
83 function showIndirectLinks( $level, $target, $limit, $from = 0, $back = 0 ) {
84 global $wgOut, $wgMaxRedirectLinksRetrieved;
85 $dbr = wfGetDB( DB_SLAVE
);
88 $ns = $this->request
->getIntOrNull( 'namespace' );
90 $options['namespace'] = $ns;
91 $this->setNamespace( $options['namespace'] );
93 $options['namespace'] = '';
99 'pl_namespace' => $target->getNamespace(),
100 'pl_title' => $target->getDBkey(),
105 'tl_namespace' => $target->getNamespace(),
106 'tl_title' => $target->getDBkey(),
109 if ( $this->namespace !== null ){
110 $plConds['page_namespace'] = (int)$this->namespace;
111 $tlConds['page_namespace'] = (int)$this->namespace;
115 $from = (int)$from; // just in case
116 $tlConds[] = "tl_from >= $from";
117 $plConds[] = "pl_from >= $from";
120 // Read an extra row as an at-end check
121 $queryLimit = $limit +
1;
123 // enforce join order, sometimes namespace selector may
124 // trigger filesorts which are far less efficient than scanning many entries
125 $options[] = 'STRAIGHT_JOIN';
127 $options['LIMIT'] = $queryLimit;
128 $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect' );
130 if( $this->fetchlinks
) {
131 $options['ORDER BY'] = 'pl_from';
132 $plRes = $dbr->select( array( 'pagelinks', 'page' ), $fields,
133 $plConds, __METHOD__
, $options );
136 if( !$this->hidetrans
) {
137 $options['ORDER BY'] = 'tl_from';
138 $tlRes = $dbr->select( array( 'templatelinks', 'page' ), $fields,
139 $tlConds, __METHOD__
, $options );
142 if( ( !$this->fetchlinks ||
!$dbr->numRows( $plRes ) ) && ( $this->hidetrans ||
!$dbr->numRows( $tlRes ) ) ) {
144 $options = array(); // reinitialize for a further namespace search
145 // really no links to here
146 $options['namespace'] = $this->namespace;
147 $options['target'] = $this->target
->getPrefixedText();
148 list( $options['limit'], $options['offset']) = wfCheckLimits();
149 $wgOut->addHTML( $this->whatlinkshereForm( $options ) );
150 $errMsg = isset( $this->namespace ) ?
'nolinkshere-ns' : 'nolinkshere';
151 $wgOut->addWikiMsg( $errMsg, $this->target
->getPrefixedText() );
152 // Show filters only if there are links
153 if( $this->hidelinks ||
$this->hidetrans ||
$this->hideredirs
)
154 $wgOut->addHTML( $this->getFilterPanel() );
160 list( $options['limit'], $options['offset']) = wfCheckLimits();
161 if ( ( $ns = $this->request
->getVal( 'namespace', null ) ) !== null && $ns !== '' && ctype_digit($ns) ) {
162 $options['namespace'] = intval( $ns );
163 $this->setNamespace( $options['namespace'] );
165 $options['namespace'] = '';
166 $this->setNamespace( null );
168 $options['offset'] = $this->request
->getVal( 'offset' );
169 /* Offset must be an integral. */
170 if ( !strlen( $options['offset'] ) ||
!preg_match( '/^[0-9]+$/', $options['offset'] ) )
171 $options['offset'] = '';
172 $options['target'] = $this->target
->getPrefixedText();
174 // Read the rows into an array and remove duplicates
175 // templatelinks comes second so that the templatelinks row overwrites the
176 // pagelinks row, so we get (inclusion) rather than nothing
177 if( $this->fetchlinks
) {
178 while ( $row = $dbr->fetchObject( $plRes ) ) {
179 $row->is_template
= 0;
180 $rows[$row->page_id
] = $row;
182 $dbr->freeResult( $plRes );
185 if( !$this->hidetrans
) {
186 while ( $row = $dbr->fetchObject( $tlRes ) ) {
187 $row->is_template
= 1;
188 $rows[$row->page_id
] = $row;
190 $dbr->freeResult( $tlRes );
193 // Sort by key and then change the keys to 0-based indices
195 $rows = array_values( $rows );
197 $numRows = count( $rows );
199 // Work out the start and end IDs, for prev/next links
200 if ( $numRows > $limit ) {
201 // More rows available after these ones
202 // Get the ID from the last row in the result set
203 $nextId = $rows[$limit]->page_id
;
204 // Remove undisplayed rows
205 $rows = array_slice( $rows, 0, $limit );
207 // No more rows after
213 $wgOut->addHTML( $this->whatlinkshereForm( $options ) );
214 $wgOut->addHTML( $this->getFilterPanel() );
215 $wgOut->addWikiMsg( 'linkshere', $this->target
->getPrefixedText() );
217 $prevnext = $this->getPrevNext( $limit, $prevId, $nextId, $options['namespace'] );
218 $wgOut->addHTML( $prevnext );
221 $wgOut->addHTML( $this->listStart() );
222 foreach ( $rows as $row ) {
223 if( $this->hideredirs
&& $row->page_is_redirect
)
225 if( $this->hidelinks
&& ( !$row->page_is_redirect
&& !$row->is_template
) )
228 $nt = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
230 $wgOut->addHTML( $this->listItem( $row, $nt ) );
232 if ( $row->page_is_redirect
&& $level < 2 ) {
233 $this->showIndirectLinks( $level +
1, $nt, $wgMaxRedirectLinksRetrieved );
237 $wgOut->addHTML( $this->listEnd() );
240 $wgOut->addHTML( $prevnext );
244 protected function listStart() {
245 return Xml
::openElement( 'ul' );
248 protected function listItem( $row, $nt ) {
249 # local message cache
250 static $msgcache = null;
251 if ( $msgcache === null ) {
252 static $msgs = array( 'isredirect', 'istemplate', 'semicolon-separator',
253 'whatlinkshere-links' );
255 foreach ( $msgs as $msg ) {
256 $msgcache[$msg] = wfMsgHtml( $msg );
260 $suppressRedirect = $row->page_is_redirect ?
'redirect=no' : '';
261 $link = $this->skin
->makeKnownLinkObj( $nt, '', $suppressRedirect );
263 // Display properties (redirect or template)
266 if ( $row->page_is_redirect
)
267 $props[] = $msgcache['isredirect'];
268 if ( $row->is_template
)
269 $props[] = $msgcache['istemplate'];
271 if ( count( $props ) ) {
272 $propsText = '(' . implode( $msgcache['semicolon-separator'], $props ) . ')';
275 # Space for utilities links, with a what-links-here link provided
276 $wlhLink = $this->wlhLink( $nt, $msgcache['whatlinkshere-links'] );
277 $wlh = Xml
::wrapClass( "($wlhLink)", 'mw-whatlinkshere-tools' );
279 return Xml
::tags( 'li', null, "$link $propsText $wlh" ) . "\n";
282 protected function listEnd() {
283 return Xml
::closeElement( 'ul' );
286 protected function wlhLink( Title
$target, $text ) {
287 static $title = null;
288 if ( $title === null )
289 $title = SpecialPage
::getTitleFor( 'Whatlinkshere' );
291 $targetText = $target->getPrefixedUrl();
292 return $this->skin
->makeKnownLinkObj( $title, $text, 'target=' . $targetText );
295 function makeSelfLink( $text, $query ) {
296 return $this->skin
->makeKnownLinkObj( $this->selfTitle
, $text, $query );
299 /** Get a query string to append to hide appropriate entries */
300 function getHideParams() {
301 return ($this->hidetrans ?
'&hidetrans=1' : '')
302 . ($this->hidelinks ?
'&hidelinks=1' : '')
303 . ($this->hideredirs ?
'&hideredirs=1' : '');
306 function getPrevNext( $limit, $prevId, $nextId ) {
308 $fmtLimit = $wgLang->formatNum( $limit );
309 $prev = wfMsgExt( 'whatlinkshere-prev', array( 'parsemag', 'escape' ), $fmtLimit );
310 $next = wfMsgExt( 'whatlinkshere-next', array( 'parsemag', 'escape' ), $fmtLimit );
313 if( is_int($this->namespace) ) {
314 $nsText = "&namespace={$this->namespace}";
317 $hideParams = $this->getHideParams();
319 if ( 0 != $prevId ) {
320 $prevLink = $this->makeSelfLink( $prev, "limit={$limit}&from={$this->back}{$nsText}{$hideParams}" );
324 if ( 0 != $nextId ) {
325 $nextLink = $this->makeSelfLink( $next, "limit={$limit}&from={$nextId}&back={$prevId}{$nsText}{$hideParams}" );
329 $nums = $this->numLink( 20, $prevId ) . ' | ' .
330 $this->numLink( 50, $prevId ) . ' | ' .
331 $this->numLink( 100, $prevId ) . ' | ' .
332 $this->numLink( 250, $prevId ) . ' | ' .
333 $this->numLink( 500, $prevId );
335 return wfMsgHtml( 'viewprevnext', $prevLink, $nextLink, $nums );
338 function numLink( $limit, $from, $ns = null ) {
340 $query = "limit={$limit}&from={$from}";
341 if( is_int($this->namespace) ) { $query .= "&namespace={$this->namespace}";}
342 $fmtLimit = $wgLang->formatNum( $limit );
343 return $this->makeSelfLink( $fmtLimit, $query.$this->getHideParams() );
346 function whatlinkshereForm( $options = array( 'target' => '', 'namespace' => '' ) ) {
347 global $wgScript, $wgTitle;
349 $options['title'] = $wgTitle->getPrefixedText();
351 $f = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) ) .
352 Xml
::openElement( 'fieldset' ) .
353 Xml
::element( 'legend', array(), wfMsg( 'whatlinkshere' ) ) .
354 Xml
::inputLabel( wfMsg( 'whatlinkshere-page' ), 'target', 'mw-whatlinkshere-target', 40, $options['target'] ) . ' ';
356 foreach ( $options as $name => $value ) {
357 if( $name === 'namespace' ||
$name === 'target' )
359 $f .= "\t" . Xml
::hidden( $name, $value ). "\n";
362 $f .= Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
363 Xml
::namespaceSelector( $options['namespace'], '' ) .
364 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) .
365 Xml
::closeElement( 'fieldset' ) .
366 Xml
::closeElement( 'form' ) . "\n";
371 /** Set the namespace we are filtering on */
372 private function setNamespace( $ns ) {
373 $this->namespace = $ns;
376 function getFilterPanel() {
377 $show = wfMsgHtml( 'show' );
378 $hide = wfMsgHtml( 'hide' );
380 foreach( array( 'hidetrans', 'hidelinks', 'hideredirs' ) as $type ) {
381 $chosen = $this->$type;
382 $msg = wfMsgHtml( "whatlinkshere-{$type}", $chosen ?
$show : $hide );
383 $url = $this->request
->appendQueryValue( $type, intval( !$chosen ), true );
384 $links[] = $this->makeSelfLink( $msg, $url );
386 return '<p>' . implode( ' | ', $links ) . '</p>';