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