Fix username autocomplete on Newpages
[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 * @var FormOptions
32 */
33 protected $opts;
34 protected $customFilters;
35
36 protected $showNavigation = false;
37
38 public function __construct() {
39 parent::__construct( 'Newpages' );
40 }
41
42 protected function setup( $par ) {
43 // Options
44 $opts = new FormOptions();
45 $this->opts = $opts; // bind
46 $opts->add( 'hideliu', false );
47 $opts->add( 'hidepatrolled', $this->getUser()->getBoolOption( 'newpageshidepatrolled' ) );
48 $opts->add( 'hidebots', false );
49 $opts->add( 'hideredirs', true );
50 $opts->add( 'limit', $this->getUser()->getIntOption( 'rclimit' ) );
51 $opts->add( 'offset', '' );
52 $opts->add( 'namespace', '0' );
53 $opts->add( 'username', '' );
54 $opts->add( 'feed', '' );
55 $opts->add( 'tagfilter', '' );
56 $opts->add( 'invert', false );
57
58 $this->customFilters = array();
59 wfRunHooks( 'SpecialNewPagesFilters', array( $this, &$this->customFilters ) );
60 foreach ( $this->customFilters as $key => $params ) {
61 $opts->add( $key, $params['default'] );
62 }
63
64 // Set values
65 $opts->fetchValuesFromRequest( $this->getRequest() );
66 if ( $par ) {
67 $this->parseParams( $par );
68 }
69
70 // Validate
71 $opts->validateIntBounds( 'limit', 0, 5000 );
72 }
73
74 protected function parseParams( $par ) {
75 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
76 foreach ( $bits as $bit ) {
77 if ( 'shownav' == $bit ) {
78 $this->showNavigation = true;
79 }
80 if ( 'hideliu' === $bit ) {
81 $this->opts->setValue( 'hideliu', true );
82 }
83 if ( 'hidepatrolled' == $bit ) {
84 $this->opts->setValue( 'hidepatrolled', true );
85 }
86 if ( 'hidebots' == $bit ) {
87 $this->opts->setValue( 'hidebots', true );
88 }
89 if ( 'showredirs' == $bit ) {
90 $this->opts->setValue( 'hideredirs', false );
91 }
92 if ( is_numeric( $bit ) ) {
93 $this->opts->setValue( 'limit', intval( $bit ) );
94 }
95
96 $m = array();
97 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) {
98 $this->opts->setValue( 'limit', intval( $m[1] ) );
99 }
100 // PG offsets not just digits!
101 if ( preg_match( '/^offset=([^=]+)$/', $bit, $m ) ) {
102 $this->opts->setValue( 'offset', intval( $m[1] ) );
103 }
104 if ( preg_match( '/^username=(.*)$/', $bit, $m ) ) {
105 $this->opts->setValue( 'username', $m[1] );
106 }
107 if ( preg_match( '/^namespace=(.*)$/', $bit, $m ) ) {
108 $ns = $this->getLanguage()->getNsIndex( $m[1] );
109 if ( $ns !== false ) {
110 $this->opts->setValue( 'namespace', $ns );
111 }
112 }
113 }
114 }
115
116 /**
117 * Show a form for filtering namespace and username
118 *
119 * @param string $par
120 */
121 public function execute( $par ) {
122 $out = $this->getOutput();
123 $out->addModules( 'mediawiki.userSuggest' );
124
125 $this->setHeaders();
126 $this->outputHeader();
127
128 $this->showNavigation = !$this->including(); // Maybe changed in setup
129 $this->setup( $par );
130
131 if ( !$this->including() ) {
132 // Settings
133 $this->form();
134
135 $feedType = $this->opts->getValue( 'feed' );
136 if ( $feedType ) {
137 $this->feed( $feedType );
138
139 return;
140 }
141
142 $allValues = $this->opts->getAllValues();
143 unset( $allValues['feed'] );
144 $out->setFeedAppendQuery( wfArrayToCgi( $allValues ) );
145 }
146
147 $pager = new NewPagesPager( $this, $this->opts );
148 $pager->mLimit = $this->opts->getValue( 'limit' );
149 $pager->mOffset = $this->opts->getValue( 'offset' );
150
151 if ( $pager->getNumRows() ) {
152 $navigation = '';
153 if ( $this->showNavigation ) {
154 $navigation = $pager->getNavigationBar();
155 }
156 $out->addHTML( $navigation . $pager->getBody() . $navigation );
157 } else {
158 $out->addWikiMsg( 'specialpage-empty' );
159 }
160 }
161
162 protected function filterLinks() {
163 // show/hide links
164 $showhide = array( $this->msg( 'show' )->escaped(), $this->msg( 'hide' )->escaped() );
165
166 // Option value -> message mapping
167 $filters = array(
168 'hideliu' => 'rcshowhideliu',
169 'hidepatrolled' => 'rcshowhidepatr',
170 'hidebots' => 'rcshowhidebots',
171 'hideredirs' => 'whatlinkshere-hideredirs'
172 );
173 foreach ( $this->customFilters as $key => $params ) {
174 $filters[$key] = $params['msg'];
175 }
176
177 // Disable some if needed
178 if ( !User::groupHasPermission( '*', 'createpage' ) ) {
179 unset( $filters['hideliu'] );
180 }
181 if ( !$this->getUser()->useNPPatrol() ) {
182 unset( $filters['hidepatrolled'] );
183 }
184
185 $links = array();
186 $changed = $this->opts->getChangedValues();
187 unset( $changed['offset'] ); // Reset offset if query type changes
188
189 $self = $this->getPageTitle();
190 foreach ( $filters as $key => $msg ) {
191 $onoff = 1 - $this->opts->getValue( $key );
192 $link = Linker::link( $self, $showhide[$onoff], array(),
193 array( $key => $onoff ) + $changed
194 );
195 $links[$key] = $this->msg( $msg )->rawParams( $link )->escaped();
196 }
197
198 return $this->getLanguage()->pipeList( $links );
199 }
200
201 protected function form() {
202 $out = $this->getOutput();
203 // Consume values
204 $this->opts->consumeValue( 'offset' ); // don't carry offset, DWIW
205 $namespace = $this->opts->consumeValue( 'namespace' );
206 $username = $this->opts->consumeValue( 'username' );
207 $tagFilterVal = $this->opts->consumeValue( 'tagfilter' );
208 $nsinvert = $this->opts->consumeValue( 'invert' );
209
210 // Check username input validity
211 $ut = Title::makeTitleSafe( NS_USER, $username );
212 $userText = $ut ? $ut->getText() : '';
213
214 // Store query values in hidden fields so that form submission doesn't lose them
215 $hidden = array();
216 foreach ( $this->opts->getUnconsumedValues() as $key => $value ) {
217 $hidden[] = Html::hidden( $key, $value );
218 }
219 $hidden = implode( "\n", $hidden );
220
221 $form = array(
222 'namespace' => array(
223 'type' => 'namespaceselect',
224 'name' => 'namespace',
225 'label-message' => 'namespace',
226 'default' => $namespace,
227 ),
228 'nsinvert' => array(
229 'type' => 'check',
230 'name' => 'nsinvert',
231 'label-message' => 'invert',
232 'default' => $nsinvert,
233 'tooltip' => $this->msg( 'tooltip-invert' )->text(),
234 ),
235 'tagFilter' => array(
236 'type' => 'tagfilter',
237 'name' => 'tagfilter',
238 'label-raw' => wfMessage( 'tag-filter' )->parse(),
239 'default' => $tagFilterVal,
240 ),
241 'username' => array(
242 'type' => 'text',
243 'name' => 'username',
244 'label-message' => 'newpages-username',
245 'default' => $userText,
246 'id' => 'mw-np-username',
247 'size' => 30,
248 'cssclass' => 'mw-autocomplete-user', // used by mediawiki.userSuggest
249 ),
250 );
251
252 $htmlForm = new HTMLForm( $form, $this->getContext() );
253
254 $htmlForm->setSubmitText( $this->msg( 'allpagessubmit' )->text() );
255 $htmlForm->setSubmitProgressive();
256 // The form should be visible on each request (inclusive requests with submitted forms), so
257 // return always false here.
258 $htmlForm->setSubmitCallback(
259 function () {
260 return false;
261 }
262 );
263 $htmlForm->setMethod( 'get' );
264
265 $out->addHtml( Xml::fieldset( $this->msg( 'newpages' )->text() ) );
266
267 $htmlForm->show();
268
269 $out->addHtml(
270 Html::rawElement(
271 'div',
272 null,
273 $this->filterLinks()
274 ) .
275 Xml::closeElement( 'fieldset' )
276 );
277 }
278
279 /**
280 * Format a row, providing the timestamp, links to the page/history,
281 * size, user links, and a comment
282 *
283 * @param object $result Result row
284 * @return string
285 */
286 public function formatRow( $result ) {
287 $title = Title::newFromRow( $result );
288
289 # Revision deletion works on revisions, so we should cast one
290 $row = array(
291 'comment' => $result->rc_comment,
292 'deleted' => $result->rc_deleted,
293 'user_text' => $result->rc_user_text,
294 'user' => $result->rc_user,
295 );
296 $rev = new Revision( $row );
297 $rev->setTitle( $title );
298
299 $classes = array();
300
301 $lang = $this->getLanguage();
302 $dm = $lang->getDirMark();
303
304 $spanTime = Html::element( 'span', array( 'class' => 'mw-newpages-time' ),
305 $lang->userTimeAndDate( $result->rc_timestamp, $this->getUser() )
306 );
307 $time = Linker::linkKnown(
308 $title,
309 $spanTime,
310 array(),
311 array( 'oldid' => $result->rc_this_oldid ),
312 array()
313 );
314
315 $query = array( 'redirect' => 'no' );
316
317 // Linker::linkKnown() uses 'known' and 'noclasses' options.
318 // This breaks the colouration for stubs.
319 $plink = Linker::link(
320 $title,
321 null,
322 array( 'class' => 'mw-newpages-pagename' ),
323 $query,
324 array( 'known' )
325 );
326 $histLink = Linker::linkKnown(
327 $title,
328 $this->msg( 'hist' )->escaped(),
329 array(),
330 array( 'action' => 'history' )
331 );
332 $hist = Html::rawElement( 'span', array( 'class' => 'mw-newpages-history' ),
333 $this->msg( 'parentheses' )->rawParams( $histLink )->escaped() );
334
335 $length = Html::element(
336 'span',
337 array( 'class' => 'mw-newpages-length' ),
338 $this->msg( 'brackets' )->params( $this->msg( 'nbytes' )
339 ->numParams( $result->length )->text()
340 )
341 );
342
343 $ulink = Linker::revUserTools( $rev );
344 $comment = Linker::revComment( $rev );
345
346 if ( $this->patrollable( $result ) ) {
347 $classes[] = 'not-patrolled';
348 }
349
350 # Add a class for zero byte pages
351 if ( $result->length == 0 ) {
352 $classes[] = 'mw-newpages-zero-byte-page';
353 }
354
355 # Tags, if any.
356 if ( isset( $result->ts_tags ) ) {
357 list( $tagDisplay, $newClasses ) = ChangeTags::formatSummaryRow(
358 $result->ts_tags,
359 'newpages'
360 );
361 $classes = array_merge( $classes, $newClasses );
362 } else {
363 $tagDisplay = '';
364 }
365
366 $css = count( $classes ) ? ' class="' . implode( ' ', $classes ) . '"' : '';
367
368 # Display the old title if the namespace/title has been changed
369 $oldTitleText = '';
370 $oldTitle = Title::makeTitle( $result->rc_namespace, $result->rc_title );
371
372 if ( !$title->equals( $oldTitle ) ) {
373 $oldTitleText = $oldTitle->getPrefixedText();
374 $oldTitleText = $this->msg( 'rc-old-title' )->params( $oldTitleText )->escaped();
375 }
376
377 return "<li{$css}>{$time} {$dm}{$plink} {$hist} {$dm}{$length} "
378 . "{$dm}{$ulink} {$comment} {$tagDisplay} {$oldTitleText}</li>\n";
379 }
380
381 /**
382 * Should a specific result row provide "patrollable" links?
383 *
384 * @param object $result Result row
385 * @return bool
386 */
387 protected function patrollable( $result ) {
388 return ( $this->getUser()->useNPPatrol() && !$result->rc_patrolled );
389 }
390
391 /**
392 * Output a subscription feed listing recent edits to this page.
393 *
394 * @param string $type
395 */
396 protected function feed( $type ) {
397 if ( !$this->getConfig()->get( 'Feed' ) ) {
398 $this->getOutput()->addWikiMsg( 'feed-unavailable' );
399
400 return;
401 }
402
403 $feedClasses = $this->getConfig()->get( 'FeedClasses' );
404 if ( !isset( $feedClasses[$type] ) ) {
405 $this->getOutput()->addWikiMsg( 'feed-invalid' );
406
407 return;
408 }
409
410 $feed = new $feedClasses[$type](
411 $this->feedTitle(),
412 $this->msg( 'tagline' )->text(),
413 $this->getPageTitle()->getFullURL()
414 );
415
416 $pager = new NewPagesPager( $this, $this->opts );
417 $limit = $this->opts->getValue( 'limit' );
418 $pager->mLimit = min( $limit, $this->getConfig()->get( 'FeedLimit' ) );
419
420 $feed->outHeader();
421 if ( $pager->getNumRows() > 0 ) {
422 foreach ( $pager->mResult as $row ) {
423 $feed->outItem( $this->feedItem( $row ) );
424 }
425 }
426 $feed->outFooter();
427 }
428
429 protected function feedTitle() {
430 $desc = $this->getDescription();
431 $code = $this->getConfig()->get( 'LanguageCode' );
432 $sitename = $this->getConfig()->get( 'Sitename' );
433
434 return "$sitename - $desc [$code]";
435 }
436
437 protected function feedItem( $row ) {
438 $title = Title::makeTitle( intval( $row->rc_namespace ), $row->rc_title );
439 if ( $title ) {
440 $date = $row->rc_timestamp;
441 $comments = $title->getTalkPage()->getFullURL();
442
443 return new FeedItem(
444 $title->getPrefixedText(),
445 $this->feedItemDesc( $row ),
446 $title->getFullURL(),
447 $date,
448 $this->feedItemAuthor( $row ),
449 $comments
450 );
451 } else {
452 return null;
453 }
454 }
455
456 protected function feedItemAuthor( $row ) {
457 return isset( $row->rc_user_text ) ? $row->rc_user_text : '';
458 }
459
460 protected function feedItemDesc( $row ) {
461 $revision = Revision::newFromId( $row->rev_id );
462 if ( $revision ) {
463 //XXX: include content model/type in feed item?
464 return '<p>' . htmlspecialchars( $revision->getUserText() ) .
465 $this->msg( 'colon-separator' )->inContentLanguage()->escaped() .
466 htmlspecialchars( FeedItem::stripComment( $revision->getComment() ) ) .
467 "</p>\n<hr />\n<div>" .
468 nl2br( htmlspecialchars( $revision->getContent()->serialize() ) ) . "</div>";
469 }
470
471 return '';
472 }
473
474 protected function getGroupName() {
475 return 'changes';
476 }
477 }
478
479 /**
480 * @ingroup SpecialPage Pager
481 */
482 class NewPagesPager extends ReverseChronologicalPager {
483 // Stored opts
484 protected $opts;
485
486 /**
487 * @var HtmlForm
488 */
489 protected $mForm;
490
491 function __construct( $form, FormOptions $opts ) {
492 parent::__construct( $form->getContext() );
493 $this->mForm = $form;
494 $this->opts = $opts;
495 }
496
497 function getQueryInfo() {
498 $conds = array();
499 $conds['rc_new'] = 1;
500
501 $namespace = $this->opts->getValue( 'namespace' );
502 $namespace = ( $namespace === 'all' ) ? false : intval( $namespace );
503
504 $username = $this->opts->getValue( 'username' );
505 $user = Title::makeTitleSafe( NS_USER, $username );
506
507 $rcIndexes = array();
508
509 if ( $namespace !== false ) {
510 if ( $this->opts->getValue( 'invert' ) ) {
511 $conds[] = 'rc_namespace != ' . $this->mDb->addQuotes( $namespace );
512 } else {
513 $conds['rc_namespace'] = $namespace;
514 }
515 }
516
517 if ( $user ) {
518 $conds['rc_user_text'] = $user->getText();
519 $rcIndexes = 'rc_user_text';
520 } elseif ( User::groupHasPermission( '*', 'createpage' ) &&
521 $this->opts->getValue( 'hideliu' )
522 ) {
523 # If anons cannot make new pages, don't "exclude logged in users"!
524 $conds['rc_user'] = 0;
525 }
526
527 # If this user cannot see patrolled edits or they are off, don't do dumb queries!
528 if ( $this->opts->getValue( 'hidepatrolled' ) && $this->getUser()->useNPPatrol() ) {
529 $conds['rc_patrolled'] = 0;
530 }
531
532 if ( $this->opts->getValue( 'hidebots' ) ) {
533 $conds['rc_bot'] = 0;
534 }
535
536 if ( $this->opts->getValue( 'hideredirs' ) ) {
537 $conds['page_is_redirect'] = 0;
538 }
539
540 // Allow changes to the New Pages query
541 $tables = array( 'recentchanges', 'page' );
542 $fields = array(
543 'rc_namespace', 'rc_title', 'rc_cur_id', 'rc_user', 'rc_user_text',
544 'rc_comment', 'rc_timestamp', 'rc_patrolled', 'rc_id', 'rc_deleted',
545 'length' => 'page_len', 'rev_id' => 'page_latest', 'rc_this_oldid',
546 'page_namespace', 'page_title'
547 );
548 $join_conds = array( 'page' => array( 'INNER JOIN', 'page_id=rc_cur_id' ) );
549
550 wfRunHooks( 'SpecialNewpagesConditions',
551 array( &$this, $this->opts, &$conds, &$tables, &$fields, &$join_conds ) );
552
553 $options = array();
554
555 if ( $rcIndexes ) {
556 $options = array( 'USE INDEX' => array( 'recentchanges' => $rcIndexes ) );
557 }
558
559 $info = array(
560 'tables' => $tables,
561 'fields' => $fields,
562 'conds' => $conds,
563 'options' => $options,
564 'join_conds' => $join_conds
565 );
566
567 // Modify query for tags
568 ChangeTags::modifyDisplayQuery(
569 $info['tables'],
570 $info['fields'],
571 $info['conds'],
572 $info['join_conds'],
573 $info['options'],
574 $this->opts['tagfilter']
575 );
576
577 return $info;
578 }
579
580 function getIndexField() {
581 return 'rc_timestamp';
582 }
583
584 function formatRow( $row ) {
585 return $this->mForm->formatRow( $row );
586 }
587
588 function getStartBody() {
589 # Do a batch existence check on pages
590 $linkBatch = new LinkBatch();
591 foreach ( $this->mResult as $row ) {
592 $linkBatch->add( NS_USER, $row->rc_user_text );
593 $linkBatch->add( NS_USER_TALK, $row->rc_user_text );
594 $linkBatch->add( $row->rc_namespace, $row->rc_title );
595 }
596 $linkBatch->execute();
597
598 return '<ul>';
599 }
600
601 function getEndBody() {
602 return '</ul>';
603 }
604 }