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