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