4 * @addtogroup SpecialPage
9 * @param string $par An article name ??
11 function wfSpecialWhatlinkshere($par = NULL) {
13 $page = new WhatLinksHerePage( $wgRequest, $par );
17 class WhatLinksHerePage
{
19 var $limit, $from, $back, $target, $namespace;
20 var $selfTitle, $skin;
22 function WhatLinksHerePage( &$request, $par = null ) {
24 $this->request
=& $request;
25 $this->skin
= $wgUser->getSkin();
32 $this->limit
= min( $this->request
->getInt( 'limit', 50 ), 5000 );
33 if ( $this->limit
<= 0 ) {
36 $this->from
= $this->request
->getInt( 'from' );
37 $this->back
= $this->request
->getInt( 'back' );
39 $targetString = isset($this->par
) ?
$this->par
: $this->request
->getVal( 'target' );
41 if (is_null($targetString)) {
42 $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
46 $this->target
= Title
::newFromURL( $targetString );
47 if( !$this->target
) {
48 $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
51 $this->selfTitle
= Title
::makeTitleSafe( NS_SPECIAL
,
52 'Whatlinkshere/' . $this->target
->getPrefixedDBkey() );
53 $wgOut->setPagetitle( $this->target
->getPrefixedText() );
54 $wgOut->setSubtitle( wfMsg( 'linklistsub' ) );
56 $wgOut->addHTML( wfMsg( 'whatlinkshere-barrow' ) . ' ' .$this->skin
->makeLinkObj($this->target
, '', 'redirect=no' )."<br />\n");
58 $this->showIndirectLinks( 0, $this->target
, $this->limit
, $this->from
, $this->back
);
62 * @param int $level Recursion level
63 * @param Title $target Target title
64 * @param int $limit Number of entries to display
65 * @param Title $from Display from this article ID
66 * @param Title $back Display from this article ID at backwards scrolling
69 function showIndirectLinks( $level, $target, $limit, $from = 0, $back = 0 ) {
71 $fname = 'WhatLinksHerePage::showIndirectLinks';
72 $dbr = wfGetDB( DB_READ
);
75 $ns = $this->request
->getIntOrNull( 'namespace' );
77 $options['namespace'] = $ns;
78 $this->setNamespace( $options['namespace'] );
80 $options['namespace'] = '';
86 'pl_namespace' => $target->getNamespace(),
87 'pl_title' => $target->getDBkey(),
92 'tl_namespace' => $target->getNamespace(),
93 'tl_title' => $target->getDBkey(),
96 if ( $this->namespace !== null ){
97 $plConds['page_namespace'] = (int)$this->namespace;
98 $tlConds['page_namespace'] = (int)$this->namespace;
102 $offsetCond = "page_id >= $from";
106 $options['ORDER BY'] = 'page_id';
108 // Read an extra row as an at-end check
109 $queryLimit = $limit +
1;
110 $options['LIMIT'] = $queryLimit;
112 $tlConds[] = $offsetCond;
113 $plConds[] = $offsetCond;
115 $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect' );
117 $plRes = $dbr->select( array( 'pagelinks', 'page' ), $fields,
118 $plConds, $fname, $options );
119 $tlRes = $dbr->select( array( 'templatelinks', 'page' ), $fields,
120 $tlConds, $fname, $options );
121 if ( !$dbr->numRows( $plRes ) && !$dbr->numRows( $tlRes ) ) {
122 if ( 0 == $level && !isset( $this->namespace ) ) {
123 // really no links to here
124 $wgOut->addWikiText( wfMsg( 'nolinkshere', $this->target
->getPrefixedText() ) );
125 } elseif ( 0 == $level && isset( $this->namespace ) ) {
126 // no links from requested namespace to here
127 $options = array(); // reinitialize for a further namespace search
128 $options['namespace'] = $this->namespace;
129 $options['target'] = $this->target
->getPrefixedText();
130 list( $options['limit'], $options['offset']) = wfCheckLimits();
131 $wgOut->addHTML( $this->whatlinkshereForm( $options ) );
132 $wgOut->addWikiText( wfMsg( 'nolinkshere-ns', $this->target
->getPrefixedText() ) );
138 list( $options['limit'], $options['offset']) = wfCheckLimits();
139 if ( ( $ns = $this->request
->getVal( 'namespace', null ) ) !== null && $ns !== '' && ctype_digit($ns) ) {
140 $options['namespace'] = intval( $ns );
141 $this->setNamespace( $options['namespace'] );
143 $options['namespace'] = '';
144 $this->setNamespace( null );
146 $options['offset'] = $this->request
->getVal( 'offset' );
147 /* Offset must be an integral. */
148 if ( !strlen( $options['offset'] ) ||
!preg_match( '/^[0-9]+$/', $options['offset'] ) )
149 $options['offset'] = '';
150 $options['target'] = $this->target
->getPrefixedDBkey();
152 // Read the rows into an array and remove duplicates
153 // templatelinks comes second so that the templatelinks row overwrites the
154 // pagelinks row, so we get (inclusion) rather than nothing
155 while ( $row = $dbr->fetchObject( $plRes ) ) {
156 $row->is_template
= 0;
157 $rows[$row->page_id
] = $row;
159 $dbr->freeResult( $plRes );
160 while ( $row = $dbr->fetchObject( $tlRes ) ) {
161 $row->is_template
= 1;
162 $rows[$row->page_id
] = $row;
164 $dbr->freeResult( $tlRes );
166 // Sort by key and then change the keys to 0-based indices
168 $rows = array_values( $rows );
170 $numRows = count( $rows );
172 // Work out the start and end IDs, for prev/next links
173 if ( $numRows > $limit ) {
174 // More rows available after these ones
175 // Get the ID from the last row in the result set
176 $nextId = $rows[$limit]->page_id
;
177 // Remove undisplayed rows
178 $rows = array_slice( $rows, 0, $limit );
180 // No more rows after
186 $wgOut->addHTML( $this->whatlinkshereForm( $options ) );
187 $wgOut->addWikiText( wfMsg( 'linkshere', $this->target
->getPrefixedText() ) );
189 $isredir = wfMsg( 'isredirect' );
190 $istemplate = wfMsg( 'istemplate' );
193 $prevnext = $this->getPrevNext( $limit, $prevId, $nextId, $options['namespace'] );
194 $wgOut->addHTML( $prevnext );
197 $wgOut->addHTML( '<ul>' );
198 foreach ( $rows as $row ) {
199 $nt = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
201 if ( $row->page_is_redirect
) {
202 $extra = 'redirect=no';
207 $link = $this->skin
->makeKnownLinkObj( $nt, '', $extra );
208 $wgOut->addHTML( '<li>'.$link );
210 // Display properties (redirect or template)
212 if ( $row->page_is_redirect
) {
215 if ( $row->is_template
) {
216 $props[] = $istemplate;
218 if ( count( $props ) ) {
219 // FIXME? Cultural assumption, hard-coded punctuation
220 $wgOut->addHTML( ' (' . implode( ', ', $props ) . ') ' );
223 if ( $row->page_is_redirect
) {
225 $this->showIndirectLinks( $level +
1, $nt, 500 );
228 $wgOut->addHTML( "</li>\n" );
230 $wgOut->addHTML( "</ul>\n" );
233 $wgOut->addHTML( $prevnext );
237 function makeSelfLink( $text, $query ) {
238 return $this->skin
->makeKnownLinkObj( $this->selfTitle
, $text, $query );
241 function getPrevNext( $limit, $prevId, $nextId, $namespace ) {
243 $fmtLimit = $wgLang->formatNum( $limit );
244 $prev = wfMsgExt( 'whatlinkshere-prev', array( 'parsemag', 'escape' ), $fmtLimit );
245 $next = wfMsgExt( 'whatlinkshere-next', array( 'parsemag', 'escape' ), $fmtLimit );
247 if ( 0 != $prevId ) {
248 $prevLink = $this->makeSelfLink( $prev, "limit={$limit}&from={$this->back}&namespace={$namespace}" );
252 if ( 0 != $nextId ) {
253 $nextLink = $this->makeSelfLink( $next, "limit={$limit}&from={$nextId}&back={$prevId}&namespace={$namespace}" );
257 $nums = $this->numLink( 20, $prevId ) . ' | ' .
258 $this->numLink( 50, $prevId ) . ' | ' .
259 $this->numLink( 100, $prevId ) . ' | ' .
260 $this->numLink( 250, $prevId ) . ' | ' .
261 $this->numLink( 500, $prevId );
263 return wfMsg( 'viewprevnext', $prevLink, $nextLink, $nums );
266 function numLink( $limit, $from ) {
268 $query = "limit={$limit}&from={$from}";
269 $fmtLimit = $wgLang->formatNum( $limit );
270 return $this->makeSelfLink( $fmtLimit, $query );
273 function whatlinkshereForm( $options ) {
274 global $wgScript, $wgTitle;
276 $options['title'] = $wgTitle->getPrefixedText();
278 $f = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => "$wgScript" ) ) .
280 Xml
::element( 'legend', array(), wfMsg( 'whatlinkshere' ) );
282 foreach ( $options as $name => $value ) {
283 if( $name === 'namespace') continue;
284 $f .= "\t" . Xml
::hidden( $name, $value ). "\n";
287 $f .= Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
288 Xml
::namespaceSelector( $options['namespace'], '' ) .
289 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) .
291 Xml
::closeElement( 'form' ) . "\n";
296 function setNamespace( $ns ) {
297 $this->namespace = $ns;