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 .= wfMsgHtml('namespace');
250 $nsform .= HTMLnamespaceselector($ns, '');
252 $nsform .= wfElement("input", array(
254 "value" => wfMsg('allpagessubmit')));
255 $nsform .= "</p></form>\n";
257 $wgOut->addHTML($nsform);
259 $contribsPage = Title
::makeTitle( NS_SPECIAL
, 'Contributions' );
260 $contribs = $finder->find();
262 if (count($contribs) == 0) {
263 $wgOut->addWikiText( wfMsg( "nocontribs" ) );
267 list($early, $late) = $finder->get_edit_limits();
268 $lastts = count($contribs) ?
$contribs[count($contribs) - 1]->rev_timestamp
: 0;
269 $atstart = (!count($contribs) ||
$late == $contribs[0]->rev_timestamp
);
270 $atend = (!count($contribs) ||
$early == $lastts);
272 $lasturl = $wgTitle->escapeLocalURL("action=history&limit={$limit}");
274 $firsttext = wfMsgHtml("histfirst");
275 $lasttext = wfMsgHtml("histlast");
277 $prevtext = wfMsg("prevn", $limit);
279 $lastlink = $lasttext;
280 $prevlink = $prevtext;
282 $lastlink = "<a href=\"$myurl&limit=$limit$xnsurl\">$lasttext</a>";
283 $prevlink = "<a href=\"$myurl&offset=$offset&limit=$limit$xnsurl&go=prev\">$prevtext</a>";
286 $nexttext = wfMsg("nextn", $limit);
288 $firstlink = $firsttext;
289 $nextlink = $nexttext;
291 $firstlink = "<a href=\"$myurl&limit=$limit$xnsurl&go=first\">$firsttext</a>";
292 $nextlink = "<a href=\"$myurl&offset=$lastts&limit=$limit$xnsurl\">$nexttext</a>";
294 $firstlast = "($lastlink | $firstlink)";
297 foreach (array(20, 50, 100, 250, 500) as $num)
298 $urls[] = "<a href=\"$myurl&offset=$offset&limit={$num}$xnsurl\">".$wgLang->formatNum($num)."</a>";
299 $bits = implode($urls, ' | ');
301 $prevnextbits = "$firstlast " . wfMsgHtml("viewprevnext", $prevlink, $nextlink, $bits);
303 $wgOut->addHTML( "<p>{$prevnextbits}</p>\n");
305 $wgOut->addHTML( "<ul>\n" );
307 foreach ($contribs as $contrib)
308 $wgOut->addHTML(ucListEdit($sk, $contrib));
310 $wgOut->addHTML( "</ul>\n" );
311 $wgOut->addHTML( "<p>{$prevnextbits}</p>\n");
316 * Generates each row in the contributions list.
318 * Contributions which are marked "top" are currently on top of the history.
319 * For these contributions, a [rollback] link is shown for users with sysop
320 * privileges. The rollback link restores the most recent version that was not
321 * written by the target user.
323 * If the contributions page is called with the parameter &bot=1, all rollback
324 * links also get that parameter. It causes the edit itself and the rollback
325 * to be marked as "bot" edits. Bot edits are hidden by default from recent
326 * changes, so this allows sysops to combat a busy vandal without bothering
329 * @todo This would probably look a lot nicer in a table.
331 function ucListEdit( $sk, $row ) {
332 $fname = 'ucListEdit';
333 wfProfileIn( $fname );
335 global $wgLang, $wgOut, $wgUser, $wgRequest;
337 if( !isset( $messages ) ) {
338 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist minoreditletter' ) as $msg ) {
339 $messages[$msg] = wfMsg( $msg );
343 $page =& Title
::makeTitle( $row->page_namespace
, $row->page_title
);
344 $link = $sk->makeKnownLinkObj( $page, '' );
345 $difftext = $topmarktext = '';
346 if( $row->rev_id
== $row->page_latest
) {
347 $topmarktext .= '<strong>' . $messages['uctop'] . '</strong>';
348 if( !$row->page_is_new
) {
349 $difftext .= $sk->makeKnownLinkObj( $page, '(' . $messages['diff'] . ')', 'diff=0' );
351 $difftext .= $messages['newarticle'];
354 if( $wgUser->isAllowed('rollback') ) {
355 $extraRollback = $wgRequest->getBool( 'bot' ) ?
'&bot=1' : '';
356 $extraRollback .= '&token=' . urlencode(
357 $wgUser->editToken( array( $page->getPrefixedText(), $row->rev_user_text
) ) );
358 $topmarktext .= ' ['. $sk->makeKnownLinkObj( $page,
359 $messages['rollbacklink'],
360 'action=rollback&from=' . urlencode( $row->rev_user_text
) . $extraRollback ) .']';
364 if( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) {
365 $difftext = '(' . $messages['diff'] . ')';
367 $difftext = $sk->makeKnownLinkObj( $page, '(' . $messages['diff'].')', 'diff=prev&oldid='.$row->rev_id
);
369 $histlink='('.$sk->makeKnownLinkObj( $page, $messages['hist'], 'action=history' ) . ')';
371 $comment = $sk->commentBlock( $row->rev_comment
, $page );
372 $d = $wgLang->timeanddate( $row->rev_timestamp
, true );
374 if( $row->rev_minor_edit
) {
375 $mflag = '<span class="minor">' . $messages['minoreditletter'] . '</span> ';
380 $ret = "{$d} {$histlink} {$difftext} {$mflag} {$link} {$comment} {$topmarktext}";
381 if( $row->rev_deleted
) {
382 $ret = '<span class="deleted">' . $ret . '</span> ' . htmlspecialchars( wfMsg( 'deletedrev' ) );
384 $ret = "<li>$ret</li>\n";
385 wfProfileOut( $fname );