Constructor parameters on a QueryPage need to be optional, so the class can be loaded...
[lhc/web/wiklou.git] / includes / SpecialNewpages.php
1 <?php
2 /**
3 *
4 * @addtogroup SpecialPage
5 */
6
7 /**
8 * implements Special:Newpages
9 * @addtogroup SpecialPage
10 */
11 class NewPagesPage extends QueryPage {
12
13 var $namespace;
14 var $username;
15 var $hideliu;
16 var $hidepatrolled;
17 var $hidebots;
18 var $defaults;
19
20 function NewPagesPage( $namespace=NS_MAIN, $username='', $hideliu=false, $hidepatrolled=false, $hidebots=false, $defaults=array()) {
21 $this->namespace = $namespace;
22 $this->username = $username;
23 $this->hideliu = $hideliu;
24 $this->hidepatrolled = $hidepatrolled;
25 $this->hidebots = $hidebots;
26 $this->defaults = $defaults;
27 }
28
29 function getName() {
30 return 'Newpages';
31 }
32
33 function isExpensive() {
34 # Indexed on RC, and will *not* work with querycache yet.
35 return false;
36 }
37
38 function makeUserWhere( &$dbo ) {
39 global $wgGroupPermissions;
40 $where = '';
41 if ($this->hidepatrolled)
42 $where .= ' AND rc_patrolled = 0';
43 if ($this->hidebots)
44 $where .= ' AND rc_bot = 0';
45 if ($wgGroupPermissions['*']['createpage'] == true && $this->hideliu) {
46 $where .= ' AND rc_user = 0';
47 } else {
48 $title = Title::makeTitleSafe( NS_USER, $this->username );
49 if( $title ) {
50 $where .= ' AND rc_user_text = ' . $dbo->addQuotes( $title->getText() );
51 }
52 }
53 return $where;
54 }
55
56 private function makeNamespaceWhere() {
57 return $this->namespace !== 'all'
58 ? ' AND rc_namespace = ' . intval( $this->namespace )
59 : '';
60 }
61
62 function getSQL() {
63 global $wgUser, $wgUseNPPatrol;
64 $usepatrol = ( $wgUseNPPatrol ) ? 1 : 0;
65 $dbr = wfGetDB( DB_SLAVE );
66 list( $recentchanges, $page ) = $dbr->tableNamesN( 'recentchanges', 'page' );
67
68 $nsfilter = $this->makeNamespaceWhere();
69 $uwhere = $this->makeUserWhere( $dbr );
70
71 # FIXME: text will break with compression
72 return
73 "SELECT 'Newpages' as type,
74 rc_namespace AS namespace,
75 rc_title AS title,
76 rc_cur_id AS cur_id,
77 rc_user AS \"user\",
78 rc_user_text AS user_text,
79 rc_comment as \"comment\",
80 rc_timestamp AS timestamp,
81 rc_timestamp AS value,
82 '{$usepatrol}' as usepatrol,
83 rc_patrolled AS patrolled,
84 rc_id AS rcid,
85 page_len as length,
86 page_latest as rev_id
87 FROM $recentchanges,$page
88 WHERE rc_cur_id=page_id AND rc_new=1
89 {$nsfilter}
90 AND page_is_redirect = 0
91 {$uwhere}";
92 }
93
94 function preprocessResults( &$dbo, &$res ) {
95 # Do a batch existence check on the user and talk pages
96 $linkBatch = new LinkBatch();
97 while( $row = $dbo->fetchObject( $res ) ) {
98 $linkBatch->addObj( Title::makeTitleSafe( NS_USER, $row->user_text ) );
99 $linkBatch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_text ) );
100 }
101 $linkBatch->execute();
102 # Seek to start
103 if( $dbo->numRows( $res ) > 0 )
104 $dbo->dataSeek( $res, 0 );
105 }
106
107 /**
108 * Format a row, providing the timestamp, links to the page/history, size, user links, and a comment
109 *
110 * @param $skin Skin to use
111 * @param $result Result row
112 * @return string
113 */
114 function formatResult( $skin, $result ) {
115 global $wgLang, $wgContLang;
116 $dm = $wgContLang->getDirMark();
117
118 $title = Title::makeTitleSafe( $result->namespace, $result->title );
119 $time = $wgLang->timeAndDate( $result->timestamp, true );
120 $plink = $skin->makeKnownLinkObj( $title, '', $this->patrollable( $result ) ? 'rcid=' . $result->rcid : '' );
121 $hist = $skin->makeKnownLinkObj( $title, wfMsgHtml( 'hist' ), 'action=history' );
122 $length = wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ), $wgLang->formatNum( htmlspecialchars( $result->length ) ) );
123 $ulink = $skin->userLink( $result->user, $result->user_text ) . ' ' . $skin->userToolLinks( $result->user, $result->user_text );
124 $comment = $skin->commentBlock( $result->comment );
125
126 return "{$time} {$dm}{$plink} ({$hist}) {$dm}[{$length}] {$dm}{$ulink} {$comment}";
127 }
128
129 /**
130 * Should a specific result row provide "patrollable" links?
131 *
132 * @param $result Result row
133 * @return bool
134 */
135 function patrollable( $result ) {
136 global $wgUser, $wgUseNPPatrol;
137 return $wgUseNPPatrol && $wgUser->isAllowed( 'patrol' ) && !$result->patrolled;
138 }
139
140 function feedItemDesc( $row ) {
141 if( isset( $row->rev_id ) ) {
142 $revision = Revision::newFromId( $row->rev_id );
143 if( $revision ) {
144 return '<p>' . htmlspecialchars( wfMsg( 'summary' ) ) . ': ' .
145 htmlspecialchars( $revision->getComment() ) . "</p>\n<hr />\n<div>" .
146 nl2br( htmlspecialchars( $revision->getText() ) ) . "</div>";
147 }
148 }
149 return parent::feedItemDesc( $row );
150 }
151
152 /**
153 * Show a form for filtering namespace and username
154 *
155 * @return string
156 */
157 function getPageHeader() {
158 global $wgScript, $wgContLang, $wgGroupPermissions, $wgUser, $wgUseNPPatrol;
159 $align = $wgContLang->isRTL() ? 'left' : 'right';
160 $self = SpecialPage::getTitleFor( $this->getName() );
161
162 // show/hide links
163 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ));
164
165 $nondefaults = array();
166 wfAppendToArrayIfNotDefault( 'hidepatrolled', $this->hidepatrolled, $this->defaults, $nondefaults);
167 wfAppendToArrayIfNotDefault( 'hideliu', $this->hideliu, $this->defaults, $nondefaults);
168 wfAppendToArrayIfNotDefault( 'namespace', $this->namespace, $this->defaults, $nondefaults);
169 wfAppendToArrayIfNotDefault( 'limit', $this->limit , $this->defaults, $nondefaults);
170 wfAppendToArrayIfNotDefault( 'offset', $this->offset , $this->defaults, $nondefaults);
171 wfAppendToArrayIfNotDefault( 'username', $this->username , $this->defaults, $nondefaults);
172
173 $liuLink = $wgUser->getSkin()->makeKnownLink( $wgContLang->specialPage( 'Newpages' ),
174 htmlspecialchars( $showhide[1-$this->hideliu] ), wfArrayToCGI( array( 'hideliu' => 1-$this->hideliu ), $nondefaults ) );
175 $patrLink = $wgUser->getSkin()->makeKnownLink( $wgContLang->specialPage( 'Newpages' ),
176 htmlspecialchars( $showhide[1-$this->hidepatrolled] ), wfArrayToCGI( array( 'hidepatrolled' => 1-$this->hidepatrolled ), $nondefaults ) );
177 $botsLink = $wgUser->getSkin()->makeKnownLink( $wgContLang->specialPage( 'Newpages' ),
178 htmlspecialchars( $showhide[1-$this->hidebots] ), wfArrayToCGI( array( 'hidebots' => 1-$this->hidebots ), $nondefaults ) );
179 $links = array();
180 if( $wgGroupPermissions['*']['createpage'] == true )
181 $links[] = wfMsgHtml( 'rcshowhideliu', $liuLink );
182 if( $wgUseNPPatrol )
183 $links[] = wfMsgHtml( 'rcshowhidepatr', $patrLink );
184 $links[] = wfMsgHtml( 'rcshowhidebots', $botsLink );
185 $hl = implode( ' | ', $links );
186
187 $form = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) ) .
188 Xml::hidden( 'title', $self->getPrefixedDBkey() ) .
189 Xml::openElement( 'table' ) .
190 "<tr>
191 <td align=\"$align\">" .
192 Xml::label( wfMsg( 'namespace' ), 'namespace' ) .
193 "</td>
194 <td>" .
195 Xml::namespaceSelector( intval( $this->namespace ), 'all' ) .
196 "</td>
197 </tr>
198 <tr>
199 <td align=\"$align\">" .
200 Xml::label( wfMsg( 'newpages-username' ), 'mw-np-username' ) .
201 "</td>
202 <td>" .
203 Xml::input( 'username', 30, $this->username, array( 'id' => 'mw-np-username' ) ) .
204 "</td>
205 </tr><tr><td></td><td>" . $hl . "</td></tr>
206 <tr> <td></td>
207 <td>" .
208 Xml::submitButton( wfMsg( 'allpagessubmit' ) ) .
209 "</td>
210 </tr>" .
211 Xml::closeElement( 'table' ) .
212 Xml::hidden( 'offset', $this->offset ) .
213 Xml::hidden( 'limit', $this->limit ) .
214 Xml::closeElement( 'form' );
215 return $form;
216 }
217
218 /**
219 * Link parameters
220 *
221 * @return array
222 */
223 function linkParameters() {
224 return( array( 'namespace' => $this->namespace, 'username' => $this->username, 'hideliu' => $this->hideliu, 'hidepatrolled' => $this->hidepatrolled ) );
225 }
226
227 }
228
229 /**
230 * constructor
231 */
232 function wfSpecialNewpages($par, $specialPage) {
233 global $wgRequest, $wgContLang;
234
235
236 list( $limit, $offset ) = wfCheckLimits();
237
238 $defaults = array(
239 /* bool */ 'hideliu' => false,
240 /* bool */ 'hidepatrolled' => false,
241 /* bool */ 'hidebots' => false,
242 /* text */ 'namespace' => NS_MAIN,
243 /* text */ 'username' => '',
244 /* int */ 'offset' => $offset,
245 /* int */ 'limit' => $limit,
246 );
247
248 extract($defaults);
249
250 if ( $par ) {
251 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
252 foreach ( $bits as $bit ) {
253 if ( 'shownav' == $bit )
254 $shownavigation = true;
255 if ( 'hideliu' == $bit )
256 $hideliu = true;
257 if ( 'hidepatrolled' == $bit )
258 $hidepatrolled = true;
259 if ( 'hidebots' == $bit )
260 $hidebots = true;
261 if ( is_numeric( $bit ) )
262 $limit = $bit;
263
264 $m = array();
265 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) )
266 $limit = intval($m[1]);
267 if ( preg_match( '/^offset=(\d+)$/', $bit, $m ) )
268 $offset = intval($m[1]);
269 if ( preg_match( '/^namespace=(.*)$/', $bit, $m ) ) {
270 $ns = $wgContLang->getNsIndex( $m[1] );
271 if( $ns !== false ) {
272 $namespace = $ns;
273 }
274 }
275 }
276 } else {
277 if( $ns = $wgRequest->getText( 'namespace', NS_MAIN ) )
278 $namespace = $ns;
279 if( $un = $wgRequest->getText( 'username' ) )
280 $username = $un;
281 if( $hliu = $wgRequest->getBool( 'hideliu' ) )
282 $hideliu = $hliu;
283 if( $hpatrolled = $wgRequest->getBool( 'hidepatrolled' ) )
284 $hidepatrolled = $hpatrolled;
285 if( $hbots = $wgRequest->getBool( 'hidebots' ) )
286 $hidebots = $hbots;
287 }
288
289 if ( ! isset( $shownavigation ) )
290 $shownavigation = ! $specialPage->including();
291
292 $npp = new NewPagesPage( $namespace, $username, $hideliu, $hidepatrolled, $hidebots, $defaults );
293
294 if ( ! $npp->doFeed( $wgRequest->getVal( 'feed' ), $limit ) )
295 $npp->doQuery( $offset, $limit, $shownavigation );
296 }