Code housekeeping stuff (and barring any stuff-ups on my behalf, there should be...
[lhc/web/wiklou.git] / includes / SpecialWhatlinkshere.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 * @subpackage SpecialPage
6 */
7
8 /**
9 * Entry point
10 * @param string $par An article name ??
11 */
12 function wfSpecialWhatlinkshere($par = NULL) {
13 global $wgRequest;
14 $page = new WhatLinksHerePage( $wgRequest, $par );
15 $page->execute();
16 }
17
18 class WhatLinksHerePage {
19 var $request, $par;
20 var $limit, $from, $dir, $target;
21 var $selfTitle, $skin;
22
23 function WhatLinksHerePage( &$request, $par = null ) {
24 global $wgUser;
25 $this->request =& $request;
26 $this->skin =& $wgUser->getSkin();
27 $this->par = $par;
28 }
29
30 function execute() {
31 global $wgOut;
32
33 $this->limit = min( $this->request->getInt( 'limit', 50 ), 5000 );
34 if ( $this->limit <= 0 ) {
35 $this->limit = 50;
36 }
37 $this->from = $this->request->getInt( 'from' );
38 $this->dir = $this->request->getText( 'dir', 'next' );
39 if ( $this->dir != 'prev' ) {
40 $this->dir = 'next';
41 }
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->dir );
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 string $dir 'next' or 'prev', whether $fromTitle is the start or end of the list
71 * @private
72 */
73 function showIndirectLinks( $level, $target, $limit, $from = 0, $dir = 'next' ) {
74 global $wgOut;
75 $fname = 'WhatLinksHerePage::showIndirectLinks';
76
77 $dbr =& wfGetDB( DB_READ );
78
79 extract( $dbr->tableNames( 'pagelinks', 'templatelinks', 'page' ) );
80
81 // Some extra validation
82 $from = intval( $from );
83 if ( !$from && $dir == 'prev' ) {
84 // Before start? No make sense
85 $dir = 'next';
86 }
87
88 // Make the query
89 $plConds = array(
90 'page_id=pl_from',
91 'pl_namespace' => $target->getNamespace(),
92 'pl_title' => $target->getDBkey(),
93 );
94
95 $tlConds = array(
96 'page_id=tl_from',
97 'tl_namespace' => $target->getNamespace(),
98 'tl_title' => $target->getDBkey(),
99 );
100
101 if ( $from ) {
102 if ( 'prev' == $dir ) {
103 $offsetCond = "page_id < $from";
104 $options = array( 'ORDER BY page_id DESC' );
105 } else {
106 $offsetCond = "page_id >= $from";
107 $options = array( 'ORDER BY page_id' );
108 }
109 } else {
110 $offsetCond = false;
111 $options = array( 'ORDER BY page_id,is_template DESC' );
112 }
113 // Read an extra row as an at-end check
114 $queryLimit = $limit + 1;
115 $options['LIMIT'] = $queryLimit;
116 if ( $offsetCond ) {
117 $tlConds[] = $offsetCond;
118 $plConds[] = $offsetCond;
119 }
120 $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect' );
121
122 $plRes = $dbr->select( array( 'pagelinks', 'page' ), $fields,
123 $plConds, $fname, $options );
124 $tlRes = $dbr->select( array( 'templatelinks', 'page' ), $fields,
125 $tlConds, $fname, $options );
126
127 if ( !$dbr->numRows( $plRes ) && !$dbr->numRows( $tlRes ) ) {
128 if ( 0 == $level ) {
129 $wgOut->addWikiText( wfMsg( 'nolinkshere', $this->target->getPrefixedText() ) );
130 }
131 return;
132 }
133
134 // Read the rows into an array and remove duplicates
135 // templatelinks comes second so that the templatelinks row overwrites the
136 // pagelinks row, so we get (inclusion) rather than nothing
137 while ( $row = $dbr->fetchObject( $plRes ) ) {
138 $row->is_template = 0;
139 $rows[$row->page_id] = $row;
140 }
141 $dbr->freeResult( $plRes );
142 while ( $row = $dbr->fetchObject( $tlRes ) ) {
143 $row->is_template = 1;
144 $rows[$row->page_id] = $row;
145 }
146 $dbr->freeResult( $tlRes );
147
148 // Sort by key and then change the keys to 0-based indices
149 ksort( $rows );
150 $rows = array_values( $rows );
151
152 $numRows = count( $rows );
153
154 // Work out the start and end IDs, for prev/next links
155 if ( $dir == 'prev' ) {
156 // Descending order
157 if ( $numRows > $limit ) {
158 // More rows available before these ones
159 // Get the ID from the next row past the end of the displayed set
160 $prevId = $rows[$limit]->page_id;
161 // Remove undisplayed rows
162 $rows = array_slice( $rows, 0, $limit );
163 } else {
164 // No more rows available before
165 $prevId = 0;
166 }
167 // Assume that the ID specified in $from exists, so there must be another page
168 $nextId = $from;
169
170 // Reverse order ready for display
171 $rows = array_reverse( $rows );
172 } else {
173 // Ascending
174 if ( $numRows > $limit ) {
175 // More rows available after these ones
176 // Get the ID from the last row in the result set
177 $nextId = $rows[$limit]->page_id;
178 // Remove undisplayed rows
179 $rows = array_slice( $rows, 0, $limit );
180 } else {
181 // No more rows after
182 $nextId = false;
183 }
184 $prevId = $from;
185 }
186
187 if ( 0 == $level ) {
188 $wgOut->addWikiText( wfMsg( 'linkshere', $this->target->getPrefixedText() ) );
189 }
190 $isredir = wfMsg( 'isredirect' );
191 $istemplate = wfMsg( 'istemplate' );
192
193 if( $level == 0 ) {
194 $prevnext = $this->getPrevNext( $limit, $prevId, $nextId );
195 $wgOut->addHTML( $prevnext );
196 }
197
198 $wgOut->addHTML( '<ul>' );
199 foreach ( $rows as $row ) {
200 $nt = Title::makeTitle( $row->page_namespace, $row->page_title );
201
202 if ( $row->page_is_redirect ) {
203 $extra = 'redirect=no';
204 } else {
205 $extra = '';
206 }
207
208 $link = $this->skin->makeKnownLinkObj( $nt, '', $extra );
209 $wgOut->addHTML( '<li>'.$link );
210
211 // Display properties (redirect or template)
212 $props = array();
213 if ( $row->page_is_redirect ) {
214 $props[] = $isredir;
215 }
216 if ( $row->is_template ) {
217 $props[] = $istemplate;
218 }
219 if ( count( $props ) ) {
220 // FIXME? Cultural assumption, hard-coded punctuation
221 $wgOut->addHTML( ' (' . implode( ', ', $props ) . ') ' );
222 }
223
224 if ( $row->page_is_redirect ) {
225 if ( $level < 2 ) {
226 $this->showIndirectLinks( $level + 1, $nt, 500 );
227 }
228 }
229 $wgOut->addHTML( "</li>\n" );
230 }
231 $wgOut->addHTML( "</ul>\n" );
232
233 if( $level == 0 ) {
234 $wgOut->addHTML( $prevnext );
235 }
236 }
237
238 function makeSelfLink( $text, $query ) {
239 return $this->skin->makeKnownLinkObj( $this->selfTitle, $text, $query );
240 }
241
242 function getPrevNext( $limit, $prevId, $nextId ) {
243 global $wgLang;
244 $fmtLimit = $wgLang->formatNum( $limit );
245 $prev = wfMsg( 'prevn', $fmtLimit );
246 $next = wfMsg( 'nextn', $fmtLimit );
247
248 if ( 0 != $prevId ) {
249 $prevLink = $this->makeSelfLink( $prev, "limit={$limit}&from={$prevId}&dir=prev" );
250 } else {
251 $prevLink = $prev;
252 }
253 if ( 0 != $nextId ) {
254 $nextLink = $this->makeSelfLink( $next, "limit={$limit}&from={$nextId}" );
255 } else {
256 $nextLink = $next;
257 }
258 $nums = $this->numLink( 20, $prevId ) . ' | ' .
259 $this->numLink( 50, $prevId ) . ' | ' .
260 $this->numLink( 100, $prevId ) . ' | ' .
261 $this->numLink( 250, $prevId ) . ' | ' .
262 $this->numLink( 500, $prevId );
263
264 return wfMsg( 'viewprevnext', $prevLink, $nextLink, $nums );
265 }
266
267 function numLink( $limit, $from ) {
268 global $wgLang;
269 $query = "limit={$limit}&from={$from}";
270 $fmtLimit = $wgLang->formatNum( $limit );
271 return $this->makeSelfLink( $fmtLimit, $query );
272 }
273 }
274
275 ?>