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