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