USE INDEX -> DB::useIndexClause()
[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 $minorQuery = "AND rev_minor_edit=0";
77 $mlink = $sk->makeKnownLink( $wgContLang->specialPage( "Contributions" ),
78 WfMsg( "show" ), "target=" . htmlspecialchars( $nt->getPrefixedURL() ) .
79 "&offset={$offset}&limit={$limit}&hideminor=0&namespace={$namespace}" );
80 } else {
81 $minorQuery = "";
82 $mlink = $sk->makeKnownLink( $wgContLang->specialPage( "Contributions" ),
83 WfMsg( 'hide' ), 'target=' . htmlspecialchars( $nt->getPrefixedURL() ) .
84 "&offset={$offset}&limit={$limit}&hideminor=1&namespace={$namespace}" );
85 }
86
87 if( !is_null($namespace) ) {
88 $minorQuery .= " AND page_namespace = {$namespace}";
89 }
90
91 extract( $dbr->tableNames( 'page', 'revision' ) );
92 if ( $userCond == "" ) {
93 $condition = "rev_user_text=" . $dbr->addQuotes( $nt->getText() );
94 $index = 'usertext_timestamp';
95 } else {
96 $condition = "rev_user {$userCond}";
97 $index = 'user_timestamp';
98 }
99
100
101 $use_index = $dbr->useIndexClause( $index );
102 $sql = "SELECT
103 page_namespace,page_title,page_is_new,page_latest,
104 rev_id,rev_timestamp,rev_comment,rev_minor_edit,rev_user_text
105 FROM $page,$revision $use_index
106 WHERE page_id=rev_page AND $condition $minorQuery " .
107 "ORDER BY inverse_timestamp LIMIT {$querylimit}";
108 $res = $dbr->query( $sql, $fname );
109 $numRows = $dbr->numRows( $res );
110
111 $wgOut->addHTML( namespaceForm( $target, $hideminor, $namespace ) );
112
113 $top = wfShowingResults( $offset, $limit );
114 $wgOut->addHTML( "<p>{$top}\n" );
115
116 $sl = wfViewPrevNext( $offset, $limit,
117 $wgContLang->specialpage( "Contributions" ),
118 "hideminor={$hideminor}&namespace={$namespace}&target=" . wfUrlEncode( $target ),
119 ($numRows) <= $offlimit);
120
121 $shm = wfMsg( "showhideminor", $mlink );
122 $wgOut->addHTML( "<br />{$sl} ($shm)</p>\n");
123
124
125 if ( 0 == $numRows ) {
126 $wgOut->addHTML( "\n<p>" . wfMsg( "nocontribs" ) . "</p>\n" );
127 return;
128 }
129
130 $wgOut->addHTML( "<ul>\n" );
131 while( $obj = $dbr->fetchObject( $res ) ) {
132 ucListEdit( $sk,
133 $obj->page_namespace,
134 $obj->page_title,
135 $obj->rev_timestamp,
136 ($obj->rev_id == $obj->page_latest),
137 $obj->rev_comment,
138 ($obj->rev_minor_edit),
139 $obj->page_is_new,
140 $obj->rev_user_text,
141 $obj->rev_id );
142 }
143 $wgOut->addHTML( "</ul>\n" );
144
145 # Validations
146 global $wgUseValidation;
147 if( $wgUseValidation ) {
148 require_once( 'SpecialValidate.php' );
149 $val = new Validation ;
150 $val = $val->countUserValidations ( $id ) ;
151 $wgOut->addHTML( wfMsg ( 'val_user_validations', $val ) );
152 }
153
154 $wgOut->addHTML( "<br />{$sl} ($shm)\n");
155 }
156
157
158 /**
159 * Generates each row in the contributions list.
160 *
161 * Contributions which are marked "top" are currently on top of the history.
162 * For these contributions, a [rollback] link is shown for users with sysop
163 * privileges. The rollback link restores the most recent version that was not
164 * written by the target user.
165 *
166 * If the contributions page is called with the parameter &bot=1, all rollback
167 * links also get that parameter. It causes the edit itself and the rollback
168 * to be marked as "bot" edits. Bot edits are hidden by default from recent
169 * changes, so this allows sysops to combat a busy vandal without bothering
170 * other users.
171 *
172 * @todo This would probably look a lot nicer in a table.
173 */
174 function ucListEdit( $sk, $ns, $t, $ts, $topmark, $comment, $isminor, $isnew, $target, $oldid ) {
175 $fname = 'ucListEdit';
176 wfProfileIn( $fname );
177
178 global $wgLang, $wgOut, $wgUser, $wgRequest;
179 static $messages;
180 if( !isset( $messages ) ) {
181 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist minoreditletter' ) as $msg ) {
182 $messages[$msg] = wfMsg( $msg );
183 }
184 }
185
186 $page =& Title::makeTitle( $ns, $t );
187 $link = $sk->makeKnownLinkObj( $page, '' );
188 $difftext = $topmarktext = '';
189 if($topmark) {
190 $topmarktext .= '<strong>' . $messages['uctop'] . '</strong>';
191 if(!$isnew) {
192 $difftext .= $sk->makeKnownLinkObj( $page, '(' . $messages['diff'] . ')', 'diff=0' );
193 } else {
194 $difftext .= $messages['newarticle'];
195 }
196
197 if( $wgUser->isAllowed('rollback') ) {
198 $extraRollback = $wgRequest->getBool( 'bot' ) ? '&bot=1' : '';
199 # $target = $wgRequest->getText( 'target' );
200 $topmarktext .= ' ['. $sk->makeKnownLinkObj( $page,
201 $messages['rollbacklink'],
202 'action=rollback&from=' . urlencode( $target ) . $extraRollback ) .']';
203 }
204
205 }
206 if ( $oldid ) {
207 $difftext= $sk->makeKnownLinkObj( $page, '(' . $messages['diff'].')', 'diff=prev&oldid='.$oldid );
208 }
209 $histlink='('.$sk->makeKnownLinkObj( $page, $messages['hist'], 'action=history' ) . ')';
210
211 if( $comment ) {
212 $comment = '<em>(' . $sk->formatComment( $comment, $page ) . ')</em> ';
213 }
214 $d = $wgLang->timeanddate( $ts, true );
215
216 if ($isminor) {
217 $mflag = '<span class="minor">' . $messages['minoreditletter'] . '</span> ';
218 } else {
219 $mflag = '';
220 }
221
222 $wgOut->addHTML( "<li>{$d} {$histlink} {$difftext} {$mflag} {$link} {$comment} {$topmarktext}</li>\n" );
223 wfProfileOut( $fname );
224 }
225
226 /**
227 *
228 */
229 function ucCountLink( $lim, $d ) {
230 global $wgUser, $wgContLang, $wgRequest;
231
232 $target = $wgRequest->getText( 'target' );
233 $sk = $wgUser->getSkin();
234 $s = $sk->makeKnownLink( $wgContLang->specialPage( "Contributions" ),
235 "{$lim}", "target={$target}&days={$d}&limit={$lim}" );
236 return $s;
237 }
238
239 /**
240 *
241 */
242 function ucDaysLink( $lim, $d ) {
243 global $wgUser, $wgContLang, $wgRequest;
244
245 $target = $wgRequest->getText( 'target' );
246 $sk = $wgUser->getSkin();
247 $s = $sk->makeKnownLink( $wgContLang->specialPage( 'Contributions' ),
248 "{$d}", "target={$target}&days={$d}&limit={$lim}" );
249 return $s;
250 }
251
252 /**
253 * Generates a form used to restrict display of contributions
254 * to a specific namespace
255 *
256 * @return none
257 * @param string $target target user to show contributions for
258 * @param string $hideminor whether minor contributions are hidden
259 * @param string $namespace currently selected namespace, NULL for show all
260 */
261 function namespaceForm ( $target, $hideminor, $namespace ) {
262 global $wgContLang, $wgScript;
263
264 $namespaceselect = '<select name="namespace">';
265 $namespaceselect .= '<option value="" '.(is_null($namespace) ? ' selected="selected"' : '').'>'.wfMsg( 'all' ).'</option>';
266 $arr = $wgContLang->getNamespaces();
267 foreach( array_keys( $arr ) as $i ) {
268 if( $i < 0 ) {
269 continue;
270 }
271 $namespacename = str_replace ( "_", " ", $arr[$i] );
272 $n = ($i == 0) ? wfMsg ( 'articlenamespace' ) : $namespacename;
273 $sel = ($i === $namespace) ? ' selected="selected"' : '';
274 $namespaceselect .= "<option value='{$i}'{$sel}>{$n}</option>";
275 }
276 $namespaceselect .= '</select>';
277
278 $submitbutton = '<input type="submit" value="' . wfMsg( 'allpagessubmit' ) . '" />';
279
280 $out = "<div class='namespaceselector'><form method='get' action='{$wgScript}'>";
281 $out .= '<input type="hidden" name="title" value="'.$wgContLang->specialpage( 'Contributions' ).'" />';
282 $out .= '<input type="hidden" name="target" value="'.htmlspecialchars( $target ).'" />';
283 $out .= '<input type="hidden" name="hideminor" value="'.$hideminor.'" />';
284 $out .= wfMsg ( 'allpagesformtext2', $namespaceselect, $submitbutton );
285 $out .= '</form></div>';
286 return $out;
287 }
288 ?>