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