Handle multiple warnings correctly in ApiBase::setWarning(). Calling this function...
[lhc/web/wiklou.git] / includes / SpecialNewpages.php
1 <?php
2 /**
3 * @file
4 * @ingroup SpecialPage
5 */
6
7
8 /**
9 * Start point
10 */
11 function wfSpecialNewPages( $par, $sp ) {
12 $page = new NewPagesForm();
13 $page->execute( $par, $sp->including() );
14 }
15
16 /**
17 * implements Special:Newpages
18 * @ingroup SpecialPage
19 */
20 class NewPagesForm {
21
22 // Stored objects
23 protected $opts, $title, $skin;
24
25 // Some internal settings
26 protected $showNavigation = false;
27
28 protected function setup( $par ) {
29 global $wgRequest, $wgUser, $wgEnableNewpagesUserFilter;
30
31 // Options
32 $opts = new FormOptions();
33 $this->opts = $opts; // bind
34 $opts->add( 'hideliu', false );
35 $opts->add( 'hidepatrolled', false );
36 $opts->add( 'hidebots', false );
37 $opts->add( 'limit', 50 );
38 $opts->add( 'offset', '' );
39 $opts->add( 'namespace', '0' );
40 $opts->add( 'username', '' );
41 $opts->add( 'feed', '' );
42
43 // Set values
44 $opts->fetchValuesFromRequest( $wgRequest );
45 if ( $par ) $this->parseParams( $par );
46
47 // Validate
48 $opts->validateIntBounds( 'limit', 0, 5000 );
49 if( !$wgEnableNewpagesUserFilter ) {
50 $opts->setValue( 'username', '' );
51 }
52
53 // Store some objects
54 $this->skin = $wgUser->getSkin();
55 $this->title = SpecialPage::getTitleFor( 'NewPages' );
56 }
57
58 protected function parseParams( $par ) {
59 global $wgLang;
60 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
61 foreach ( $bits as $bit ) {
62 if ( 'shownav' == $bit )
63 $this->showNavigation = true;
64 if ( 'hideliu' === $bit )
65 $this->opts->setValue( 'hideliu', true );
66 if ( 'hidepatrolled' == $bit )
67 $this->opts->setValue( 'hidepatrolled', true );
68 if ( 'hidebots' == $bit )
69 $this->opts->setValue( 'hidebots', true );
70 if ( is_numeric( $bit ) )
71 $this->opts->setValue( 'limit', intval( $bit ) );
72
73 $m = array();
74 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) )
75 $this->opts->setValue( 'limit', intval($m[1]) );
76 // PG offsets not just digits!
77 if ( preg_match( '/^offset=([^=]+)$/', $bit, $m ) )
78 $this->opts->setValue( 'offset', intval($m[1]) );
79 if ( preg_match( '/^namespace=(.*)$/', $bit, $m ) ) {
80 $ns = $wgLang->getNsIndex( $m[1] );
81 if( $ns !== false ) {
82 $this->opts->setValue( 'namespace', $ns );
83 }
84 }
85 }
86 }
87
88 /**
89 * Show a form for filtering namespace and username
90 *
91 * @param string $par
92 * @param bool $including true if the page is being included with {{Special:Newpages}}
93 * @return string
94 */
95 public function execute( $par, $including ) {
96 global $wgLang, $wgGroupPermissions, $wgUser, $wgOut;
97
98 $this->showNavigation = !$including; // Maybe changed in setup
99 $this->setup( $par );
100
101 if( !$including ) {
102 // Settings
103 $this->form();
104
105 $this->setSyndicated();
106 $feedType = $this->opts->getValue( 'feed' );
107 if( $feedType ) {
108 return $this->feed( $feedType, $options );
109 }
110 }
111
112 $pager = new NewPagesPager( $this, $this->opts );
113 $pager->mLimit = $this->opts->getValue( 'limit' );
114 $pager->mOffset = $this->opts->getValue( 'offset' );
115
116 if( $pager->getNumRows() ) {
117 $navigation = '';
118 if ( $this->showNavigation ) $navigation = $pager->getNavigationBar();
119 $wgOut->addHTML( $navigation . $pager->getBody() . $navigation );
120 } else {
121 $wgOut->addWikiMsg( 'specialpage-empty' );
122 }
123 }
124
125 protected function filterLinks() {
126 global $wgGroupPermissions, $wgUser;
127
128 // show/hide links
129 $showhide = array( wfMsgHtml( 'show' ), wfMsgHtml( 'hide' ) );
130
131 // Option value -> message mapping
132 $filters = array(
133 'hideliu' => 'rcshowhideliu',
134 'hidepatrolled' => 'rcshowhidepatr',
135 'hidebots' => 'rcshowhidebots'
136 );
137
138 // Disable some if needed
139 if ( $wgGroupPermissions['*']['createpage'] !== true )
140 unset($filters['hideliu']);
141
142 if ( !$wgUser->useNPPatrol() )
143 unset($filters['hidepatrolled']);
144
145 $links = array();
146 $changed = $this->opts->getChangedValues();
147 unset($changed['offset']); // Reset offset if query type changes
148
149 foreach ( $filters as $key => $msg ) {
150 $onoff = 1 - $this->opts->getValue($key);
151 $link = $this->skin->makeKnownLinkObj( $this->title, $showhide[$onoff],
152 wfArrayToCGI( array( $key => $onoff ), $changed )
153 );
154 $links[$key] = wfMsgHtml( $msg, $link );
155 }
156
157 return implode( ' | ', $links );
158 }
159
160 protected function form() {
161 global $wgOut, $wgEnableNewpagesUserFilter, $wgScript;
162
163 // Consume values
164 $this->opts->consumeValue( 'offset' ); // don't carry offset, DWIW
165 $namespace = $this->opts->consumeValue( 'namespace' );
166 $username = $this->opts->consumeValue( 'username' );
167
168 // Check username input validity
169 $ut = Title::makeTitleSafe( NS_USER, $username );
170 $userText = $ut ? $ut->getText() : '';
171
172 // Store query values in hidden fields so that form submission doesn't lose them
173 $hidden = array();
174 foreach ( $this->opts->getUnconsumedValues() as $key => $value ) {
175 $hidden[] = Xml::hidden( $key, $value );
176 }
177 $hidden = implode( "\n", $hidden );
178
179 $form = Xml::openElement( 'form', array( 'action' => $wgScript ) ) .
180 Xml::hidden( 'title', $this->title->getPrefixedDBkey() ) .
181 Xml::fieldset( wfMsg( 'newpages' ) ) .
182 Xml::openElement( 'table', array( 'id' => 'mw-newpages-table' ) ) .
183 "<tr>
184 <td class='mw-label'>" .
185 Xml::label( wfMsg( 'namespace' ), 'namespace' ) .
186 "</td>
187 <td class='mw-input'>" .
188 Xml::namespaceSelector( $namespace, 'all' ) .
189 "</td>
190 </tr>" .
191 ($wgEnableNewpagesUserFilter ?
192 "<tr>
193 <td class='mw-label'>" .
194 Xml::label( wfMsg( 'newpages-username' ), 'mw-np-username' ) .
195 "</td>
196 <td class='mw-input'>" .
197 Xml::input( 'username', 30, $userText, array( 'id' => 'mw-np-username' ) ) .
198 "</td>
199 </tr>" : "" ) .
200 "<tr> <td></td>
201 <td class='mw-submit'>" .
202 Xml::submitButton( wfMsg( 'allpagessubmit' ) ) .
203 "</td>
204 </tr>" .
205 "<tr>
206 <td></td>
207 <td class='mw-input'>" .
208 $this->filterLinks() .
209 "</td>
210 </tr>" .
211 Xml::closeElement( 'table' ) .
212 Xml::closeElement( 'fieldset' ) .
213 $hidden .
214 Xml::closeElement( 'form' );
215
216 $wgOut->addHTML( $form );
217 }
218
219 protected function setSyndicated() {
220 global $wgOut;
221 $queryParams = array(
222 'namespace' => $this->opts->getValue( 'namespace' ),
223 'username' => $this->opts->getValue( 'username' )
224 );
225 $wgOut->setSyndicated( true );
226 $wgOut->setFeedAppendQuery( wfArrayToCGI( $queryParams ) );
227 }
228
229 /**
230 * Format a row, providing the timestamp, links to the page/history, size, user links, and a comment
231 *
232 * @param $skin Skin to use
233 * @param $result Result row
234 * @return string
235 */
236 public function formatRow( $result ) {
237 global $wgLang, $wgContLang, $wgUser;
238 $dm = $wgContLang->getDirMark();
239
240 $title = Title::makeTitleSafe( $result->rc_namespace, $result->rc_title );
241 $time = $wgLang->timeAndDate( $result->rc_timestamp, true );
242 $plink = $this->skin->makeKnownLinkObj( $title, '', $this->patrollable( $result ) ? 'rcid=' . $result->rc_id : '' );
243 $hist = $this->skin->makeKnownLinkObj( $title, wfMsgHtml( 'hist' ), 'action=history' );
244 $length = wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ),
245 $wgLang->formatNum( $result->length ) );
246 $ulink = $this->skin->userLink( $result->rc_user, $result->rc_user_text ) . ' ' .
247 $this->skin->userToolLinks( $result->rc_user, $result->rc_user_text );
248 $comment = $this->skin->commentBlock( $result->rc_comment );
249 $css = $this->patrollable( $result ) ? " class='not-patrolled'" : '';
250
251 return "<li{$css}>{$time} {$dm}{$plink} ({$hist}) {$dm}[{$length}] {$dm}{$ulink} {$comment}</li>\n";
252 }
253
254 /**
255 * Should a specific result row provide "patrollable" links?
256 *
257 * @param $result Result row
258 * @return bool
259 */
260 protected function patrollable( $result ) {
261 global $wgUser;
262 return ( $wgUser->useNPPatrol() && !$result->rc_patrolled );
263 }
264
265 /**
266 * Output a subscription feed listing recent edits to this page.
267 * @param string $type
268 */
269 protected function feed( $type, $options ) {
270 require_once 'SpecialRecentchanges.php';
271
272 global $wgFeed, $wgFeedClasses;
273
274 if ( !$wgFeed ) {
275 global $wgOut;
276 $wgOut->addWikiMsg( 'feed-unavailable' );
277 return;
278 }
279
280 if( !isset( $wgFeedClasses[$type] ) ) {
281 global $wgOut;
282 $wgOut->addWikiMsg( 'feed-invalid' );
283 return;
284 }
285
286 $feed = new $wgFeedClasses[$type](
287 $this->feedTitle(),
288 wfMsg( 'tagline' ),
289 $this->title->getFullUrl() );
290
291 $pager = new NewPagesPager( $this, $this->opts );
292 $limit = $this->opts->getValue( 'limit' );
293 global $wgFeedLimit;
294 if( $limit > $wgFeedLimit ) {
295 $limit = $wgFeedLimit;
296 }
297 $pager->mLimit = $limit;
298
299 $feed->outHeader();
300 if( $pager->getNumRows() > 0 ) {
301 while( $row = $pager->mResult->fetchObject() ) {
302 $feed->outItem( $this->feedItem( $row ) );
303 }
304 }
305 $feed->outFooter();
306 }
307
308 protected function feedTitle() {
309 global $wgContLanguageCode, $wgSitename;
310 $page = SpecialPage::getPage( 'Newpages' );
311 $desc = $page->getDescription();
312 return "$wgSitename - $desc [$wgContLanguageCode]";
313 }
314
315 protected function feedItem( $row ) {
316 $title = Title::MakeTitle( intval( $row->rc_namespace ), $row->rc_title );
317 if( $title ) {
318 $date = $row->rc_timestamp;
319 $comments = $this->stripComment( $row->rc_comment );
320
321 return new FeedItem(
322 $title->getPrefixedText(),
323 $this->feedItemDesc( $row ),
324 $title->getFullURL(),
325 $date,
326 $this->feedItemAuthor( $row ),
327 $comments);
328 } else {
329 return NULL;
330 }
331 }
332
333 /**
334 * Quickie hack... strip out wikilinks to more legible form from the comment.
335 */
336 protected function stripComment( $text ) {
337 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
338 }
339
340 protected function feedItemAuthor( $row ) {
341 return isset( $row->rc_user_text ) ? $row->rc_user_text : '';
342 }
343
344 protected function feedItemDesc( $row ) {
345 $revision = Revision::newFromId( $row->rev_id );
346 if( $revision ) {
347 return '<p>' . htmlspecialchars( $revision->getUserText() ) . ': ' .
348 htmlspecialchars( $revision->getComment() ) .
349 "</p>\n<hr />\n<div>" .
350 nl2br( htmlspecialchars( $revision->getText() ) ) . "</div>";
351 }
352 return '';
353 }
354 }
355
356 /**
357 * @ingroup SpecialPage Pager
358 */
359 class NewPagesPager extends ReverseChronologicalPager {
360 // Stored opts
361 protected $opts, $mForm;
362
363 private $hideliu, $hidepatrolled, $hidebots, $namespace, $user, $spTitle;
364
365 function __construct( $form, FormOptions $opts ) {
366 parent::__construct();
367 $this->mForm = $form;
368 $this->opts = $opts;
369 }
370
371 function getTitle(){
372 static $title = null;
373 if ( $title === null )
374 $title = SpecialPage::getTitleFor( 'Newpages' );
375 return $title;
376 }
377
378 function getQueryInfo() {
379 global $wgEnableNewpagesUserFilter, $wgGroupPermissions, $wgUser;
380 $conds = array();
381 $conds['rc_new'] = 1;
382
383 $namespace = $this->opts->getValue( 'namespace' );
384 $namespace = ( $namespace === 'all' ) ? false : intval( $namespace );
385
386 $username = $this->opts->getValue( 'username' );
387 $user = Title::makeTitleSafe( NS_USER, $username );
388
389 if( $namespace !== false ) {
390 $conds['rc_namespace'] = $namespace;
391 $rcIndexes = array( 'new_name_timestamp' );
392 } else {
393 $rcIndexes = array( 'rc_timestamp' );
394 }
395 $conds[] = 'page_id = rc_cur_id';
396 $conds['page_is_redirect'] = 0;
397 # $wgEnableNewpagesUserFilter - temp WMF hack
398 if( $wgEnableNewpagesUserFilter && $user ) {
399 $conds['rc_user_text'] = $user->getText();
400 $rcIndexes = 'rc_user_text';
401 # If anons cannot make new pages, don't "exclude logged in users"!
402 } elseif( $wgGroupPermissions['*']['createpage'] && $this->opts->getValue( 'hideliu' ) ) {
403 $conds['rc_user'] = 0;
404 }
405 # If this user cannot see patrolled edits or they are off, don't do dumb queries!
406 if( $this->opts->getValue( 'hidepatrolled' ) && $wgUser->useNPPatrol() ) {
407 $conds['rc_patrolled'] = 0;
408 }
409 if( $this->opts->getValue( 'hidebots' ) ) {
410 $conds['rc_bot'] = 0;
411 }
412
413 return array(
414 'tables' => array( 'recentchanges', 'page' ),
415 'fields' => 'rc_namespace,rc_title, rc_cur_id, rc_user,rc_user_text,rc_comment,
416 rc_timestamp,rc_patrolled,rc_id,page_len as length, page_latest as rev_id',
417 'conds' => $conds,
418 'options' => array( 'USE INDEX' => array('recentchanges' => $rcIndexes) )
419 );
420 }
421
422 function getIndexField() {
423 return 'rc_timestamp';
424 }
425
426 function formatRow( $row ) {
427 return $this->mForm->formatRow( $row );
428 }
429
430 function getStartBody() {
431 # Do a batch existence check on pages
432 $linkBatch = new LinkBatch();
433 while( $row = $this->mResult->fetchObject() ) {
434 $linkBatch->add( NS_USER, $row->rc_user_text );
435 $linkBatch->add( NS_USER_TALK, $row->rc_user_text );
436 $linkBatch->add( $row->rc_namespace, $row->rc_title );
437 }
438 $linkBatch->execute();
439 return "<ul>";
440 }
441
442 function getEndBody() {
443 return "</ul>";
444 }
445 }