Make these h4
[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, $specialPage ) {
12 $page = new NewPagesPage( $specialPage );
13 $page->execute( $par );
14 }
15
16 /**
17 * implements Special:Newpages
18 * @addtogroup SpecialPage
19 */
20 class NewPagesPage extends QueryPage {
21
22 protected $options = array();
23 protected $nondefaults = array();
24 protected $specialPage;
25
26 public function __construct( $specialPage=null ) {
27 $this->specialPage = $specialPage;
28 }
29
30 public function execute( $par ) {
31 global $wgRequest, $wgLang;
32
33 $shownavigation = is_object( $this->specialPage ) && !$this->specialPage->including();
34
35 $defaults = array(
36 /* bool */ 'hideliu' => false,
37 /* bool */ 'hidepatrolled' => false,
38 /* bool */ 'hidebots' => false,
39 /* text */ 'namespace' => "0",
40 /* text */ 'username' => '',
41 /* int */ 'offset' => 0,
42 /* int */ 'limit' => 50,
43 );
44
45 if( $shownavigation ) {
46 // Some hopefully reasonable limits...
47 $max = array(
48 /* int */ 'offset' => 20000,
49 /* int */ 'limit' => 500,
50 );
51 } else {
52 // Embedded? Be a lot more strict...
53 $max = array(
54 /* int */ 'offset' => 0,
55 /* int */ 'limit' => 200,
56 );
57 }
58
59 $options = $defaults;
60
61 if ( $par ) {
62 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
63 foreach ( $bits as $bit ) {
64 if ( 'shownav' == $bit )
65 $shownavigation = true;
66 if ( 'hideliu' === $bit )
67 $options['hideliu'] = true;
68 if ( 'hidepatrolled' == $bit )
69 $options['hidepatrolled'] = true;
70 if ( 'hidebots' == $bit )
71 $options['hidebots'] = true;
72 if ( is_numeric( $bit ) )
73 $options['limit'] = intval( $bit );
74
75 $m = array();
76 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) )
77 $options['limit'] = intval($m[1]);
78 if ( preg_match( '/^offset=(\d+)$/', $bit, $m ) )
79 $options['offset'] = intval($m[1]);
80 if ( preg_match( '/^namespace=(.*)$/', $bit, $m ) ) {
81 $ns = $wgLang->getNsIndex( $m[1] );
82 if( $ns !== false ) {
83 $options['namespace'] = $ns;
84 }
85 }
86 }
87 }
88
89 // Override all values from requests, if specified
90 foreach ( $defaults as $v => $t ) {
91 if ( is_bool($t) ) {
92 $options[$v] = $wgRequest->getBool( $v, $options[$v] );
93 } elseif( is_int($t) ) {
94 $options[$v] = $wgRequest->getInt( $v, $options[$v] );
95 } elseif( is_string($t) ) {
96 $options[$v] = $wgRequest->getText( $v, $options[$v] );
97 }
98 }
99
100 // Validate limit and offset params
101 if ( $options['limit'] <= 0 ) {
102 $options['limit'] = $defaults['limit'];
103 }
104 if ( $options['limit'] > $max['limit'] ) {
105 $options['limit'] = $max['limit'];
106 }
107
108 if ( $options['offset'] < 0 ) {
109 $options['offset'] = $defaults['offset'];
110 }
111 if ( $options['offset'] > $max['offset'] ) {
112 $options['offset'] = $max['offset'];
113 }
114
115 $nondefaults = array();
116 foreach ( $options as $v => $t ) {
117 if ( $v === 'offset' ) continue; # Reset offset if parameters change
118 wfAppendToArrayIfNotDefault( $v, $t, $defaults, $nondefaults );
119 }
120
121 # bind to class
122 $this->options = $options;
123 $this->nondefaults = $nondefaults;
124
125 if ( !$this->doFeed( $wgRequest->getVal( 'feed' ), $options['limit'] ) ) {
126 $this->doQuery( $options['offset'], $options['limit'], $shownavigation );
127 }
128 }
129
130 function linkParameters() {
131 $nondefaults = $this->nondefaults;
132 // QueryPage seems to handle limit and offset itself
133 if ( isset( $nondefaults['limit'] ) ) {
134 unset($nondefaults['limit']);
135 }
136 return $nondefaults;
137 }
138
139 function getName() {
140 return 'Newpages';
141 }
142
143 function isExpensive() {
144 # Indexed on RC, and will *not* work with querycache yet.
145 return false;
146 }
147
148 function makeUserWhere( $db ) {
149 global $wgGroupPermissions;
150 $conds = array();
151 if ($this->options['hidepatrolled']) {
152 $conds['rc_patrolled'] = 0;
153 }
154 if ($this->options['hidebots']) {
155 $conds['rc_bot'] = 0;
156 }
157 if ($wgGroupPermissions['*']['createpage'] == true && $this->options['hideliu']) {
158 $conds['rc_user'] = 0;
159 } else {
160 $title = Title::makeTitleSafe( NS_USER, $this->options['username'] );
161 if( $title ) {
162 $conds['rc_user_text'] = $title->getText();
163 }
164 }
165 return $conds;
166 }
167
168 function getSQL() {
169 global $wgUser, $wgUseNPPatrol, $wgUseRCPatrol;
170 $usepatrol = ( $wgUseNPPatrol || $wgUseRCPatrol ) ? 1 : 0;
171 $dbr = wfGetDB( DB_SLAVE );
172 list( $recentchanges, $page ) = $dbr->tableNamesN( 'recentchanges', 'page' );
173
174 $conds = array();
175 $conds['rc_new'] = 1;
176 if ( $this->options['namespace'] !== 'all' ) {
177 $conds['rc_namespace'] = intval( $this->options['namespace'] );
178 }
179 $conds['page_is_redirect'] = 0;
180 $conds += $this->makeUserWhere( $dbr );
181 $condstext = $dbr->makeList( $conds, LIST_AND );
182
183 # FIXME: text will break with compression
184 return
185 "SELECT 'Newpages' as type,
186 rc_namespace AS namespace,
187 rc_title AS title,
188 rc_cur_id AS cur_id,
189 rc_user AS \"user\",
190 rc_user_text AS user_text,
191 rc_comment as \"comment\",
192 rc_timestamp AS timestamp,
193 rc_timestamp AS value,
194 '{$usepatrol}' as usepatrol,
195 rc_patrolled AS patrolled,
196 rc_id AS rcid,
197 page_len as length,
198 page_latest as rev_id
199 FROM $recentchanges,$page
200 WHERE rc_cur_id=page_id AND $condstext";
201 }
202
203 function preprocessResults( $db, $res ) {
204 # Do a batch existence check on the user and talk pages
205 $linkBatch = new LinkBatch();
206 while( $row = $db->fetchObject( $res ) ) {
207 $linkBatch->add( NS_USER, $row->user_text );
208 $linkBatch->add( NS_USER_TALK, $row->user_text );
209 }
210 $linkBatch->execute();
211 # Seek to start
212 if( $db->numRows( $res ) > 0 )
213 $db->dataSeek( $res, 0 );
214 }
215
216 /**
217 * Format a row, providing the timestamp, links to the page/history, size, user links, and a comment
218 *
219 * @param $skin Skin to use
220 * @param $result Result row
221 * @return string
222 */
223 function formatResult( $skin, $result ) {
224 global $wgLang, $wgContLang;
225 $dm = $wgContLang->getDirMark();
226
227 $title = Title::makeTitleSafe( $result->namespace, $result->title );
228 $time = $wgLang->timeAndDate( $result->timestamp, true );
229 $plink = $skin->makeKnownLinkObj( $title, '', $this->patrollable( $result ) ? 'rcid=' . $result->rcid : '' );
230 $hist = $skin->makeKnownLinkObj( $title, wfMsgHtml( 'hist' ), 'action=history' );
231 $length = wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ), $wgLang->formatNum( htmlspecialchars( $result->length ) ) );
232 $ulink = $skin->userLink( $result->user, $result->user_text ) . ' ' . $skin->userToolLinks( $result->user, $result->user_text );
233 $comment = $skin->commentBlock( $result->comment );
234
235 return "{$time} {$dm}{$plink} ({$hist}) {$dm}[{$length}] {$dm}{$ulink} {$comment}";
236 }
237
238 /**
239 * Should a specific result row provide "patrollable" links?
240 *
241 * @param $result Result row
242 * @return bool
243 */
244 function patrollable( $result ) {
245 global $wgUser, $wgUseRCPatrol, $wgUseNPPatrol;
246 return ( $wgUseRCPatrol || $wgUseNPPatrol )
247 && ( $wgUser->isAllowed( 'patrol' ) || $wgUser->isAllowed( 'patrolmarks' ) )
248 && !$result->patrolled;
249 }
250
251 function feedItemDesc( $row ) {
252 if( isset( $row->rev_id ) ) {
253 $revision = Revision::newFromId( $row->rev_id );
254 if( $revision ) {
255 return '<p>' . htmlspecialchars( wfMsg( 'summary' ) ) . ': ' .
256 htmlspecialchars( $revision->getComment() ) . "</p>\n<hr />\n<div>" .
257 nl2br( htmlspecialchars( $revision->getText() ) ) . "</div>";
258 }
259 }
260 return parent::feedItemDesc( $row );
261 }
262
263 /**
264 * Show a form for filtering namespace and username
265 *
266 * @return string
267 */
268 function getPageHeader() {
269 global $wgScript, $wgContLang, $wgGroupPermissions, $wgUser, $wgUseRCPatrol, $wgUseNPPatrol;
270 $sk = $wgUser->getSkin();
271 $align = $wgContLang->isRTL() ? 'left' : 'right';
272 $self = SpecialPage::getTitleFor( $this->getName() );
273
274 // show/hide links
275 $showhide = array( wfMsgHtml( 'show' ), wfMsgHtml( 'hide' ));
276
277 $hidelinks = array();
278
279 if ( $wgGroupPermissions['*']['createpage'] === true ) {
280 $hidelinks['hideliu'] = 'rcshowhideliu';
281 }
282 if ( $wgUseNPPatrol || $wgUseRCPatrol ) {
283 $hidelinks['hidepatrolled'] = 'rcshowhidepatr';
284 }
285 $hidelinks['hidebots'] = 'rcshowhidebots';
286
287 $links = array();
288 foreach ( $hidelinks as $key => $msg ) {
289 $reversed = 1-$this->options[$key];
290 $link = $sk->makeKnownLinkObj( $self, $showhide[$reversed],
291 wfArrayToCGI( array( $key => $reversed ), $this->nondefaults )
292 );
293 $links[$key] = wfMsgHtml( $msg, $link );
294 }
295
296 $hl = implode( ' | ', $links );
297
298 // Store query values in hidden fields so that form submission doesn't lose them
299 $hidden = array();
300 foreach ( $this->nondefaults as $key => $value ) {
301 if ( $key === 'namespace' ) continue;
302 if ( $key === 'username' ) continue;
303 $hidden[] = Xml::hidden( $key, $value );
304 }
305 $hidden = implode( "\n", $hidden );
306
307 $form = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) ) .
308 Xml::hidden( 'title', $self->getPrefixedDBkey() ) .
309 Xml::openElement( 'fieldset' ) .
310 Xml::element( 'legend', null, wfMsg( 'newpages' ) ) .
311 Xml::openElement( 'table', array( 'id' => 'mw-newpages-table' ) ) .
312 "<tr>
313 <td align=\"$align\">" .
314 Xml::label( wfMsg( 'namespace' ), 'namespace' ) .
315 "</td>
316 <td>" .
317 Xml::namespaceSelector( $this->options['namespace'], 'all' ) .
318 "</td>
319 </tr>
320 <tr>
321 <td align=\"$align\">" .
322 Xml::label( wfMsg( 'newpages-username' ), 'mw-np-username' ) .
323 "</td>
324 <td>" .
325 Xml::input( 'username', 30, $this->options['username'], array( 'id' => 'mw-np-username' ) ) .
326 "</td>
327 </tr>
328 <tr> <td></td>
329 <td>" .
330 Xml::submitButton( wfMsg( 'allpagessubmit' ) ) .
331 "</td>
332 </tr>" .
333 "<tr>
334 <td></td>
335 <td>" .
336 $hl .
337 "</td>
338 </tr>" .
339 Xml::closeElement( 'table' ) .
340 Xml::closeElement( 'fieldset' ) .
341 $hidden .
342 Xml::closeElement( 'form' );
343 return $form;
344 }
345 }