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 /* private */ function make_sql() {
108 $userCond = $condition = $index = $minorQuery = $nsQuery
109 = $offsetQuery = $limitQuery = $nsinvert = "";
111 $minorQuery = $this->get_minor_cond();
112 $nsQuery = $this->get_namespace_cond();
114 extract($this->dbr
->tableNames('page', 'revision'));
115 list($index, $userCond) = $this->get_user_cond();
117 $limitQuery = "LIMIT {$this->limit}";
119 $offsetQuery = "AND rev_timestamp < {$this->offset}";
121 $use_index = $this->dbr
->useIndexClause($index);
123 page_namespace,page_title,page_is_new,page_latest,
124 rev_id,rev_timestamp,rev_comment,rev_minor_edit,rev_user_text,
126 FROM $page,$revision $use_index
127 WHERE page_id=rev_page AND $userCond $minorQuery $nsQuery $offsetQuery
128 ORDER BY rev_timestamp DESC $limitQuery";
134 $res = $this->dbr
->query($this->make_sql(), "contribs_finder::find");
135 while ($c = $this->dbr
->fetchObject($res))
137 $this->dbr
->freeResult($res);
143 * Special page "user contributions".
144 * Shows a list of the contributions of a user.
147 * @param string $par (optional) user name of the user for which to show the contributions
149 function wfSpecialContributions( $par = null ) {
150 global $wgUser, $wgOut, $wgLang, $wgContLang, $wgRequest, $wgTitle;
151 $fname = 'wfSpecialContributions';
153 $target = isset($par) ?
$par : $wgRequest->getVal( 'target' );
154 if (!strlen($target)) {
155 $wgOut->errorpage('notargettitle', 'notargettext');
159 $nt = Title
::newFromURL( $target );
161 $wgOut->errorpage( 'notargettitle', 'notargettext' );
164 $nt =& Title
::makeTitle(NS_USER
, $nt->getDBkey());
166 $namespace = $wgRequest->getIntOrNull('namespace');
167 $invert = $wgRequest->getBool('invert');
168 $hideminor = $wgRequest->getBool('hideminor');
169 $limit = min($wgRequest->getInt('limit', 50), 500);
170 $offset = $wgRequest->getVal('offset');
171 /* Offset must be an integral. */
172 if (!strlen($offset) ||
!preg_match("/^[0-9]+$/", $offset))
175 $title = Title
::makeTitle(NS_SPECIAL
, "Contributions");
176 $urlbits = "hideminor=$hideminor&namespace=$namespace&invert=$invert&target=" . wfUrlEncode($target);
177 $myurl = $title->escapeLocalURL($urlbits);
179 $finder = new contribs_finder($nt->getText());
181 $finder->set_namespace($namespace, $invert);
182 $finder->set_hide_minor($hideminor);
183 $finder->set_limit($limit);
184 $finder->set_offset($offset);
186 if ($wgRequest->getText('go') == "prev") {
187 $prevts = $finder->get_previous_offset_for_paging();
188 $prevurl = $title->getLocalURL($urlbits . "&offset=$prevts&limit=$limit");
189 $wgOut->redirect($prevurl);
193 $sk = $wgUser->getSkin();
195 $id = User
::idFromName($nt->getText());
198 $ul = $nt->getText();
200 $ul = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
201 $userCond = '=' . $id;
203 $talk = $nt->getTalkPage();
205 $ul .= ' (' . $sk->makeLinkObj( $talk, $wgLang->getNsText( NS_TALK
) ) . ')';
208 if ($target == 'newbies') {
209 $ul = wfMsg ('newbies');
212 $wgOut->setSubtitle( wfMsgHtml( 'contribsub', $ul ) );
214 $contribsPage = Title
::makeTitle( NS_SPECIAL
, 'Contributions' );
215 $mlink = $sk->makeKnownLinkObj( $contribsPage,
216 $hideminor ?
wfMsgHtml( 'show' ) : wfMsgHtml( 'hide' ),
218 'target' => $nt->getPrefixedDbKey(),
221 'hideminor' => $hideminor ?
0 : 1,
222 'namespace' => $namespace ) ) );
224 $contribs = $finder->find();
226 if (count($contribs) == 0) {
227 $wgOut->addWikiText( wfMsg( "nocontribs" ) );
231 list($early, $late) = $finder->get_edit_limits();
232 $lastts = count($contribs) ?
$contribs[count($contribs) - 1]->rev_timestamp
: 0;
233 $atstart = (!count($contribs) ||
$late == $contribs[0]->rev_timestamp
);
234 $atend = (!count($contribs) ||
$early == $lastts);
235 wfdebug("early=$early late=$late lastts=$lastts\n");
236 $wgOut->addHTML(ucNamespaceForm($target, $hideminor, $namespace, $invert));
238 $prevtext = wfMsg("prevn", $limit);
240 $prevlink = $prevtext;
242 $prevlink = "<a href=\"$myurl&offset=$offset&limit=$limit&go=prev\">$prevtext</a>";
244 $nexttext = wfMsg("nextn", $limit);
246 $nextlink = $nexttext;
248 $nextlink = "<a href=\"$myurl&offset=$lastts&limit=$limit\">$nexttext</a>";
251 foreach (array(20, 50, 100, 250, 500) as $num)
252 $urls[] = "<a href=\"$myurl&offset=$offset&limit={$num}\">".$wgLang->formatNum($num)."</a>";
253 $bits = implode($urls, ' | ');
255 $prevnextbits = wfMsgHtml("viewprevnext", $prevlink, $nextlink, $bits);
257 $shm = wfMsgHtml( "showhideminor", $mlink );
258 $wgOut->addHTML( "<br />{$prevnextbits} ($shm)</p>\n");
260 $wgOut->addHTML( "<ul>\n" );
262 foreach ($contribs as $contrib)
263 $wgOut->addHTML(ucListEdit($sk, $contrib));
265 $wgOut->addHTML( "</ul>\n" );
266 $wgOut->addHTML( "<br />{$prevnextbits} ($shm)\n");
271 * Generates each row in the contributions list.
273 * Contributions which are marked "top" are currently on top of the history.
274 * For these contributions, a [rollback] link is shown for users with sysop
275 * privileges. The rollback link restores the most recent version that was not
276 * written by the target user.
278 * If the contributions page is called with the parameter &bot=1, all rollback
279 * links also get that parameter. It causes the edit itself and the rollback
280 * to be marked as "bot" edits. Bot edits are hidden by default from recent
281 * changes, so this allows sysops to combat a busy vandal without bothering
284 * @todo This would probably look a lot nicer in a table.
286 function ucListEdit( $sk, $row ) {
287 $fname = 'ucListEdit';
288 wfProfileIn( $fname );
290 global $wgLang, $wgOut, $wgUser, $wgRequest;
292 if( !isset( $messages ) ) {
293 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist minoreditletter' ) as $msg ) {
294 $messages[$msg] = wfMsg( $msg );
298 $page =& Title
::makeTitle( $row->page_namespace
, $row->page_title
);
299 $link = $sk->makeKnownLinkObj( $page, '' );
300 $difftext = $topmarktext = '';
301 if( $row->rev_id
== $row->page_latest
) {
302 $topmarktext .= '<strong>' . $messages['uctop'] . '</strong>';
303 if( !$row->page_is_new
) {
304 $difftext .= $sk->makeKnownLinkObj( $page, '(' . $messages['diff'] . ')', 'diff=0' );
306 $difftext .= $messages['newarticle'];
309 if( $wgUser->isAllowed('rollback') ) {
310 $extraRollback = $wgRequest->getBool( 'bot' ) ?
'&bot=1' : '';
311 $extraRollback .= '&token=' . urlencode(
312 $wgUser->editToken( array( $page->getPrefixedText(), $row->rev_user_text
) ) );
313 $topmarktext .= ' ['. $sk->makeKnownLinkObj( $page,
314 $messages['rollbacklink'],
315 'action=rollback&from=' . urlencode( $row->rev_user_text
) . $extraRollback ) .']';
319 if( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) {
320 $difftext = '(' . $messages['diff'] . ')';
322 $difftext = $sk->makeKnownLinkObj( $page, '(' . $messages['diff'].')', 'diff=prev&oldid='.$row->rev_id
);
324 $histlink='('.$sk->makeKnownLinkObj( $page, $messages['hist'], 'action=history' ) . ')';
326 $comment = $sk->commentBlock( $row->rev_comment
, $page );
327 $d = $wgLang->timeanddate( $row->rev_timestamp
, true );
329 if( $row->rev_minor_edit
) {
330 $mflag = '<span class="minor">' . $messages['minoreditletter'] . '</span> ';
335 $ret = "{$d} {$histlink} {$difftext} {$mflag} {$link} {$comment} {$topmarktext}";
336 if( $row->rev_deleted
) {
337 $ret = '<span class="deleted">' . $ret . '</span> ' . htmlspecialchars( wfMsg( 'deletedrev' ) );
339 $ret = "<li>$ret</li>\n";
340 wfProfileOut( $fname );
345 * Generates a form used to restrict display of contributions
346 * to a specific namespace
349 * @param string $target target user to show contributions for
350 * @param string $hideminor whether minor contributions are hidden
351 * @param string $namespace currently selected namespace, NULL for show all
352 * @param bool $invert inverts the namespace selection on true (default null)
354 function ucNamespaceForm ( $target, $hideminor, $namespace, $invert ) {
355 global $wgContLang, $wgScript;
357 $namespaceselect = "<select name='namespace' id='nsselectbox'>";
358 $namespaceselect .= '<option value="" '.(is_null($namespace) ?
' selected="selected"' : '').'>'.wfMsgHtml( 'contributionsall' ).'</option>';
359 $arr = $wgContLang->getFormattedNamespaces();
360 foreach( $arr as $ns => $name ) {
363 $n = $ns === NS_MAIN ?
wfMsgHtml( 'blanknamespace' ) : htmlspecialchars( $name );
364 $sel = $namespace == $ns ?
' selected="selected"' : '';
365 $namespaceselect .= "<option value='$ns'$sel>$n</option>";
367 $namespaceselect .= '</select>';
369 $action = htmlspecialchars( $wgScript );
370 $out = "<div class='namespaceselector'><form method='get' action=\"$action\">";
371 $out .= '<input type="hidden" name="title" value="' . htmlspecialchars( $wgContLang->specialpage( 'Contributions' ) ) . '" />';
372 $out .= '<input type="hidden" name="target" value="' . htmlspecialchars( $target ) . '" />';
373 $out .= '<input type="hidden" name="hideminor" value="' . ( $hideminor ?
1 : 0 ) .'" />';
375 <div id='nsselect' class='contributions'>
376 <label for='nsselectbox'>" . wfMsgHtml('namespace') . "</label>
378 <input type='submit' value=\"" . wfMsgHtml( 'allpagessubmit' ) . "\" />
379 <input type='checkbox' name='invert' value='1' id='nsinvert'" . ( $invert ?
' checked="checked"' : '' ) . " />
380 <label for='nsinvert'>" . wfMsgHtml('invert') . "</label>
382 $out .= '</form></div>';