"@todo no-op" sounds a bit peculiar. ;)
[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 $options = array();
74
75 $ns = $this->request->getIntOrNull( 'namespace' );
76 if ( isset( $ns ) ) {
77 $options['namespace'] = $ns;
78 $this->setNamespace( $options['namespace'] );
79 } else {
80 $options['namespace'] = '';
81 }
82
83 // Make the query
84 $plConds = array(
85 'page_id=pl_from',
86 'pl_namespace' => $target->getNamespace(),
87 'pl_title' => $target->getDBkey(),
88 );
89
90 $tlConds = array(
91 'page_id=tl_from',
92 'tl_namespace' => $target->getNamespace(),
93 'tl_title' => $target->getDBkey(),
94 );
95
96 if ( $this->namespace !== null ){
97 $plConds['page_namespace'] = (int)$this->namespace;
98 $tlConds['page_namespace'] = (int)$this->namespace;
99 }
100
101 if ( $from ) {
102 $offsetCond = "page_id >= $from";
103 } else {
104 $offsetCond = false;
105 }
106 $options['ORDER BY'] = 'page_id';
107
108 // Read an extra row as an at-end check
109 $queryLimit = $limit + 1;
110 $options['LIMIT'] = $queryLimit;
111 if ( $offsetCond ) {
112 $tlConds[] = $offsetCond;
113 $plConds[] = $offsetCond;
114 }
115 $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect' );
116
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() ) );
133 }
134 return;
135 }
136
137 $options = array();
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'] );
142 } else {
143 $options['namespace'] = '';
144 $this->setNamespace( null );
145 }
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();
151
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;
158 }
159 $dbr->freeResult( $plRes );
160 while ( $row = $dbr->fetchObject( $tlRes ) ) {
161 $row->is_template = 1;
162 $rows[$row->page_id] = $row;
163 }
164 $dbr->freeResult( $tlRes );
165
166 // Sort by key and then change the keys to 0-based indices
167 ksort( $rows );
168 $rows = array_values( $rows );
169
170 $numRows = count( $rows );
171
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 );
179 } else {
180 // No more rows after
181 $nextId = false;
182 }
183 $prevId = $from;
184
185 if ( $level == 0 ) {
186 $wgOut->addHTML( $this->whatlinkshereForm( $options ) );
187 $wgOut->addWikiText( wfMsg( 'linkshere', $this->target->getPrefixedText() ) );
188 }
189 $isredir = wfMsg( 'isredirect' );
190 $istemplate = wfMsg( 'istemplate' );
191
192 if( $level == 0 ) {
193 $prevnext = $this->getPrevNext( $limit, $prevId, $nextId, $options['namespace'] );
194 $wgOut->addHTML( $prevnext );
195 }
196
197 $wgOut->addHTML( '<ul>' );
198 foreach ( $rows as $row ) {
199 $nt = Title::makeTitle( $row->page_namespace, $row->page_title );
200
201 if ( $row->page_is_redirect ) {
202 $extra = 'redirect=no';
203 } else {
204 $extra = '';
205 }
206
207 $link = $this->skin->makeKnownLinkObj( $nt, '', $extra );
208 $wgOut->addHTML( '<li>'.$link );
209
210 // Display properties (redirect or template)
211 $props = array();
212 if ( $row->page_is_redirect ) {
213 $props[] = $isredir;
214 }
215 if ( $row->is_template ) {
216 $props[] = $istemplate;
217 }
218 if ( count( $props ) ) {
219 // FIXME? Cultural assumption, hard-coded punctuation
220 $wgOut->addHTML( ' (' . implode( ', ', $props ) . ') ' );
221 }
222
223 if ( $row->page_is_redirect ) {
224 if ( $level < 2 ) {
225 $this->showIndirectLinks( $level + 1, $nt, 500 );
226 }
227 }
228 $wgOut->addHTML( "</li>\n" );
229 }
230 $wgOut->addHTML( "</ul>\n" );
231
232 if( $level == 0 ) {
233 $wgOut->addHTML( $prevnext );
234 }
235 }
236
237 function makeSelfLink( $text, $query ) {
238 return $this->skin->makeKnownLinkObj( $this->selfTitle, $text, $query );
239 }
240
241 function getPrevNext( $limit, $prevId, $nextId, $namespace ) {
242 global $wgLang;
243 $fmtLimit = $wgLang->formatNum( $limit );
244 $prev = wfMsgExt( 'whatlinkshere-prev', array( 'parsemag', 'escape' ), $fmtLimit );
245 $next = wfMsgExt( 'whatlinkshere-next', array( 'parsemag', 'escape' ), $fmtLimit );
246
247 if ( 0 != $prevId ) {
248 $prevLink = $this->makeSelfLink( $prev, "limit={$limit}&from={$this->back}&namespace={$namespace}" );
249 } else {
250 $prevLink = $prev;
251 }
252 if ( 0 != $nextId ) {
253 $nextLink = $this->makeSelfLink( $next, "limit={$limit}&from={$nextId}&back={$prevId}&namespace={$namespace}" );
254 } else {
255 $nextLink = $next;
256 }
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 );
262
263 return wfMsg( 'viewprevnext', $prevLink, $nextLink, $nums );
264 }
265
266 function numLink( $limit, $from ) {
267 global $wgLang;
268 $query = "limit={$limit}&from={$from}";
269 $fmtLimit = $wgLang->formatNum( $limit );
270 return $this->makeSelfLink( $fmtLimit, $query );
271 }
272
273 function whatlinkshereForm( $options ) {
274 global $wgScript, $wgTitle;
275
276 $options['title'] = $wgTitle->getPrefixedText();
277
278 $f = Xml::openElement( 'form', array( 'method' => 'get', 'action' => "$wgScript" ) ) .
279 '<fieldset>' .
280 Xml::element( 'legend', array(), wfMsg( 'whatlinkshere' ) );
281
282 foreach ( $options as $name => $value ) {
283 if( $name === 'namespace') continue;
284 $f .= "\t" . Xml::hidden( $name, $value ). "\n";
285 }
286
287 $f .= Xml::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
288 Xml::namespaceSelector( $options['namespace'], '' ) .
289 Xml::submitButton( wfMsg( 'allpagessubmit' ) ) .
290 '</fieldset>' .
291 Xml::closeElement( 'form' ) . "\n";
292
293 return $f;
294 }
295
296 function setNamespace( $ns ) {
297 $this->namespace = $ns;
298 }
299
300 }
301
302 ?>