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