cabb915cf32f25074001b30ed3c930b549bb24e8
[lhc/web/wiklou.git] / includes / SpecialContributions.php
1 <?php
2 /**
3 * @package MediaWiki
4 * @subpackage SpecialPage
5 */
6
7 /**
8 * Special page "user contributions".
9 * Shows a list of the contributions of a user.
10 *
11 * @return none
12 * @param string $par (optional) user name of the user for which to show the contributions
13 */
14 function wfSpecialContributions( $par = null ) {
15 global $wgUser, $wgOut, $wgLang, $wgContLang, $wgRequest;
16 $fname = 'wfSpecialContributions';
17
18 // GET values
19 $target = isset($par) ? $par : $wgRequest->getVal( 'target' );
20 $namespace = $wgRequest->getVal( 'namespace', '' );
21 $namespace = $namespace === '' ? NULL : $namespace;
22 $invert = $wgRequest->getBool( 'invert' );
23 $hideminor = ($wgRequest->getBool( 'hideminor' ) ? true : false);
24
25 if ( '' == $target ) {
26 $wgOut->errorpage( 'notargettitle', 'notargettext' );
27 return;
28 }
29
30 # FIXME: Change from numeric offsets to date offsets
31 list( $limit, $offset ) = wfCheckLimits( 50, '' );
32 $offlimit = $limit + $offset;
33 $querylimit = $offlimit + 1;
34 $sk = $wgUser->getSkin();
35 $dbr =& wfGetDB( DB_SLAVE );
36 $userCond = "";
37
38 $nt = Title::newFromURL( $target );
39 if ( !$nt ) {
40 $wgOut->errorpage( 'notargettitle', 'notargettext' );
41 return;
42 }
43 $nt =& Title::makeTitle( NS_USER, $nt->getDBkey() );
44
45 $id = User::idFromName( $nt->getText() );
46
47 if ( 0 == $id ) {
48 $ul = $nt->getText();
49 } else {
50 $ul = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
51 $userCond = '=' . $id;
52 }
53 $talk = $nt->getTalkPage();
54 if( $talk ) {
55 $ul .= ' (' . $sk->makeLinkObj( $talk, $wgLang->getNsText( NS_TALK ) ) . ')';
56 }
57
58
59 if ( $target == 'newbies' ) {
60 # View the contributions of all recently created accounts
61 $max = $dbr->selectField( 'user', 'max(user_id)', false, $fname );
62 $userCond = '>' . ($max - $max / 100);
63 $ul = wfMsg ( 'newbies' );
64 $id = 0;
65 }
66
67 $wgOut->setSubtitle( wfMsg( 'contribsub', $ul ) );
68
69 if ( $hideminor ) {
70 $minorQuery = "AND rev_minor_edit=0";
71 $mlink = $sk->makeKnownLink( $wgContLang->specialPage( "Contributions" ),
72 WfMsg( "show" ), "target=" . htmlspecialchars( $nt->getPrefixedURL() ) .
73 "&offset={$offset}&limit={$limit}&hideminor=0&namespace={$namespace}" );
74 } else {
75 $minorQuery = "";
76 $mlink = $sk->makeKnownLink( $wgContLang->specialPage( "Contributions" ),
77 WfMsg( 'hide' ), 'target=' . htmlspecialchars( $nt->getPrefixedURL() ) .
78 "&offset={$offset}&limit={$limit}&hideminor=1&namespace={$namespace}" );
79 }
80
81 if( !is_null($namespace) ) {
82 $minorQuery .= ' AND page_namespace ' . ($invert ? '!' : '') . "= {$namespace}";
83 }
84
85 extract( $dbr->tableNames( 'page', 'revision' ) );
86 if ( $userCond == "" ) {
87 $condition = "rev_user_text=" . $dbr->addQuotes( $nt->getText() );
88 $index = 'usertext_timestamp';
89 } else {
90 $condition = "rev_user {$userCond}";
91 $index = 'user_timestamp';
92 }
93
94
95 $use_index = $dbr->useIndexClause( $index );
96 $sql = "SELECT
97 page_namespace,page_title,page_is_new,page_latest,
98 rev_id,rev_timestamp,rev_comment,rev_minor_edit,rev_user_text,
99 rev_deleted
100 FROM $page,$revision $use_index
101 WHERE page_id=rev_page AND $condition $minorQuery " .
102 "ORDER BY rev_timestamp DESC LIMIT {$querylimit}";
103 $res = $dbr->query( $sql, $fname );
104 $numRows = $dbr->numRows( $res );
105
106 $wgOut->addHTML( namespaceForm( $target, $hideminor, $namespace, $invert ) );
107
108 $top = wfShowingResults( $offset, $limit );
109 $wgOut->addHTML( "<p>{$top}\n" );
110
111 $sl = wfViewPrevNext( $offset, $limit,
112 $wgContLang->specialpage( "Contributions" ),
113 "hideminor={$hideminor}&namespace={$namespace}&target=" . wfUrlEncode( $target ),
114 ($numRows) <= $offlimit);
115
116 $shm = wfMsg( "showhideminor", $mlink );
117 $wgOut->addHTML( "<br />{$sl} ($shm)</p>\n");
118
119
120 if ( 0 == $numRows ) {
121 $wgOut->addHTML( "\n<p>" . wfMsg( "nocontribs" ) . "</p>\n" );
122 return;
123 }
124
125 $wgOut->addHTML( "<ul>\n" );
126 while( $obj = $dbr->fetchObject( $res ) ) {
127 $wgOut->addHTML( ucListEdit( $sk, $obj ) );
128 }
129 $wgOut->addHTML( "</ul>\n" );
130
131 $wgOut->addHTML( "<br />{$sl} ($shm)\n");
132 }
133
134
135 /**
136 * Generates each row in the contributions list.
137 *
138 * Contributions which are marked "top" are currently on top of the history.
139 * For these contributions, a [rollback] link is shown for users with sysop
140 * privileges. The rollback link restores the most recent version that was not
141 * written by the target user.
142 *
143 * If the contributions page is called with the parameter &bot=1, all rollback
144 * links also get that parameter. It causes the edit itself and the rollback
145 * to be marked as "bot" edits. Bot edits are hidden by default from recent
146 * changes, so this allows sysops to combat a busy vandal without bothering
147 * other users.
148 *
149 * @todo This would probably look a lot nicer in a table.
150 */
151 function ucListEdit( $sk, $row ) {
152 $fname = 'ucListEdit';
153 wfProfileIn( $fname );
154
155 global $wgLang, $wgOut, $wgUser, $wgRequest;
156 static $messages;
157 if( !isset( $messages ) ) {
158 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist minoreditletter' ) as $msg ) {
159 $messages[$msg] = wfMsg( $msg );
160 }
161 }
162
163 $page =& Title::makeTitle( $row->page_namespace, $row->page_title );
164 $link = $sk->makeKnownLinkObj( $page, '' );
165 $difftext = $topmarktext = '';
166 if( $row->rev_id == $row->page_latest ) {
167 $topmarktext .= '<strong>' . $messages['uctop'] . '</strong>';
168 if( !$row->page_is_new ) {
169 $difftext .= $sk->makeKnownLinkObj( $page, '(' . $messages['diff'] . ')', 'diff=0' );
170 } else {
171 $difftext .= $messages['newarticle'];
172 }
173
174 if( $wgUser->isAllowed('rollback') ) {
175 $extraRollback = $wgRequest->getBool( 'bot' ) ? '&bot=1' : '';
176 $extraRollback .= '&token=' . urlencode(
177 $wgUser->editToken( array( $page->getPrefixedText(), $row->rev_user_text ) ) );
178 $topmarktext .= ' ['. $sk->makeKnownLinkObj( $page,
179 $messages['rollbacklink'],
180 'action=rollback&from=' . urlencode( $row->rev_user_text ) . $extraRollback ) .']';
181 }
182
183 }
184 if( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) {
185 $difftext = '(' . $messages['diff'] . ')';
186 } else {
187 $difftext = $sk->makeKnownLinkObj( $page, '(' . $messages['diff'].')', 'diff=prev&oldid='.$row->rev_id );
188 }
189 $histlink='('.$sk->makeKnownLinkObj( $page, $messages['hist'], 'action=history' ) . ')';
190
191 $comment = $sk->commentBlock( $row->rev_comment, $page );
192 $d = $wgLang->timeanddate( $row->rev_timestamp, true );
193
194 if( $row->rev_minor_edit ) {
195 $mflag = '<span class="minor">' . $messages['minoreditletter'] . '</span> ';
196 } else {
197 $mflag = '';
198 }
199
200 $ret = "{$d} {$histlink} {$difftext} {$mflag} {$link} {$comment} {$topmarktext}";
201 if( $row->rev_deleted ) {
202 $ret = '<span class="deleted">' . $ret . '</span> ' . htmlspecialchars( wfMsg( 'deletedrev' ) );
203 }
204 $ret = "<li>$ret</li>\n";
205 wfProfileOut( $fname );
206 return $ret;
207 }
208
209 /**
210 * Generates a form used to restrict display of contributions
211 * to a specific namespace
212 *
213 * @return none
214 * @param string $target target user to show contributions for
215 * @param string $hideminor whether minor contributions are hidden
216 * @param string $namespace currently selected namespace, NULL for show all
217 * @param bool $invert inverts the namespace selection on true (default null)
218 */
219 function namespaceForm ( $target, $hideminor, $namespace, $invert ) {
220 global $wgContLang, $wgScript;
221
222 $namespaceselect = "<select name='namespace' id='nsselectbox'>";
223 $namespaceselect .= '<option value="" '.(is_null($namespace) ? ' selected="selected"' : '').'>'.wfMsg( 'contributionsall' ).'</option>';
224 $arr = $wgContLang->getFormattedNamespaces();
225 foreach( $arr as $ns => $name ) {
226 if( $ns < NS_MAIN )
227 continue;
228 $n = $ns === NS_MAIN ? wfMsg ( 'blanknamespace' ) : $name;
229 $sel = $namespace === $ns ? ' selected="selected"' : '';
230 $namespaceselect .= "<option value='$ns'$sel>$n</option>";
231 }
232 $namespaceselect .= '</select>';
233
234 $out = "<div class='namespaceselector'><form method='get' action='{$wgScript}'>";
235 $out .= '<input type="hidden" name="title" value="'.$wgContLang->specialpage( 'Contributions' ).'" />';
236 $out .= '<input type="hidden" name="target" value="'.htmlspecialchars( $target ).'" />';
237 $out .= '<input type="hidden" name="hideminor" value="'.$hideminor.'" />';
238 $out .= "
239 <div id='nsselect' class='contributions'>
240 <label for='nsselectbox'>" . wfMsg('namespace') . "</label>
241 $namespaceselect
242 <input type='submit' value='" . wfMsg( 'allpagessubmit' ) . "' />
243 <input type='checkbox' name='invert' value='1' id='nsinvert'" . ( $invert ? ' checked="checked"' : '' ) . " />
244 <label for='nsinvert'>" . wfMsg('invert') . "</label>
245 </div>";
246 $out .= '</form></div>';
247 return $out;
248 }
249 ?>