4 * @subpackage SpecialPage
7 /** @package MediaWiki */
8 class contribs_finder
{
9 var $username, $offset, $limit, $namespace;
12 function contribs_finder($username) {
13 $this->username
= $username;
14 $this->namespace = false;
15 $this->dbr
=& wfGetDB(DB_SLAVE
);
18 function set_namespace($ns) {
19 $this->namespace = $ns;
22 function set_limit($limit) {
23 $this->limit
= $limit;
26 function set_offset($offset) {
27 $this->offset
= $offset;
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";
40 $res = $this->dbr
->query($sql, "contribs_finder::get_edit_limit");
41 while ($o = $this->dbr
->fetchObject($res))
43 return $row->rev_timestamp
;
46 function get_edit_limits() {
48 $this->get_edit_limit("ASC"),
49 $this->get_edit_limit("DESC")
53 function get_user_cond() {
56 if ($this->username
== 'newbies') {
57 $max = $this->dbr
->selectField('user', 'max(user_id)', false, "make_sql");
58 $condition = '>' . (int)($max - $max / 100);
61 if ($condition == "") {
62 $condition = " rev_user_text=" . $this->dbr
->addQuotes($this->username
);
63 $index = 'usertext_timestamp';
65 $condition = " rev_user {$condition}";
66 $index = 'user_timestamp';
68 return array($index, $condition);
71 function get_namespace_cond() {
72 if ($this->namespace !== false)
73 return " AND page_namespace = " . (int)$this->namespace;
77 function get_previous_offset_for_paging() {
78 list($index, $usercond) = $this->get_user_cond();
79 $nscond = $this->get_namespace_cond();
81 $use_index = $this->dbr
->useIndexClause($index);
82 extract($this->dbr
->tableNames('page', 'revision'));
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
) .
88 $sql .= " ORDER BY rev_timestamp ASC LIMIT " . ($this->limit+
1);
89 $res = $this->dbr
->query($sql);
91 while ($obj = $this->dbr
->fetchObject($res))
93 $this->dbr
->freeResult($res);
94 return $rows[count($rows) - 1]->rev_timestamp
;
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
) .
106 $sql .= " ORDER BY rev_timestamp ASC LIMIT " . $this->limit
;
107 $res = $this->dbr
->query($sql);
109 while ($obj = $this->dbr
->fetchObject($res))
111 $this->dbr
->freeResult($res);
112 return $rows[count($rows) - 1]->rev_timestamp
;
115 /* private */ function make_sql() {
116 $userCond = $condition = $index = $offsetQuery = $limitQuery = "";
118 extract($this->dbr
->tableNames('page', 'revision'));
119 list($index, $userCond) = $this->get_user_cond();
121 $limitQuery = "LIMIT {$this->limit}";
123 $offsetQuery = "AND rev_timestamp <= '{$this->offset}'";
125 $nscond = $this->get_namespace_cond();
126 $use_index = $this->dbr
->useIndexClause($index);
128 page_namespace,page_title,page_is_new,page_latest,
129 rev_id,rev_timestamp,rev_comment,rev_minor_edit,rev_user_text,
131 FROM $page,$revision $use_index
132 WHERE page_id=rev_page AND $userCond $nscond $offsetQuery
133 ORDER BY rev_timestamp DESC $limitQuery";
139 $res = $this->dbr
->query($this->make_sql(), "contribs_finder::find");
140 while ($c = $this->dbr
->fetchObject($res))
142 $this->dbr
->freeResult($res);
148 * Special page "user contributions".
149 * Shows a list of the contributions of a user.
152 * @param string $par (optional) user name of the user for which to show the contributions
154 function wfSpecialContributions( $par = null ) {
155 global $wgUser, $wgOut, $wgLang, $wgContLang, $wgRequest, $wgTitle,
157 $fname = 'wfSpecialContributions';
159 $target = isset($par) ?
$par : $wgRequest->getVal( 'target' );
160 if (!strlen($target)) {
161 $wgOut->errorpage('notargettitle', 'notargettext');
165 $nt = Title
::newFromURL( $target );
167 $wgOut->errorpage( 'notargettitle', 'notargettext' );
170 $nt =& Title
::makeTitle(NS_USER
, $nt->getDBkey());
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))
178 $title = Title
::makeTitle(NS_SPECIAL
, "Contributions");
179 $urlbits = "target=" . wfUrlEncode($target);
180 $myurl = $title->escapeLocalURL($urlbits);
182 $finder = new contribs_finder(($target == 'newbies') ?
'newbies' : $nt->getText());
184 $finder->set_limit($limit);
185 $finder->set_offset($offset);
187 $nsurl = $xnsurl = "";
188 if (($ns = $wgRequest->getVal('namespace', null)) !== null) {
189 $nsurl = "&namespace=$ns";
190 $xnsurl = htmlspecialchars($nsurl);
191 $finder->set_namespace($ns);
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);
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);
208 $sk = $wgUser->getSkin();
210 $id = User
::idFromName($nt->getText());
213 $ul = $nt->getText();
215 $ul = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
216 $userCond = '=' . $id;
218 $talk = $nt->getTalkPage();
220 $ul .= ' (' . $sk->makeLinkObj( $talk, $wgLang->getNsText( NS_TALK
) ) . ')';
223 if ($target == 'newbies') {
224 $ul = wfMsg ('newbies');
227 $wgOut->setSubtitle( wfMsgHtml( 'contribsub', $ul ) );
229 $arr = $wgContLang->getFormattedNamespaces();
230 $nsform = "<form method='get' action=\"$wgScript\">\n";
231 $nsform .= wfElement("input", array(
234 "value" => $wgTitle->getPrefixedText()));
235 $nsform .= wfElement("input", array(
238 "value" => $offset));
239 $nsform .= wfElement("input", array(
243 $nsform .= wfElement("input", array(
246 "value" => $target));
248 $nsform .= htmlspecialchars(wfMsg('namespace')) . " <select name='namespace'>\n";
249 foreach (array("" => wfMsg('contributionsall')) +
$arr as $nsn => $name) {
252 $name = $nsn!==0 ?
$name : wfMsg('blanknamespace');
253 $nsform .= ("$nsn" == "$ns") ?
255 array("value" => $nsn, "selected" => "selected"),
258 wfElement("option", array("value" => $nsn), $name);
260 $nsform .= "</select>\n";
261 $nsform .= wfElement("input", array(
263 "value" => wfMsg('allpagessubmit')));
264 $nsform .= "</p></form>\n";
266 $wgOut->addHTML($nsform);
268 $contribsPage = Title
::makeTitle( NS_SPECIAL
, 'Contributions' );
269 $contribs = $finder->find();
271 if (count($contribs) == 0) {
272 $wgOut->addWikiText( wfMsg( "nocontribs" ) );
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);
281 $lasturl = $wgTitle->escapeLocalURL("action=history&limit={$limit}");
283 $firsttext = wfMsgHtml("histfirst");
284 $lasttext = wfMsgHtml("histlast");
286 $prevtext = wfMsg("prevn", $limit);
288 $lastlink = $lasttext;
289 $prevlink = $prevtext;
291 $lastlink = "<a href=\"$myurl&limit=$limit$xnsurl\">$lasttext</a>";
292 $prevlink = "<a href=\"$myurl&offset=$offset&limit=$limit$xnsurl&go=prev\">$prevtext</a>";
295 $nexttext = wfMsg("nextn", $limit);
297 $firstlink = $firsttext;
298 $nextlink = $nexttext;
300 $firstlink = "<a href=\"$myurl&limit=$limit$xnsurl&go=first\">$firsttext</a>";
301 $nextlink = "<a href=\"$myurl&offset=$lastts&limit=$limit$xnsurl\">$nexttext</a>";
303 $firstlast = "($lastlink | $firstlink)";
306 foreach (array(20, 50, 100, 250, 500) as $num)
307 $urls[] = "<a href=\"$myurl&offset=$offset&limit={$num}$xnsurl\">".$wgLang->formatNum($num)."</a>";
308 $bits = implode($urls, ' | ');
310 $prevnextbits = "$firstlast " . wfMsgHtml("viewprevnext", $prevlink, $nextlink, $bits);
312 $wgOut->addHTML( "<p>{$prevnextbits}</p>\n");
314 $wgOut->addHTML( "<ul>\n" );
316 foreach ($contribs as $contrib)
317 $wgOut->addHTML(ucListEdit($sk, $contrib));
319 $wgOut->addHTML( "</ul>\n" );
320 $wgOut->addHTML( "<p>{$prevnextbits}</p>\n");
325 * Generates each row in the contributions list.
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.
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
338 * @todo This would probably look a lot nicer in a table.
340 function ucListEdit( $sk, $row ) {
341 $fname = 'ucListEdit';
342 wfProfileIn( $fname );
344 global $wgLang, $wgOut, $wgUser, $wgRequest;
346 if( !isset( $messages ) ) {
347 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist minoreditletter' ) as $msg ) {
348 $messages[$msg] = wfMsg( $msg );
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' );
360 $difftext .= $messages['newarticle'];
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 ) .']';
373 if( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) {
374 $difftext = '(' . $messages['diff'] . ')';
376 $difftext = $sk->makeKnownLinkObj( $page, '(' . $messages['diff'].')', 'diff=prev&oldid='.$row->rev_id
);
378 $histlink='('.$sk->makeKnownLinkObj( $page, $messages['hist'], 'action=history' ) . ')';
380 $comment = $sk->commentBlock( $row->rev_comment
, $page );
381 $d = $wgLang->timeanddate( $row->rev_timestamp
, true );
383 if( $row->rev_minor_edit
) {
384 $mflag = '<span class="minor">' . $messages['minoreditletter'] . '</span> ';
389 $ret = "{$d} {$histlink} {$difftext} {$mflag} {$link} {$comment} {$topmarktext}";
390 if( $row->rev_deleted
) {
391 $ret = '<span class="deleted">' . $ret . '</span> ' . htmlspecialchars( wfMsg( 'deletedrev' ) );
393 $ret = "<li>$ret</li>\n";
394 wfProfileOut( $fname );