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