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
{
27 protected $opts, $target, $selfTitle;
30 protected $skin, $request;
32 protected $limits = array( 20, 50, 100, 250, 500 );
34 function WhatLinksHerePage( $request, $par = null ) {
36 $this->request
= $request;
37 $this->skin
= $wgUser->getSkin();
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 );
55 $opts->fetchValuesFromRequest( $this->request
);
56 $opts->validateIntBounds( 'limit', 0, 5000 );
57 if ( isset($this->par
) ) {
58 $opts->setValue( 'target', $this->par
);
61 // Bind to member variable
64 $this->target
= Title
::newFromURL( $opts->getValue( 'target' ) );
65 if( !$this->target
) {
66 $wgOut->addHTML( $this->whatlinkshereForm() );
69 $this->selfTitle
= Title
::makeTitleSafe( NS_SPECIAL
,
70 'Whatlinkshere/' . $this->target
->getPrefixedDBkey() );
72 $wgOut->setPageTitle( wfMsg( 'whatlinkshere-title', $this->target
->getPrefixedText() ) );
73 $wgOut->setSubtitle( wfMsg( 'linklistsub' ) );
75 $wgOut->addHTML( wfMsgExt( 'whatlinkshere-barrow', array( 'escapenoentities') ) . ' ' .$this->skin
->makeLinkObj($this->target
, '', 'redirect=no' )."<br />\n");
77 $this->showIndirectLinks( 0, $this->target
, $opts->getValue( 'limit' ),
78 $opts->getValue( 'from' ), $opts->getValue( 'back' ) );
82 * @param int $level Recursion level
83 * @param Title $target Target title
84 * @param int $limit Number of entries to display
85 * @param Title $from Display from this article ID
86 * @param Title $back Display from this article ID at backwards scrolling
89 function showIndirectLinks( $level, $target, $limit, $from = 0, $back = 0 ) {
90 global $wgOut, $wgMaxRedirectLinksRetrieved;
91 $dbr = wfGetDB( DB_SLAVE
);
94 $hidelinks = $this->opts
->getValue( 'hidelinks' );
95 $hideredirs = $this->opts
->getValue( 'hideredirs' );
96 $hidetrans = $this->opts
->getValue( 'hidetrans' );
98 $fetchlinks = !$hidelinks ||
!$hideredirs;
103 'pl_namespace' => $target->getNamespace(),
104 'pl_title' => $target->getDBkey(),
107 $plConds['page_is_redirect'] = 0;
108 } elseif( $hidelinks ) {
109 $plConds['page_is_redirect'] = 1;
114 'tl_namespace' => $target->getNamespace(),
115 'tl_title' => $target->getDBkey(),
119 $namespace = $this->opts
->getValue( 'namespace' );
120 if ( is_int($namespace) ){
121 $plConds['page_namespace'] = $namespace;
122 $tlConds['page_namespace'] = $namespace;
126 $tlConds[] = "tl_from >= $from";
127 $plConds[] = "pl_from >= $from";
130 // Read an extra row as an at-end check
131 $queryLimit = $limit +
1;
133 // enforce join order, sometimes namespace selector may
134 // trigger filesorts which are far less efficient than scanning many entries
135 $options[] = 'STRAIGHT_JOIN';
137 $options['LIMIT'] = $queryLimit;
138 $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect' );
141 $options['ORDER BY'] = 'pl_from';
142 $plRes = $dbr->select( array( 'pagelinks', 'page' ), $fields,
143 $plConds, __METHOD__
, $options );
147 $options['ORDER BY'] = 'tl_from';
148 $tlRes = $dbr->select( array( 'templatelinks', 'page' ), $fields,
149 $tlConds, __METHOD__
, $options );
152 if( ( !$fetchlinks ||
!$dbr->numRows( $plRes ) ) && ( $hidetrans ||
!$dbr->numRows( $tlRes ) ) ) {
154 $wgOut->addHTML( $this->whatlinkshereForm() );
155 $errMsg = is_int($namespace) ?
'nolinkshere-ns' : 'nolinkshere';
156 $wgOut->addWikiMsg( $errMsg, $this->target
->getPrefixedText() );
157 // Show filters only if there are links
158 if( $hidelinks ||
$hidetrans ||
$hideredirs )
159 $wgOut->addHTML( $this->getFilterPanel() );
164 // Read the rows into an array and remove duplicates
165 // templatelinks comes second so that the templatelinks row overwrites the
166 // pagelinks row, so we get (inclusion) rather than nothing
168 while ( $row = $dbr->fetchObject( $plRes ) ) {
169 $row->is_template
= 0;
170 $rows[$row->page_id
] = $row;
172 $dbr->freeResult( $plRes );
176 while ( $row = $dbr->fetchObject( $tlRes ) ) {
177 $row->is_template
= 1;
178 $rows[$row->page_id
] = $row;
180 $dbr->freeResult( $tlRes );
183 // Sort by key and then change the keys to 0-based indices
185 $rows = array_values( $rows );
187 $numRows = count( $rows );
189 // Work out the start and end IDs, for prev/next links
190 if ( $numRows > $limit ) {
191 // More rows available after these ones
192 // Get the ID from the last row in the result set
193 $nextId = $rows[$limit]->page_id
;
194 // Remove undisplayed rows
195 $rows = array_slice( $rows, 0, $limit );
197 // No more rows after
203 $wgOut->addHTML( $this->whatlinkshereForm( ) );
204 $wgOut->addHTML( $this->getFilterPanel() );
205 $wgOut->addWikiMsg( 'linkshere', $this->target
->getPrefixedText() );
207 $prevnext = $this->getPrevNext( $prevId, $nextId );
208 $wgOut->addHTML( $prevnext );
211 $wgOut->addHTML( $this->listStart() );
212 foreach ( $rows as $row ) {
213 $nt = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
215 $wgOut->addHTML( $this->listItem( $row, $nt ) );
217 if ( $row->page_is_redirect
&& $level < 2 ) {
218 $this->showIndirectLinks( $level +
1, $nt, $wgMaxRedirectLinksRetrieved );
222 $wgOut->addHTML( $this->listEnd() );
225 $wgOut->addHTML( $prevnext );
229 protected function listStart() {
230 return Xml
::openElement( 'ul' );
233 protected function listItem( $row, $nt ) {
234 # local message cache
235 static $msgcache = null;
236 if ( $msgcache === null ) {
237 static $msgs = array( 'isredirect', 'istemplate', 'semicolon-separator',
238 'whatlinkshere-links' );
240 foreach ( $msgs as $msg ) {
241 $msgcache[$msg] = wfMsgHtml( $msg );
245 $suppressRedirect = $row->page_is_redirect ?
'redirect=no' : '';
246 $link = $this->skin
->makeKnownLinkObj( $nt, '', $suppressRedirect );
248 // Display properties (redirect or template)
251 if ( $row->page_is_redirect
)
252 $props[] = $msgcache['isredirect'];
253 if ( $row->is_template
)
254 $props[] = $msgcache['istemplate'];
256 if ( count( $props ) ) {
257 $propsText = '(' . implode( $msgcache['semicolon-separator'], $props ) . ')';
260 # Space for utilities links, with a what-links-here link provided
261 $wlhLink = $this->wlhLink( $nt, $msgcache['whatlinkshere-links'] );
262 $wlh = Xml
::wrapClass( "($wlhLink)", 'mw-whatlinkshere-tools' );
264 return Xml
::tags( 'li', null, "$link $propsText $wlh" ) . "\n";
267 protected function listEnd() {
268 return Xml
::closeElement( 'ul' );
271 protected function wlhLink( Title
$target, $text ) {
272 static $title = null;
273 if ( $title === null )
274 $title = SpecialPage
::getTitleFor( 'Whatlinkshere' );
276 $targetText = $target->getPrefixedUrl();
277 return $this->skin
->makeKnownLinkObj( $title, $text, 'target=' . $targetText );
280 function makeSelfLink( $text, $query ) {
281 return $this->skin
->makeKnownLinkObj( $this->selfTitle
, $text, $query );
284 function getPrevNext( $prevId, $nextId ) {
286 $currentLimit = $this->opts
->getValue( 'limit' );
287 $fmtLimit = $wgLang->formatNum( $currentLimit );
288 $prev = wfMsgExt( 'whatlinkshere-prev', array( 'parsemag', 'escape' ), $fmtLimit );
289 $next = wfMsgExt( 'whatlinkshere-next', array( 'parsemag', 'escape' ), $fmtLimit );
291 $changed = $this->opts
->getChangedValues();
292 unset($changed['target']); // Already in the request title
294 if ( 0 != $prevId ) {
295 $overrides = array( 'from' => $this->opts
->getValue( 'back' ) );
296 $prev = $this->makeSelfLink( $prev, wfArrayToCGI( $overrides, $changed ) );
298 if ( 0 != $nextId ) {
299 $overrides = array( 'from' => $nextId, 'back' => $prevId );
300 $next = $this->makeSelfLink( $next, wfArrayToCGI( $overrides, $changed ) );
303 $limitLinks = array();
304 foreach ( $this->limits
as $limit ) {
305 $prettyLimit = $wgLang->formatNum( $limit );
306 $overrides = array( 'limit' => $limit );
307 $limitLinks[] = $this->makeSelfLink( $prettyLimit, wfArrayToCGI( $overrides, $changed ) );
310 $nums = implode ( ' | ', $limitLinks );
312 return wfMsgHtml( 'viewprevnext', $prev, $next, $nums );
315 function whatlinkshereForm() {
316 global $wgScript, $wgTitle;
318 // We get nicer value from the title object
319 $this->opts
->consumeValue( 'target' );
320 // Reset these for new requests
321 $this->opts
->consumeValues( array( 'back', 'from' ) );
323 $target = $this->target ?
$this->target
->getPrefixedText() : '';
324 $namespace = $this->opts
->consumeValue( 'namespace' );
327 $f = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
329 # Values that should not be forgotten
330 $f .= Xml
::hidden( 'title', $wgTitle->getPrefixedText() );
331 foreach ( $this->opts
->getUnconsumedValues() as $name => $value ) {
332 $f .= Xml
::hidden( $name, $value );
335 $f .= Xml
::openElement( 'fieldset' );
336 $f .= Xml
::element( 'legend', null, wfMsg( 'whatlinkshere' ) );
339 $f .= Xml
::inputLabel( wfMsg( 'whatlinkshere-page' ), 'target',
340 'mw-whatlinkshere-target', 40, $target );
345 $f .= Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
346 Xml
::namespaceSelector( $namespace, '' );
349 $f .= Xml
::submitButton( wfMsg( 'allpagessubmit' ) );
352 $f .= Xml
::closeElement( 'fieldset' ) . Xml
::closeElement( 'form' ) . "\n";
357 function getFilterPanel() {
358 $show = wfMsgHtml( 'show' );
359 $hide = wfMsgHtml( 'hide' );
361 $changed = $this->opts
->getChangedValues();
362 unset($changed['target']); // Already in the request title
365 foreach( array( 'hidetrans', 'hidelinks', 'hideredirs' ) as $type ) {
366 $chosen = $this->opts
->getValue( $type );
367 $msg = wfMsgHtml( "whatlinkshere-{$type}", $chosen ?
$show : $hide );
368 $overrides = array( $type => !$chosen );
369 $links[] = $this->makeSelfLink( $msg, wfArrayToCGI( $overrides, $changed ) );
371 return Xml
::tags( 'fieldset', null,
372 Xml
::element( 'legend', null, wfMsg( 'whatlinkshere-filters' ) ) .
373 implode( ' | ', $links )