fix for r20616. Thanks to Brion whispering to me ;-)
[lhc/web/wiklou.git] / includes / SpecialWhatlinkshere.php
1 <?php
2 /**
3 *
4 * @addtogroup SpecialPage
5 */
6
7 /**
8 * Entry point
9 * @param string $par An article name ??
10 */
11 function wfSpecialWhatlinkshere($par = NULL) {
12 global $wgRequest;
13 $page = new WhatLinksHerePage( $wgRequest, $par );
14 $page->execute();
15 }
16
17 class WhatLinksHerePage {
18 var $request, $par;
19 var $limit, $from, $back, $target, $namespace;
20 var $selfTitle, $skin;
21
22 function WhatLinksHerePage( &$request, $par = null ) {
23 global $wgUser;
24 $this->request =& $request;
25 $this->skin = $wgUser->getSkin();
26 $this->par = $par;
27 }
28
29 function execute() {
30 global $wgOut;
31
32 $this->limit = min( $this->request->getInt( 'limit', 50 ), 5000 );
33 if ( $this->limit <= 0 ) {
34 $this->limit = 50;
35 }
36 $this->from = $this->request->getInt( 'from' );
37 $this->back = $this->request->getInt( 'back' );
38
39 $targetString = isset($this->par) ? $this->par : $this->request->getVal( 'target' );
40
41 if (is_null($targetString)) {
42 $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
43 return;
44 }
45
46 $this->target = Title::newFromURL( $targetString );
47 if( !$this->target ) {
48 $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
49 return;
50 }
51 $this->selfTitle = Title::makeTitleSafe( NS_SPECIAL,
52 'Whatlinkshere/' . $this->target->getPrefixedDBkey() );
53 $wgOut->setPagetitle( $this->target->getPrefixedText() );
54 $wgOut->setSubtitle( wfMsg( 'linklistsub' ) );
55
56 $wgOut->addHTML( wfMsg( 'whatlinkshere-barrow' ) . ' ' .$this->skin->makeLinkObj($this->target, '', 'redirect=no' )."<br />\n");
57
58 $this->showIndirectLinks( 0, $this->target, $this->limit, $this->from, $this->back );
59 }
60
61 /**
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
67 * @private
68 */
69 function showIndirectLinks( $level, $target, $limit, $from = 0, $back = 0 ) {
70 global $wgOut;
71 $fname = 'WhatLinksHerePage::showIndirectLinks';
72 $dbr = wfGetDB( DB_READ );
73
74 $ns = $this->request->getIntOrNull( 'namespace' );
75 if ( isset( $ns ) ) {
76 $options['namespace'] = $ns;
77 $this->setNamespace( $options['namespace'] );
78 } else {
79 $options['namespace'] = '';
80 }
81
82 // Make the query
83 $plConds = array(
84 'page_id=pl_from',
85 'pl_namespace' => $target->getNamespace(),
86 'pl_title' => $target->getDBkey(),
87 );
88
89 $tlConds = array(
90 'page_id=tl_from',
91 'tl_namespace' => $target->getNamespace(),
92 'tl_title' => $target->getDBkey(),
93 );
94
95 if ( $this->namespace !== null ){
96 $plConds['page_namespace'] = (int)$this->namespace;
97 $tlConds['page_namespace'] = (int)$this->namespace;
98 }
99
100 if ( $from ) {
101 $offsetCond = "page_id >= $from";
102 $options = array( 'ORDER BY page_id' );
103 } else {
104 $offsetCond = false;
105 $options = array( 'ORDER BY page_id,is_template DESC' );
106 }
107 // Read an extra row as an at-end check
108 $queryLimit = $limit + 1;
109 $options['LIMIT'] = $queryLimit;
110 if ( $offsetCond ) {
111 $tlConds[] = $offsetCond;
112 $plConds[] = $offsetCond;
113 }
114 $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect' );
115
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() ) );
132 }
133 return;
134 }
135
136 $options = array();
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'] );
141 } else {
142 $options['namespace'] = '';
143 $this->setNamespace( null );
144 }
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();
150
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;
157 }
158 $dbr->freeResult( $plRes );
159 while ( $row = $dbr->fetchObject( $tlRes ) ) {
160 $row->is_template = 1;
161 $rows[$row->page_id] = $row;
162 }
163 $dbr->freeResult( $tlRes );
164
165 // Sort by key and then change the keys to 0-based indices
166 ksort( $rows );
167 $rows = array_values( $rows );
168
169 $numRows = count( $rows );
170
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 );
178 } else {
179 // No more rows after
180 $nextId = false;
181 }
182 $prevId = $from;
183
184 if ( $level == 0 ) {
185 $wgOut->addHTML( $this->whatlinkshereForm( $options ) );
186 $wgOut->addWikiText( wfMsg( 'linkshere', $this->target->getPrefixedText() ) );
187 }
188 $isredir = wfMsg( 'isredirect' );
189 $istemplate = wfMsg( 'istemplate' );
190
191 if( $level == 0 ) {
192 $prevnext = $this->getPrevNext( $limit, $prevId, $nextId, $options['namespace'] );
193 $wgOut->addHTML( $prevnext );
194 }
195
196 $wgOut->addHTML( '<ul>' );
197 foreach ( $rows as $row ) {
198 $nt = Title::makeTitle( $row->page_namespace, $row->page_title );
199
200 if ( $row->page_is_redirect ) {
201 $extra = 'redirect=no';
202 } else {
203 $extra = '';
204 }
205
206 $link = $this->skin->makeKnownLinkObj( $nt, '', $extra );
207 $wgOut->addHTML( '<li>'.$link );
208
209 // Display properties (redirect or template)
210 $props = array();
211 if ( $row->page_is_redirect ) {
212 $props[] = $isredir;
213 }
214 if ( $row->is_template ) {
215 $props[] = $istemplate;
216 }
217 if ( count( $props ) ) {
218 // FIXME? Cultural assumption, hard-coded punctuation
219 $wgOut->addHTML( ' (' . implode( ', ', $props ) . ') ' );
220 }
221
222 if ( $row->page_is_redirect ) {
223 if ( $level < 2 ) {
224 $this->showIndirectLinks( $level + 1, $nt, 500 );
225 }
226 }
227 $wgOut->addHTML( "</li>\n" );
228 }
229 $wgOut->addHTML( "</ul>\n" );
230
231 if( $level == 0 ) {
232 $wgOut->addHTML( $prevnext );
233 }
234 }
235
236 function makeSelfLink( $text, $query ) {
237 return $this->skin->makeKnownLinkObj( $this->selfTitle, $text, $query );
238 }
239
240 function getPrevNext( $limit, $prevId, $nextId, $namespace ) {
241 global $wgLang;
242 $fmtLimit = $wgLang->formatNum( $limit );
243 $prev = wfMsg( 'whatlinkshere-prev', $fmtLimit );
244 $next = wfMsg( 'whatlinkshere-next', $fmtLimit );
245
246 if ( 0 != $prevId ) {
247 $prevLink = $this->makeSelfLink( $prev, "limit={$limit}&from={$this->back}&back={$fromId}&namespace={$namespace}" );
248 } else {
249 $prevLink = $prev;
250 }
251 if ( 0 != $nextId ) {
252 $nextLink = $this->makeSelfLink( $next, "limit={$limit}&from={$nextId}&back={$prevId}&namespace={$namespace}" );
253 } else {
254 $nextLink = $next;
255 }
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 );
261
262 return wfMsg( 'viewprevnext', $prevLink, $nextLink, $nums );
263 }
264
265 function numLink( $limit, $from ) {
266 global $wgLang;
267 $query = "limit={$limit}&from={$from}";
268 $fmtLimit = $wgLang->formatNum( $limit );
269 return $this->makeSelfLink( $fmtLimit, $query );
270 }
271
272 function whatlinkshereForm( $options ) {
273 global $wgScript, $wgTitle;
274
275 $options['title'] = $wgTitle->getPrefixedText();
276
277 $f = Xml::openElement( 'form', array( 'method' => 'get', 'action' => "$wgScript" ) ) .
278 '<fieldset>' .
279 Xml::element( 'legend', array(), wfMsg( 'whatlinkshere' ) );
280
281 foreach ( $options as $name => $value ) {
282 if( $name === 'namespace') continue;
283 $f .= "\t" . Xml::hidden( $name, $value ). "\n";
284 }
285
286 $f .= Xml::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
287 Xml::namespaceSelector( $options['namespace'], '' ) .
288 Xml::submitButton( wfMsg( 'allpagessubmit' ) ) .
289 '</fieldset>' .
290 Xml::closeElement( 'form' ) . "\n";
291
292 return $f;
293 }
294
295 function setNamespace( $ns ) {
296 $this->namespace = $ns;
297 }
298
299 }
300
301 ?>