Retire 'newpages-showhideredirs' in favour of 'whatlinkshere-hideredirs'. Identical...
[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( 'hideredirs', true );
30 $opts->add( 'limit', (int)$wgUser->getOption( 'rclimit' ) );
31 $opts->add( 'offset', '' );
32 $opts->add( 'namespace', '0' );
33 $opts->add( 'username', '' );
34 $opts->add( 'feed', '' );
35
36 // Set values
37 $opts->fetchValuesFromRequest( $wgRequest );
38 if ( $par ) $this->parseParams( $par );
39
40 // Validate
41 $opts->validateIntBounds( 'limit', 0, 5000 );
42 if( !$wgEnableNewpagesUserFilter ) {
43 $opts->setValue( 'username', '' );
44 }
45
46 // Store some objects
47 $this->skin = $wgUser->getSkin();
48 }
49
50 protected function parseParams( $par ) {
51 global $wgLang;
52 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
53 foreach ( $bits as $bit ) {
54 if ( 'shownav' == $bit )
55 $this->showNavigation = true;
56 if ( 'hideliu' === $bit )
57 $this->opts->setValue( 'hideliu', true );
58 if ( 'hidepatrolled' == $bit )
59 $this->opts->setValue( 'hidepatrolled', true );
60 if ( 'hidebots' == $bit )
61 $this->opts->setValue( 'hidebots', true );
62 if ( 'showredirs' == $bit )
63 $this->opts->setValue( 'hideredirs', false );
64 if ( is_numeric( $bit ) )
65 $this->opts->setValue( 'limit', intval( $bit ) );
66
67 $m = array();
68 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) )
69 $this->opts->setValue( 'limit', intval($m[1]) );
70 // PG offsets not just digits!
71 if ( preg_match( '/^offset=([^=]+)$/', $bit, $m ) )
72 $this->opts->setValue( 'offset', intval($m[1]) );
73 if ( preg_match( '/^namespace=(.*)$/', $bit, $m ) ) {
74 $ns = $wgLang->getNsIndex( $m[1] );
75 if( $ns !== false ) {
76 $this->opts->setValue( 'namespace', $ns );
77 }
78 }
79 }
80 }
81
82 /**
83 * Show a form for filtering namespace and username
84 *
85 * @param string $par
86 * @return string
87 */
88 public function execute( $par ) {
89 global $wgLang, $wgUser, $wgOut;
90
91 $this->setHeaders();
92 $this->outputHeader();
93
94 $this->showNavigation = !$this->including(); // Maybe changed in setup
95 $this->setup( $par );
96
97 if( !$this->including() ) {
98 // Settings
99 $this->form();
100
101 $this->setSyndicated();
102 $feedType = $this->opts->getValue( 'feed' );
103 if( $feedType ) {
104 return $this->feed( $feedType );
105 }
106 }
107
108 $pager = new NewPagesPager( $this, $this->opts );
109 $pager->mLimit = $this->opts->getValue( 'limit' );
110 $pager->mOffset = $this->opts->getValue( 'offset' );
111
112 if( $pager->getNumRows() ) {
113 $navigation = '';
114 if ( $this->showNavigation ) $navigation = $pager->getNavigationBar();
115 $wgOut->addHTML( $navigation . $pager->getBody() . $navigation );
116 } else {
117 $wgOut->addWikiMsg( 'specialpage-empty' );
118 }
119 }
120
121 protected function filterLinks() {
122 global $wgGroupPermissions, $wgUser;
123
124 // show/hide links
125 $showhide = array( wfMsgHtml( 'show' ), wfMsgHtml( 'hide' ) );
126
127 // Option value -> message mapping
128 $filters = array(
129 'hideliu' => 'rcshowhideliu',
130 'hidepatrolled' => 'rcshowhidepatr',
131 'hidebots' => 'rcshowhidebots',
132 'hideredirs' => 'whatlinkshere-hideredirs'
133 );
134
135 // Disable some if needed
136 if ( $wgGroupPermissions['*']['createpage'] !== true )
137 unset($filters['hideliu']);
138
139 if ( !$wgUser->useNPPatrol() )
140 unset($filters['hidepatrolled']);
141
142 $links = array();
143 $changed = $this->opts->getChangedValues();
144 unset($changed['offset']); // Reset offset if query type changes
145
146 $self = $this->getTitle();
147 foreach ( $filters as $key => $msg ) {
148 $onoff = 1 - $this->opts->getValue($key);
149 $link = $this->skin->link( $self, $showhide[$onoff], array(),
150 array( $key => $onoff ) + $changed
151 );
152 $links[$key] = wfMsgHtml( $msg, $link );
153 }
154
155 return implode( ' | ', $links );
156 }
157
158 protected function form() {
159 global $wgOut, $wgEnableNewpagesUserFilter, $wgScript;
160
161 // Consume values
162 $this->opts->consumeValue( 'offset' ); // don't carry offset, DWIW
163 $namespace = $this->opts->consumeValue( 'namespace' );
164 $username = $this->opts->consumeValue( 'username' );
165
166 // Check username input validity
167 $ut = Title::makeTitleSafe( NS_USER, $username );
168 $userText = $ut ? $ut->getText() : '';
169
170 // Store query values in hidden fields so that form submission doesn't lose them
171 $hidden = array();
172 foreach ( $this->opts->getUnconsumedValues() as $key => $value ) {
173 $hidden[] = Xml::hidden( $key, $value );
174 }
175 $hidden = implode( "\n", $hidden );
176
177 $form = Xml::openElement( 'form', array( 'action' => $wgScript ) ) .
178 Xml::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) .
179 Xml::fieldset( wfMsg( 'newpages' ) ) .
180 Xml::openElement( 'table', array( 'id' => 'mw-newpages-table' ) ) .
181 "<tr>
182 <td class='mw-label'>" .
183 Xml::label( wfMsg( 'namespace' ), 'namespace' ) .
184 "</td>
185 <td class='mw-input'>" .
186 Xml::namespaceSelector( $namespace, 'all' ) .
187 "</td>
188 </tr>" .
189 ($wgEnableNewpagesUserFilter ?
190 "<tr>
191 <td class='mw-label'>" .
192 Xml::label( wfMsg( 'newpages-username' ), 'mw-np-username' ) .
193 "</td>
194 <td class='mw-input'>" .
195 Xml::input( 'username', 30, $userText, array( 'id' => 'mw-np-username' ) ) .
196 "</td>
197 </tr>" : "" ) .
198 "<tr> <td></td>
199 <td class='mw-submit'>" .
200 Xml::submitButton( wfMsg( 'allpagessubmit' ) ) .
201 "</td>
202 </tr>" .
203 "<tr>
204 <td></td>
205 <td class='mw-input'>" .
206 $this->filterLinks() .
207 "</td>
208 </tr>" .
209 Xml::closeElement( 'table' ) .
210 Xml::closeElement( 'fieldset' ) .
211 $hidden .
212 Xml::closeElement( 'form' );
213
214 $wgOut->addHTML( $form );
215 }
216
217 protected function setSyndicated() {
218 global $wgOut;
219 $queryParams = array(
220 'namespace' => $this->opts->getValue( 'namespace' ),
221 'username' => $this->opts->getValue( 'username' )
222 );
223 $wgOut->setSyndicated( true );
224 $wgOut->setFeedAppendQuery( wfArrayToCGI( $queryParams ) );
225 }
226
227 /**
228 * Format a row, providing the timestamp, links to the page/history, size, user links, and a comment
229 *
230 * @param $skin Skin to use
231 * @param $result Result row
232 * @return string
233 */
234 public function formatRow( $result ) {
235 global $wgLang, $wgContLang, $wgUser;
236 $dm = $wgContLang->getDirMark();
237
238 $title = Title::makeTitleSafe( $result->rc_namespace, $result->rc_title );
239 $time = $wgLang->timeAndDate( $result->rc_timestamp, true );
240 $plink = $this->skin->makeKnownLinkObj( $title, '', $this->patrollable( $result ) ? 'rcid=' . $result->rc_id : '' );
241 $hist = $this->skin->makeKnownLinkObj( $title, wfMsgHtml( 'hist' ), 'action=history' );
242 $length = wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ),
243 $wgLang->formatNum( $result->length ) );
244 $ulink = $this->skin->userLink( $result->rc_user, $result->rc_user_text ) . ' ' .
245 $this->skin->userToolLinks( $result->rc_user, $result->rc_user_text );
246 $comment = $this->skin->commentBlock( $result->rc_comment );
247 $css = $this->patrollable( $result ) ? " class='not-patrolled'" : '';
248
249 return "<li{$css}>{$time} {$dm}{$plink} ({$hist}) {$dm}[{$length}] {$dm}{$ulink} {$comment}</li>\n";
250 }
251
252 /**
253 * Should a specific result row provide "patrollable" links?
254 *
255 * @param $result Result row
256 * @return bool
257 */
258 protected function patrollable( $result ) {
259 global $wgUser;
260 return ( $wgUser->useNPPatrol() && !$result->rc_patrolled );
261 }
262
263 /**
264 * Output a subscription feed listing recent edits to this page.
265 * @param string $type
266 */
267 protected function feed( $type ) {
268 global $wgFeed, $wgFeedClasses, $wgFeedLimit;
269
270 if ( !$wgFeed ) {
271 global $wgOut;
272 $wgOut->addWikiMsg( 'feed-unavailable' );
273 return;
274 }
275
276 if( !isset( $wgFeedClasses[$type] ) ) {
277 global $wgOut;
278 $wgOut->addWikiMsg( 'feed-invalid' );
279 return;
280 }
281
282 $feed = new $wgFeedClasses[$type](
283 $this->feedTitle(),
284 wfMsg( 'tagline' ),
285 $this->getTitle()->getFullUrl() );
286
287 $pager = new NewPagesPager( $this, $this->opts );
288 $limit = $this->opts->getValue( 'limit' );
289 $pager->mLimit = min( $limit, $wgFeedLimit );
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( $this->stripComment( $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 function __construct( $form, FormOptions $opts ) {
356 parent::__construct();
357 $this->mForm = $form;
358 $this->opts = $opts;
359 }
360
361 function getTitle() {
362 static $title = null;
363 if ( $title === null )
364 $title = $this->mForm->getTitle();
365 return $title;
366 }
367
368 function getQueryInfo() {
369 global $wgEnableNewpagesUserFilter, $wgGroupPermissions, $wgUser;
370 $conds = array();
371 $conds['rc_new'] = 1;
372
373 $namespace = $this->opts->getValue( 'namespace' );
374 $namespace = ( $namespace === 'all' ) ? false : intval( $namespace );
375
376 $username = $this->opts->getValue( 'username' );
377 $user = Title::makeTitleSafe( NS_USER, $username );
378
379 if( $namespace !== false ) {
380 $conds['rc_namespace'] = $namespace;
381 $rcIndexes = array( 'new_name_timestamp' );
382 } else {
383 $rcIndexes = array( 'rc_timestamp' );
384 }
385 $conds[] = 'page_id = rc_cur_id';
386
387 # $wgEnableNewpagesUserFilter - temp WMF hack
388 if( $wgEnableNewpagesUserFilter && $user ) {
389 $conds['rc_user_text'] = $user->getText();
390 $rcIndexes = 'rc_user_text';
391 # If anons cannot make new pages, don't "exclude logged in users"!
392 } elseif( $wgGroupPermissions['*']['createpage'] && $this->opts->getValue( 'hideliu' ) ) {
393 $conds['rc_user'] = 0;
394 }
395 # If this user cannot see patrolled edits or they are off, don't do dumb queries!
396 if( $this->opts->getValue( 'hidepatrolled' ) && $wgUser->useNPPatrol() ) {
397 $conds['rc_patrolled'] = 0;
398 }
399 if( $this->opts->getValue( 'hidebots' ) ) {
400 $conds['rc_bot'] = 0;
401 }
402
403 if ( $this->opts->getValue( 'hideredirs' ) ) {
404 $conds['page_is_redirect'] = 0;
405 }
406
407 return array(
408 'tables' => array( 'recentchanges', 'page' ),
409 'fields' => 'rc_namespace,rc_title, rc_cur_id, rc_user,rc_user_text,rc_comment,
410 rc_timestamp,rc_patrolled,rc_id,page_len as length, page_latest as rev_id',
411 'conds' => $conds,
412 'options' => array( 'USE INDEX' => array('recentchanges' => $rcIndexes) )
413 );
414 }
415
416 function getIndexField() {
417 return 'rc_timestamp';
418 }
419
420 function formatRow( $row ) {
421 return $this->mForm->formatRow( $row );
422 }
423
424 function getStartBody() {
425 # Do a batch existence check on pages
426 $linkBatch = new LinkBatch();
427 while( $row = $this->mResult->fetchObject() ) {
428 $linkBatch->add( NS_USER, $row->rc_user_text );
429 $linkBatch->add( NS_USER_TALK, $row->rc_user_text );
430 $linkBatch->add( $row->rc_namespace, $row->rc_title );
431 }
432 $linkBatch->execute();
433 return "<ul>";
434 }
435
436 function getEndBody() {
437 return "</ul>";
438 }
439 }