* (bug 12148) Text highlight wasn't applied to cleanly deleted and added
[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, $wgUseRCPatrol;
64 $usepatrol = ( $wgUseNPPatrol || $wgUseRCPatrol ) ? 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, $wgUseRCPatrol, $wgUseNPPatrol;
137 return ( $wgUseRCPatrol || $wgUseNPPatrol )
138 && $wgUser->isAllowed( 'patrol' )
139 && !$result->patrolled;
140 }
141
142 function feedItemDesc( $row ) {
143 if( isset( $row->rev_id ) ) {
144 $revision = Revision::newFromId( $row->rev_id );
145 if( $revision ) {
146 return '<p>' . htmlspecialchars( wfMsg( 'summary' ) ) . ': ' .
147 htmlspecialchars( $revision->getComment() ) . "</p>\n<hr />\n<div>" .
148 nl2br( htmlspecialchars( $revision->getText() ) ) . "</div>";
149 }
150 }
151 return parent::feedItemDesc( $row );
152 }
153
154 /**
155 * Show a form for filtering namespace and username
156 *
157 * @return string
158 */
159 function getPageHeader() {
160 global $wgScript, $wgContLang, $wgGroupPermissions, $wgUser, $wgUseRCPatrol, $wgUseNPPatrol;
161 $align = $wgContLang->isRTL() ? 'left' : 'right';
162 $self = SpecialPage::getTitleFor( $this->getName() );
163
164 // show/hide links
165 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ));
166
167 $nondefaults = array();
168 wfAppendToArrayIfNotDefault( 'hidepatrolled', $this->hidepatrolled, $this->defaults, $nondefaults);
169 wfAppendToArrayIfNotDefault( 'hideliu', $this->hideliu, $this->defaults, $nondefaults);
170 wfAppendToArrayIfNotDefault( 'hidebots', $this->hidebots, $this->defaults, $nondefaults);
171 wfAppendToArrayIfNotDefault( 'namespace', $this->namespace, $this->defaults, $nondefaults);
172 wfAppendToArrayIfNotDefault( 'limit', $this->limit , $this->defaults, $nondefaults);
173 wfAppendToArrayIfNotDefault( 'offset', $this->offset , $this->defaults, $nondefaults);
174 wfAppendToArrayIfNotDefault( 'username', $this->username , $this->defaults, $nondefaults);
175
176 $liuLink = $wgUser->getSkin()->makeKnownLink( $wgContLang->specialPage( 'Newpages' ),
177 htmlspecialchars( $showhide[1-$this->hideliu] ), wfArrayToCGI( array( 'hideliu' => 1-$this->hideliu ), $nondefaults ) );
178 $patrLink = $wgUser->getSkin()->makeKnownLink( $wgContLang->specialPage( 'Newpages' ),
179 htmlspecialchars( $showhide[1-$this->hidepatrolled] ), wfArrayToCGI( array( 'hidepatrolled' => 1-$this->hidepatrolled ), $nondefaults ) );
180 $botsLink = $wgUser->getSkin()->makeKnownLink( $wgContLang->specialPage( 'Newpages' ),
181 htmlspecialchars( $showhide[1-$this->hidebots] ), wfArrayToCGI( array( 'hidebots' => 1-$this->hidebots ), $nondefaults ) );
182 $links = array();
183 if( $wgGroupPermissions['*']['createpage'] == true )
184 $links[] = wfMsgHtml( 'rcshowhideliu', $liuLink );
185 if( $wgUseNPPatrol || $wgUseRCPatrol )
186 $links[] = wfMsgHtml( 'rcshowhidepatr', $patrLink );
187 $links[] = wfMsgHtml( 'rcshowhidebots', $botsLink );
188 $hl = implode( ' | ', $links );
189
190 $form = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) ) .
191 Xml::hidden( 'title', $self->getPrefixedDBkey() ) .
192 Xml::openElement( 'table' ) .
193 "<tr>
194 <td align=\"$align\">" .
195 Xml::label( wfMsg( 'namespace' ), 'namespace' ) .
196 "</td>
197 <td>" .
198 Xml::namespaceSelector( intval( $this->namespace ), 'all' ) .
199 "</td>
200 </tr>
201 <tr>
202 <td align=\"$align\">" .
203 Xml::label( wfMsg( 'newpages-username' ), 'mw-np-username' ) .
204 "</td>
205 <td>" .
206 Xml::input( 'username', 30, $this->username, array( 'id' => 'mw-np-username' ) ) .
207 "</td>
208 </tr><tr><td></td><td>" . $hl . "</td></tr>
209 <tr> <td></td>
210 <td>" .
211 Xml::submitButton( wfMsg( 'allpagessubmit' ) ) .
212 "</td>
213 </tr>" .
214 Xml::closeElement( 'table' ) .
215 Xml::hidden( 'offset', $this->offset ) .
216 Xml::hidden( 'limit', $this->limit ) .
217 Xml::closeElement( 'form' );
218 return $form;
219 }
220
221 /**
222 * Link parameters
223 *
224 * @return array
225 */
226 function linkParameters() {
227 return( array( 'namespace' => $this->namespace, 'username' => $this->username, 'hideliu' => $this->hideliu, 'hidepatrolled' => $this->hidepatrolled ) );
228 }
229
230 }
231
232 /**
233 * constructor
234 */
235 function wfSpecialNewpages($par, $specialPage) {
236 global $wgRequest, $wgContLang;
237
238
239 list( $limit, $offset ) = wfCheckLimits();
240
241 $defaults = array(
242 /* bool */ 'hideliu' => false,
243 /* bool */ 'hidepatrolled' => false,
244 /* bool */ 'hidebots' => false,
245 /* text */ 'namespace' => NS_MAIN,
246 /* text */ 'username' => '',
247 /* int */ 'offset' => $offset,
248 /* int */ 'limit' => $limit,
249 );
250
251 extract($defaults);
252
253 if ( $par ) {
254 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
255 foreach ( $bits as $bit ) {
256 if ( 'shownav' == $bit )
257 $shownavigation = true;
258 if ( 'hideliu' == $bit )
259 $hideliu = true;
260 if ( 'hidepatrolled' == $bit )
261 $hidepatrolled = true;
262 if ( 'hidebots' == $bit )
263 $hidebots = true;
264 if ( is_numeric( $bit ) )
265 $limit = $bit;
266
267 $m = array();
268 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) )
269 $limit = intval($m[1]);
270 if ( preg_match( '/^offset=(\d+)$/', $bit, $m ) )
271 $offset = intval($m[1]);
272 if ( preg_match( '/^namespace=(.*)$/', $bit, $m ) ) {
273 $ns = $wgContLang->getNsIndex( $m[1] );
274 if( $ns !== false ) {
275 $namespace = $ns;
276 }
277 }
278 }
279 } else {
280 if( $ns = $wgRequest->getInt( 'namespace', NS_MAIN ) )
281 $namespace = $ns;
282 if( $un = $wgRequest->getText( 'username' ) )
283 $username = $un;
284 if( $hliu = $wgRequest->getBool( 'hideliu' ) )
285 $hideliu = $hliu;
286 if( $hpatrolled = $wgRequest->getBool( 'hidepatrolled' ) )
287 $hidepatrolled = $hpatrolled;
288 if( $hbots = $wgRequest->getBool( 'hidebots' ) )
289 $hidebots = $hbots;
290 }
291
292 if ( ! isset( $shownavigation ) )
293 $shownavigation = ! $specialPage->including();
294
295 $npp = new NewPagesPage( $namespace, $username, $hideliu, $hidepatrolled, $hidebots, $defaults );
296
297 if ( ! $npp->doFeed( $wgRequest->getVal( 'feed' ), $limit ) )
298 $npp->doQuery( $offset, $limit, $shownavigation );
299 }