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