ffd5c6da1d9daaaff55fc6da7d4a1561fd04e0ae
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 $sql = "SELECT MIN(rev_timestamp) as earliest, MAX(rev_timestamp) as latest " .
36 "WHERE rev_user_text = " . $this->dbr
->addQuotes($this->username
);
37 $res = $this->dbr
->query($sql, "contribs_finder::get_edit_limits");
38 $row = $this->dbr
->fetchObject($res);
40 return array(null, null);
41 $this->dbr
->freeResult($res);
42 return array($row->earliest
, $row->latest
);
45 function get_previous_offset_for_paging() {
46 $sql = "SELECT rev_timestamp FROM revision " .
47 "WHERE rev_timestamp > " . $this->offset
. " AND " .
48 "rev_user_text = " . $this->dbr
->addQuotes($this->username
) .
49 " ORDER BY rev_timestamp ASC LIMIT " . $this->limit
;
50 $res = $this->dbr
->query($sql);
52 while ($obj = $this->dbr
->fetchObject($res))
54 $this->dbr
->freeResult($res);
55 return $rows[count($rows) - 1]->rev_timestamp
;
58 /* private */ function make_sql() {
59 $userCond = $condition = $index = $minorQuery = $nsQuery
60 = $offsetQuery = $limitQuery = $nsinvert = "";
62 if ($this->username
== 'newbies') {
63 $max = $this->dbr
->selectField('user', 'max(user_id)', false, "make_sql");
64 $userCond = '>' . ($max - $max / 100);
68 if ($this->hide_minor
)
69 $minorQuery = 'AND rev_minor_edit=0';
74 if (!is_null($this->namespace))
75 $nsQuery .= "AND page_namespace {$nsinvert}= {$this->namespace}";
77 extract($this->dbr
->tableNames('page', 'revision'));
78 if ($userCond == "") {
79 $condition = "rev_user_text=" . $this->dbr
->addQuotes($this->username
);
80 $index = 'usertext_timestamp';
82 $condition = "rev_user {$userCond}";
83 $index = 'user_timestamp';
86 $limitQuery = "LIMIT {$this->limit}";
88 $offsetQuery = "AND rev_timestamp < {$this->offset}";
90 $use_index = $this->dbr
->useIndexClause($index);
92 page_namespace,page_title,page_is_new,page_latest,
93 rev_id,rev_timestamp,rev_comment,rev_minor_edit,rev_user_text,
95 FROM $page,$revision $use_index
96 WHERE page_id=rev_page AND $condition $minorQuery $nsQuery $offsetQuery
97 ORDER BY rev_timestamp DESC $limitQuery";
103 $res = $this->dbr
->query($this->make_sql(), "contribs_finder::find");
104 while ($c = $this->dbr
->fetchObject($res))
106 $this->dbr
->freeResult($res);
112 * Special page "user contributions".
113 * Shows a list of the contributions of a user.
116 * @param string $par (optional) user name of the user for which to show the contributions
118 function wfSpecialContributions( $par = null ) {
119 global $wgUser, $wgOut, $wgLang, $wgContLang, $wgRequest, $wgTitle;
120 $fname = 'wfSpecialContributions';
122 $target = isset($par) ?
$par : $wgRequest->getVal( 'target' );
123 if (!strlen($target)) {
124 $wgOut->errorpage('notargettitle', 'notargettext');
128 $nt = Title
::newFromURL( $target );
130 $wgOut->errorpage( 'notargettitle', 'notargettext' );
133 $nt =& Title
::makeTitle(NS_USER
, $nt->getDBkey());
135 $namespace = $wgRequest->getIntOrNull('namespace');
136 $invert = $wgRequest->getBool('invert');
137 $hideminor = $wgRequest->getBool('hideminor');
138 $limit = min($wgRequest->getInt('limit', 50), 500);
139 $offset = $wgRequest->getText('offset');
140 /* Offset must be an integral. */
141 if (!strlen($offset) ||
!preg_match("/^[0-9]+$/", $offset))
144 $title = Title
::newFromText($wgContLang->specialpage("Contributions"));
145 $urlbits = "hideminor=$hideminor&namespace=$namespace&invert=$invert&target=" . wfUrlEncode($target);
146 $myurl = $title->escapeLocalURL($urlbits);
148 $finder = new contribs_finder($nt->getText());
150 $finder->set_namespace($namespace, $invert);
151 $finder->set_hide_minor($hideminor);
152 $finder->set_limit($limit);
153 $finder->set_offset($offset);
155 if ($wgRequest->getText('go') == "prev") {
156 $prevts = $finder->get_previous_offset_for_paging();
157 $prevurl = $title->getLocalURL($urlbits . "&offset=$prevts&limit=$limit");
158 $wgOut->redirect($prevurl);
162 $sk = $wgUser->getSkin();
164 $id = User
::idFromName($nt->getText());
167 $ul = $nt->getText();
169 $ul = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
170 $userCond = '=' . $id;
172 $talk = $nt->getTalkPage();
174 $ul .= ' (' . $sk->makeLinkObj( $talk, $wgLang->getNsText( NS_TALK
) ) . ')';
177 if ($target == 'newbies') {
178 $ul = wfMsg ('newbies');
181 $wgOut->setSubtitle( wfMsgHtml( 'contribsub', $ul ) );
183 $contribsPage = Title
::makeTitle( NS_SPECIAL
, 'Contributions' );
184 $mlink = $sk->makeKnownLinkObj( $contribsPage,
185 $hideminor ?
wfMsgHtml( 'show' ) : wfMsgHtml( 'hide' ),
187 'target' => $nt->getPrefixedDbKey(),
190 'hideminor' => $hideminor ?
0 : 1,
191 'namespace' => $namespace ) ) );
193 $contribs = $finder->find();
195 if (count($contribs) == 0) {
196 $wgOut->addWikiText( wfMsg( "nocontribs" ) );
200 list($early, $late) = $finder->get_edit_limits();
201 $lastts = count($contribs) ?
$contribs[count($contribs) - 1]->rev_timestamp
: 0;
202 $atstart = (!count($contribs) ||
$late == $contribs[0]->rev_timestamp
);
203 $atend = (!count($contribs) ||
$early == $lastts);
204 wfdebug("early=$early late=$late lastts=$lastts\n");
205 $wgOut->addHTML(ucNamespaceForm($target, $hideminor, $namespace, $invert));
207 $prevtext = wfMsg("prevn", $limit);
209 $prevlink = $prevtext;
211 $prevlink = "<a href=\"$myurl&offset=$offset&limit=$limit&go=prev\">$prevtext</a>";
213 $nexttext = wfMsg("nextn", $limit);
215 $nextlink = $nexttext;
217 $nextlink = "<a href=\"$myurl&offset=$lastts&limit=$limit\">$nexttext</a>";
220 foreach (array(20, 50, 100, 250, 500) as $num)
221 $urls[] = "<a href=\"$myurl&offset=$offset&limit={$num}\">".$wgLang->formatNum($num)."</a>";
222 $bits = implode($urls, ' | ');
224 $prevnextbits = wfMsgHtml("viewprevnext", $prevlink, $nextlink, $bits);
226 $shm = wfMsgHtml( "showhideminor", $mlink );
227 $wgOut->addHTML( "<br />{$prevnextbits} ($shm)</p>\n");
229 $wgOut->addHTML( "<ul>\n" );
231 foreach ($contribs as $contrib)
232 $wgOut->addHTML(ucListEdit($sk, $contrib));
234 $wgOut->addHTML( "</ul>\n" );
235 $wgOut->addHTML( "<br />{$prevnextbits} ($shm)\n");
240 * Generates each row in the contributions list.
242 * Contributions which are marked "top" are currently on top of the history.
243 * For these contributions, a [rollback] link is shown for users with sysop
244 * privileges. The rollback link restores the most recent version that was not
245 * written by the target user.
247 * If the contributions page is called with the parameter &bot=1, all rollback
248 * links also get that parameter. It causes the edit itself and the rollback
249 * to be marked as "bot" edits. Bot edits are hidden by default from recent
250 * changes, so this allows sysops to combat a busy vandal without bothering
253 * @todo This would probably look a lot nicer in a table.
255 function ucListEdit( $sk, $row ) {
256 $fname = 'ucListEdit';
257 wfProfileIn( $fname );
259 global $wgLang, $wgOut, $wgUser, $wgRequest;
261 if( !isset( $messages ) ) {
262 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist minoreditletter' ) as $msg ) {
263 $messages[$msg] = wfMsg( $msg );
267 $page =& Title
::makeTitle( $row->page_namespace
, $row->page_title
);
268 $link = $sk->makeKnownLinkObj( $page, '' );
269 $difftext = $topmarktext = '';
270 if( $row->rev_id
== $row->page_latest
) {
271 $topmarktext .= '<strong>' . $messages['uctop'] . '</strong>';
272 if( !$row->page_is_new
) {
273 $difftext .= $sk->makeKnownLinkObj( $page, '(' . $messages['diff'] . ')', 'diff=0' );
275 $difftext .= $messages['newarticle'];
278 if( $wgUser->isAllowed('rollback') ) {
279 $extraRollback = $wgRequest->getBool( 'bot' ) ?
'&bot=1' : '';
280 $extraRollback .= '&token=' . urlencode(
281 $wgUser->editToken( array( $page->getPrefixedText(), $row->rev_user_text
) ) );
282 $topmarktext .= ' ['. $sk->makeKnownLinkObj( $page,
283 $messages['rollbacklink'],
284 'action=rollback&from=' . urlencode( $row->rev_user_text
) . $extraRollback ) .']';
288 if( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) {
289 $difftext = '(' . $messages['diff'] . ')';
291 $difftext = $sk->makeKnownLinkObj( $page, '(' . $messages['diff'].')', 'diff=prev&oldid='.$row->rev_id
);
293 $histlink='('.$sk->makeKnownLinkObj( $page, $messages['hist'], 'action=history' ) . ')';
295 $comment = $sk->commentBlock( $row->rev_comment
, $page );
296 $d = $wgLang->timeanddate( $row->rev_timestamp
, true );
298 if( $row->rev_minor_edit
) {
299 $mflag = '<span class="minor">' . $messages['minoreditletter'] . '</span> ';
304 $ret = "{$d} {$histlink} {$difftext} {$mflag} {$link} {$comment} {$topmarktext}";
305 if( $row->rev_deleted
) {
306 $ret = '<span class="deleted">' . $ret . '</span> ' . htmlspecialchars( wfMsg( 'deletedrev' ) );
308 $ret = "<li>$ret</li>\n";
309 wfProfileOut( $fname );
314 * Generates a form used to restrict display of contributions
315 * to a specific namespace
318 * @param string $target target user to show contributions for
319 * @param string $hideminor whether minor contributions are hidden
320 * @param string $namespace currently selected namespace, NULL for show all
321 * @param bool $invert inverts the namespace selection on true (default null)
323 function ucNamespaceForm ( $target, $hideminor, $namespace, $invert ) {
324 global $wgContLang, $wgScript;
326 $namespaceselect = "<select name='namespace' id='nsselectbox'>";
327 $namespaceselect .= '<option value="" '.(is_null($namespace) ?
' selected="selected"' : '').'>'.wfMsgHtml( 'contributionsall' ).'</option>';
328 $arr = $wgContLang->getFormattedNamespaces();
329 foreach( $arr as $ns => $name ) {
332 $n = $ns === NS_MAIN ?
wfMsgHtml( 'blanknamespace' ) : htmlspecialchars( $name );
333 $sel = $namespace == $ns ?
' selected="selected"' : '';
334 $namespaceselect .= "<option value='$ns'$sel>$n</option>";
336 $namespaceselect .= '</select>';
338 $action = htmlspecialchars( $wgScript );
339 $out = "<div class='namespaceselector'><form method='get' action=\"$action\">";
340 $out .= '<input type="hidden" name="title" value="' . htmlspecialchars( $wgContLang->specialpage( 'Contributions' ) ) . '" />';
341 $out .= '<input type="hidden" name="target" value="' . htmlspecialchars( $target ) . '" />';
342 $out .= '<input type="hidden" name="hideminor" value="' . ( $hideminor ?
1 : 0 ) .'" />';
344 <div id='nsselect' class='contributions'>
345 <label for='nsselectbox'>" . wfMsgHtml('namespace') . "</label>
347 <input type='submit' value=\"" . wfMsgHtml( 'allpagessubmit' ) . "\" />
348 <input type='checkbox' name='invert' value='1' id='nsinvert'" . ( $invert ?
' checked="checked"' : '' ) . " />
349 <label for='nsinvert'>" . wfMsgHtml('invert') . "</label>
351 $out .= '</form></div>';