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