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 $offsetCond = "page_id >= $from";
112 $options['ORDER BY'] = 'page_id';
114 // Read an extra row as an at-end check
115 $queryLimit = $limit +
1;
116 $options['LIMIT'] = $queryLimit;
118 $tlConds[] = $offsetCond;
119 $plConds[] = $offsetCond;
121 $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect' );
123 $plRes = $dbr->select( array( 'pagelinks', 'page' ), $fields,
124 $plConds, $fname, $options );
125 $tlRes = $dbr->select( array( 'templatelinks', 'page' ), $fields,
126 $tlConds, $fname, $options );
127 if ( !$dbr->numRows( $plRes ) && !$dbr->numRows( $tlRes ) ) {
128 if ( 0 == $level && !isset( $this->namespace ) ) {
129 // really no links to here
130 $wgOut->addWikiText( wfMsg( 'nolinkshere', $this->target
->getPrefixedText() ) );
131 } elseif ( 0 == $level && isset( $this->namespace ) ) {
132 // no links from requested namespace to here
133 $options = array(); // reinitialize for a further namespace search
134 $options['namespace'] = $this->namespace;
135 $options['target'] = $this->target
->getPrefixedText();
136 list( $options['limit'], $options['offset']) = wfCheckLimits();
137 $wgOut->addHTML( $this->whatlinkshereForm( $options ) );
138 $wgOut->addWikiText( wfMsg( 'nolinkshere-ns', $this->target
->getPrefixedText() ) );
144 list( $options['limit'], $options['offset']) = wfCheckLimits();
145 if ( ( $ns = $this->request
->getVal( 'namespace', null ) ) !== null && $ns !== '' && ctype_digit($ns) ) {
146 $options['namespace'] = intval( $ns );
147 $this->setNamespace( $options['namespace'] );
149 $options['namespace'] = '';
150 $this->setNamespace( null );
152 $options['offset'] = $this->request
->getVal( 'offset' );
153 /* Offset must be an integral. */
154 if ( !strlen( $options['offset'] ) ||
!preg_match( '/^[0-9]+$/', $options['offset'] ) )
155 $options['offset'] = '';
156 $options['target'] = $this->target
->getPrefixedDBkey();
158 // Read the rows into an array and remove duplicates
159 // templatelinks comes second so that the templatelinks row overwrites the
160 // pagelinks row, so we get (inclusion) rather than nothing
161 while ( $row = $dbr->fetchObject( $plRes ) ) {
162 $row->is_template
= 0;
163 $rows[$row->page_id
] = $row;
165 $dbr->freeResult( $plRes );
166 while ( $row = $dbr->fetchObject( $tlRes ) ) {
167 $row->is_template
= 1;
168 $rows[$row->page_id
] = $row;
170 $dbr->freeResult( $tlRes );
172 // Sort by key and then change the keys to 0-based indices
174 $rows = array_values( $rows );
176 $numRows = count( $rows );
178 // Work out the start and end IDs, for prev/next links
179 if ( $numRows > $limit ) {
180 // More rows available after these ones
181 // Get the ID from the last row in the result set
182 $nextId = $rows[$limit]->page_id
;
183 // Remove undisplayed rows
184 $rows = array_slice( $rows, 0, $limit );
186 // No more rows after
192 $wgOut->addHTML( $this->whatlinkshereForm( $options ) );
193 $wgOut->addWikiText( wfMsg( 'linkshere', $this->target
->getPrefixedText() ) );
195 $isredir = wfMsg( 'isredirect' );
196 $istemplate = wfMsg( 'istemplate' );
199 $prevnext = $this->getPrevNext( $limit, $prevId, $nextId, $options['namespace'] );
200 $wgOut->addHTML( $prevnext );
203 $wgOut->addHTML( '<ul>' );
204 foreach ( $rows as $row ) {
205 $nt = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
207 if ( $row->page_is_redirect
) {
208 $extra = 'redirect=no';
213 $link = $this->skin
->makeKnownLinkObj( $nt, '', $extra );
214 $wgOut->addHTML( '<li>'.$link );
216 // Display properties (redirect or template)
218 if ( $row->page_is_redirect
) {
221 if ( $row->is_template
) {
222 $props[] = $istemplate;
224 if ( count( $props ) ) {
225 // FIXME? Cultural assumption, hard-coded punctuation
226 $wgOut->addHTML( ' (' . implode( ', ', $props ) . ') ' );
229 if ( $row->page_is_redirect
) {
231 $this->showIndirectLinks( $level +
1, $nt, 500 );
234 $wgOut->addHTML( "</li>\n" );
236 $wgOut->addHTML( "</ul>\n" );
239 $wgOut->addHTML( $prevnext );
243 function makeSelfLink( $text, $query ) {
244 return $this->skin
->makeKnownLinkObj( $this->selfTitle
, $text, $query );
247 function getPrevNext( $limit, $prevId, $nextId ) {
249 $fmtLimit = $wgLang->formatNum( $limit );
250 $prev = wfMsgExt( 'whatlinkshere-prev', array( 'parsemag', 'escape' ), $fmtLimit );
251 $next = wfMsgExt( 'whatlinkshere-next', array( 'parsemag', 'escape' ), $fmtLimit );
254 if( is_int($this->namespace) ) {
255 $nsText = "&namespace={$this->namespace}";
258 if ( 0 != $prevId ) {
259 $prevLink = $this->makeSelfLink( $prev, "limit={$limit}&from={$this->back}{$nsText}" );
263 if ( 0 != $nextId ) {
264 $nextLink = $this->makeSelfLink( $next, "limit={$limit}&from={$nextId}&back={$prevId}{$nsText}" );
268 $nums = $this->numLink( 20, $prevId ) . ' | ' .
269 $this->numLink( 50, $prevId ) . ' | ' .
270 $this->numLink( 100, $prevId ) . ' | ' .
271 $this->numLink( 250, $prevId ) . ' | ' .
272 $this->numLink( 500, $prevId );
274 return wfMsg( 'viewprevnext', $prevLink, $nextLink, $nums );
277 function numLink( $limit, $from, $ns = null ) {
279 $query = "limit={$limit}&from={$from}";
280 if( is_int($this->namespace) ) { $query .= "&namespace={$this->namespace}";}
281 $fmtLimit = $wgLang->formatNum( $limit );
282 return $this->makeSelfLink( $fmtLimit, $query );
285 function whatlinkshereForm( $options ) {
286 global $wgScript, $wgTitle;
288 $options['title'] = $wgTitle->getPrefixedText();
290 $f = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => "$wgScript" ) ) .
292 Xml
::element( 'legend', array(), wfMsg( 'whatlinkshere' ) );
294 foreach ( $options as $name => $value ) {
295 if( $name === 'namespace') continue;
296 $f .= "\t" . Xml
::hidden( $name, $value ). "\n";
299 $f .= Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
300 Xml
::namespaceSelector( $options['namespace'], '' ) .
301 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) .
303 Xml
::closeElement( 'form' ) . "\n";
308 /** Set the namespace we are filtering on */
309 private function setNamespace( $ns ) {
310 $this->namespace = $ns;