Do not add an empty class=''
[lhc/web/wiklou.git] / includes / SpecialNewpages.php
1 <?php
2 /**
3 *
4 * @addtogroup 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 * @addtogroup 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 $opts->add( 'hideliu', false );
34 $opts->add( 'hidepatrolled', false );
35 $opts->add( 'hidebots', false );
36 $opts->add( 'limit', 50 );
37 $opts->add( 'offset', 0 );
38 $opts->add( 'namespace', '0' );
39 $opts->add( 'username', '' );
40 $opts->add( 'feed', '' );
41
42 // Set values
43 $opts->fetchValuesFromRequest( $wgRequest );
44 if ( $par ) $this->parseParams( $par );
45
46 // Validate
47 $opts->validateIntBounds( 'limit', 0, 5000 );
48 if( !$wgEnableNewpagesUserFilter ) {
49 $opts->setValue( 'username', '' );
50 }
51
52 // Store some objects
53 $this->opts = $opts;
54 $this->skin = $wgUser->getSkin();
55 $this->title = SpecialPage::getTitleFor( 'NewPages' );
56 }
57
58 protected function parseParams( $par ) {
59 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
60 foreach ( $bits as $bit ) {
61 if ( 'shownav' == $bit )
62 $this->showNavigation = true;
63 if ( 'hideliu' === $bit )
64 $this->opts->setValue( 'hideliu', true );
65 if ( 'hidepatrolled' == $bit )
66 $this->opts->setValue( 'hidepatrolled', true );
67 if ( 'hidebots' == $bit )
68 $this->opts->setValue( 'hidebots', true );
69 if ( is_numeric( $bit ) )
70 $this->opts->setValue( 'limit', intval( $bit ) );
71
72 $m = array();
73 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) )
74 $this->opts->setValue( 'limit', intval($m[1]) );
75 if ( preg_match( '/^offset=(\d+)$/', $bit, $m ) )
76 $this->opts->setValue( 'offset', intval($m[1]) );
77 if ( preg_match( '/^namespace=(.*)$/', $bit, $m ) ) {
78 $ns = $wgLang->getNsIndex( $m[1] );
79 if( $ns !== false ) {
80 $this->opts->setValue( 'namespace', $ns );
81 }
82 }
83 }
84 }
85
86 /**
87 * Show a form for filtering namespace and username
88 *
89 * @param string $par
90 * @param bool $including true if the page is being included with {{Special:Newpages}}
91 * @return string
92 */
93 public function execute( $par, $including ) {
94 global $wgLang, $wgGroupPermissions, $wgUser, $wgOut;
95
96 $this->showNavigation = !$including; // Maybe changed in setup
97 $this->setup( $par );
98
99 // Settings
100 $this->form();
101
102 if( !$including ){
103 $this->setSyndicated();
104 $feedType = $this->opts->getValue( 'feed' );
105 if( $feedType ) {
106 return $this->feed( $feedType, $options );
107 }
108 }
109
110 $pager = new NewPagesPager( $this, $this->opts );
111 $pager->mLimit = $this->opts->getValue( 'limit' );
112 $pager->mOffset = $this->opts->getValue( 'offset' );
113
114 if( $pager->getNumRows() ) {
115 $navigation = '';
116 if ( $this->showNavigation ) $navigation = $pager->getNavigationBar();
117 $wgOut->addHTML( $navigation . $pager->getBody() . $navigation );
118 } else {
119 $wgOut->addWikiMsg( 'specialpage-empty' );
120 }
121 }
122
123 protected function filterLinks() {
124 global $wgGroupPermissions, $wgUser;
125
126 // show/hide links
127 $showhide = array( wfMsgHtml( 'show' ), wfMsgHtml( 'hide' ) );
128
129 // Option value -> message mapping
130 $filters = array(
131 'hideliu' => 'rcshowhideliu',
132 'hidepatrolled' => 'rcshowhidepatr',
133 'hidebots' => 'rcshowhidebots'
134 );
135
136 // Disable some if needed
137 if ( $wgGroupPermissions['*']['createpage'] !== true )
138 unset($filters['hideliu']);
139
140 if ( $wgUser->useNPPatrol() )
141 unset($filters['hidepatrolled']);
142
143 $links = array();
144 $changed = $this->opts->getChangedValues();
145 unset($changed['offset']); // Reset offset if query type changes
146
147 foreach ( $filters as $key => $msg ) {
148 $onoff = 1 - $this->opts->getValue($key);
149 $link = $this->skin->makeKnownLinkObj( $this->title, $showhide[$onoff],
150 wfArrayToCGI( 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->title->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, $options ) {
268 require_once 'SpecialRecentchanges.php';
269
270 global $wgFeed, $wgFeedClasses;
271
272 if ( !$wgFeed ) {
273 global $wgOut;
274 $wgOut->addWikiMsg( 'feed-unavailable' );
275 return;
276 }
277
278 if( !isset( $wgFeedClasses[$type] ) ) {
279 global $wgOut;
280 $wgOut->addWikiMsg( 'feed-invalid' );
281 return;
282 }
283
284 $feed = new $wgFeedClasses[$type](
285 $this->feedTitle(),
286 wfMsg( 'tagline' ),
287 $this->title->getFullUrl() );
288
289 $pager = new NewPagesPager( $this, $this->opts );
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 = $this->stripComment( $row->rc_comment );
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 * @addtogroup 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 = SpecialPage::getTitleFor( 'Newpages' );
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 }