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
);
74 $ns = $this->request
->getIntOrNull( 'namespace' );
76 $options['namespace'] = $ns;
77 $this->setNamespace( $options['namespace'] );
79 $options['namespace'] = '';
85 'pl_namespace' => $target->getNamespace(),
86 'pl_title' => $target->getDBkey(),
91 'tl_namespace' => $target->getNamespace(),
92 'tl_title' => $target->getDBkey(),
95 if ( $this->namespace !== null ){
96 $plConds['page_namespace'] = (int)$this->namespace;
97 $tlConds['page_namespace'] = (int)$this->namespace;
101 $offsetCond = "page_id >= $from";
102 $options = array( 'ORDER BY page_id' );
105 $options = array( 'ORDER BY page_id,is_template DESC' );
107 // Read an extra row as an at-end check
108 $queryLimit = $limit +
1;
109 $options['LIMIT'] = $queryLimit;
111 $tlConds[] = $offsetCond;
112 $plConds[] = $offsetCond;
114 $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect' );
116 $plRes = $dbr->select( array( 'pagelinks', 'page' ), $fields,
117 $plConds, $fname, $options );
118 $tlRes = $dbr->select( array( 'templatelinks', 'page' ), $fields,
119 $tlConds, $fname, $options );
120 if ( !$dbr->numRows( $plRes ) && !$dbr->numRows( $tlRes ) ) {
121 if ( 0 == $level && !isset( $this->namespace ) ) {
122 // really no links to here
123 $wgOut->addWikiText( wfMsg( 'nolinkshere', $this->target
->getPrefixedText() ) );
124 } elseif ( 0 == $level && isset( $this->namespace ) ) {
125 // no links from requested namespace to here
126 $options = array(); // reinitialize for a further namespace search
127 $options['namespace'] = $this->namespace;
128 $options['target'] = $this->target
->getPrefixedText();
129 list( $options['limit'], $options['offset']) = wfCheckLimits();
130 $wgOut->addHTML( $this->whatlinkshereForm( $options ) );
131 $wgOut->addWikiText( wfMsg( 'nolinkshere-ns', $this->target
->getPrefixedText() ) );
137 list( $options['limit'], $options['offset']) = wfCheckLimits();
138 if ( ( $ns = $this->request
->getVal( 'namespace', null ) ) !== null && $ns !== '' && ctype_digit($ns) ) {
139 $options['namespace'] = intval( $ns );
140 $this->setNamespace( $options['namespace'] );
142 $options['namespace'] = '';
143 $this->setNamespace( null );
145 $options['offset'] = $this->request
->getVal( 'offset' );
146 /* Offset must be an integral. */
147 if ( !strlen( $options['offset'] ) ||
!preg_match( '/^[0-9]+$/', $options['offset'] ) )
148 $options['offset'] = '';
149 $options['target'] = $this->target
->getPrefixedDBkey();
151 // Read the rows into an array and remove duplicates
152 // templatelinks comes second so that the templatelinks row overwrites the
153 // pagelinks row, so we get (inclusion) rather than nothing
154 while ( $row = $dbr->fetchObject( $plRes ) ) {
155 $row->is_template
= 0;
156 $rows[$row->page_id
] = $row;
158 $dbr->freeResult( $plRes );
159 while ( $row = $dbr->fetchObject( $tlRes ) ) {
160 $row->is_template
= 1;
161 $rows[$row->page_id
] = $row;
163 $dbr->freeResult( $tlRes );
165 // Sort by key and then change the keys to 0-based indices
167 $rows = array_values( $rows );
169 $numRows = count( $rows );
171 // Work out the start and end IDs, for prev/next links
172 if ( $numRows > $limit ) {
173 // More rows available after these ones
174 // Get the ID from the last row in the result set
175 $nextId = $rows[$limit]->page_id
;
176 // Remove undisplayed rows
177 $rows = array_slice( $rows, 0, $limit );
179 // No more rows after
185 $wgOut->addHTML( $this->whatlinkshereForm( $options ) );
186 $wgOut->addWikiText( wfMsg( 'linkshere', $this->target
->getPrefixedText() ) );
188 $isredir = wfMsg( 'isredirect' );
189 $istemplate = wfMsg( 'istemplate' );
192 $prevnext = $this->getPrevNext( $limit, $prevId, $nextId, $options['namespace'] );
193 $wgOut->addHTML( $prevnext );
196 $wgOut->addHTML( '<ul>' );
197 foreach ( $rows as $row ) {
198 $nt = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
200 if ( $row->page_is_redirect
) {
201 $extra = 'redirect=no';
206 $link = $this->skin
->makeKnownLinkObj( $nt, '', $extra );
207 $wgOut->addHTML( '<li>'.$link );
209 // Display properties (redirect or template)
211 if ( $row->page_is_redirect
) {
214 if ( $row->is_template
) {
215 $props[] = $istemplate;
217 if ( count( $props ) ) {
218 // FIXME? Cultural assumption, hard-coded punctuation
219 $wgOut->addHTML( ' (' . implode( ', ', $props ) . ') ' );
222 if ( $row->page_is_redirect
) {
224 $this->showIndirectLinks( $level +
1, $nt, 500 );
227 $wgOut->addHTML( "</li>\n" );
229 $wgOut->addHTML( "</ul>\n" );
232 $wgOut->addHTML( $prevnext );
236 function makeSelfLink( $text, $query ) {
237 return $this->skin
->makeKnownLinkObj( $this->selfTitle
, $text, $query );
240 function getPrevNext( $limit, $prevId, $nextId, $namespace ) {
242 $fmtLimit = $wgLang->formatNum( $limit );
243 $prev = wfMsgExt( 'whatlinkshere-prev', array( 'parsemag', 'escape' ), $fmtLimit );
244 $next = wfMsgExt( 'whatlinkshere-next', array( 'parsemag', 'escape' ), $fmtLimit );
246 if ( 0 != $prevId ) {
247 $prevLink = $this->makeSelfLink( $prev, "limit={$limit}&from={$this->back}&namespace={$namespace}" );
251 if ( 0 != $nextId ) {
252 $nextLink = $this->makeSelfLink( $next, "limit={$limit}&from={$nextId}&back={$prevId}&namespace={$namespace}" );
256 $nums = $this->numLink( 20, $prevId ) . ' | ' .
257 $this->numLink( 50, $prevId ) . ' | ' .
258 $this->numLink( 100, $prevId ) . ' | ' .
259 $this->numLink( 250, $prevId ) . ' | ' .
260 $this->numLink( 500, $prevId );
262 return wfMsg( 'viewprevnext', $prevLink, $nextLink, $nums );
265 function numLink( $limit, $from ) {
267 $query = "limit={$limit}&from={$from}";
268 $fmtLimit = $wgLang->formatNum( $limit );
269 return $this->makeSelfLink( $fmtLimit, $query );
272 function whatlinkshereForm( $options ) {
273 global $wgScript, $wgTitle;
275 $options['title'] = $wgTitle->getPrefixedText();
277 $f = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => "$wgScript" ) ) .
279 Xml
::element( 'legend', array(), wfMsg( 'whatlinkshere' ) );
281 foreach ( $options as $name => $value ) {
282 if( $name === 'namespace') continue;
283 $f .= "\t" . Xml
::hidden( $name, $value ). "\n";
286 $f .= Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
287 Xml
::namespaceSelector( $options['namespace'], '' ) .
288 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) .
290 Xml
::closeElement( 'form' ) . "\n";
295 function setNamespace( $ns ) {
296 $this->namespace = $ns;