honouring page metadata is too slow for this query
[lhc/web/wiklou.git] / includes / SpecialContributions.php
1 <?php
2 /**
3 * @package MediaWiki
4 * @subpackage SpecialPage
5 */
6
7 class contribs_finder {
8 var $username, $offset, $limit, $namespace, $invert;
9 var $dbr;
10
11 function contribs_finder($username) {
12 $this->username = $username;
13 $this->dbr =& wfGetDB(DB_SLAVE);
14 }
15
16 function set_limit($limit) {
17 $this->limit = $limit;
18 }
19
20 function set_offset($offset) {
21 $this->offset = $offset;
22 }
23
24 function set_namespace($namespace, $invert = false) {
25 $this->namespace = $namespace;
26 $this->invert = $invert;
27 }
28
29 function set_hide_minor($hm) {
30 $this->hide_minor = $hm;
31 }
32
33 function get_edit_limits() {
34 list($index, $usercond) = $this->get_user_cond();
35
36 $use_index = $this->dbr->useIndexClause($index);
37 #$sql = "SELECT MIN(rev_timestamp) as earliest, MAX(rev_timestamp) as latest " .
38 # "FROM page, revision $use_index WHERE page_id = rev_page " .
39 # "AND ";
40
41 #$sql .= $usercond;
42 #$sql .= $this->get_namespace_cond();
43 #$sql .= $this->get_minor_cond();
44 $sql = "SELECT MIN(rev_timestamp) as earliest, MAX(rev_timestamp) as latest " .
45 "FROM revision $use_index WHERE " . $usercond;
46
47 $res = $this->dbr->query($sql, "contribs_finder::get_edit_limits");
48 $rows = array();
49 while ($o = $this->dbr->fetchObject($res))
50 $rows[] = $o;
51 $row = $rows[count($rows) - 1];
52 return array($row->earliest, $row->latest);
53 }
54
55 function get_user_cond() {
56 $condition = "";
57
58 if ($this->username == 'newbies') {
59 $max = $this->dbr->selectField('user', 'max(user_id)', false, "make_sql");
60 $condition = '>' . ($max - $max / 100);
61 }
62
63 if ($condition == "") {
64 $condition = " rev_user_text=" . $this->dbr->addQuotes($this->username);
65 $index = 'usertext_timestamp';
66 } else {
67 $condition = " rev_user {$condition}";
68 $index = 'user_timestamp';
69 }
70
71 return array($index, $condition);
72 }
73
74 function get_minor_cond() {
75 if ($this->hide_minor)
76 return ' AND rev_minor_edit=0';
77 return '';
78 }
79
80 function get_namespace_cond() {
81 $nsQuery = $nsinvert = "";
82
83 if ($this->invert)
84 $nsinvert = "!";
85
86 if (!is_null($this->namespace))
87 $nsQuery .= "AND page_namespace {$nsinvert}= {$this->namespace}";
88
89 return $nsQuery;
90 }
91
92 function get_previous_offset_for_paging() {
93 list($index, $usercond) = $this->get_user_cond();
94 $use_index = $this->dbr->useIndexClause($index);
95
96 $sql = "SELECT rev_timestamp FROM page, revision $use_index " .
97 "WHERE page_id = rev_page AND rev_timestamp > '" . $this->offset . "' AND " .
98 "rev_user_text = " . $this->dbr->addQuotes($this->username);
99 $sql .= $this->get_namespace_cond();
100 $sql .= $this->get_minor_cond();
101 $sql .= " ORDER BY rev_timestamp ASC LIMIT " . $this->limit;
102 $res = $this->dbr->query($sql);
103 $rows = array();
104 while ($obj = $this->dbr->fetchObject($res))
105 $rows[] = $obj;
106 $this->dbr->freeResult($res);
107 return $rows[count($rows) - 1]->rev_timestamp;
108 }
109
110 function get_first_offset_for_paging() {
111 list($index, $usercond) = $this->get_user_cond();
112 $use_index = $this->dbr->useIndexClause($index);
113
114 $sql = "SELECT rev_timestamp FROM page, revision $use_index " .
115 "WHERE page_id = rev_page AND " .
116 "rev_user_text = " . $this->dbr->addQuotes($this->username);
117 $sql .= $this->get_namespace_cond();
118 $sql .= $this->get_minor_cond();
119 $sql .= " ORDER BY rev_timestamp ASC LIMIT " . ($this->limit + 1);
120 $res = $this->dbr->query($sql);
121 $rows = array();
122 while ($obj = $this->dbr->fetchObject($res))
123 $rows[] = $obj;
124 $this->dbr->freeResult($res);
125 return $rows[count($rows) - 1]->rev_timestamp;
126 }
127
128 /* private */ function make_sql() {
129 $userCond = $condition = $index = $minorQuery = $nsQuery
130 = $offsetQuery = $limitQuery = $nsinvert = "";
131
132 $minorQuery = $this->get_minor_cond();
133 $nsQuery = $this->get_namespace_cond();
134
135 extract($this->dbr->tableNames('page', 'revision'));
136 list($index, $userCond) = $this->get_user_cond();
137
138 $limitQuery = "LIMIT {$this->limit}";
139 if ($this->offset)
140 $offsetQuery = "AND rev_timestamp < '{$this->offset}'";
141
142 $use_index = $this->dbr->useIndexClause($index);
143 $sql = "SELECT
144 page_namespace,page_title,page_is_new,page_latest,
145 rev_id,rev_timestamp,rev_comment,rev_minor_edit,rev_user_text,
146 rev_deleted
147 FROM $page,$revision $use_index
148 WHERE page_id=rev_page AND $userCond $minorQuery $nsQuery $offsetQuery
149 ORDER BY rev_timestamp DESC $limitQuery";
150 return $sql;
151 }
152
153 function find() {
154 $contribs = array();
155 $res = $this->dbr->query($this->make_sql(), "contribs_finder::find");
156 while ($c = $this->dbr->fetchObject($res))
157 $contribs[] = $c;
158 $this->dbr->freeResult($res);
159 return $contribs;
160 }
161 };
162
163 /**
164 * Special page "user contributions".
165 * Shows a list of the contributions of a user.
166 *
167 * @return none
168 * @param string $par (optional) user name of the user for which to show the contributions
169 */
170 function wfSpecialContributions( $par = null ) {
171 global $wgUser, $wgOut, $wgLang, $wgContLang, $wgRequest, $wgTitle;
172 $fname = 'wfSpecialContributions';
173
174 $target = isset($par) ? $par : $wgRequest->getVal( 'target' );
175 if (!strlen($target)) {
176 $wgOut->errorpage('notargettitle', 'notargettext');
177 return;
178 }
179
180 $nt = Title::newFromURL( $target );
181 if (!$nt) {
182 $wgOut->errorpage( 'notargettitle', 'notargettext' );
183 return;
184 }
185 $nt =& Title::makeTitle(NS_USER, $nt->getDBkey());
186
187 $namespace = $wgRequest->getIntOrNull('namespace');
188 $invert = $wgRequest->getBool('invert');
189 $hideminor = $wgRequest->getBool('hideminor');
190 $limit = min($wgRequest->getInt('limit', 50), 500);
191 $offset = $wgRequest->getVal('offset');
192 /* Offset must be an integral. */
193 if (!strlen($offset) || !preg_match("/^[0-9]+$/", $offset))
194 $offset = 0;
195
196 $title = Title::makeTitle(NS_SPECIAL, "Contributions");
197 $urlbits = "hideminor=$hideminor&namespace=$namespace&invert=$invert&target=" . wfUrlEncode($target);
198 $myurl = $title->escapeLocalURL($urlbits);
199
200 $finder = new contribs_finder($nt->getText());
201
202 $finder->set_namespace($namespace, $invert);
203 $finder->set_hide_minor($hideminor);
204 $finder->set_limit($limit);
205 $finder->set_offset($offset);
206
207 if ($wgRequest->getText('go') == "prev") {
208 $prevts = $finder->get_previous_offset_for_paging();
209 $prevurl = $title->getLocalURL($urlbits . "&offset=$prevts&limit=$limit");
210 $wgOut->redirect($prevurl);
211 return;
212 }
213
214 if ($wgRequest->getText('go') == "first") {
215 $prevts = $finder->get_first_offset_for_paging();
216 $prevurl = $title->getLocalURL($urlbits . "&offset=$prevts&limit=$limit");
217 $wgOut->redirect($prevurl);
218 return;
219 }
220
221 $sk = $wgUser->getSkin();
222
223 $id = User::idFromName($nt->getText());
224
225 if ( 0 == $id ) {
226 $ul = $nt->getText();
227 } else {
228 $ul = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
229 $userCond = '=' . $id;
230 }
231 $talk = $nt->getTalkPage();
232 if( $talk ) {
233 $ul .= ' (' . $sk->makeLinkObj( $talk, $wgLang->getNsText( NS_TALK ) ) . ')';
234 }
235
236 if ($target == 'newbies') {
237 $ul = wfMsg ('newbies');
238 }
239
240 $wgOut->setSubtitle( wfMsgHtml( 'contribsub', $ul ) );
241
242 $contribsPage = Title::makeTitle( NS_SPECIAL, 'Contributions' );
243 $mlink = $sk->makeKnownLinkObj( $contribsPage,
244 $hideminor ? wfMsgHtml( 'show' ) : wfMsgHtml( 'hide' ),
245 wfArrayToCGI( array(
246 'target' => $nt->getPrefixedDbKey(),
247 'offset' => $offset,
248 'limit' => $limit,
249 'hideminor' => $hideminor ? 0 : 1,
250 'namespace' => $namespace ) ) );
251
252 $contribs = $finder->find();
253
254 if (count($contribs) == 0) {
255 $wgOut->addWikiText( wfMsg( "nocontribs" ) );
256 return;
257 }
258
259 list($early, $late) = $finder->get_edit_limits();
260 $lastts = count($contribs) ? $contribs[count($contribs) - 1]->rev_timestamp : 0;
261 $atstart = (!count($contribs) || $late == $contribs[0]->rev_timestamp);
262 $atend = (!count($contribs) || $early == $lastts);
263 wfdebug("early=$early late=$late lastts=$lastts\n");
264 $wgOut->addHTML(ucNamespaceForm($target, $hideminor, $namespace, $invert));
265
266 $lasturl = $wgTitle->escapeLocalURL("action=history&limit={$limit}");
267
268 $firsttext = wfMsgHtml("histfirst");
269 $lasttext = wfMsgHtml("histlast");
270
271 $prevtext = wfMsg("prevn", $limit);
272 if ($atstart) {
273 $lastlink = $lasttext;
274 $prevlink = $prevtext;
275 } else {
276 $lastlink = "<a href=\"$myurl&amp;limit=$limit\">$lasttext</a>";
277 $prevlink = "<a href=\"$myurl&amp;offset=$offset&amp;limit=$limit&amp;go=prev\">$prevtext</a>";
278 }
279
280 $nexttext = wfMsg("nextn", $limit);
281 if ($atend) {
282 $firstlink = $firsttext;
283 $nextlink = $nexttext;
284 } else {
285 $firstlink = "<a href=\"$myurl&amp;limit=$limit&;go=first\">$firsttext</a>";
286 $nextlink = "<a href=\"$myurl&amp;offset=$lastts&amp;limit=$limit\">$nexttext</a>";
287 }
288 $firstlast = "($lastlink | $firstlink)";
289
290 $urls = array();
291 foreach (array(20, 50, 100, 250, 500) as $num)
292 $urls[] = "<a href=\"$myurl&offset=$offset&limit={$num}\">".$wgLang->formatNum($num)."</a>";
293 $bits = implode($urls, ' | ');
294
295 $prevnextbits = "$firstlast " . wfMsgHtml("viewprevnext", $prevlink, $nextlink, $bits);
296
297 $shm = wfMsgHtml( "contribs-showhideminor", $mlink );
298 $wgOut->addHTML( "<br />{$prevnextbits} ($shm)</p>\n");
299
300 $wgOut->addHTML( "<ul>\n" );
301
302 foreach ($contribs as $contrib)
303 $wgOut->addHTML(ucListEdit($sk, $contrib));
304
305 $wgOut->addHTML( "</ul>\n" );
306 $wgOut->addHTML( "<br />{$prevnextbits} ($shm)\n");
307 }
308
309
310 /**
311 * Generates each row in the contributions list.
312 *
313 * Contributions which are marked "top" are currently on top of the history.
314 * For these contributions, a [rollback] link is shown for users with sysop
315 * privileges. The rollback link restores the most recent version that was not
316 * written by the target user.
317 *
318 * If the contributions page is called with the parameter &bot=1, all rollback
319 * links also get that parameter. It causes the edit itself and the rollback
320 * to be marked as "bot" edits. Bot edits are hidden by default from recent
321 * changes, so this allows sysops to combat a busy vandal without bothering
322 * other users.
323 *
324 * @todo This would probably look a lot nicer in a table.
325 */
326 function ucListEdit( $sk, $row ) {
327 $fname = 'ucListEdit';
328 wfProfileIn( $fname );
329
330 global $wgLang, $wgOut, $wgUser, $wgRequest;
331 static $messages;
332 if( !isset( $messages ) ) {
333 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist minoreditletter' ) as $msg ) {
334 $messages[$msg] = wfMsg( $msg );
335 }
336 }
337
338 $page =& Title::makeTitle( $row->page_namespace, $row->page_title );
339 $link = $sk->makeKnownLinkObj( $page, '' );
340 $difftext = $topmarktext = '';
341 if( $row->rev_id == $row->page_latest ) {
342 $topmarktext .= '<strong>' . $messages['uctop'] . '</strong>';
343 if( !$row->page_is_new ) {
344 $difftext .= $sk->makeKnownLinkObj( $page, '(' . $messages['diff'] . ')', 'diff=0' );
345 } else {
346 $difftext .= $messages['newarticle'];
347 }
348
349 if( $wgUser->isAllowed('rollback') ) {
350 $extraRollback = $wgRequest->getBool( 'bot' ) ? '&bot=1' : '';
351 $extraRollback .= '&token=' . urlencode(
352 $wgUser->editToken( array( $page->getPrefixedText(), $row->rev_user_text ) ) );
353 $topmarktext .= ' ['. $sk->makeKnownLinkObj( $page,
354 $messages['rollbacklink'],
355 'action=rollback&from=' . urlencode( $row->rev_user_text ) . $extraRollback ) .']';
356 }
357
358 }
359 if( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) {
360 $difftext = '(' . $messages['diff'] . ')';
361 } else {
362 $difftext = $sk->makeKnownLinkObj( $page, '(' . $messages['diff'].')', 'diff=prev&oldid='.$row->rev_id );
363 }
364 $histlink='('.$sk->makeKnownLinkObj( $page, $messages['hist'], 'action=history' ) . ')';
365
366 $comment = $sk->commentBlock( $row->rev_comment, $page );
367 $d = $wgLang->timeanddate( $row->rev_timestamp, true );
368
369 if( $row->rev_minor_edit ) {
370 $mflag = '<span class="minor">' . $messages['minoreditletter'] . '</span> ';
371 } else {
372 $mflag = '';
373 }
374
375 $ret = "{$d} {$histlink} {$difftext} {$mflag} {$link} {$comment} {$topmarktext}";
376 if( $row->rev_deleted ) {
377 $ret = '<span class="deleted">' . $ret . '</span> ' . htmlspecialchars( wfMsg( 'deletedrev' ) );
378 }
379 $ret = "<li>$ret</li>\n";
380 wfProfileOut( $fname );
381 return $ret;
382 }
383
384 /**
385 * Generates a form used to restrict display of contributions
386 * to a specific namespace
387 *
388 * @return none
389 * @param string $target target user to show contributions for
390 * @param string $hideminor whether minor contributions are hidden
391 * @param string $namespace currently selected namespace, NULL for show all
392 * @param bool $invert inverts the namespace selection on true (default null)
393 */
394 function ucNamespaceForm ( $target, $hideminor, $namespace, $invert ) {
395 global $wgContLang, $wgScript;
396
397 $namespaceselect = "<select name='namespace' id='nsselectbox'>";
398 $namespaceselect .= '<option value="" '.(is_null($namespace) ? ' selected="selected"' : '').'>'.wfMsgHtml( 'contributionsall' ).'</option>';
399 $arr = $wgContLang->getFormattedNamespaces();
400 foreach( $arr as $ns => $name ) {
401 if( $ns < NS_MAIN )
402 continue;
403 $n = $ns === NS_MAIN ? wfMsgHtml( 'blanknamespace' ) : htmlspecialchars( $name );
404 $sel = $namespace == $ns ? ' selected="selected"' : '';
405 $namespaceselect .= "<option value='$ns'$sel>$n</option>";
406 }
407 $namespaceselect .= '</select>';
408
409 $action = htmlspecialchars( $wgScript );
410 $out = "<div class='namespaceselector'><form method='get' action=\"$action\">";
411 $out .= '<input type="hidden" name="title" value="' . htmlspecialchars( $wgContLang->specialpage( 'Contributions' ) ) . '" />';
412 $out .= '<input type="hidden" name="target" value="' . htmlspecialchars( $target ) . '" />';
413 $out .= '<input type="hidden" name="hideminor" value="' . ( $hideminor ? 1 : 0 ) .'" />';
414 $out .= "
415 <div id='nsselect' class='contributions'>
416 <label for='nsselectbox'>" . wfMsgHtml('namespace') . "</label>
417 $namespaceselect
418 <input type='submit' value=\"" . wfMsgHtml( 'allpagessubmit' ) . "\" />
419 <input type='checkbox' name='invert' value='1' id='nsinvert'" . ( $invert ? ' checked="checked"' : '' ) . " />
420 <label for='nsinvert'>" . wfMsgHtml('invert') . "</label>
421 </div>";
422 $out .= '</form></div>';
423 return $out;
424 }
425 ?>