Make includable special pages extend IncludableSpecialPage so that it's easier to...
[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 $wgLang, $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 = htmlspecialchars( $wgLang->timeAndDate( $result->rc_timestamp, true ) );
271
272 $query = array( 'redirect' => 'no' );
273
274 if( $this->patrollable( $result ) )
275 $query['rcid'] = $result->rc_id;
276
277 $plink = $this->skin->linkKnown(
278 $title,
279 null,
280 array(),
281 $query
282 );
283 $hist = $this->skin->linkKnown(
284 $title,
285 wfMsgHtml( 'hist' ),
286 array(),
287 array( 'action' => 'history' )
288 );
289 $length = wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ),
290 $wgLang->formatNum( $result->length ) );
291 $ulink = $this->skin->userLink( $result->rc_user, $result->rc_user_text ) . ' ' .
292 $this->skin->userToolLinks( $result->rc_user, $result->rc_user_text );
293 $comment = $this->skin->commentBlock( $result->rc_comment );
294
295 if ( $this->patrollable( $result ) )
296 $classes[] = 'not-patrolled';
297
298 # Tags, if any.
299 list( $tagDisplay, $newClasses ) = ChangeTags::formatSummaryRow( $result->ts_tags, 'newpages' );
300 $classes = array_merge( $classes, $newClasses );
301
302 $css = count($classes) ? ' class="'.implode( " ", $classes).'"' : '';
303
304 return "<li{$css}>{$time} {$dm}{$plink} ({$hist}) {$dm}[{$length}] {$dm}{$ulink} {$comment} {$tagDisplay}</li>\n";
305 }
306
307 /**
308 * Should a specific result row provide "patrollable" links?
309 *
310 * @param $result Result row
311 * @return Boolean
312 */
313 protected function patrollable( $result ) {
314 global $wgUser;
315 return ( $wgUser->useNPPatrol() && !$result->rc_patrolled );
316 }
317
318 /**
319 * Output a subscription feed listing recent edits to this page.
320 *
321 * @param $type String
322 */
323 protected function feed( $type ) {
324 global $wgFeed, $wgFeedClasses, $wgFeedLimit;
325
326 if ( !$wgFeed ) {
327 global $wgOut;
328 $wgOut->addWikiMsg( 'feed-unavailable' );
329 return;
330 }
331
332 if( !isset( $wgFeedClasses[$type] ) ) {
333 global $wgOut;
334 $wgOut->addWikiMsg( 'feed-invalid' );
335 return;
336 }
337
338 $feed = new $wgFeedClasses[$type](
339 $this->feedTitle(),
340 wfMsgExt( 'tagline', 'parsemag' ),
341 $this->getTitle()->getFullUrl() );
342
343 $pager = new NewPagesPager( $this, $this->opts );
344 $limit = $this->opts->getValue( 'limit' );
345 $pager->mLimit = min( $limit, $wgFeedLimit );
346
347 $feed->outHeader();
348 if( $pager->getNumRows() > 0 ) {
349 while( $row = $pager->mResult->fetchObject() ) {
350 $feed->outItem( $this->feedItem( $row ) );
351 }
352 }
353 $feed->outFooter();
354 }
355
356 protected function feedTitle() {
357 global $wgContLanguageCode, $wgSitename;
358 $page = SpecialPage::getPage( 'Newpages' );
359 $desc = $page->getDescription();
360 return "$wgSitename - $desc [$wgContLanguageCode]";
361 }
362
363 protected function feedItem( $row ) {
364 $title = Title::MakeTitle( intval( $row->rc_namespace ), $row->rc_title );
365 if( $title ) {
366 $date = $row->rc_timestamp;
367 $comments = $title->getTalkPage()->getFullURL();
368
369 return new FeedItem(
370 $title->getPrefixedText(),
371 $this->feedItemDesc( $row ),
372 $title->getFullURL(),
373 $date,
374 $this->feedItemAuthor( $row ),
375 $comments);
376 } else {
377 return null;
378 }
379 }
380
381 protected function feedItemAuthor( $row ) {
382 return isset( $row->rc_user_text ) ? $row->rc_user_text : '';
383 }
384
385 protected function feedItemDesc( $row ) {
386 $revision = Revision::newFromId( $row->rev_id );
387 if( $revision ) {
388 return '<p>' . htmlspecialchars( $revision->getUserText() ) . wfMsgForContent( 'colon-separator' ) .
389 htmlspecialchars( FeedItem::stripComment( $revision->getComment() ) ) .
390 "</p>\n<hr />\n<div>" .
391 nl2br( htmlspecialchars( $revision->getText() ) ) . "</div>";
392 }
393 return '';
394 }
395 }
396
397 /**
398 * @ingroup SpecialPage Pager
399 */
400 class NewPagesPager extends ReverseChronologicalPager {
401 // Stored opts
402 protected $opts, $mForm;
403
404 function __construct( $form, FormOptions $opts ) {
405 parent::__construct();
406 $this->mForm = $form;
407 $this->opts = $opts;
408 }
409
410 function getTitle() {
411 static $title = null;
412 if ( $title === null )
413 $title = $this->mForm->getTitle();
414 return $title;
415 }
416
417 function getQueryInfo() {
418 global $wgEnableNewpagesUserFilter, $wgGroupPermissions, $wgUser;
419 $conds = array();
420 $conds['rc_new'] = 1;
421
422 $namespace = $this->opts->getValue( 'namespace' );
423 $namespace = ( $namespace === 'all' ) ? false : intval( $namespace );
424
425 $username = $this->opts->getValue( 'username' );
426 $user = Title::makeTitleSafe( NS_USER, $username );
427
428 if( $namespace !== false ) {
429 $conds['rc_namespace'] = $namespace;
430 $rcIndexes = array( 'new_name_timestamp' );
431 } else {
432 $rcIndexes = array( 'rc_timestamp' );
433 }
434
435 # $wgEnableNewpagesUserFilter - temp WMF hack
436 if( $wgEnableNewpagesUserFilter && $user ) {
437 $conds['rc_user_text'] = $user->getText();
438 $rcIndexes = 'rc_user_text';
439 # If anons cannot make new pages, don't "exclude logged in users"!
440 } elseif( $wgGroupPermissions['*']['createpage'] && $this->opts->getValue( 'hideliu' ) ) {
441 $conds['rc_user'] = 0;
442 }
443 # If this user cannot see patrolled edits or they are off, don't do dumb queries!
444 if( $this->opts->getValue( 'hidepatrolled' ) && $wgUser->useNPPatrol() ) {
445 $conds['rc_patrolled'] = 0;
446 }
447 if( $this->opts->getValue( 'hidebots' ) ) {
448 $conds['rc_bot'] = 0;
449 }
450
451 if ( $this->opts->getValue( 'hideredirs' ) ) {
452 $conds['page_is_redirect'] = 0;
453 }
454
455 // Allow changes to the New Pages query
456 wfRunHooks('SpecialNewpagesConditions', array(&$this, $this->opts, &$conds));
457
458 $info = array(
459 'tables' => array( 'recentchanges', 'page' ),
460 'fields' => 'rc_namespace,rc_title, rc_cur_id, rc_user,rc_user_text,rc_comment,
461 rc_timestamp,rc_patrolled,rc_id,page_len as length, page_latest as rev_id, ts_tags',
462 'conds' => $conds,
463 'options' => array( 'USE INDEX' => array('recentchanges' => $rcIndexes) ),
464 'join_conds' => array(
465 'page' => array('INNER JOIN', 'page_id=rc_cur_id'),
466 ),
467 );
468
469 ## Empty array for fields, it'll be set by us anyway.
470 $fields = array();
471
472 ## Modify query for tags
473 ChangeTags::modifyDisplayQuery( $info['tables'],
474 $fields,
475 $info['conds'],
476 $info['join_conds'],
477 $info['options'],
478 $this->opts['tagfilter'] );
479
480 return $info;
481 }
482
483 function getIndexField() {
484 return 'rc_timestamp';
485 }
486
487 function formatRow( $row ) {
488 return $this->mForm->formatRow( $row );
489 }
490
491 function getStartBody() {
492 # Do a batch existence check on pages
493 $linkBatch = new LinkBatch();
494 while( $row = $this->mResult->fetchObject() ) {
495 $linkBatch->add( NS_USER, $row->rc_user_text );
496 $linkBatch->add( NS_USER_TALK, $row->rc_user_text );
497 $linkBatch->add( $row->rc_namespace, $row->rc_title );
498 }
499 $linkBatch->execute();
500 return "<ul>";
501 }
502
503 function getEndBody() {
504 return "</ul>";
505 }
506 }