277e279fdb6e2fbd3431b64a2de667198191a184
[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 $offsetCond = "page_id >= $from";
109 } else {
110 $offsetCond = false;
111 }
112 $options['ORDER BY'] = 'page_id';
113
114 // Read an extra row as an at-end check
115 $queryLimit = $limit + 1;
116 $options['LIMIT'] = $queryLimit;
117 if ( $offsetCond ) {
118 $tlConds[] = $offsetCond;
119 $plConds[] = $offsetCond;
120 }
121 $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect' );
122
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() ) );
139 }
140 return;
141 }
142
143 $options = array();
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'] );
148 } else {
149 $options['namespace'] = '';
150 $this->setNamespace( null );
151 }
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();
157
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;
164 }
165 $dbr->freeResult( $plRes );
166 while ( $row = $dbr->fetchObject( $tlRes ) ) {
167 $row->is_template = 1;
168 $rows[$row->page_id] = $row;
169 }
170 $dbr->freeResult( $tlRes );
171
172 // Sort by key and then change the keys to 0-based indices
173 ksort( $rows );
174 $rows = array_values( $rows );
175
176 $numRows = count( $rows );
177
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 );
185 } else {
186 // No more rows after
187 $nextId = false;
188 }
189 $prevId = $from;
190
191 if ( $level == 0 ) {
192 $wgOut->addHTML( $this->whatlinkshereForm( $options ) );
193 $wgOut->addWikiText( wfMsg( 'linkshere', $this->target->getPrefixedText() ) );
194 }
195 $isredir = wfMsg( 'isredirect' );
196 $istemplate = wfMsg( 'istemplate' );
197
198 if( $level == 0 ) {
199 $prevnext = $this->getPrevNext( $limit, $prevId, $nextId, $options['namespace'] );
200 $wgOut->addHTML( $prevnext );
201 }
202
203 $wgOut->addHTML( '<ul>' );
204 foreach ( $rows as $row ) {
205 $nt = Title::makeTitle( $row->page_namespace, $row->page_title );
206
207 if ( $row->page_is_redirect ) {
208 $extra = 'redirect=no';
209 } else {
210 $extra = '';
211 }
212
213 $link = $this->skin->makeKnownLinkObj( $nt, '', $extra );
214 $wgOut->addHTML( '<li>'.$link );
215
216 // Display properties (redirect or template)
217 $props = array();
218 if ( $row->page_is_redirect ) {
219 $props[] = $isredir;
220 }
221 if ( $row->is_template ) {
222 $props[] = $istemplate;
223 }
224 if ( count( $props ) ) {
225 // FIXME? Cultural assumption, hard-coded punctuation
226 $wgOut->addHTML( ' (' . implode( ', ', $props ) . ') ' );
227 }
228
229 if ( $row->page_is_redirect ) {
230 if ( $level < 2 ) {
231 $this->showIndirectLinks( $level + 1, $nt, 500 );
232 }
233 }
234 $wgOut->addHTML( "</li>\n" );
235 }
236 $wgOut->addHTML( "</ul>\n" );
237
238 if( $level == 0 ) {
239 $wgOut->addHTML( $prevnext );
240 }
241 }
242
243 function makeSelfLink( $text, $query ) {
244 return $this->skin->makeKnownLinkObj( $this->selfTitle, $text, $query );
245 }
246
247 function getPrevNext( $limit, $prevId, $nextId ) {
248 global $wgLang;
249 $fmtLimit = $wgLang->formatNum( $limit );
250 $prev = wfMsgExt( 'whatlinkshere-prev', array( 'parsemag', 'escape' ), $fmtLimit );
251 $next = wfMsgExt( 'whatlinkshere-next', array( 'parsemag', 'escape' ), $fmtLimit );
252
253 $nsText = '';
254 if( is_int($this->namespace) ) {
255 $nsText = "&namespace={$this->namespace}";
256 }
257
258 if ( 0 != $prevId ) {
259 $prevLink = $this->makeSelfLink( $prev, "limit={$limit}&from={$this->back}{$nsText}" );
260 } else {
261 $prevLink = $prev;
262 }
263 if ( 0 != $nextId ) {
264 $nextLink = $this->makeSelfLink( $next, "limit={$limit}&from={$nextId}&back={$prevId}{$nsText}" );
265 } else {
266 $nextLink = $next;
267 }
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 );
273
274 return wfMsg( 'viewprevnext', $prevLink, $nextLink, $nums );
275 }
276
277 function numLink( $limit, $from, $ns = null ) {
278 global $wgLang;
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 );
283 }
284
285 function whatlinkshereForm( $options ) {
286 global $wgScript, $wgTitle;
287
288 $options['title'] = $wgTitle->getPrefixedText();
289
290 $f = Xml::openElement( 'form', array( 'method' => 'get', 'action' => "$wgScript" ) ) .
291 '<fieldset>' .
292 Xml::element( 'legend', array(), wfMsg( 'whatlinkshere' ) );
293
294 foreach ( $options as $name => $value ) {
295 if( $name === 'namespace') continue;
296 $f .= "\t" . Xml::hidden( $name, $value ). "\n";
297 }
298
299 $f .= Xml::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
300 Xml::namespaceSelector( $options['namespace'], '' ) .
301 Xml::submitButton( wfMsg( 'allpagessubmit' ) ) .
302 '</fieldset>' .
303 Xml::closeElement( 'form' ) . "\n";
304
305 return $f;
306 }
307
308 /** Set the namespace we are filtering on */
309 private function setNamespace( $ns ) {
310 $this->namespace = $ns;
311 }
312
313 }
314
315 ?>