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