Add &redirect=no to newpages links (bug 14353)
[lhc/web/wiklou.git] / includes / specials / SpecialNewpages.php
1 <?php
2
3 /**
4 * implements Special:Newpages
5 * @ingroup SpecialPage
6 */
7 class SpecialNewpages extends SpecialPage {
8
9 // Stored objects
10 protected $opts, $skin;
11
12 // Some internal settings
13 protected $showNavigation = false;
14
15 public function __construct() {
16 parent::__construct( 'Newpages' );
17 $this->includable( true );
18 }
19
20 protected function setup( $par ) {
21 global $wgRequest, $wgUser, $wgEnableNewpagesUserFilter;
22
23 // Options
24 $opts = new FormOptions();
25 $this->opts = $opts; // bind
26 $opts->add( 'hideliu', false );
27 $opts->add( 'hidepatrolled', false );
28 $opts->add( 'hidebots', false );
29 $opts->add( 'hideredirs', true );
30 $opts->add( 'limit', (int)$wgUser->getOption( 'rclimit' ) );
31 $opts->add( 'offset', '' );
32 $opts->add( 'namespace', '0' );
33 $opts->add( 'username', '' );
34 $opts->add( 'feed', '' );
35
36 // Set values
37 $opts->fetchValuesFromRequest( $wgRequest );
38 if ( $par ) $this->parseParams( $par );
39
40 // Validate
41 $opts->validateIntBounds( 'limit', 0, 5000 );
42 if( !$wgEnableNewpagesUserFilter ) {
43 $opts->setValue( 'username', '' );
44 }
45
46 // Store some objects
47 $this->skin = $wgUser->getSkin();
48 }
49
50 protected function parseParams( $par ) {
51 global $wgLang;
52 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
53 foreach ( $bits as $bit ) {
54 if ( 'shownav' == $bit )
55 $this->showNavigation = true;
56 if ( 'hideliu' === $bit )
57 $this->opts->setValue( 'hideliu', true );
58 if ( 'hidepatrolled' == $bit )
59 $this->opts->setValue( 'hidepatrolled', true );
60 if ( 'hidebots' == $bit )
61 $this->opts->setValue( 'hidebots', true );
62 if ( 'showredirs' == $bit )
63 $this->opts->setValue( 'hideredirs', false );
64 if ( is_numeric( $bit ) )
65 $this->opts->setValue( 'limit', intval( $bit ) );
66
67 $m = array();
68 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) )
69 $this->opts->setValue( 'limit', intval($m[1]) );
70 // PG offsets not just digits!
71 if ( preg_match( '/^offset=([^=]+)$/', $bit, $m ) )
72 $this->opts->setValue( 'offset', intval($m[1]) );
73 if ( preg_match( '/^username=(.*)$/', $bit, $m ) )
74 $this->opts->setValue( 'username', $m[1] );
75 if ( preg_match( '/^namespace=(.*)$/', $bit, $m ) ) {
76 $ns = $wgLang->getNsIndex( $m[1] );
77 if( $ns !== false ) {
78 $this->opts->setValue( 'namespace', $ns );
79 }
80 }
81 }
82 }
83
84 /**
85 * Show a form for filtering namespace and username
86 *
87 * @param string $par
88 * @return string
89 */
90 public function execute( $par ) {
91 global $wgLang, $wgUser, $wgOut;
92
93 $this->setHeaders();
94 $this->outputHeader();
95
96 $this->showNavigation = !$this->including(); // Maybe changed in setup
97 $this->setup( $par );
98
99 if( !$this->including() ) {
100 // Settings
101 $this->form();
102
103 $this->setSyndicated();
104 $feedType = $this->opts->getValue( 'feed' );
105 if( $feedType ) {
106 return $this->feed( $feedType );
107 }
108 }
109
110 $pager = new NewPagesPager( $this, $this->opts );
111 $pager->mLimit = $this->opts->getValue( 'limit' );
112 $pager->mOffset = $this->opts->getValue( 'offset' );
113
114 if( $pager->getNumRows() ) {
115 $navigation = '';
116 if ( $this->showNavigation ) $navigation = $pager->getNavigationBar();
117 $wgOut->addHTML( $navigation . $pager->getBody() . $navigation );
118 } else {
119 $wgOut->addWikiMsg( 'specialpage-empty' );
120 }
121 }
122
123 protected function filterLinks() {
124 global $wgGroupPermissions, $wgUser;
125
126 // show/hide links
127 $showhide = array( wfMsgHtml( 'show' ), wfMsgHtml( 'hide' ) );
128
129 // Option value -> message mapping
130 $filters = array(
131 'hideliu' => 'rcshowhideliu',
132 'hidepatrolled' => 'rcshowhidepatr',
133 'hidebots' => 'rcshowhidebots',
134 'hideredirs' => 'whatlinkshere-hideredirs'
135 );
136
137 // Disable some if needed
138 if ( $wgGroupPermissions['*']['createpage'] !== true )
139 unset($filters['hideliu']);
140
141 if ( !$wgUser->useNPPatrol() )
142 unset($filters['hidepatrolled']);
143
144 $links = array();
145 $changed = $this->opts->getChangedValues();
146 unset($changed['offset']); // Reset offset if query type changes
147
148 $self = $this->getTitle();
149 foreach ( $filters as $key => $msg ) {
150 $onoff = 1 - $this->opts->getValue($key);
151 $link = $this->skin->link( $self, $showhide[$onoff], array(),
152 array( $key => $onoff ) + $changed
153 );
154 $links[$key] = wfMsgHtml( $msg, $link );
155 }
156
157 return implode( ' | ', $links );
158 }
159
160 protected function form() {
161 global $wgOut, $wgEnableNewpagesUserFilter, $wgScript;
162
163 // Consume values
164 $this->opts->consumeValue( 'offset' ); // don't carry offset, DWIW
165 $namespace = $this->opts->consumeValue( 'namespace' );
166 $username = $this->opts->consumeValue( 'username' );
167
168 // Check username input validity
169 $ut = Title::makeTitleSafe( NS_USER, $username );
170 $userText = $ut ? $ut->getText() : '';
171
172 // Store query values in hidden fields so that form submission doesn't lose them
173 $hidden = array();
174 foreach ( $this->opts->getUnconsumedValues() as $key => $value ) {
175 $hidden[] = Xml::hidden( $key, $value );
176 }
177 $hidden = implode( "\n", $hidden );
178
179 $form = Xml::openElement( 'form', array( 'action' => $wgScript ) ) .
180 Xml::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) .
181 Xml::fieldset( wfMsg( 'newpages' ) ) .
182 Xml::openElement( 'table', array( 'id' => 'mw-newpages-table' ) ) .
183 "<tr>
184 <td class='mw-label'>" .
185 Xml::label( wfMsg( 'namespace' ), 'namespace' ) .
186 "</td>
187 <td class='mw-input'>" .
188 Xml::namespaceSelector( $namespace, 'all' ) .
189 "</td>
190 </tr>" .
191 ($wgEnableNewpagesUserFilter ?
192 "<tr>
193 <td class='mw-label'>" .
194 Xml::label( wfMsg( 'newpages-username' ), 'mw-np-username' ) .
195 "</td>
196 <td class='mw-input'>" .
197 Xml::input( 'username', 30, $userText, array( 'id' => 'mw-np-username' ) ) .
198 "</td>
199 </tr>" : "" ) .
200 "<tr> <td></td>
201 <td class='mw-submit'>" .
202 Xml::submitButton( wfMsg( 'allpagessubmit' ) ) .
203 "</td>
204 </tr>" .
205 "<tr>
206 <td></td>
207 <td class='mw-input'>" .
208 $this->filterLinks() .
209 "</td>
210 </tr>" .
211 Xml::closeElement( 'table' ) .
212 Xml::closeElement( 'fieldset' ) .
213 $hidden .
214 Xml::closeElement( 'form' );
215
216 $wgOut->addHTML( $form );
217 }
218
219 protected function setSyndicated() {
220 global $wgOut;
221 $queryParams = array(
222 'namespace' => $this->opts->getValue( 'namespace' ),
223 'username' => $this->opts->getValue( 'username' )
224 );
225 $wgOut->setSyndicated( true );
226 $wgOut->setFeedAppendQuery( wfArrayToCGI( $queryParams ) );
227 }
228
229 /**
230 * Format a row, providing the timestamp, links to the page/history, size, user links, and a comment
231 *
232 * @param $skin Skin to use
233 * @param $result Result row
234 * @return string
235 */
236 public function formatRow( $result ) {
237 global $wgLang, $wgContLang, $wgUser;
238 $dm = $wgContLang->getDirMark();
239
240 $title = Title::makeTitleSafe( $result->rc_namespace, $result->rc_title );
241 $time = $wgLang->timeAndDate( $result->rc_timestamp, true );
242 $query = $this->patrollable( $result ) ? "rcid={$result->rc_id}&redirect=no" : 'redirect=no';
243 $plink = $this->skin->makeKnownLinkObj( $title, '', $query );
244 $hist = $this->skin->makeKnownLinkObj( $title, wfMsgHtml( 'hist' ), 'action=history' );
245 $length = wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ),
246 $wgLang->formatNum( $result->length ) );
247 $ulink = $this->skin->userLink( $result->rc_user, $result->rc_user_text ) . ' ' .
248 $this->skin->userToolLinks( $result->rc_user, $result->rc_user_text );
249 $comment = $this->skin->commentBlock( $result->rc_comment );
250 $css = $this->patrollable( $result ) ? " class='not-patrolled'" : '';
251
252 return "<li{$css}>{$time} {$dm}{$plink} ({$hist}) {$dm}[{$length}] {$dm}{$ulink} {$comment}</li>\n";
253 }
254
255 /**
256 * Should a specific result row provide "patrollable" links?
257 *
258 * @param $result Result row
259 * @return bool
260 */
261 protected function patrollable( $result ) {
262 global $wgUser;
263 return ( $wgUser->useNPPatrol() && !$result->rc_patrolled );
264 }
265
266 /**
267 * Output a subscription feed listing recent edits to this page.
268 * @param string $type
269 */
270 protected function feed( $type ) {
271 global $wgFeed, $wgFeedClasses, $wgFeedLimit;
272
273 if ( !$wgFeed ) {
274 global $wgOut;
275 $wgOut->addWikiMsg( 'feed-unavailable' );
276 return;
277 }
278
279 if( !isset( $wgFeedClasses[$type] ) ) {
280 global $wgOut;
281 $wgOut->addWikiMsg( 'feed-invalid' );
282 return;
283 }
284
285 $feed = new $wgFeedClasses[$type](
286 $this->feedTitle(),
287 wfMsgExt( 'tagline', 'parsemag' ),
288 $this->getTitle()->getFullUrl() );
289
290 $pager = new NewPagesPager( $this, $this->opts );
291 $limit = $this->opts->getValue( 'limit' );
292 $pager->mLimit = min( $limit, $wgFeedLimit );
293
294 $feed->outHeader();
295 if( $pager->getNumRows() > 0 ) {
296 while( $row = $pager->mResult->fetchObject() ) {
297 $feed->outItem( $this->feedItem( $row ) );
298 }
299 }
300 $feed->outFooter();
301 }
302
303 protected function feedTitle() {
304 global $wgContLanguageCode, $wgSitename;
305 $page = SpecialPage::getPage( 'Newpages' );
306 $desc = $page->getDescription();
307 return "$wgSitename - $desc [$wgContLanguageCode]";
308 }
309
310 protected function feedItem( $row ) {
311 $title = Title::MakeTitle( intval( $row->rc_namespace ), $row->rc_title );
312 if( $title ) {
313 $date = $row->rc_timestamp;
314 $comments = $title->getTalkPage()->getFullURL();
315
316 return new FeedItem(
317 $title->getPrefixedText(),
318 $this->feedItemDesc( $row ),
319 $title->getFullURL(),
320 $date,
321 $this->feedItemAuthor( $row ),
322 $comments);
323 } else {
324 return NULL;
325 }
326 }
327
328 protected function feedItemAuthor( $row ) {
329 return isset( $row->rc_user_text ) ? $row->rc_user_text : '';
330 }
331
332 protected function feedItemDesc( $row ) {
333 $revision = Revision::newFromId( $row->rev_id );
334 if( $revision ) {
335 return '<p>' . htmlspecialchars( $revision->getUserText() ) . wfMsgForContent( 'colon-separator' ) .
336 htmlspecialchars( FeedItem::stripComment( $revision->getComment() ) ) .
337 "</p>\n<hr />\n<div>" .
338 nl2br( htmlspecialchars( $revision->getText() ) ) . "</div>";
339 }
340 return '';
341 }
342 }
343
344 /**
345 * @ingroup SpecialPage Pager
346 */
347 class NewPagesPager extends ReverseChronologicalPager {
348 // Stored opts
349 protected $opts, $mForm;
350
351 function __construct( $form, FormOptions $opts ) {
352 parent::__construct();
353 $this->mForm = $form;
354 $this->opts = $opts;
355 }
356
357 function getTitle() {
358 static $title = null;
359 if ( $title === null )
360 $title = $this->mForm->getTitle();
361 return $title;
362 }
363
364 function getQueryInfo() {
365 global $wgEnableNewpagesUserFilter, $wgGroupPermissions, $wgUser;
366 $conds = array();
367 $conds['rc_new'] = 1;
368
369 $namespace = $this->opts->getValue( 'namespace' );
370 $namespace = ( $namespace === 'all' ) ? false : intval( $namespace );
371
372 $username = $this->opts->getValue( 'username' );
373 $user = Title::makeTitleSafe( NS_USER, $username );
374
375 if( $namespace !== false ) {
376 $conds['rc_namespace'] = $namespace;
377 $rcIndexes = array( 'new_name_timestamp' );
378 } else {
379 $rcIndexes = array( 'rc_timestamp' );
380 }
381 $conds[] = 'page_id = rc_cur_id';
382
383 # $wgEnableNewpagesUserFilter - temp WMF hack
384 if( $wgEnableNewpagesUserFilter && $user ) {
385 $conds['rc_user_text'] = $user->getText();
386 $rcIndexes = 'rc_user_text';
387 # If anons cannot make new pages, don't "exclude logged in users"!
388 } elseif( $wgGroupPermissions['*']['createpage'] && $this->opts->getValue( 'hideliu' ) ) {
389 $conds['rc_user'] = 0;
390 }
391 # If this user cannot see patrolled edits or they are off, don't do dumb queries!
392 if( $this->opts->getValue( 'hidepatrolled' ) && $wgUser->useNPPatrol() ) {
393 $conds['rc_patrolled'] = 0;
394 }
395 if( $this->opts->getValue( 'hidebots' ) ) {
396 $conds['rc_bot'] = 0;
397 }
398
399 if ( $this->opts->getValue( 'hideredirs' ) ) {
400 $conds['page_is_redirect'] = 0;
401 }
402
403 return array(
404 'tables' => array( 'recentchanges', 'page' ),
405 'fields' => 'rc_namespace,rc_title, rc_cur_id, rc_user,rc_user_text,rc_comment,
406 rc_timestamp,rc_patrolled,rc_id,page_len as length, page_latest as rev_id',
407 'conds' => $conds,
408 'options' => array( 'USE INDEX' => array('recentchanges' => $rcIndexes) )
409 );
410 }
411
412 function getIndexField() {
413 return 'rc_timestamp';
414 }
415
416 function formatRow( $row ) {
417 return $this->mForm->formatRow( $row );
418 }
419
420 function getStartBody() {
421 # Do a batch existence check on pages
422 $linkBatch = new LinkBatch();
423 while( $row = $this->mResult->fetchObject() ) {
424 $linkBatch->add( NS_USER, $row->rc_user_text );
425 $linkBatch->add( NS_USER_TALK, $row->rc_user_text );
426 $linkBatch->add( $row->rc_namespace, $row->rc_title );
427 }
428 $linkBatch->execute();
429 return "<ul>";
430 }
431
432 function getEndBody() {
433 return "</ul>";
434 }
435 }