4 * @addtogroup SpecialPage
9 * @param string $par An article name ??
11 function wfSpecialWhatlinkshere($par = NULL) {
13 $page = new WhatLinksHerePage( $wgRequest, $par );
18 * implements Special:Whatlinkshere
19 * @addtogroup SpecialPage
21 class WhatLinksHerePage
{
23 var $limit, $from, $back, $target;
24 var $selfTitle, $skin;
25 var $hideredirs, $hidetrans, $hidelinks, $fetchlinks;
29 function WhatLinksHerePage( $request, $par = null ) {
31 $this->request
= $request;
32 $this->skin
= $wgUser->getSkin();
39 $this->limit
= min( $this->request
->getInt( 'limit', 50 ), 5000 );
40 if ( $this->limit
<= 0 ) {
43 $this->from
= $this->request
->getInt( 'from' );
44 $this->back
= $this->request
->getInt( 'back' );
46 $this->hideredirs
= $this->request
->getInt( 'hideredirs' );
47 $this->hidetrans
= $this->request
->getInt( 'hidetrans' );
48 $this->hidelinks
= $this->request
->getInt( 'hidelinks' );
49 $this->fetchlinks
= !$this->hidelinks ||
!$this->hideredirs
;
51 $targetString = isset($this->par
) ?
$this->par
: $this->request
->getVal( 'target' );
53 if ( is_null( $targetString ) ) {
54 $wgOut->addHTML( $this->whatlinkshereForm() );
58 $this->target
= Title
::newFromURL( $targetString );
59 if( !$this->target
) {
60 $wgOut->addHTML( $this->whatlinkshereForm() );
63 $this->selfTitle
= Title
::makeTitleSafe( NS_SPECIAL
,
64 'Whatlinkshere/' . $this->target
->getPrefixedDBkey() );
66 $wgOut->setPageTitle( wfMsg( 'whatlinkshere-title', $this->target
->getPrefixedText() ) );
67 $wgOut->setSubtitle( wfMsg( 'linklistsub' ) );
69 $wgOut->addHTML( wfMsgExt( 'whatlinkshere-barrow', array( 'escapenoentities') ) . ' ' .$this->skin
->makeLinkObj($this->target
, '', 'redirect=no' )."<br />\n");
71 $this->showIndirectLinks( 0, $this->target
, $this->limit
, $this->from
, $this->back
);
75 * @param int $level Recursion level
76 * @param Title $target Target title
77 * @param int $limit Number of entries to display
78 * @param Title $from Display from this article ID
79 * @param Title $back Display from this article ID at backwards scrolling
82 function showIndirectLinks( $level, $target, $limit, $from = 0, $back = 0 ) {
83 global $wgOut, $wgMaxRedirectLinksRetrieved;
84 $fname = 'WhatLinksHerePage::showIndirectLinks';
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, $fname, $options );
136 if( !$this->hidetrans
) {
137 $options['ORDER BY'] = 'tl_from';
138 $tlRes = $dbr->select( array( 'templatelinks', 'page' ), $fields,
139 $tlConds, $fname, $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( '<ul>' );
222 foreach ( $rows as $row ) {
223 $nt = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
225 if( $this->hideredirs
&& $row->page_is_redirect
)
227 if( $this->hidelinks
&& ( !$row->page_is_redirect
&& !$row->is_template
) )
230 if ( $row->page_is_redirect
) {
231 $extra = 'redirect=no';
236 $link = $this->skin
->makeKnownLinkObj( $nt, '', $extra );
237 $wgOut->addHTML( '<li>'.$link );
239 // Display properties (redirect or template)
241 if ( $row->page_is_redirect
) {
242 $props[] = wfMsgHtml( 'isredirect' );
244 if ( $row->is_template
) {
245 $props[] = wfMsgHtml( 'istemplate' );
247 if ( count( $props ) ) {
248 $list = implode( wfMsgHtml( 'semicolon-separator' ), $props );
249 $wgOut->addHTML( " ($list) " );
252 # Space for utilities links, with a what-links-here link provided
253 $wlh = $this->skin
->makeKnownLinkObj(
254 SpecialPage
::getTitleFor( 'Whatlinkshere' ),
255 wfMsgHtml( 'whatlinkshere-links' ),
256 'target=' . $nt->getPrefixedUrl()
258 $wgOut->addHtml( ' <span class="mw-whatlinkshere-tools">(' . $wlh . ')</span>' );
260 if ( $row->page_is_redirect
) {
262 $this->showIndirectLinks( $level +
1, $nt, $wgMaxRedirectLinksRetrieved );
265 $wgOut->addHTML( "</li>\n" );
267 $wgOut->addHTML( "</ul>\n" );
270 $wgOut->addHTML( $prevnext );
274 function makeSelfLink( $text, $query ) {
275 return $this->skin
->makeKnownLinkObj( $this->selfTitle
, $text, $query );
278 function getPrevNext( $limit, $prevId, $nextId ) {
280 $fmtLimit = $wgLang->formatNum( $limit );
281 $prev = wfMsgExt( 'whatlinkshere-prev', array( 'parsemag', 'escape' ), $fmtLimit );
282 $next = wfMsgExt( 'whatlinkshere-next', array( 'parsemag', 'escape' ), $fmtLimit );
285 if( is_int($this->namespace) ) {
286 $nsText = "&namespace={$this->namespace}";
289 if ( 0 != $prevId ) {
290 $prevLink = $this->makeSelfLink( $prev, "limit={$limit}&from={$this->back}{$nsText}" );
294 if ( 0 != $nextId ) {
295 $nextLink = $this->makeSelfLink( $next, "limit={$limit}&from={$nextId}&back={$prevId}{$nsText}" );
299 $nums = $this->numLink( 20, $prevId ) . ' | ' .
300 $this->numLink( 50, $prevId ) . ' | ' .
301 $this->numLink( 100, $prevId ) . ' | ' .
302 $this->numLink( 250, $prevId ) . ' | ' .
303 $this->numLink( 500, $prevId );
305 return wfMsgHtml( 'viewprevnext', $prevLink, $nextLink, $nums );
308 function numLink( $limit, $from, $ns = null ) {
310 $query = "limit={$limit}&from={$from}";
311 if( is_int($this->namespace) ) { $query .= "&namespace={$this->namespace}";}
312 $fmtLimit = $wgLang->formatNum( $limit );
313 return $this->makeSelfLink( $fmtLimit, $query );
316 function whatlinkshereForm( $options = array( 'target' => '', 'namespace' => '' ) ) {
317 global $wgScript, $wgTitle;
319 $options['title'] = $wgTitle->getPrefixedText();
321 $f = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) ) .
322 Xml
::openElement( 'fieldset' ) .
323 Xml
::element( 'legend', array(), wfMsg( 'whatlinkshere' ) ) .
324 Xml
::inputLabel( wfMsg( 'whatlinkshere-page' ), 'target', 'mw-whatlinkshere-target', 40, $options['target'] ) . ' ';
326 foreach ( $options as $name => $value ) {
327 if( $name === 'namespace' ||
$name === 'target' )
329 $f .= "\t" . Xml
::hidden( $name, $value ). "\n";
332 $f .= Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
333 Xml
::namespaceSelector( $options['namespace'], '' ) .
334 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) .
335 Xml
::closeElement( 'fieldset' ) .
336 Xml
::closeElement( 'form' ) . "\n";
341 /** Set the namespace we are filtering on */
342 private function setNamespace( $ns ) {
343 $this->namespace = $ns;
346 function getFilterPanel() {
347 $show = wfMsgHtml( 'show' );
348 $hide = wfMsgHtml( 'hide' );
350 foreach( array( 'hidetrans', 'hidelinks', 'hideredirs' ) as $type ) {
351 $chosen = $this->$type;
352 $msg = wfMsgHtml( "whatlinkshere-{$type}", $chosen ?
$show : $hide );
353 $url = $this->request
->appendQueryValue( $type, intval( !$chosen ), true );
354 $links[] = $this->makeSelfLink( $msg, $url );
356 return '<p>' . implode( ' | ', $links ) . '</p>';