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