Branch merge of change-tagging branch with trunk
[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, $wgUser, $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;
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 implode( ' | ', $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
169 // Check username input validity
170 $ut = Title::makeTitleSafe( NS_USER, $username );
171 $userText = $ut ? $ut->getText() : '';
172
173 // Store query values in hidden fields so that form submission doesn't lose them
174 $hidden = array();
175 foreach ( $this->opts->getUnconsumedValues() as $key => $value ) {
176 $hidden[] = Xml::hidden( $key, $value );
177 }
178 $hidden = implode( "\n", $hidden );
179
180 list( $tagFilterLabel, $tagFilterSelector ) = ChangeTags::buildTagFilterSelector( $this->opts['tagfilter'] );
181
182 $form = Xml::openElement( 'form', array( 'action' => $wgScript ) ) .
183 Xml::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) .
184 Xml::fieldset( wfMsg( 'newpages' ) ) .
185 Xml::openElement( 'table', array( 'id' => 'mw-newpages-table' ) ) .
186 "<tr>
187 <td class='mw-label'>" .
188 Xml::label( wfMsg( 'namespace' ), 'namespace' ) .
189 "</td>
190 <td class='mw-input'>" .
191 Xml::namespaceSelector( $namespace, 'all' ) .
192 "</td>
193 </tr>" .
194 "<tr>
195 <td class='mw-label'>" .
196 $tagFilterLabel .
197 "</td>
198 <td class='mw-input'>" .
199 $tagFilterSelector .
200 "</td>
201 </tr>" .
202 ($wgEnableNewpagesUserFilter ?
203 "<tr>
204 <td class='mw-label'>" .
205 Xml::label( wfMsg( 'newpages-username' ), 'mw-np-username' ) .
206 "</td>
207 <td class='mw-input'>" .
208 Xml::input( 'username', 30, $userText, array( 'id' => 'mw-np-username' ) ) .
209 "</td>
210 </tr>" : "" ) .
211 "<tr> <td></td>
212 <td class='mw-submit'>" .
213 Xml::submitButton( wfMsg( 'allpagessubmit' ) ) .
214 "</td>
215 </tr>" .
216 "<tr>
217 <td></td>
218 <td class='mw-input'>" .
219 $this->filterLinks() .
220 "</td>
221 </tr>" .
222 Xml::closeElement( 'table' ) .
223 Xml::closeElement( 'fieldset' ) .
224 $hidden .
225 Xml::closeElement( 'form' );
226
227 $wgOut->addHTML( $form );
228 }
229
230 protected function setSyndicated() {
231 global $wgOut;
232 $queryParams = array(
233 'namespace' => $this->opts->getValue( 'namespace' ),
234 'username' => $this->opts->getValue( 'username' )
235 );
236 $wgOut->setSyndicated( true );
237 $wgOut->setFeedAppendQuery( wfArrayToCGI( $queryParams ) );
238 }
239
240 /**
241 * Format a row, providing the timestamp, links to the page/history, size, user links, and a comment
242 *
243 * @param $skin Skin to use
244 * @param $result Result row
245 * @return string
246 */
247 public function formatRow( $result ) {
248 global $wgLang, $wgContLang, $wgUser;
249
250 $classes = array();
251
252 $dm = $wgContLang->getDirMark();
253
254 $title = Title::makeTitleSafe( $result->rc_namespace, $result->rc_title );
255 $time = $wgLang->timeAndDate( $result->rc_timestamp, true );
256 $query = $this->patrollable( $result ) ? "rcid={$result->rc_id}&redirect=no" : 'redirect=no';
257 $plink = $this->skin->makeKnownLinkObj( $title, '', $query );
258 $hist = $this->skin->makeKnownLinkObj( $title, wfMsgHtml( 'hist' ), 'action=history' );
259 $length = wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ),
260 $wgLang->formatNum( $result->length ) );
261 $ulink = $this->skin->userLink( $result->rc_user, $result->rc_user_text ) . ' ' .
262 $this->skin->userToolLinks( $result->rc_user, $result->rc_user_text );
263 $comment = $this->skin->commentBlock( $result->rc_comment );
264
265 if ( $this->patrollable( $result ) )
266 $classes[] = 'not-patrolled';
267
268 # Tags, if any.
269 list( $tagDisplay, $newClasses ) = ChangeTags::formatSummaryRow( $result->ts_tags, 'newpages' );
270 $classes = array_merge( $classes, $newClasses );
271
272 $css = count($classes) ? ' class="'.implode( " ", $classes).'"' : '';
273
274 return "<li{$css}>{$time} {$dm}{$plink} ({$hist}) {$dm}[{$length}] {$dm}{$ulink} {$comment} {$tagDisplay}</li>\n";
275 }
276
277 /**
278 * Should a specific result row provide "patrollable" links?
279 *
280 * @param $result Result row
281 * @return bool
282 */
283 protected function patrollable( $result ) {
284 global $wgUser;
285 return ( $wgUser->useNPPatrol() && !$result->rc_patrolled );
286 }
287
288 /**
289 * Output a subscription feed listing recent edits to this page.
290 * @param string $type
291 */
292 protected function feed( $type ) {
293 global $wgFeed, $wgFeedClasses, $wgFeedLimit;
294
295 if ( !$wgFeed ) {
296 global $wgOut;
297 $wgOut->addWikiMsg( 'feed-unavailable' );
298 return;
299 }
300
301 if( !isset( $wgFeedClasses[$type] ) ) {
302 global $wgOut;
303 $wgOut->addWikiMsg( 'feed-invalid' );
304 return;
305 }
306
307 $feed = new $wgFeedClasses[$type](
308 $this->feedTitle(),
309 wfMsgExt( 'tagline', 'parsemag' ),
310 $this->getTitle()->getFullUrl() );
311
312 $pager = new NewPagesPager( $this, $this->opts );
313 $limit = $this->opts->getValue( 'limit' );
314 $pager->mLimit = min( $limit, $wgFeedLimit );
315
316 $feed->outHeader();
317 if( $pager->getNumRows() > 0 ) {
318 while( $row = $pager->mResult->fetchObject() ) {
319 $feed->outItem( $this->feedItem( $row ) );
320 }
321 }
322 $feed->outFooter();
323 }
324
325 protected function feedTitle() {
326 global $wgContLanguageCode, $wgSitename;
327 $page = SpecialPage::getPage( 'Newpages' );
328 $desc = $page->getDescription();
329 return "$wgSitename - $desc [$wgContLanguageCode]";
330 }
331
332 protected function feedItem( $row ) {
333 $title = Title::MakeTitle( intval( $row->rc_namespace ), $row->rc_title );
334 if( $title ) {
335 $date = $row->rc_timestamp;
336 $comments = $title->getTalkPage()->getFullURL();
337
338 return new FeedItem(
339 $title->getPrefixedText(),
340 $this->feedItemDesc( $row ),
341 $title->getFullURL(),
342 $date,
343 $this->feedItemAuthor( $row ),
344 $comments);
345 } else {
346 return NULL;
347 }
348 }
349
350 protected function feedItemAuthor( $row ) {
351 return isset( $row->rc_user_text ) ? $row->rc_user_text : '';
352 }
353
354 protected function feedItemDesc( $row ) {
355 $revision = Revision::newFromId( $row->rev_id );
356 if( $revision ) {
357 return '<p>' . htmlspecialchars( $revision->getUserText() ) . wfMsgForContent( 'colon-separator' ) .
358 htmlspecialchars( FeedItem::stripComment( $revision->getComment() ) ) .
359 "</p>\n<hr />\n<div>" .
360 nl2br( htmlspecialchars( $revision->getText() ) ) . "</div>";
361 }
362 return '';
363 }
364 }
365
366 /**
367 * @ingroup SpecialPage Pager
368 */
369 class NewPagesPager extends ReverseChronologicalPager {
370 // Stored opts
371 protected $opts, $mForm;
372
373 function __construct( $form, FormOptions $opts ) {
374 parent::__construct();
375 $this->mForm = $form;
376 $this->opts = $opts;
377 }
378
379 function getTitle() {
380 static $title = null;
381 if ( $title === null )
382 $title = $this->mForm->getTitle();
383 return $title;
384 }
385
386 function getQueryInfo() {
387 global $wgEnableNewpagesUserFilter, $wgGroupPermissions, $wgUser;
388 $conds = array();
389 $conds['rc_new'] = 1;
390
391 $namespace = $this->opts->getValue( 'namespace' );
392 $namespace = ( $namespace === 'all' ) ? false : intval( $namespace );
393
394 $username = $this->opts->getValue( 'username' );
395 $user = Title::makeTitleSafe( NS_USER, $username );
396
397 if( $namespace !== false ) {
398 $conds['rc_namespace'] = $namespace;
399 $rcIndexes = array( 'new_name_timestamp' );
400 } else {
401 $rcIndexes = array( 'rc_timestamp' );
402 }
403
404 # $wgEnableNewpagesUserFilter - temp WMF hack
405 if( $wgEnableNewpagesUserFilter && $user ) {
406 $conds['rc_user_text'] = $user->getText();
407 $rcIndexes = 'rc_user_text';
408 # If anons cannot make new pages, don't "exclude logged in users"!
409 } elseif( $wgGroupPermissions['*']['createpage'] && $this->opts->getValue( 'hideliu' ) ) {
410 $conds['rc_user'] = 0;
411 }
412 # If this user cannot see patrolled edits or they are off, don't do dumb queries!
413 if( $this->opts->getValue( 'hidepatrolled' ) && $wgUser->useNPPatrol() ) {
414 $conds['rc_patrolled'] = 0;
415 }
416 if( $this->opts->getValue( 'hidebots' ) ) {
417 $conds['rc_bot'] = 0;
418 }
419
420 if ( $this->opts->getValue( 'hideredirs' ) ) {
421 $conds['page_is_redirect'] = 0;
422 }
423
424 $info = array(
425 'tables' => array( 'recentchanges', 'page' ),
426 'fields' => 'rc_namespace,rc_title, rc_cur_id, rc_user,rc_user_text,rc_comment,
427 rc_timestamp,rc_patrolled,rc_id,page_len as length, page_latest as rev_id, ts_tags',
428 'conds' => $conds,
429 'options' => array( 'USE INDEX' => array('recentchanges' => $rcIndexes) ),
430 'join_conds' => array(
431 'page' => array('INNER JOIN', 'page_id=rc_cur_id'),
432 ),
433 );
434
435 ## Empty array for fields, it'll be set by us anyway.
436 $fields = array();
437
438 ## Modify query for tags
439 ChangeTags::modifyDisplayQuery( $info['tables'], $fields, $info['conds'], $info['join_conds'], $this->opts['tagfilter'] );
440
441 return $info;
442 }
443
444 function getIndexField() {
445 return 'rc_timestamp';
446 }
447
448 function formatRow( $row ) {
449 return $this->mForm->formatRow( $row );
450 }
451
452 function getStartBody() {
453 # Do a batch existence check on pages
454 $linkBatch = new LinkBatch();
455 while( $row = $this->mResult->fetchObject() ) {
456 $linkBatch->add( NS_USER, $row->rc_user_text );
457 $linkBatch->add( NS_USER_TALK, $row->rc_user_text );
458 $linkBatch->add( $row->rc_namespace, $row->rc_title );
459 }
460 $linkBatch->execute();
461 return "<ul>";
462 }
463
464 function getEndBody() {
465 return "</ul>";
466 }
467 }