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;
28 function WhatLinksHerePage( &$request, $par = null ) {
30 $this->request
=& $request;
31 $this->skin
= $wgUser->getSkin();
38 $this->limit
= min( $this->request
->getInt( 'limit', 50 ), 5000 );
39 if ( $this->limit
<= 0 ) {
42 $this->from
= $this->request
->getInt( 'from' );
43 $this->back
= $this->request
->getInt( 'back' );
45 $targetString = isset($this->par
) ?
$this->par
: $this->request
->getVal( 'target' );
47 if ( is_null( $targetString ) ) {
48 $wgOut->addHTML( $this->whatlinkshereForm() );
52 $this->target
= Title
::newFromURL( $targetString );
53 if( !$this->target
) {
54 $wgOut->addHTML( $this->whatlinkshereForm() );
57 $this->selfTitle
= Title
::makeTitleSafe( NS_SPECIAL
,
58 'Whatlinkshere/' . $this->target
->getPrefixedDBkey() );
60 $wgOut->setPageTitle( wfMsg( 'whatlinkshere-title', $this->target
->getPrefixedText() ) );
61 $wgOut->setSubtitle( wfMsg( 'linklistsub' ) );
63 $wgOut->addHTML( wfMsg( 'whatlinkshere-barrow' ) . ' ' .$this->skin
->makeLinkObj($this->target
, '', 'redirect=no' )."<br />\n");
65 $this->showIndirectLinks( 0, $this->target
, $this->limit
, $this->from
, $this->back
);
69 * @param int $level Recursion level
70 * @param Title $target Target title
71 * @param int $limit Number of entries to display
72 * @param Title $from Display from this article ID
73 * @param Title $back Display from this article ID at backwards scrolling
76 function showIndirectLinks( $level, $target, $limit, $from = 0, $back = 0 ) {
78 $fname = 'WhatLinksHerePage::showIndirectLinks';
79 $dbr = wfGetDB( DB_READ
);
82 $ns = $this->request
->getIntOrNull( 'namespace' );
84 $options['namespace'] = $ns;
85 $this->setNamespace( $options['namespace'] );
87 $options['namespace'] = '';
93 'pl_namespace' => $target->getNamespace(),
94 'pl_title' => $target->getDBkey(),
99 'tl_namespace' => $target->getNamespace(),
100 'tl_title' => $target->getDBkey(),
103 if ( $this->namespace !== null ){
104 $plConds['page_namespace'] = (int)$this->namespace;
105 $tlConds['page_namespace'] = (int)$this->namespace;
109 $from = (int)$from; // just in case
110 $tlConds[] = "tl_from >= $from";
111 $plConds[] = "pl_from >= $from";
114 // Read an extra row as an at-end check
115 $queryLimit = $limit +
1;
117 // enforce join order, sometimes namespace selector may
118 // trigger filesorts which are far less efficient than scanning many entries
119 $options[] = 'STRAIGHT_JOIN';
121 $options['LIMIT'] = $queryLimit;
122 $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect' );
124 $options['ORDER BY'] = 'pl_from';
125 $plRes = $dbr->select( array( 'pagelinks', 'page' ), $fields,
126 $plConds, $fname, $options );
128 $options['ORDER BY'] = 'tl_from';
129 $tlRes = $dbr->select( array( 'templatelinks', 'page' ), $fields,
130 $tlConds, $fname, $options );
132 if ( !$dbr->numRows( $plRes ) && !$dbr->numRows( $tlRes ) ) {
134 $options = array(); // reinitialize for a further namespace search
135 // really no links to here
136 $options['namespace'] = $this->namespace;
137 $options['target'] = $this->target
->getPrefixedText();
138 list( $options['limit'], $options['offset']) = wfCheckLimits();
139 $wgOut->addHTML( $this->whatlinkshereForm( $options ) );
140 $errMsg = isset( $this->namespace ) ?
'nolinkshere-ns' : 'nolinkshere';
141 $wgOut->addWikiText( wfMsg( $errMsg, $this->target
->getPrefixedText() ) );
147 list( $options['limit'], $options['offset']) = wfCheckLimits();
148 if ( ( $ns = $this->request
->getVal( 'namespace', null ) ) !== null && $ns !== '' && ctype_digit($ns) ) {
149 $options['namespace'] = intval( $ns );
150 $this->setNamespace( $options['namespace'] );
152 $options['namespace'] = '';
153 $this->setNamespace( null );
155 $options['offset'] = $this->request
->getVal( 'offset' );
156 /* Offset must be an integral. */
157 if ( !strlen( $options['offset'] ) ||
!preg_match( '/^[0-9]+$/', $options['offset'] ) )
158 $options['offset'] = '';
159 $options['target'] = $this->target
->getPrefixedText();
161 // Read the rows into an array and remove duplicates
162 // templatelinks comes second so that the templatelinks row overwrites the
163 // pagelinks row, so we get (inclusion) rather than nothing
164 while ( $row = $dbr->fetchObject( $plRes ) ) {
165 $row->is_template
= 0;
166 $rows[$row->page_id
] = $row;
168 $dbr->freeResult( $plRes );
169 while ( $row = $dbr->fetchObject( $tlRes ) ) {
170 $row->is_template
= 1;
171 $rows[$row->page_id
] = $row;
173 $dbr->freeResult( $tlRes );
175 // Sort by key and then change the keys to 0-based indices
177 $rows = array_values( $rows );
179 $numRows = count( $rows );
181 // Work out the start and end IDs, for prev/next links
182 if ( $numRows > $limit ) {
183 // More rows available after these ones
184 // Get the ID from the last row in the result set
185 $nextId = $rows[$limit]->page_id
;
186 // Remove undisplayed rows
187 $rows = array_slice( $rows, 0, $limit );
189 // No more rows after
195 $wgOut->addHTML( $this->whatlinkshereForm( $options ) );
196 $wgOut->addWikiText( wfMsg( 'linkshere', $this->target
->getPrefixedText() ) );
198 $isredir = wfMsg( 'isredirect' );
199 $istemplate = wfMsg( 'istemplate' );
202 $prevnext = $this->getPrevNext( $limit, $prevId, $nextId, $options['namespace'] );
203 $wgOut->addHTML( $prevnext );
206 $wgOut->addHTML( '<ul>' );
207 foreach ( $rows as $row ) {
208 $nt = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
210 if ( $row->page_is_redirect
) {
211 $extra = 'redirect=no';
216 $link = $this->skin
->makeKnownLinkObj( $nt, '', $extra );
217 $wgOut->addHTML( '<li>'.$link );
219 // Display properties (redirect or template)
221 if ( $row->page_is_redirect
) {
224 if ( $row->is_template
) {
225 $props[] = $istemplate;
227 if ( count( $props ) ) {
228 // FIXME? Cultural assumption, hard-coded punctuation
229 $wgOut->addHTML( ' (' . implode( ', ', $props ) . ') ' );
232 # Space for utilities links, with a what-links-here link provided
233 $wlh = $this->skin
->makeKnownLinkObj(
234 SpecialPage
::getTitleFor( 'Whatlinkshere' ),
235 wfMsgHtml( 'whatlinkshere-links' ),
236 'target=' . $nt->getPrefixedUrl()
238 $wgOut->addHtml( ' <span class="mw-whatlinkshere-tools">(' . $wlh . ')</span>' );
240 if ( $row->page_is_redirect
) {
242 $this->showIndirectLinks( $level +
1, $nt, 500 );
245 $wgOut->addHTML( "</li>\n" );
247 $wgOut->addHTML( "</ul>\n" );
250 $wgOut->addHTML( $prevnext );
254 function makeSelfLink( $text, $query ) {
255 return $this->skin
->makeKnownLinkObj( $this->selfTitle
, $text, $query );
258 function getPrevNext( $limit, $prevId, $nextId ) {
260 $fmtLimit = $wgLang->formatNum( $limit );
261 $prev = wfMsgExt( 'whatlinkshere-prev', array( 'parsemag', 'escape' ), $fmtLimit );
262 $next = wfMsgExt( 'whatlinkshere-next', array( 'parsemag', 'escape' ), $fmtLimit );
265 if( is_int($this->namespace) ) {
266 $nsText = "&namespace={$this->namespace}";
269 if ( 0 != $prevId ) {
270 $prevLink = $this->makeSelfLink( $prev, "limit={$limit}&from={$this->back}{$nsText}" );
274 if ( 0 != $nextId ) {
275 $nextLink = $this->makeSelfLink( $next, "limit={$limit}&from={$nextId}&back={$prevId}{$nsText}" );
279 $nums = $this->numLink( 20, $prevId ) . ' | ' .
280 $this->numLink( 50, $prevId ) . ' | ' .
281 $this->numLink( 100, $prevId ) . ' | ' .
282 $this->numLink( 250, $prevId ) . ' | ' .
283 $this->numLink( 500, $prevId );
285 return wfMsg( 'viewprevnext', $prevLink, $nextLink, $nums );
288 function numLink( $limit, $from, $ns = null ) {
290 $query = "limit={$limit}&from={$from}";
291 if( is_int($this->namespace) ) { $query .= "&namespace={$this->namespace}";}
292 $fmtLimit = $wgLang->formatNum( $limit );
293 return $this->makeSelfLink( $fmtLimit, $query );
296 function whatlinkshereForm( $options = array( 'target' => '', 'namespace' => '' ) ) {
297 global $wgScript, $wgTitle;
299 $options['title'] = $wgTitle->getPrefixedText();
301 $f = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) ) .
302 Xml
::openElement( 'fieldset' ) .
303 Xml
::element( 'legend', array(), wfMsg( 'whatlinkshere' ) ) .
304 Xml
::inputLabel( wfMsg( 'whatlinkshere-page' ), 'target', 'mw-whatlinkshere-target', 40, $options['target'] ) . ' ';
306 foreach ( $options as $name => $value ) {
307 if( $name === 'namespace' ||
$name === 'target' )
309 $f .= "\t" . Xml
::hidden( $name, $value ). "\n";
312 $f .= Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
313 Xml
::namespaceSelector( $options['namespace'], '' ) .
314 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) .
315 Xml
::closeElement( 'fieldset' ) .
316 Xml
::closeElement( 'form' ) . "\n";
321 /** Set the namespace we are filtering on */
322 private function setNamespace( $ns ) {
323 $this->namespace = $ns;