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