* (bug 10828) typo in French
[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 /**
18 * implements Special:Whatlinkshere
19 * @addtogroup SpecialPage
20 */
21 class WhatLinksHerePage {
22 var $request, $par;
23 var $limit, $from, $back, $target;
24 var $selfTitle, $skin;
25
26 private $namespace;
27
28 function WhatLinksHerePage( &$request, $par = null ) {
29 global $wgUser;
30 $this->request =& $request;
31 $this->skin = $wgUser->getSkin();
32 $this->par = $par;
33 }
34
35 function execute() {
36 global $wgOut;
37
38 $this->limit = min( $this->request->getInt( 'limit', 50 ), 5000 );
39 if ( $this->limit <= 0 ) {
40 $this->limit = 50;
41 }
42 $this->from = $this->request->getInt( 'from' );
43 $this->back = $this->request->getInt( 'back' );
44
45 $targetString = isset($this->par) ? $this->par : $this->request->getVal( 'target' );
46
47 if (is_null($targetString)) {
48 $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
49 return;
50 }
51
52 $this->target = Title::newFromURL( $targetString );
53 if( !$this->target ) {
54 $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
55 return;
56 }
57 $this->selfTitle = Title::makeTitleSafe( NS_SPECIAL,
58 'Whatlinkshere/' . $this->target->getPrefixedDBkey() );
59 $wgOut->setPagetitle( $this->target->getPrefixedText() );
60 $wgOut->setSubtitle( wfMsg( 'linklistsub' ) );
61
62 $wgOut->addHTML( wfMsg( 'whatlinkshere-barrow' ) . ' ' .$this->skin->makeLinkObj($this->target, '', 'redirect=no' )."<br />\n");
63
64 $this->showIndirectLinks( 0, $this->target, $this->limit, $this->from, $this->back );
65 }
66
67 /**
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
73 * @private
74 */
75 function showIndirectLinks( $level, $target, $limit, $from = 0, $back = 0 ) {
76 global $wgOut;
77 $fname = 'WhatLinksHerePage::showIndirectLinks';
78 $dbr = wfGetDB( DB_READ );
79 $options = array();
80
81 $ns = $this->request->getIntOrNull( 'namespace' );
82 if ( isset( $ns ) ) {
83 $options['namespace'] = $ns;
84 $this->setNamespace( $options['namespace'] );
85 } else {
86 $options['namespace'] = '';
87 }
88
89 // Make the query
90 $plConds = array(
91 'page_id=pl_from',
92 'pl_namespace' => $target->getNamespace(),
93 'pl_title' => $target->getDBkey(),
94 );
95
96 $tlConds = array(
97 'page_id=tl_from',
98 'tl_namespace' => $target->getNamespace(),
99 'tl_title' => $target->getDBkey(),
100 );
101
102 if ( $this->namespace !== null ){
103 $plConds['page_namespace'] = (int)$this->namespace;
104 $tlConds['page_namespace'] = (int)$this->namespace;
105 }
106
107 if ( $from ) {
108 $from = (int)$from; // just in case
109 $tlConds[] = "tl_from >= $from";
110 $plConds[] = "pl_from >= $from";
111 }
112
113 // Read an extra row as an at-end check
114 $queryLimit = $limit + 1;
115
116 // enforce join order, sometimes namespace selector may
117 // trigger filesorts which are far less efficient than scanning many entries
118 $options[] = 'STRAIGHT_JOIN';
119
120 $options['LIMIT'] = $queryLimit;
121 $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect' );
122
123 $options['ORDER BY'] = 'pl_from';
124 $plRes = $dbr->select( array( 'pagelinks', 'page' ), $fields,
125 $plConds, $fname, $options );
126
127 $options['ORDER BY'] = 'tl_from';
128 $tlRes = $dbr->select( array( 'templatelinks', 'page' ), $fields,
129 $tlConds, $fname, $options );
130
131 if ( !$dbr->numRows( $plRes ) && !$dbr->numRows( $tlRes ) ) {
132 if ( 0 == $level && !isset( $this->namespace ) ) {
133 // really no links to here
134 $wgOut->addWikiText( wfMsg( 'nolinkshere', $this->target->getPrefixedText() ) );
135 } elseif ( 0 == $level && isset( $this->namespace ) ) {
136 // no links from requested namespace to here
137 $options = array(); // reinitialize for a further namespace search
138 $options['namespace'] = $this->namespace;
139 $options['target'] = $this->target->getPrefixedText();
140 list( $options['limit'], $options['offset']) = wfCheckLimits();
141 $wgOut->addHTML( $this->whatlinkshereForm( $options ) );
142 $wgOut->addWikiText( wfMsg( 'nolinkshere-ns', $this->target->getPrefixedText() ) );
143 }
144 return;
145 }
146
147 $options = array();
148 list( $options['limit'], $options['offset']) = wfCheckLimits();
149 if ( ( $ns = $this->request->getVal( 'namespace', null ) ) !== null && $ns !== '' && ctype_digit($ns) ) {
150 $options['namespace'] = intval( $ns );
151 $this->setNamespace( $options['namespace'] );
152 } else {
153 $options['namespace'] = '';
154 $this->setNamespace( null );
155 }
156 $options['offset'] = $this->request->getVal( 'offset' );
157 /* Offset must be an integral. */
158 if ( !strlen( $options['offset'] ) || !preg_match( '/^[0-9]+$/', $options['offset'] ) )
159 $options['offset'] = '';
160 $options['target'] = $this->target->getPrefixedDBkey();
161
162 // Read the rows into an array and remove duplicates
163 // templatelinks comes second so that the templatelinks row overwrites the
164 // pagelinks row, so we get (inclusion) rather than nothing
165 while ( $row = $dbr->fetchObject( $plRes ) ) {
166 $row->is_template = 0;
167 $rows[$row->page_id] = $row;
168 }
169 $dbr->freeResult( $plRes );
170 while ( $row = $dbr->fetchObject( $tlRes ) ) {
171 $row->is_template = 1;
172 $rows[$row->page_id] = $row;
173 }
174 $dbr->freeResult( $tlRes );
175
176 // Sort by key and then change the keys to 0-based indices
177 ksort( $rows );
178 $rows = array_values( $rows );
179
180 $numRows = count( $rows );
181
182 // Work out the start and end IDs, for prev/next links
183 if ( $numRows > $limit ) {
184 // More rows available after these ones
185 // Get the ID from the last row in the result set
186 $nextId = $rows[$limit]->page_id;
187 // Remove undisplayed rows
188 $rows = array_slice( $rows, 0, $limit );
189 } else {
190 // No more rows after
191 $nextId = false;
192 }
193 $prevId = $from;
194
195 if ( $level == 0 ) {
196 $wgOut->addHTML( $this->whatlinkshereForm( $options ) );
197 $wgOut->addWikiText( wfMsg( 'linkshere', $this->target->getPrefixedText() ) );
198 }
199 $isredir = wfMsg( 'isredirect' );
200 $istemplate = wfMsg( 'istemplate' );
201
202 if( $level == 0 ) {
203 $prevnext = $this->getPrevNext( $limit, $prevId, $nextId, $options['namespace'] );
204 $wgOut->addHTML( $prevnext );
205 }
206
207 $wgOut->addHTML( '<ul>' );
208 foreach ( $rows as $row ) {
209 $nt = Title::makeTitle( $row->page_namespace, $row->page_title );
210
211 if ( $row->page_is_redirect ) {
212 $extra = 'redirect=no';
213 } else {
214 $extra = '';
215 }
216
217 $link = $this->skin->makeKnownLinkObj( $nt, '', $extra );
218 $wgOut->addHTML( '<li>'.$link );
219
220 // Display properties (redirect or template)
221 $props = array();
222 if ( $row->page_is_redirect ) {
223 $props[] = $isredir;
224 }
225 if ( $row->is_template ) {
226 $props[] = $istemplate;
227 }
228 if ( count( $props ) ) {
229 // FIXME? Cultural assumption, hard-coded punctuation
230 $wgOut->addHTML( ' (' . implode( ', ', $props ) . ') ' );
231 }
232
233 # Space for utilities links, with a what-links-here link provided
234 $wlh = $this->skin->makeKnownLinkObj(
235 SpecialPage::getTitleFor( 'Whatlinkshere' ),
236 wfMsgHtml( 'whatlinkshere-links' ),
237 'target=' . $nt->getPrefixedUrl()
238 );
239 $wgOut->addHtml( ' <span class="mw-whatlinkshere-tools">(' . $wlh . ')</span>' );
240
241 if ( $row->page_is_redirect ) {
242 if ( $level < 2 ) {
243 $this->showIndirectLinks( $level + 1, $nt, 500 );
244 }
245 }
246 $wgOut->addHTML( "</li>\n" );
247 }
248 $wgOut->addHTML( "</ul>\n" );
249
250 if( $level == 0 ) {
251 $wgOut->addHTML( $prevnext );
252 }
253 }
254
255 function makeSelfLink( $text, $query ) {
256 return $this->skin->makeKnownLinkObj( $this->selfTitle, $text, $query );
257 }
258
259 function getPrevNext( $limit, $prevId, $nextId ) {
260 global $wgLang;
261 $fmtLimit = $wgLang->formatNum( $limit );
262 $prev = wfMsgExt( 'whatlinkshere-prev', array( 'parsemag', 'escape' ), $fmtLimit );
263 $next = wfMsgExt( 'whatlinkshere-next', array( 'parsemag', 'escape' ), $fmtLimit );
264
265 $nsText = '';
266 if( is_int($this->namespace) ) {
267 $nsText = "&namespace={$this->namespace}";
268 }
269
270 if ( 0 != $prevId ) {
271 $prevLink = $this->makeSelfLink( $prev, "limit={$limit}&from={$this->back}{$nsText}" );
272 } else {
273 $prevLink = $prev;
274 }
275 if ( 0 != $nextId ) {
276 $nextLink = $this->makeSelfLink( $next, "limit={$limit}&from={$nextId}&back={$prevId}{$nsText}" );
277 } else {
278 $nextLink = $next;
279 }
280 $nums = $this->numLink( 20, $prevId ) . ' | ' .
281 $this->numLink( 50, $prevId ) . ' | ' .
282 $this->numLink( 100, $prevId ) . ' | ' .
283 $this->numLink( 250, $prevId ) . ' | ' .
284 $this->numLink( 500, $prevId );
285
286 return wfMsg( 'viewprevnext', $prevLink, $nextLink, $nums );
287 }
288
289 function numLink( $limit, $from, $ns = null ) {
290 global $wgLang;
291 $query = "limit={$limit}&from={$from}";
292 if( is_int($this->namespace) ) { $query .= "&namespace={$this->namespace}";}
293 $fmtLimit = $wgLang->formatNum( $limit );
294 return $this->makeSelfLink( $fmtLimit, $query );
295 }
296
297 function whatlinkshereForm( $options ) {
298 global $wgScript, $wgTitle;
299
300 $options['title'] = $wgTitle->getPrefixedText();
301
302 $f = Xml::openElement( 'form', array( 'method' => 'get', 'action' => "$wgScript" ) ) .
303 '<fieldset>' .
304 Xml::element( 'legend', array(), wfMsg( 'whatlinkshere' ) );
305
306 foreach ( $options as $name => $value ) {
307 if( $name === 'namespace') continue;
308 $f .= "\t" . Xml::hidden( $name, $value ). "\n";
309 }
310
311 $f .= Xml::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
312 Xml::namespaceSelector( $options['namespace'], '' ) .
313 Xml::submitButton( wfMsg( 'allpagessubmit' ) ) .
314 '</fieldset>' .
315 Xml::closeElement( 'form' ) . "\n";
316
317 return $f;
318 }
319
320 /** Set the namespace we are filtering on */
321 private function setNamespace( $ns ) {
322 $this->namespace = $ns;
323 }
324
325 }
326
327