4 * @subpackage SpecialPage
7 class contribs_finder
{
8 var $username, $offset, $limit;
11 function contribs_finder($username) {
12 $this->username
= $username;
13 $this->dbr
=& wfGetDB(DB_SLAVE
);
16 function set_limit($limit) {
17 $this->limit
= $limit;
20 function set_offset($offset) {
21 $this->offset
= $offset;
24 function get_edit_limits() {
25 list($index, $usercond) = $this->get_user_cond();
27 $use_index = $this->dbr
->useIndexClause($index);
28 extract($this->dbr
->tableNames('revision'));
29 $sql = "SELECT MIN(rev_timestamp) as earliest, MAX(rev_timestamp) as latest " .
30 "FROM $revision $use_index WHERE " . $usercond;
32 $res = $this->dbr
->query($sql, "contribs_finder::get_edit_limits");
34 while ($o = $this->dbr
->fetchObject($res))
36 $row = $rows[count($rows) - 1];
37 return array($row->earliest
, $row->latest
);
40 function get_user_cond() {
43 if ($this->username
== 'newbies') {
44 $max = $this->dbr
->selectField('user', 'max(user_id)', false, "make_sql");
45 $condition = '>' . ($max - $max / 100);
48 if ($condition == "") {
49 $condition = " rev_user_text=" . $this->dbr
->addQuotes($this->username
);
50 $index = 'usertext_timestamp';
52 $condition = " rev_user {$condition}";
53 $index = 'user_timestamp';
56 return array($index, $condition);
59 function get_previous_offset_for_paging() {
60 list($index, $usercond) = $this->get_user_cond();
61 $use_index = $this->dbr
->useIndexClause($index);
62 extract($this->dbr
->tableNames('page', 'revision'));
64 $sql = "SELECT rev_timestamp FROM $page, $revision $use_index " .
65 "WHERE page_id = rev_page AND rev_timestamp > '" . $this->offset
. "' AND " .
66 "rev_user_text = " . $this->dbr
->addQuotes($this->username
);
67 $sql .= " ORDER BY rev_timestamp ASC LIMIT " . $this->limit
;
68 $res = $this->dbr
->query($sql);
70 while ($obj = $this->dbr
->fetchObject($res))
72 $this->dbr
->freeResult($res);
73 return $rows[count($rows) - 1]->rev_timestamp
;
76 function get_first_offset_for_paging() {
77 list($index, $usercond) = $this->get_user_cond();
78 $use_index = $this->dbr
->useIndexClause($index);
79 extract($this->dbr
->tableNames('page', 'revision'));
81 $sql = "SELECT rev_timestamp FROM $page, $revision $use_index " .
82 "WHERE page_id = rev_page AND " .
83 "rev_user_text = " . $this->dbr
->addQuotes($this->username
);
84 $sql .= " ORDER BY rev_timestamp ASC LIMIT " . ($this->limit +
1);
85 $res = $this->dbr
->query($sql);
87 while ($obj = $this->dbr
->fetchObject($res))
89 $this->dbr
->freeResult($res);
90 return $rows[count($rows) - 1]->rev_timestamp
;
93 /* private */ function make_sql() {
94 $userCond = $condition = $index = $offsetQuery = $limitQuery = "";
96 extract($this->dbr
->tableNames('page', 'revision'));
97 list($index, $userCond) = $this->get_user_cond();
99 $limitQuery = "LIMIT {$this->limit}";
101 $offsetQuery = "AND rev_timestamp < '{$this->offset}'";
103 $use_index = $this->dbr
->useIndexClause($index);
105 page_namespace,page_title,page_is_new,page_latest,
106 rev_id,rev_timestamp,rev_comment,rev_minor_edit,rev_user_text,
108 FROM $page,$revision $use_index
109 WHERE page_id=rev_page AND $userCond $offsetQuery
110 ORDER BY rev_timestamp DESC $limitQuery";
116 $res = $this->dbr
->query($this->make_sql(), "contribs_finder::find");
117 while ($c = $this->dbr
->fetchObject($res))
119 $this->dbr
->freeResult($res);
125 * Special page "user contributions".
126 * Shows a list of the contributions of a user.
129 * @param string $par (optional) user name of the user for which to show the contributions
131 function wfSpecialContributions( $par = null ) {
132 global $wgUser, $wgOut, $wgLang, $wgContLang, $wgRequest, $wgTitle;
133 $fname = 'wfSpecialContributions';
135 $target = isset($par) ?
$par : $wgRequest->getVal( 'target' );
136 if (!strlen($target)) {
137 $wgOut->errorpage('notargettitle', 'notargettext');
141 $nt = Title
::newFromURL( $target );
143 $wgOut->errorpage( 'notargettitle', 'notargettext' );
146 $nt =& Title
::makeTitle(NS_USER
, $nt->getDBkey());
148 $limit = min($wgRequest->getInt('limit', 50), 500);
149 $offset = $wgRequest->getVal('offset');
150 /* Offset must be an integral. */
151 if (!strlen($offset) ||
!preg_match("/^[0-9]+$/", $offset))
154 $title = Title
::makeTitle(NS_SPECIAL
, "Contributions");
155 $urlbits = "target=" . wfUrlEncode($target);
156 $myurl = $title->escapeLocalURL($urlbits);
158 $finder = new contribs_finder($nt->getText());
160 $finder->set_limit($limit);
161 $finder->set_offset($offset);
163 if ($wgRequest->getText('go') == "prev") {
164 $prevts = $finder->get_previous_offset_for_paging();
165 $prevurl = $title->getLocalURL($urlbits . "&offset=$prevts&limit=$limit");
166 $wgOut->redirect($prevurl);
170 if ($wgRequest->getText('go') == "first") {
171 $prevts = $finder->get_first_offset_for_paging();
172 $prevurl = $title->getLocalURL($urlbits . "&offset=$prevts&limit=$limit");
173 $wgOut->redirect($prevurl);
177 $sk = $wgUser->getSkin();
179 $id = User
::idFromName($nt->getText());
182 $ul = $nt->getText();
184 $ul = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
185 $userCond = '=' . $id;
187 $talk = $nt->getTalkPage();
189 $ul .= ' (' . $sk->makeLinkObj( $talk, $wgLang->getNsText( NS_TALK
) ) . ')';
192 if ($target == 'newbies') {
193 $ul = wfMsg ('newbies');
196 $wgOut->setSubtitle( wfMsgHtml( 'contribsub', $ul ) );
198 $contribsPage = Title
::makeTitle( NS_SPECIAL
, 'Contributions' );
199 $contribs = $finder->find();
201 if (count($contribs) == 0) {
202 $wgOut->addWikiText( wfMsg( "nocontribs" ) );
206 list($early, $late) = $finder->get_edit_limits();
207 $lastts = count($contribs) ?
$contribs[count($contribs) - 1]->rev_timestamp
: 0;
208 $atstart = (!count($contribs) ||
$late == $contribs[0]->rev_timestamp
);
209 $atend = (!count($contribs) ||
$early == $lastts);
211 $lasturl = $wgTitle->escapeLocalURL("action=history&limit={$limit}");
213 $firsttext = wfMsgHtml("histfirst");
214 $lasttext = wfMsgHtml("histlast");
216 $prevtext = wfMsg("prevn", $limit);
218 $lastlink = $lasttext;
219 $prevlink = $prevtext;
221 $lastlink = "<a href=\"$myurl&limit=$limit\">$lasttext</a>";
222 $prevlink = "<a href=\"$myurl&offset=$offset&limit=$limit&go=prev\">$prevtext</a>";
225 $nexttext = wfMsg("nextn", $limit);
227 $firstlink = $firsttext;
228 $nextlink = $nexttext;
230 $firstlink = "<a href=\"$myurl&limit=$limit&go=first\">$firsttext</a>";
231 $nextlink = "<a href=\"$myurl&offset=$lastts&limit=$limit\">$nexttext</a>";
233 $firstlast = "($lastlink | $firstlink)";
236 foreach (array(20, 50, 100, 250, 500) as $num)
237 $urls[] = "<a href=\"$myurl&offset=$offset&limit={$num}\">".$wgLang->formatNum($num)."</a>";
238 $bits = implode($urls, ' | ');
240 $prevnextbits = "$firstlast " . wfMsgHtml("viewprevnext", $prevlink, $nextlink, $bits);
242 $wgOut->addHTML( "<p>{$prevnextbits}</p>\n");
244 $wgOut->addHTML( "<ul>\n" );
246 foreach ($contribs as $contrib)
247 $wgOut->addHTML(ucListEdit($sk, $contrib));
249 $wgOut->addHTML( "</ul>\n" );
250 $wgOut->addHTML( "<p>{$prevnextbits}</p>\n");
255 * Generates each row in the contributions list.
257 * Contributions which are marked "top" are currently on top of the history.
258 * For these contributions, a [rollback] link is shown for users with sysop
259 * privileges. The rollback link restores the most recent version that was not
260 * written by the target user.
262 * If the contributions page is called with the parameter &bot=1, all rollback
263 * links also get that parameter. It causes the edit itself and the rollback
264 * to be marked as "bot" edits. Bot edits are hidden by default from recent
265 * changes, so this allows sysops to combat a busy vandal without bothering
268 * @todo This would probably look a lot nicer in a table.
270 function ucListEdit( $sk, $row ) {
271 $fname = 'ucListEdit';
272 wfProfileIn( $fname );
274 global $wgLang, $wgOut, $wgUser, $wgRequest;
276 if( !isset( $messages ) ) {
277 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist minoreditletter' ) as $msg ) {
278 $messages[$msg] = wfMsg( $msg );
282 $page =& Title
::makeTitle( $row->page_namespace
, $row->page_title
);
283 $link = $sk->makeKnownLinkObj( $page, '' );
284 $difftext = $topmarktext = '';
285 if( $row->rev_id
== $row->page_latest
) {
286 $topmarktext .= '<strong>' . $messages['uctop'] . '</strong>';
287 if( !$row->page_is_new
) {
288 $difftext .= $sk->makeKnownLinkObj( $page, '(' . $messages['diff'] . ')', 'diff=0' );
290 $difftext .= $messages['newarticle'];
293 if( $wgUser->isAllowed('rollback') ) {
294 $extraRollback = $wgRequest->getBool( 'bot' ) ?
'&bot=1' : '';
295 $extraRollback .= '&token=' . urlencode(
296 $wgUser->editToken( array( $page->getPrefixedText(), $row->rev_user_text
) ) );
297 $topmarktext .= ' ['. $sk->makeKnownLinkObj( $page,
298 $messages['rollbacklink'],
299 'action=rollback&from=' . urlencode( $row->rev_user_text
) . $extraRollback ) .']';
303 if( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) {
304 $difftext = '(' . $messages['diff'] . ')';
306 $difftext = $sk->makeKnownLinkObj( $page, '(' . $messages['diff'].')', 'diff=prev&oldid='.$row->rev_id
);
308 $histlink='('.$sk->makeKnownLinkObj( $page, $messages['hist'], 'action=history' ) . ')';
310 $comment = $sk->commentBlock( $row->rev_comment
, $page );
311 $d = $wgLang->timeanddate( $row->rev_timestamp
, true );
313 if( $row->rev_minor_edit
) {
314 $mflag = '<span class="minor">' . $messages['minoreditletter'] . '</span> ';
319 $ret = "{$d} {$histlink} {$difftext} {$mflag} {$link} {$comment} {$topmarktext}";
320 if( $row->rev_deleted
) {
321 $ret = '<span class="deleted">' . $ret . '</span> ' . htmlspecialchars( wfMsg( 'deletedrev' ) );
323 $ret = "<li>$ret</li>\n";
324 wfProfileOut( $fname );