4 * @subpackage SpecialPage
7 class contribs_finder
{
8 var $username, $offset, $limit, $namespace, $invert;
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 set_namespace($namespace, $invert = false) {
25 $this->namespace = $namespace;
26 $this->invert
= $invert;
29 function set_hide_minor($hm) {
30 $this->hide_minor
= $hm;
33 function get_edit_limits() {
34 list($index, $usercond) = $this->get_user_cond();
36 $use_index = $this->dbr
->useIndexClause($index);
37 extract($this->dbr
->tableNames('page', 'revision'));
39 #$sql = "SELECT MIN(rev_timestamp) as earliest, MAX(rev_timestamp) as latest " .
40 # "FROM page, revision $use_index WHERE page_id = rev_page " .
44 #$sql .= $this->get_namespace_cond();
45 #$sql .= $this->get_minor_cond();
46 $sql = "SELECT MIN(rev_timestamp) as earliest, MAX(rev_timestamp) as latest " .
47 "FROM $revision $use_index WHERE " . $usercond;
49 $res = $this->dbr
->query($sql, "contribs_finder::get_edit_limits");
51 while ($o = $this->dbr
->fetchObject($res))
53 $row = $rows[count($rows) - 1];
54 return array($row->earliest
, $row->latest
);
57 function get_user_cond() {
60 if ($this->username
== 'newbies') {
61 $max = $this->dbr
->selectField('user', 'max(user_id)', false, "make_sql");
62 $condition = '>' . ($max - $max / 100);
65 if ($condition == "") {
66 $condition = " rev_user_text=" . $this->dbr
->addQuotes($this->username
);
67 $index = 'usertext_timestamp';
69 $condition = " rev_user {$condition}";
70 $index = 'user_timestamp';
73 return array($index, $condition);
76 function get_minor_cond() {
77 if ($this->hide_minor
)
78 return ' AND rev_minor_edit=0';
82 function get_namespace_cond() {
83 $nsQuery = $nsinvert = "";
88 if (!is_null($this->namespace))
89 $nsQuery .= "AND page_namespace {$nsinvert}= {$this->namespace}";
94 function get_previous_offset_for_paging() {
95 list($index, $usercond) = $this->get_user_cond();
96 $use_index = $this->dbr
->useIndexClause($index);
97 extract($this->dbr
->tableNames('page', 'revision'));
99 $sql = "SELECT rev_timestamp FROM $page, $revision $use_index " .
100 "WHERE page_id = rev_page AND rev_timestamp > '" . $this->offset
. "' AND " .
101 "rev_user_text = " . $this->dbr
->addQuotes($this->username
);
102 $sql .= $this->get_namespace_cond();
103 $sql .= $this->get_minor_cond();
104 $sql .= " ORDER BY rev_timestamp ASC LIMIT " . $this->limit
;
105 $res = $this->dbr
->query($sql);
107 while ($obj = $this->dbr
->fetchObject($res))
109 $this->dbr
->freeResult($res);
110 return $rows[count($rows) - 1]->rev_timestamp
;
113 function get_first_offset_for_paging() {
114 list($index, $usercond) = $this->get_user_cond();
115 $use_index = $this->dbr
->useIndexClause($index);
116 extract($this->dbr
->tableNames('page', 'revision'));
118 $sql = "SELECT rev_timestamp FROM $page, $revision $use_index " .
119 "WHERE page_id = rev_page AND " .
120 "rev_user_text = " . $this->dbr
->addQuotes($this->username
);
121 $sql .= $this->get_namespace_cond();
122 $sql .= $this->get_minor_cond();
123 $sql .= " ORDER BY rev_timestamp ASC LIMIT " . ($this->limit +
1);
124 $res = $this->dbr
->query($sql);
126 while ($obj = $this->dbr
->fetchObject($res))
128 $this->dbr
->freeResult($res);
129 return $rows[count($rows) - 1]->rev_timestamp
;
132 /* private */ function make_sql() {
133 $userCond = $condition = $index = $minorQuery = $nsQuery
134 = $offsetQuery = $limitQuery = $nsinvert = "";
136 $minorQuery = $this->get_minor_cond();
137 $nsQuery = $this->get_namespace_cond();
139 extract($this->dbr
->tableNames('page', 'revision'));
140 list($index, $userCond) = $this->get_user_cond();
142 $limitQuery = "LIMIT {$this->limit}";
144 $offsetQuery = "AND rev_timestamp < '{$this->offset}'";
146 $use_index = $this->dbr
->useIndexClause($index);
148 page_namespace,page_title,page_is_new,page_latest,
149 rev_id,rev_timestamp,rev_comment,rev_minor_edit,rev_user_text,
151 FROM $page,$revision $use_index
152 WHERE page_id=rev_page AND $userCond $minorQuery $nsQuery $offsetQuery
153 ORDER BY rev_timestamp DESC $limitQuery";
159 $res = $this->dbr
->query($this->make_sql(), "contribs_finder::find");
160 while ($c = $this->dbr
->fetchObject($res))
162 $this->dbr
->freeResult($res);
168 * Special page "user contributions".
169 * Shows a list of the contributions of a user.
172 * @param string $par (optional) user name of the user for which to show the contributions
174 function wfSpecialContributions( $par = null ) {
175 global $wgUser, $wgOut, $wgLang, $wgContLang, $wgRequest, $wgTitle;
176 $fname = 'wfSpecialContributions';
178 $target = isset($par) ?
$par : $wgRequest->getVal( 'target' );
179 if (!strlen($target)) {
180 $wgOut->errorpage('notargettitle', 'notargettext');
184 $nt = Title
::newFromURL( $target );
186 $wgOut->errorpage( 'notargettitle', 'notargettext' );
189 $nt =& Title
::makeTitle(NS_USER
, $nt->getDBkey());
191 $namespace = $wgRequest->getIntOrNull('namespace');
192 $invert = $wgRequest->getBool('invert');
193 $hideminor = $wgRequest->getBool('hideminor');
194 $limit = min($wgRequest->getInt('limit', 50), 500);
195 $offset = $wgRequest->getVal('offset');
196 /* Offset must be an integral. */
197 if (!strlen($offset) ||
!preg_match("/^[0-9]+$/", $offset))
200 $title = Title
::makeTitle(NS_SPECIAL
, "Contributions");
201 $urlbits = "hideminor=$hideminor&namespace=$namespace&invert=$invert&target=" . wfUrlEncode($target);
202 $myurl = $title->escapeLocalURL($urlbits);
204 $finder = new contribs_finder($nt->getText());
206 $finder->set_namespace($namespace, $invert);
207 $finder->set_hide_minor($hideminor);
208 $finder->set_limit($limit);
209 $finder->set_offset($offset);
211 if ($wgRequest->getText('go') == "prev") {
212 $prevts = $finder->get_previous_offset_for_paging();
213 $prevurl = $title->getLocalURL($urlbits . "&offset=$prevts&limit=$limit");
214 $wgOut->redirect($prevurl);
218 if ($wgRequest->getText('go') == "first") {
219 $prevts = $finder->get_first_offset_for_paging();
220 $prevurl = $title->getLocalURL($urlbits . "&offset=$prevts&limit=$limit");
221 $wgOut->redirect($prevurl);
225 $sk = $wgUser->getSkin();
227 $id = User
::idFromName($nt->getText());
230 $ul = $nt->getText();
232 $ul = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
233 $userCond = '=' . $id;
235 $talk = $nt->getTalkPage();
237 $ul .= ' (' . $sk->makeLinkObj( $talk, $wgLang->getNsText( NS_TALK
) ) . ')';
240 if ($target == 'newbies') {
241 $ul = wfMsg ('newbies');
244 $wgOut->setSubtitle( wfMsgHtml( 'contribsub', $ul ) );
246 $contribsPage = Title
::makeTitle( NS_SPECIAL
, 'Contributions' );
247 $mlink = $sk->makeKnownLinkObj( $contribsPage,
248 $hideminor ?
wfMsgHtml( 'show' ) : wfMsgHtml( 'hide' ),
250 'target' => $nt->getPrefixedDbKey(),
253 'hideminor' => $hideminor ?
0 : 1,
254 'namespace' => $namespace ) ) );
256 $contribs = $finder->find();
258 if (count($contribs) == 0) {
259 $wgOut->addWikiText( wfMsg( "nocontribs" ) );
263 list($early, $late) = $finder->get_edit_limits();
264 $lastts = count($contribs) ?
$contribs[count($contribs) - 1]->rev_timestamp
: 0;
265 $atstart = (!count($contribs) ||
$late == $contribs[0]->rev_timestamp
);
266 $atend = (!count($contribs) ||
$early == $lastts);
267 wfdebug("early=$early late=$late lastts=$lastts\n");
268 $wgOut->addHTML(ucNamespaceForm($target, $hideminor, $namespace, $invert));
270 $lasturl = $wgTitle->escapeLocalURL("action=history&limit={$limit}");
272 $firsttext = wfMsgHtml("histfirst");
273 $lasttext = wfMsgHtml("histlast");
275 $prevtext = wfMsg("prevn", $limit);
277 $lastlink = $lasttext;
278 $prevlink = $prevtext;
280 $lastlink = "<a href=\"$myurl&limit=$limit\">$lasttext</a>";
281 $prevlink = "<a href=\"$myurl&offset=$offset&limit=$limit&go=prev\">$prevtext</a>";
284 $nexttext = wfMsg("nextn", $limit);
286 $firstlink = $firsttext;
287 $nextlink = $nexttext;
289 $firstlink = "<a href=\"$myurl&limit=$limit&go=first\">$firsttext</a>";
290 $nextlink = "<a href=\"$myurl&offset=$lastts&limit=$limit\">$nexttext</a>";
292 $firstlast = "($lastlink | $firstlink)";
295 foreach (array(20, 50, 100, 250, 500) as $num)
296 $urls[] = "<a href=\"$myurl&offset=$offset&limit={$num}\">".$wgLang->formatNum($num)."</a>";
297 $bits = implode($urls, ' | ');
299 $prevnextbits = "$firstlast " . wfMsgHtml("viewprevnext", $prevlink, $nextlink, $bits);
301 $shm = wfMsgHtml( "contribs-showhideminor", $mlink );
302 $wgOut->addHTML( "<br />{$prevnextbits} ($shm)</p>\n");
304 $wgOut->addHTML( "<ul>\n" );
306 foreach ($contribs as $contrib)
307 $wgOut->addHTML(ucListEdit($sk, $contrib));
309 $wgOut->addHTML( "</ul>\n" );
310 $wgOut->addHTML( "<br />{$prevnextbits} ($shm)\n");
315 * Generates each row in the contributions list.
317 * Contributions which are marked "top" are currently on top of the history.
318 * For these contributions, a [rollback] link is shown for users with sysop
319 * privileges. The rollback link restores the most recent version that was not
320 * written by the target user.
322 * If the contributions page is called with the parameter &bot=1, all rollback
323 * links also get that parameter. It causes the edit itself and the rollback
324 * to be marked as "bot" edits. Bot edits are hidden by default from recent
325 * changes, so this allows sysops to combat a busy vandal without bothering
328 * @todo This would probably look a lot nicer in a table.
330 function ucListEdit( $sk, $row ) {
331 $fname = 'ucListEdit';
332 wfProfileIn( $fname );
334 global $wgLang, $wgOut, $wgUser, $wgRequest;
336 if( !isset( $messages ) ) {
337 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist minoreditletter' ) as $msg ) {
338 $messages[$msg] = wfMsg( $msg );
342 $page =& Title
::makeTitle( $row->page_namespace
, $row->page_title
);
343 $link = $sk->makeKnownLinkObj( $page, '' );
344 $difftext = $topmarktext = '';
345 if( $row->rev_id
== $row->page_latest
) {
346 $topmarktext .= '<strong>' . $messages['uctop'] . '</strong>';
347 if( !$row->page_is_new
) {
348 $difftext .= $sk->makeKnownLinkObj( $page, '(' . $messages['diff'] . ')', 'diff=0' );
350 $difftext .= $messages['newarticle'];
353 if( $wgUser->isAllowed('rollback') ) {
354 $extraRollback = $wgRequest->getBool( 'bot' ) ?
'&bot=1' : '';
355 $extraRollback .= '&token=' . urlencode(
356 $wgUser->editToken( array( $page->getPrefixedText(), $row->rev_user_text
) ) );
357 $topmarktext .= ' ['. $sk->makeKnownLinkObj( $page,
358 $messages['rollbacklink'],
359 'action=rollback&from=' . urlencode( $row->rev_user_text
) . $extraRollback ) .']';
363 if( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) {
364 $difftext = '(' . $messages['diff'] . ')';
366 $difftext = $sk->makeKnownLinkObj( $page, '(' . $messages['diff'].')', 'diff=prev&oldid='.$row->rev_id
);
368 $histlink='('.$sk->makeKnownLinkObj( $page, $messages['hist'], 'action=history' ) . ')';
370 $comment = $sk->commentBlock( $row->rev_comment
, $page );
371 $d = $wgLang->timeanddate( $row->rev_timestamp
, true );
373 if( $row->rev_minor_edit
) {
374 $mflag = '<span class="minor">' . $messages['minoreditletter'] . '</span> ';
379 $ret = "{$d} {$histlink} {$difftext} {$mflag} {$link} {$comment} {$topmarktext}";
380 if( $row->rev_deleted
) {
381 $ret = '<span class="deleted">' . $ret . '</span> ' . htmlspecialchars( wfMsg( 'deletedrev' ) );
383 $ret = "<li>$ret</li>\n";
384 wfProfileOut( $fname );
389 * Generates a form used to restrict display of contributions
390 * to a specific namespace
393 * @param string $target target user to show contributions for
394 * @param string $hideminor whether minor contributions are hidden
395 * @param string $namespace currently selected namespace, NULL for show all
396 * @param bool $invert inverts the namespace selection on true (default null)
398 function ucNamespaceForm ( $target, $hideminor, $namespace, $invert ) {
399 global $wgContLang, $wgScript;
401 $namespaceselect = "<select name='namespace' id='nsselectbox'>";
402 $namespaceselect .= '<option value="" '.(is_null($namespace) ?
' selected="selected"' : '').'>'.wfMsgHtml( 'contributionsall' ).'</option>';
403 $arr = $wgContLang->getFormattedNamespaces();
404 foreach( $arr as $ns => $name ) {
407 $n = $ns === NS_MAIN ?
wfMsgHtml( 'blanknamespace' ) : htmlspecialchars( $name );
408 $sel = $namespace == $ns ?
' selected="selected"' : '';
409 $namespaceselect .= "<option value='$ns'$sel>$n</option>";
411 $namespaceselect .= '</select>';
413 $action = htmlspecialchars( $wgScript );
414 $out = "<div class='namespaceselector'><form method='get' action=\"$action\">";
415 $out .= '<input type="hidden" name="title" value="' . htmlspecialchars( $wgContLang->specialpage( 'Contributions' ) ) . '" />';
416 $out .= '<input type="hidden" name="target" value="' . htmlspecialchars( $target ) . '" />';
417 $out .= '<input type="hidden" name="hideminor" value="' . ( $hideminor ?
1 : 0 ) .'" />';
419 <div id='nsselect' class='contributions'>
420 <label for='nsselectbox'>" . wfMsgHtml('namespace') . "</label>
422 <input type='submit' value=\"" . wfMsgHtml( 'allpagessubmit' ) . "\" />
423 <input type='checkbox' name='invert' value='1' id='nsinvert'" . ( $invert ?
' checked="checked"' : '' ) . " />
424 <label for='nsinvert'>" . wfMsgHtml('invert') . "</label>
426 $out .= '</form></div>';