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->showErrorPage( 'notargettitle', 'notargettext' );
52 $this->target
= Title
::newFromURL( $targetString );
53 if( !$this->target
) {
54 $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
57 $this->selfTitle
= Title
::makeTitleSafe( NS_SPECIAL
,
58 'Whatlinkshere/' . $this->target
->getPrefixedDBkey() );
59 $wgOut->setPagetitle( $this->target
->getPrefixedText() );
60 $wgOut->setSubtitle( wfMsg( 'linklistsub' ) );
62 $wgOut->addHTML( wfMsg( 'whatlinkshere-barrow' ) . ' ' .$this->skin
->makeLinkObj($this->target
, '', 'redirect=no' )."<br />\n");
64 $this->showIndirectLinks( 0, $this->target
, $this->limit
, $this->from
, $this->back
);
68 * @param int $level Recursion level
69 * @param Title $target Target title
70 * @param int $limit Number of entries to display
71 * @param Title $from Display from this article ID
72 * @param Title $back Display from this article ID at backwards scrolling
75 function showIndirectLinks( $level, $target, $limit, $from = 0, $back = 0 ) {
77 $fname = 'WhatLinksHerePage::showIndirectLinks';
78 $dbr = wfGetDB( DB_READ
);
81 $ns = $this->request
->getIntOrNull( 'namespace' );
83 $options['namespace'] = $ns;
84 $this->setNamespace( $options['namespace'] );
86 $options['namespace'] = '';
92 'pl_namespace' => $target->getNamespace(),
93 'pl_title' => $target->getDBkey(),
98 'tl_namespace' => $target->getNamespace(),
99 'tl_title' => $target->getDBkey(),
102 if ( $this->namespace !== null ){
103 $plConds['page_namespace'] = (int)$this->namespace;
104 $tlConds['page_namespace'] = (int)$this->namespace;
108 $from = (int)$from; // just in case
109 $tlConds[] = "tl_from >= $from";
110 $plConds[] = "pl_from >= $from";
113 // Read an extra row as an at-end check
114 $queryLimit = $limit +
1;
115 $options['LIMIT'] = $queryLimit;
116 $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect' );
118 $options['ORDER BY'] = 'pl_from';
119 $plRes = $dbr->select( array( 'pagelinks', 'page' ), $fields,
120 $plConds, $fname, $options );
122 $options['ORDER BY'] = 'tl_from';
123 $tlRes = $dbr->select( array( 'templatelinks', 'page' ), $fields,
124 $tlConds, $fname, $options );
126 if ( !$dbr->numRows( $plRes ) && !$dbr->numRows( $tlRes ) ) {
127 if ( 0 == $level && !isset( $this->namespace ) ) {
128 // really no links to here
129 $wgOut->addWikiText( wfMsg( 'nolinkshere', $this->target
->getPrefixedText() ) );
130 } elseif ( 0 == $level && isset( $this->namespace ) ) {
131 // no links from requested namespace to here
132 $options = array(); // reinitialize for a further namespace search
133 $options['namespace'] = $this->namespace;
134 $options['target'] = $this->target
->getPrefixedText();
135 list( $options['limit'], $options['offset']) = wfCheckLimits();
136 $wgOut->addHTML( $this->whatlinkshereForm( $options ) );
137 $wgOut->addWikiText( wfMsg( 'nolinkshere-ns', $this->target
->getPrefixedText() ) );
143 list( $options['limit'], $options['offset']) = wfCheckLimits();
144 if ( ( $ns = $this->request
->getVal( 'namespace', null ) ) !== null && $ns !== '' && ctype_digit($ns) ) {
145 $options['namespace'] = intval( $ns );
146 $this->setNamespace( $options['namespace'] );
148 $options['namespace'] = '';
149 $this->setNamespace( null );
151 $options['offset'] = $this->request
->getVal( 'offset' );
152 /* Offset must be an integral. */
153 if ( !strlen( $options['offset'] ) ||
!preg_match( '/^[0-9]+$/', $options['offset'] ) )
154 $options['offset'] = '';
155 $options['target'] = $this->target
->getPrefixedDBkey();
157 // Read the rows into an array and remove duplicates
158 // templatelinks comes second so that the templatelinks row overwrites the
159 // pagelinks row, so we get (inclusion) rather than nothing
160 while ( $row = $dbr->fetchObject( $plRes ) ) {
161 $row->is_template
= 0;
162 $rows[$row->page_id
] = $row;
164 $dbr->freeResult( $plRes );
165 while ( $row = $dbr->fetchObject( $tlRes ) ) {
166 $row->is_template
= 1;
167 $rows[$row->page_id
] = $row;
169 $dbr->freeResult( $tlRes );
171 // Sort by key and then change the keys to 0-based indices
173 $rows = array_values( $rows );
175 $numRows = count( $rows );
177 // Work out the start and end IDs, for prev/next links
178 if ( $numRows > $limit ) {
179 // More rows available after these ones
180 // Get the ID from the last row in the result set
181 $nextId = $rows[$limit]->page_id
;
182 // Remove undisplayed rows
183 $rows = array_slice( $rows, 0, $limit );
185 // No more rows after
191 $wgOut->addHTML( $this->whatlinkshereForm( $options ) );
192 $wgOut->addWikiText( wfMsg( 'linkshere', $this->target
->getPrefixedText() ) );
194 $isredir = wfMsg( 'isredirect' );
195 $istemplate = wfMsg( 'istemplate' );
198 $prevnext = $this->getPrevNext( $limit, $prevId, $nextId, $options['namespace'] );
199 $wgOut->addHTML( $prevnext );
202 $wgOut->addHTML( '<ul>' );
203 foreach ( $rows as $row ) {
204 $nt = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
206 if ( $row->page_is_redirect
) {
207 $extra = 'redirect=no';
212 $link = $this->skin
->makeKnownLinkObj( $nt, '', $extra );
213 $wgOut->addHTML( '<li>'.$link );
215 // Display properties (redirect or template)
217 if ( $row->page_is_redirect
) {
220 if ( $row->is_template
) {
221 $props[] = $istemplate;
223 if ( count( $props ) ) {
224 // FIXME? Cultural assumption, hard-coded punctuation
225 $wgOut->addHTML( ' (' . implode( ', ', $props ) . ') ' );
228 if ( $row->page_is_redirect
) {
230 $this->showIndirectLinks( $level +
1, $nt, 500 );
233 $wgOut->addHTML( "</li>\n" );
235 $wgOut->addHTML( "</ul>\n" );
238 $wgOut->addHTML( $prevnext );
242 function makeSelfLink( $text, $query ) {
243 return $this->skin
->makeKnownLinkObj( $this->selfTitle
, $text, $query );
246 function getPrevNext( $limit, $prevId, $nextId ) {
248 $fmtLimit = $wgLang->formatNum( $limit );
249 $prev = wfMsgExt( 'whatlinkshere-prev', array( 'parsemag', 'escape' ), $fmtLimit );
250 $next = wfMsgExt( 'whatlinkshere-next', array( 'parsemag', 'escape' ), $fmtLimit );
253 if( is_int($this->namespace) ) {
254 $nsText = "&namespace={$this->namespace}";
257 if ( 0 != $prevId ) {
258 $prevLink = $this->makeSelfLink( $prev, "limit={$limit}&from={$this->back}{$nsText}" );
262 if ( 0 != $nextId ) {
263 $nextLink = $this->makeSelfLink( $next, "limit={$limit}&from={$nextId}&back={$prevId}{$nsText}" );
267 $nums = $this->numLink( 20, $prevId ) . ' | ' .
268 $this->numLink( 50, $prevId ) . ' | ' .
269 $this->numLink( 100, $prevId ) . ' | ' .
270 $this->numLink( 250, $prevId ) . ' | ' .
271 $this->numLink( 500, $prevId );
273 return wfMsg( 'viewprevnext', $prevLink, $nextLink, $nums );
276 function numLink( $limit, $from, $ns = null ) {
278 $query = "limit={$limit}&from={$from}";
279 if( is_int($this->namespace) ) { $query .= "&namespace={$this->namespace}";}
280 $fmtLimit = $wgLang->formatNum( $limit );
281 return $this->makeSelfLink( $fmtLimit, $query );
284 function whatlinkshereForm( $options ) {
285 global $wgScript, $wgTitle;
287 $options['title'] = $wgTitle->getPrefixedText();
289 $f = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => "$wgScript" ) ) .
291 Xml
::element( 'legend', array(), wfMsg( 'whatlinkshere' ) );
293 foreach ( $options as $name => $value ) {
294 if( $name === 'namespace') continue;
295 $f .= "\t" . Xml
::hidden( $name, $value ). "\n";
298 $f .= Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
299 Xml
::namespaceSelector( $options['namespace'], '' ) .
300 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) .
302 Xml
::closeElement( 'form' ) . "\n";
307 /** Set the namespace we are filtering on */
308 private function setNamespace( $ns ) {
309 $this->namespace = $ns;