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