Fix index usage in contribs
[lhc/web/wiklou.git] / includes / SpecialContributions.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 * @subpackage SpecialPage
6 */
7
8 /**
9 * Special page "user contributions".
10 * Shows a list of the contributions of a user.
11 *
12 * @return none
13 * @param string $par (optional) user name of the user for which to show the contributions
14 */
15 function wfSpecialContributions( $par = '' ) {
16 global $wgUser, $wgOut, $wgLang, $wgContLang, $wgRequest;
17 $fname = 'wfSpecialContributions';
18
19 if( $par )
20 $target = $par;
21 else
22 $target = $wgRequest->getVal( 'target' );
23
24 if ( '' == $target ) {
25 $wgOut->errorpage( 'notargettitle', 'notargettext' );
26 return;
27 }
28
29 # FIXME: Change from numeric offsets to date offsets
30 list( $limit, $offset ) = wfCheckLimits( 50, '' );
31 $offlimit = $limit + $offset;
32 $querylimit = $offlimit + 1;
33 $hideminor = ($wgRequest->getVal( 'hideminor' ) ? 1 : 0);
34 $sk = $wgUser->getSkin();
35 $dbr =& wfGetDB( DB_SLAVE );
36 $userCond = "";
37 $namespace = $wgRequest->getVal( 'namespace', '' );
38 if( $namespace != '' ) {
39 $namespace = IntVal( $namespace );
40 } else {
41 $namespace = NULL;
42 }
43
44 $nt = Title::newFromURL( $target );
45 if ( !$nt ) {
46 $wgOut->errorpage( 'notargettitle', 'notargettext' );
47 return;
48 }
49 $nt =& Title::makeTitle( NS_USER, $nt->getDBkey() );
50
51 $id = User::idFromName( $nt->getText() );
52
53 if ( 0 == $id ) {
54 $ul = $nt->getText();
55 } else {
56 $ul = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
57 $userCond = '=' . $id;
58 }
59 $talk = $nt->getTalkPage();
60 if( $talk ) {
61 $ul .= ' (' . $sk->makeLinkObj( $talk, $wgLang->getNsText(Namespace::getTalk(0)) ) . ')';
62 }
63
64
65 if ( $target == 'newbies' ) {
66 # View the contributions of all recently created accounts
67 $max = $dbr->selectField( 'user', 'max(user_id)', false, $fname );
68 $userCond = '>' . ($max - $max / 100);
69 $ul = wfMsg ( 'newbies' );
70 $id = 0;
71 }
72
73 $wgOut->setSubtitle( wfMsg( 'contribsub', $ul ) );
74
75 if ( $hideminor ) {
76 $cmq = 'AND cur_minor_edit=0';
77 $omq = 'AND old_minor_edit=0';
78 $mlink = $sk->makeKnownLink( $wgContLang->specialPage( 'Contributions' ),
79 WfMsg( 'show' ), "target=" . htmlspecialchars( $nt->getPrefixedURL() ) .
80 "&offset={$offset}&limit={$limit}&hideminor=0&namespace={$namespace}" );
81 } else {
82 $cmq = $omq = '';
83 $mlink = $sk->makeKnownLink( $wgContLang->specialPage( "Contributions" ),
84 WfMsg( 'hide' ), 'target=' . htmlspecialchars( $nt->getPrefixedURL() ) .
85 "&offset={$offset}&limit={$limit}&hideminor=1&namespace={$namespace}" );
86 }
87
88 if( !is_null($namespace) ) {
89 $cmq .= " AND cur_namespace = {$namespace}";
90 $omq .= " AND old_namespace = {$namespace}";
91 }
92
93 extract( $dbr->tableNames( 'old', 'cur' ) );
94 if ( $userCond == '' ) {
95 # We may have to force the index, as some options will cause
96 # MySQL to incorrectly pick eg the namespace index.
97 list( $useIndex, $tailOpts ) = $dbr->makeSelectOptions( array(
98 'USE INDEX' => 'usertext_timestamp',
99 'LIMIT' => $querylimit ) );
100
101 $sql = "SELECT cur_namespace,cur_title,cur_timestamp,cur_comment,cur_minor_edit,cur_is_new,cur_user_text FROM $cur $useIndex " .
102 "WHERE cur_user_text='" . $dbr->strencode( $nt->getText() ) . "' {$cmq} " .
103 "ORDER BY inverse_timestamp $tailOpts";
104 $res1 = $dbr->query( $sql, $fname );
105
106 $sql = "SELECT old_namespace,old_title,old_timestamp,old_comment,old_minor_edit,old_user_text,old_id FROM $old $useIndex " .
107 "WHERE old_user_text='" . $dbr->strencode( $nt->getText() ) . "' {$omq} " .
108 "ORDER BY inverse_timestamp $tailOpts";
109 $res2 = $dbr->query( $sql, $fname );
110 } else {
111 list( $useIndex, $tailOpts ) = $dbr->makeSelectOptions( array(
112 'USE INDEX' => 'user_timestamp',
113 'LIMIT' => $querylimit ) );
114
115 $sql = "SELECT cur_namespace,cur_title,cur_timestamp,cur_comment,cur_minor_edit,cur_is_new,cur_user_text FROM $cur $useIndex " .
116 "WHERE cur_user {$userCond} {$cmq} ORDER BY inverse_timestamp $tailOpts";
117 $res1 = $dbr->query( $sql, $fname );
118
119 $sql = "SELECT old_namespace,old_title,old_timestamp,old_comment,old_minor_edit,old_user_text,old_id FROM $old $useIndex " .
120 "WHERE old_user {$userCond} {$omq} ORDER BY inverse_timestamp $tailOpts";
121 $res2 = $dbr->query( $sql, $fname );
122 }
123 $nCur = $dbr->numRows( $res1 );
124 $nOld = $dbr->numRows( $res2 );
125
126 $wgOut->addHTML( namespaceForm( $target, $hideminor, $namespace ) );
127
128 $top = wfShowingResults( $offset, $limit );
129 $wgOut->addHTML( "<p>{$top}\n" );
130
131 $sl = wfViewPrevNext( $offset, $limit,
132 $wgContLang->specialpage( 'Contributions' ),
133 "hideminor={$hideminor}&namespace={$namespace}&target=" . wfUrlEncode( $target ),
134 ($nCur + $nOld) <= $offlimit);
135
136 $shm = wfMsg( 'showhideminor', $mlink );
137 $wgOut->addHTML( "<br />{$sl} ($shm)</p>\n");
138
139
140 if ( 0 == $nCur && 0 == $nOld ) {
141 $wgOut->addHTML( "\n<p>" . wfMsg( 'nocontribs' ) . "</p>\n" );
142 return;
143 }
144 if ( 0 != $nCur ) { $obj1 = $dbr->fetchObject( $res1 ); }
145 if ( 0 != $nOld ) { $obj2 = $dbr->fetchObject( $res2 ); }
146
147 $wgOut->addHTML( "<ul>\n" );
148 for( $n = 0; $n < $offlimit; $n++ ) {
149 if ( 0 == $nCur && 0 == $nOld ) { break; }
150
151 if ( ( 0 == $nOld ) ||
152 ( ( 0 != $nCur ) &&
153 ( $obj1->cur_timestamp >= $obj2->old_timestamp ) ) ) {
154 $ns = $obj1->cur_namespace;
155 $t = $obj1->cur_title;
156 $ts = $obj1->cur_timestamp;
157 $comment =$obj1->cur_comment;
158 $me = $obj1->cur_minor_edit;
159 $isnew = $obj1->cur_is_new;
160 $usertext = $obj1->cur_user_text;
161
162 $obj1 = $dbr->fetchObject( $res1 );
163 $topmark = true;
164 $oldid = false;
165 --$nCur;
166 } else {
167 $ns = $obj2->old_namespace;
168 $t = $obj2->old_title;
169 $ts = $obj2->old_timestamp;
170 $comment =$obj2->old_comment;
171 $me = $obj2->old_minor_edit;
172 $usertext = $obj2->old_user_text;
173 $oldid = $obj2->old_id;
174
175 $obj2 = $dbr->fetchObject( $res2 );
176 $topmark = false;
177 $isnew = false;
178 --$nOld;
179 }
180 if( $n >= $offset )
181 ucListEdit( $sk, $ns, $t, $ts, $topmark, $comment, ( $me > 0), $isnew, $usertext, $oldid );
182 }
183 $wgOut->addHTML( "</ul>\n" );
184
185 # Validations
186 global $wgUseValidation;
187 if( $wgUseValidation ) {
188 require_once( 'SpecialValidate.php' );
189 $val = new Validation ;
190 $val = $val->countUserValidations ( $id ) ;
191 $wgOut->addHTML( wfMsg ( 'val_user_validations', $val ) );
192 }
193
194 $wgOut->addHTML( "<br />{$sl} ($shm)</p>\n");
195 }
196
197
198 /**
199 * Generates each row in the contributions list.
200 *
201 * Contributions which are marked "top" are currently on top of the history.
202 * For these contributions, a [rollback] link is shown for users with sysop
203 * privileges. The rollback link restores the most recent version that was not
204 * written by the target user.
205 *
206 * If the contributions page is called with the parameter &bot=1, all rollback
207 * links also get that parameter. It causes the edit itself and the rollback
208 * to be marked as "bot" edits. Bot edits are hidden by default from recent
209 * changes, so this allows sysops to combat a busy vandal without bothering
210 * other users.
211 *
212 * @todo This would probably look a lot nicer in a table.
213 */
214 function ucListEdit( $sk, $ns, $t, $ts, $topmark, $comment, $isminor, $isnew, $target, $oldid ) {
215 $fname = 'ucListEdit';
216 wfProfileIn( $fname );
217
218 global $wgLang, $wgOut, $wgUser, $wgRequest;
219 static $messages;
220 if( !isset( $messages ) ) {
221 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist minoreditletter' ) as $msg ) {
222 $messages[$msg] = wfMsg( $msg );
223 }
224 }
225
226 $page =& Title::makeTitle( $ns, $t );
227 $link = $sk->makeKnownLinkObj( $page, '' );
228 $difftext = $topmarktext = '';
229 if($topmark) {
230 $topmarktext .= '<strong>' . $messages['uctop'] . '</strong>';
231 if(!$isnew) {
232 $difftext .= $sk->makeKnownLinkObj( $page, '(' . $messages['diff'] . ')', 'diff=0' );
233 } else {
234 $difftext .= $messages['newarticle'];
235 }
236
237 if( $wgUser->isAllowed('rollback') ) {
238 $extraRollback = $wgRequest->getBool( 'bot' ) ? '&bot=1' : '';
239 # $target = $wgRequest->getText( 'target' );
240 $topmarktext .= ' ['. $sk->makeKnownLinkObj( $page,
241 $messages['rollbacklink'],
242 'action=rollback&from=' . urlencode( $target ) . $extraRollback ) .']';
243 }
244
245 }
246 if ( $oldid ) {
247 $difftext= $sk->makeKnownLinkObj( $page, '(' . $messages['diff'].')', 'diff=prev&oldid='.$oldid );
248 }
249 $histlink='('.$sk->makeKnownLinkObj( $page, $messages['hist'], 'action=history' ) . ')';
250
251 if( $comment ) {
252 $comment = '<em>(' . $sk->formatComment( $comment, $page ) . ')</em> ';
253 }
254 $d = $wgLang->timeanddate( $ts, true );
255
256 if ($isminor) {
257 $mflag = '<span class="minor">' . $messages['minoreditletter'] . '</span> ';
258 } else {
259 $mflag = '';
260 }
261
262 $wgOut->addHTML( "<li>{$d} {$histlink} {$difftext} {$mflag} {$link} {$comment} {$topmarktext}</li>\n" );
263 wfProfileOut( $fname );
264 }
265
266 /**
267 *
268 */
269 function ucCountLink( $lim, $d ) {
270 global $wgUser, $wgContLang, $wgRequest;
271
272 $target = $wgRequest->getText( 'target' );
273 $sk = $wgUser->getSkin();
274 $s = $sk->makeKnownLink( $wgContLang->specialPage( "Contributions" ),
275 "{$lim}", "target={$target}&days={$d}&limit={$lim}" );
276 return $s;
277 }
278
279 /**
280 *
281 */
282 function ucDaysLink( $lim, $d ) {
283 global $wgUser, $wgContLang, $wgRequest;
284
285 $target = $wgRequest->getText( 'target' );
286 $sk = $wgUser->getSkin();
287 $s = $sk->makeKnownLink( $wgContLang->specialPage( 'Contributions' ),
288 "{$d}", "target={$target}&days={$d}&limit={$lim}" );
289 return $s;
290 }
291
292 /**
293 * Generates a form used to restrict display of contributions
294 * to a specific namespace
295 *
296 * @return none
297 * @param string $target target user to show contributions for
298 * @param string $hideminor whether minor contributions are hidden
299 * @param string $namespace currently selected namespace, NULL for show all
300 */
301 function namespaceForm ( $target, $hideminor, $namespace ) {
302 global $wgContLang, $wgScript;
303
304 $namespaceselect = '<form><select name="namespace">';
305 $namespaceselect .= '<option value="" '.(is_null($namespace) ? ' selected="selected"' : '').'>'.wfMsg( 'all' ).'</option>';
306 $arr = $wgContLang->getNamespaces();
307 foreach( array_keys( $arr ) as $i ) {
308 if( $i < 0 ) {
309 continue;
310 }
311 $namespacename = str_replace ( "_", " ", $arr[$i] );
312 $n = ($i == 0) ? wfMsg ( 'articlenamespace' ) : $namespacename;
313 $sel = ($i === $namespace) ? ' selected="selected"' : '';
314 $namespaceselect .= "<option value='{$i}'{$sel}>{$n}</option>";
315 }
316 $namespaceselect .= '</select>';
317
318 $submitbutton = '<input type="submit" value="' . wfMsg( 'allpagessubmit' ) . '" />';
319
320 $out = "<div class='namespaceselector'><form method='get' action='{$wgScript}'>";
321 $out .= '<input type="hidden" name="title" value="'.$wgContLang->specialpage( 'Contributions' ).'" />';
322 $out .= '<input type="hidden" name="target" value="'.htmlspecialchars( $target ).'" />';
323 $out .= '<input type="hidden" name="hideminor" value="'.$hideminor.'" />';
324 $out .= wfMsg ( 'allpagesformtext2', $namespaceselect, $submitbutton );
325 $out .= '</form></div>';
326 return $out;
327 }
328 ?>