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 $sql = "SELECT MIN(rev_timestamp) as earliest, MAX(rev_timestamp) as latest " .
38 "FROM page, revision $use_index WHERE page_id = rev_page " .
42 $sql .= $this->get_namespace_cond();
43 $sql .= $this->get_minor_cond();
44 $res = $this->dbr
->query($sql, "contribs_finder::get_edit_limits");
46 while ($o = $this->dbr
->fetchObject($res))
48 $row = $rows[count($rows) - 1];
49 return array($row->earliest
, $row->latest
);
52 function get_user_cond() {
55 if ($this->username
== 'newbies') {
56 $max = $this->dbr
->selectField('user', 'max(user_id)', false, "make_sql");
57 $condition = '>' . ($max - $max / 100);
60 if ($condition == "") {
61 $condition = " rev_user_text=" . $this->dbr
->addQuotes($this->username
);
62 $index = 'usertext_timestamp';
64 $condition = " rev_user {$condition}";
65 $index = 'user_timestamp';
68 return array($index, $condition);
71 function get_minor_cond() {
72 if ($this->hide_minor
)
73 return ' AND rev_minor_edit=0';
77 function get_namespace_cond() {
78 $nsQuery = $nsinvert = "";
83 if (!is_null($this->namespace))
84 $nsQuery .= "AND page_namespace {$nsinvert}= {$this->namespace}";
89 function get_previous_offset_for_paging() {
90 list($index, $usercond) = $this->get_user_cond();
91 $use_index = $this->dbr
->useIndexClause($index);
93 $sql = "SELECT rev_timestamp FROM page, revision $use_index " .
94 "WHERE page_id = rev_page AND rev_timestamp > '" . $this->offset
. "' AND " .
95 "rev_user_text = " . $this->dbr
->addQuotes($this->username
);
96 $sql .= $this->get_namespace_cond();
97 $sql .= $this->get_minor_cond();
98 $sql .= " ORDER BY rev_timestamp ASC LIMIT " . $this->limit
;
99 $res = $this->dbr
->query($sql);
101 while ($obj = $this->dbr
->fetchObject($res))
103 $this->dbr
->freeResult($res);
104 return $rows[count($rows) - 1]->rev_timestamp
;
107 function get_first_offset_for_paging() {
108 list($index, $usercond) = $this->get_user_cond();
109 $use_index = $this->dbr
->useIndexClause($index);
111 $sql = "SELECT rev_timestamp FROM page, revision $use_index " .
112 "WHERE page_id = rev_page AND " .
113 "rev_user_text = " . $this->dbr
->addQuotes($this->username
);
114 $sql .= $this->get_namespace_cond();
115 $sql .= $this->get_minor_cond();
116 $sql .= " ORDER BY rev_timestamp ASC LIMIT " . ($this->limit +
1);
117 $res = $this->dbr
->query($sql);
119 while ($obj = $this->dbr
->fetchObject($res))
121 $this->dbr
->freeResult($res);
122 return $rows[count($rows) - 1]->rev_timestamp
;
125 /* private */ function make_sql() {
126 $userCond = $condition = $index = $minorQuery = $nsQuery
127 = $offsetQuery = $limitQuery = $nsinvert = "";
129 $minorQuery = $this->get_minor_cond();
130 $nsQuery = $this->get_namespace_cond();
132 extract($this->dbr
->tableNames('page', 'revision'));
133 list($index, $userCond) = $this->get_user_cond();
135 $limitQuery = "LIMIT {$this->limit}";
137 $offsetQuery = "AND rev_timestamp < '{$this->offset}'";
139 $use_index = $this->dbr
->useIndexClause($index);
141 page_namespace,page_title,page_is_new,page_latest,
142 rev_id,rev_timestamp,rev_comment,rev_minor_edit,rev_user_text,
144 FROM $page,$revision $use_index
145 WHERE page_id=rev_page AND $userCond $minorQuery $nsQuery $offsetQuery
146 ORDER BY rev_timestamp DESC $limitQuery";
152 $res = $this->dbr
->query($this->make_sql(), "contribs_finder::find");
153 while ($c = $this->dbr
->fetchObject($res))
155 $this->dbr
->freeResult($res);
161 * Special page "user contributions".
162 * Shows a list of the contributions of a user.
165 * @param string $par (optional) user name of the user for which to show the contributions
167 function wfSpecialContributions( $par = null ) {
168 global $wgUser, $wgOut, $wgLang, $wgContLang, $wgRequest, $wgTitle;
169 $fname = 'wfSpecialContributions';
171 $target = isset($par) ?
$par : $wgRequest->getVal( 'target' );
172 if (!strlen($target)) {
173 $wgOut->errorpage('notargettitle', 'notargettext');
177 $nt = Title
::newFromURL( $target );
179 $wgOut->errorpage( 'notargettitle', 'notargettext' );
182 $nt =& Title
::makeTitle(NS_USER
, $nt->getDBkey());
184 $namespace = $wgRequest->getIntOrNull('namespace');
185 $invert = $wgRequest->getBool('invert');
186 $hideminor = $wgRequest->getBool('hideminor');
187 $limit = min($wgRequest->getInt('limit', 50), 500);
188 $offset = $wgRequest->getVal('offset');
189 /* Offset must be an integral. */
190 if (!strlen($offset) ||
!preg_match("/^[0-9]+$/", $offset))
193 $title = Title
::makeTitle(NS_SPECIAL
, "Contributions");
194 $urlbits = "hideminor=$hideminor&namespace=$namespace&invert=$invert&target=" . wfUrlEncode($target);
195 $myurl = $title->escapeLocalURL($urlbits);
197 $finder = new contribs_finder($nt->getText());
199 $finder->set_namespace($namespace, $invert);
200 $finder->set_hide_minor($hideminor);
201 $finder->set_limit($limit);
202 $finder->set_offset($offset);
204 if ($wgRequest->getText('go') == "prev") {
205 $prevts = $finder->get_previous_offset_for_paging();
206 $prevurl = $title->getLocalURL($urlbits . "&offset=$prevts&limit=$limit");
207 $wgOut->redirect($prevurl);
211 if ($wgRequest->getText('go') == "first") {
212 $prevts = $finder->get_first_offset_for_paging();
213 $prevurl = $title->getLocalURL($urlbits . "&offset=$prevts&limit=$limit");
214 $wgOut->redirect($prevurl);
218 $sk = $wgUser->getSkin();
220 $id = User
::idFromName($nt->getText());
223 $ul = $nt->getText();
225 $ul = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
226 $userCond = '=' . $id;
228 $talk = $nt->getTalkPage();
230 $ul .= ' (' . $sk->makeLinkObj( $talk, $wgLang->getNsText( NS_TALK
) ) . ')';
233 if ($target == 'newbies') {
234 $ul = wfMsg ('newbies');
237 $wgOut->setSubtitle( wfMsgHtml( 'contribsub', $ul ) );
239 $contribsPage = Title
::makeTitle( NS_SPECIAL
, 'Contributions' );
240 $mlink = $sk->makeKnownLinkObj( $contribsPage,
241 $hideminor ?
wfMsgHtml( 'show' ) : wfMsgHtml( 'hide' ),
243 'target' => $nt->getPrefixedDbKey(),
246 'hideminor' => $hideminor ?
0 : 1,
247 'namespace' => $namespace ) ) );
249 $contribs = $finder->find();
251 if (count($contribs) == 0) {
252 $wgOut->addWikiText( wfMsg( "nocontribs" ) );
256 list($early, $late) = $finder->get_edit_limits();
257 $lastts = count($contribs) ?
$contribs[count($contribs) - 1]->rev_timestamp
: 0;
258 $atstart = (!count($contribs) ||
$late == $contribs[0]->rev_timestamp
);
259 $atend = (!count($contribs) ||
$early == $lastts);
260 wfdebug("early=$early late=$late lastts=$lastts\n");
261 $wgOut->addHTML(ucNamespaceForm($target, $hideminor, $namespace, $invert));
263 $lasturl = $wgTitle->escapeLocalURL("action=history&limit={$limit}");
265 $firsttext = wfMsgHtml("histfirst");
266 $lasttext = wfMsgHtml("histlast");
268 $prevtext = wfMsg("prevn", $limit);
270 $lastlink = $lasttext;
271 $prevlink = $prevtext;
273 $lastlink = "<a href=\"$myurl&limit=$limit\">$lasttext</a>";
274 $prevlink = "<a href=\"$myurl&offset=$offset&limit=$limit&go=prev\">$prevtext</a>";
277 $nexttext = wfMsg("nextn", $limit);
279 $firstlink = $firsttext;
280 $nextlink = $nexttext;
282 $firstlink = "<a href=\"$myurl&limit=$limit&;go=first\">$firsttext</a>";
283 $nextlink = "<a href=\"$myurl&offset=$lastts&limit=$limit\">$nexttext</a>";
285 $firstlast = "($lastlink | $firstlink)";
288 foreach (array(20, 50, 100, 250, 500) as $num)
289 $urls[] = "<a href=\"$myurl&offset=$offset&limit={$num}\">".$wgLang->formatNum($num)."</a>";
290 $bits = implode($urls, ' | ');
292 $prevnextbits = "$firstlast " . wfMsgHtml("viewprevnext", $prevlink, $nextlink, $bits);
294 $shm = wfMsgHtml( "contribs-showhideminor", $mlink );
295 $wgOut->addHTML( "<br />{$prevnextbits} ($shm)</p>\n");
297 $wgOut->addHTML( "<ul>\n" );
299 foreach ($contribs as $contrib)
300 $wgOut->addHTML(ucListEdit($sk, $contrib));
302 $wgOut->addHTML( "</ul>\n" );
303 $wgOut->addHTML( "<br />{$prevnextbits} ($shm)\n");
308 * Generates each row in the contributions list.
310 * Contributions which are marked "top" are currently on top of the history.
311 * For these contributions, a [rollback] link is shown for users with sysop
312 * privileges. The rollback link restores the most recent version that was not
313 * written by the target user.
315 * If the contributions page is called with the parameter &bot=1, all rollback
316 * links also get that parameter. It causes the edit itself and the rollback
317 * to be marked as "bot" edits. Bot edits are hidden by default from recent
318 * changes, so this allows sysops to combat a busy vandal without bothering
321 * @todo This would probably look a lot nicer in a table.
323 function ucListEdit( $sk, $row ) {
324 $fname = 'ucListEdit';
325 wfProfileIn( $fname );
327 global $wgLang, $wgOut, $wgUser, $wgRequest;
329 if( !isset( $messages ) ) {
330 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist minoreditletter' ) as $msg ) {
331 $messages[$msg] = wfMsg( $msg );
335 $page =& Title
::makeTitle( $row->page_namespace
, $row->page_title
);
336 $link = $sk->makeKnownLinkObj( $page, '' );
337 $difftext = $topmarktext = '';
338 if( $row->rev_id
== $row->page_latest
) {
339 $topmarktext .= '<strong>' . $messages['uctop'] . '</strong>';
340 if( !$row->page_is_new
) {
341 $difftext .= $sk->makeKnownLinkObj( $page, '(' . $messages['diff'] . ')', 'diff=0' );
343 $difftext .= $messages['newarticle'];
346 if( $wgUser->isAllowed('rollback') ) {
347 $extraRollback = $wgRequest->getBool( 'bot' ) ?
'&bot=1' : '';
348 $extraRollback .= '&token=' . urlencode(
349 $wgUser->editToken( array( $page->getPrefixedText(), $row->rev_user_text
) ) );
350 $topmarktext .= ' ['. $sk->makeKnownLinkObj( $page,
351 $messages['rollbacklink'],
352 'action=rollback&from=' . urlencode( $row->rev_user_text
) . $extraRollback ) .']';
356 if( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) {
357 $difftext = '(' . $messages['diff'] . ')';
359 $difftext = $sk->makeKnownLinkObj( $page, '(' . $messages['diff'].')', 'diff=prev&oldid='.$row->rev_id
);
361 $histlink='('.$sk->makeKnownLinkObj( $page, $messages['hist'], 'action=history' ) . ')';
363 $comment = $sk->commentBlock( $row->rev_comment
, $page );
364 $d = $wgLang->timeanddate( $row->rev_timestamp
, true );
366 if( $row->rev_minor_edit
) {
367 $mflag = '<span class="minor">' . $messages['minoreditletter'] . '</span> ';
372 $ret = "{$d} {$histlink} {$difftext} {$mflag} {$link} {$comment} {$topmarktext}";
373 if( $row->rev_deleted
) {
374 $ret = '<span class="deleted">' . $ret . '</span> ' . htmlspecialchars( wfMsg( 'deletedrev' ) );
376 $ret = "<li>$ret</li>\n";
377 wfProfileOut( $fname );
382 * Generates a form used to restrict display of contributions
383 * to a specific namespace
386 * @param string $target target user to show contributions for
387 * @param string $hideminor whether minor contributions are hidden
388 * @param string $namespace currently selected namespace, NULL for show all
389 * @param bool $invert inverts the namespace selection on true (default null)
391 function ucNamespaceForm ( $target, $hideminor, $namespace, $invert ) {
392 global $wgContLang, $wgScript;
394 $namespaceselect = "<select name='namespace' id='nsselectbox'>";
395 $namespaceselect .= '<option value="" '.(is_null($namespace) ?
' selected="selected"' : '').'>'.wfMsgHtml( 'contributionsall' ).'</option>';
396 $arr = $wgContLang->getFormattedNamespaces();
397 foreach( $arr as $ns => $name ) {
400 $n = $ns === NS_MAIN ?
wfMsgHtml( 'blanknamespace' ) : htmlspecialchars( $name );
401 $sel = $namespace == $ns ?
' selected="selected"' : '';
402 $namespaceselect .= "<option value='$ns'$sel>$n</option>";
404 $namespaceselect .= '</select>';
406 $action = htmlspecialchars( $wgScript );
407 $out = "<div class='namespaceselector'><form method='get' action=\"$action\">";
408 $out .= '<input type="hidden" name="title" value="' . htmlspecialchars( $wgContLang->specialpage( 'Contributions' ) ) . '" />';
409 $out .= '<input type="hidden" name="target" value="' . htmlspecialchars( $target ) . '" />';
410 $out .= '<input type="hidden" name="hideminor" value="' . ( $hideminor ?
1 : 0 ) .'" />';
412 <div id='nsselect' class='contributions'>
413 <label for='nsselectbox'>" . wfMsgHtml('namespace') . "</label>
415 <input type='submit' value=\"" . wfMsgHtml( 'allpagessubmit' ) . "\" />
416 <input type='checkbox' name='invert' value='1' id='nsinvert'" . ( $invert ?
' checked="checked"' : '' ) . " />
417 <label for='nsinvert'>" . wfMsgHtml('invert') . "</label>
419 $out .= '</form></div>';