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