API bug 10046: incorrect action produces invalid response format
[lhc/web/wiklou.git] / includes / SpecialContributions.php
1 <?php
2 /**
3 * Special:Contributions, show user contributions in a paged list
4 * @addtogroup SpecialPage
5 */
6
7 class ContribsPager extends IndexPager {
8 public $mDefaultDirection = true;
9 var $messages, $target;
10 var $namespace = '', $mDb;
11
12 function __construct( $target, $namespace = false ) {
13 parent::__construct();
14 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist minoreditletter' ) as $msg ) {
15 $this->messages[$msg] = wfMsgExt( $msg, array( 'escape') );
16 }
17 $this->target = $target;
18 $this->namespace = $namespace;
19 $this->mDb = wfGetDB( DB_SLAVE, 'contributions' );
20 }
21
22 function getDefaultQuery() {
23 $query = parent::getDefaultQuery();
24 $query['target'] = $this->target;
25 return $query;
26 }
27
28 function getQueryInfo() {
29 list( $index, $userCond ) = $this->getUserCond();
30 $conds = array_merge( array( 'page_id=rev_page' ), $userCond, $this->getNamespaceCond() );
31
32 return array(
33 'tables' => array( 'page', 'revision' ),
34 'fields' => array(
35 'page_namespace', 'page_title', 'page_is_new', 'page_latest', 'rev_id', 'rev_page',
36 'rev_text_id', 'rev_timestamp', 'rev_comment', 'rev_minor_edit', 'rev_user',
37 'rev_user_text', 'rev_deleted'
38 ),
39 'conds' => $conds,
40 'options' => array( 'FORCE INDEX' => $index )
41 );
42 }
43
44 function getUserCond() {
45 $condition = array();
46
47 if ( $this->target == 'newbies' ) {
48 $max = $this->mDb->selectField( 'user', 'max(user_id)', false, __METHOD__ );
49 $condition[] = 'rev_user >' . (int)($max - $max / 100);
50 $index = 'user_timestamp';
51 } else {
52 $condition['rev_user_text'] = $this->target;
53 $index = 'usertext_timestamp';
54 }
55 return array( $index, $condition );
56 }
57
58 function getNamespaceCond() {
59 if ( $this->namespace !== '' ) {
60 return array( 'page_namespace' => (int)$this->namespace );
61 } else {
62 return array();
63 }
64 }
65
66 function getIndexField() {
67 return 'rev_timestamp';
68 }
69
70 function getStartBody() {
71 return "<ul>\n";
72 }
73
74 function getEndBody() {
75 return "</ul>\n";
76 }
77
78 function getNavigationBar() {
79 if ( isset( $this->mNavigationBar ) ) {
80 return $this->mNavigationBar;
81 }
82 $linkTexts = array(
83 'prev' => wfMsgHtml( "sp-contributions-newer", $this->mLimit ),
84 'next' => wfMsgHtml( 'sp-contributions-older', $this->mLimit ),
85 'first' => wfMsgHtml('sp-contributions-newest'),
86 'last' => wfMsgHtml( 'sp-contributions-oldest' )
87 );
88
89 $pagingLinks = $this->getPagingLinks( $linkTexts );
90 $limitLinks = $this->getLimitLinks();
91 $limits = implode( ' | ', $limitLinks );
92
93 $this->mNavigationBar = "({$pagingLinks['first']} | {$pagingLinks['last']}) " .
94 wfMsgHtml("viewprevnext", $pagingLinks['prev'], $pagingLinks['next'], $limits);
95 return $this->mNavigationBar;
96 }
97
98 /**
99 * Generates each row in the contributions list.
100 *
101 * Contributions which are marked "top" are currently on top of the history.
102 * For these contributions, a [rollback] link is shown for users with sysop
103 * privileges. The rollback link restores the most recent version that was not
104 * written by the target user.
105 *
106 * @todo This would probably look a lot nicer in a table.
107 */
108 function formatRow( $row ) {
109 wfProfileIn( __METHOD__ );
110
111 global $wgLang, $wgUser;
112
113 $sk = $this->getSkin();
114 $rev = new Revision( $row );
115
116 $page = Title::makeTitle( $row->page_namespace, $row->page_title );
117 $link = $sk->makeKnownLinkObj( $page );
118 $difftext = $topmarktext = '';
119 if( $row->rev_id == $row->page_latest ) {
120 $topmarktext .= '<strong>' . $this->messages['uctop'] . '</strong>';
121 if( !$row->page_is_new ) {
122 $difftext .= '(' . $sk->makeKnownLinkObj( $page, $this->messages['diff'], 'diff=0' ) . ')';
123 } else {
124 $difftext .= $this->messages['newarticle'];
125 }
126
127 if( $wgUser->isAllowed( 'rollback' ) ) {
128 $topmarktext .= ' '.$sk->generateRollback( $rev );
129 }
130
131 }
132 if( $rev->userCan( Revision::DELETED_TEXT ) ) {
133 $difftext = '(' . $sk->makeKnownLinkObj( $page, $this->messages['diff'], 'diff=prev&oldid='.$row->rev_id ) . ')';
134 } else {
135 $difftext = '(' . $this->messages['diff'] . ')';
136 }
137 $histlink='('.$sk->makeKnownLinkObj( $page, $this->messages['hist'], 'action=history' ) . ')';
138
139 $comment = $sk->revComment( $rev );
140 $d = $wgLang->timeanddate( wfTimestamp( TS_MW, $row->rev_timestamp ), true );
141
142 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
143 $d = '<span class="history-deleted">' . $d . '</span>';
144 }
145
146 if( $row->rev_minor_edit ) {
147 $mflag = '<span class="minor">' . $this->messages['minoreditletter'] . '</span> ';
148 } else {
149 $mflag = '';
150 }
151
152 $ret = "{$d} {$histlink} {$difftext} {$mflag} {$link} {$comment} {$topmarktext}";
153 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
154 $ret .= ' ' . wfMsgHtml( 'deletedrev' );
155 }
156 $ret = "<li>$ret</li>\n";
157 wfProfileOut( __METHOD__ );
158 return $ret;
159 }
160
161 /**
162 * Get the Database object in use
163 *
164 * @return Database
165 */
166 public function getDatabase() {
167 return $this->mDb;
168 }
169
170 }
171
172 /**
173 * Special page "user contributions".
174 * Shows a list of the contributions of a user.
175 *
176 * @return none
177 * @param $par String: (optional) user name of the user for which to show the contributions
178 */
179 function wfSpecialContributions( $par = null ) {
180 global $wgUser, $wgOut, $wgLang, $wgRequest;
181
182 $options = array();
183
184 if ( isset( $par ) && $par == 'newbies' ) {
185 $target = 'newbies';
186 $options['contribs'] = 'newbie';
187 } elseif ( isset( $par ) ) {
188 $target = $par;
189 } else {
190 $target = $wgRequest->getVal( 'target' );
191 }
192
193 // check for radiobox
194 if ( $wgRequest->getVal( 'contribs' ) == 'newbie' ) {
195 $target = 'newbies';
196 $options['contribs'] = 'newbie';
197 }
198
199 if ( !strlen( $target ) ) {
200 $wgOut->addHTML( contributionsForm( '' ) );
201 return;
202 }
203
204 $options['limit'] = $wgRequest->getInt( 'limit', 50 );
205 $options['target'] = $target;
206
207 $nt = Title::makeTitleSafe( NS_USER, $target );
208 if ( !$nt ) {
209 $wgOut->addHTML( contributionsForm( '' ) );
210 return;
211 }
212 $id = User::idFromName( $nt->getText() );
213
214 if ( $target != 'newbies' ) {
215 $target = $nt->getText();
216 $wgOut->setSubtitle( contributionsSub( $nt, $id ) );
217 } else {
218 $wgOut->setSubtitle( wfMsgHtml( 'sp-contributions-newbies-sub') );
219 }
220
221 if ( ( $ns = $wgRequest->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
222 $options['namespace'] = intval( $ns );
223 } else {
224 $options['namespace'] = '';
225 }
226 if ( $wgUser->isAllowed( 'rollback' ) && $wgRequest->getBool( 'bot' ) ) {
227 $options['bot'] = '1';
228 }
229
230 wfRunHooks( 'SpecialContributionsBeforeMainOutput', $id );
231
232 $wgOut->addHTML( contributionsForm( $options ) );
233
234 $pager = new ContribsPager( $target, $options['namespace'] );
235 if ( !$pager->getNumRows() ) {
236 $wgOut->addWikiText( wfMsg( 'nocontribs' ) );
237 return;
238 }
239
240 # Show a message about slave lag, if applicable
241 if( ( $lag = $pager->getDatabase()->getLag() ) > 0 )
242 $wgOut->showLagWarning( $lag );
243
244 $wgOut->addHTML(
245 '<p>' . $pager->getNavigationBar() . '</p>' .
246 $pager->getBody() .
247 '<p>' . $pager->getNavigationBar() . '</p>' );
248
249 # If there were contributions, and it was a valid user or IP, show
250 # the appropriate "footer" message - WHOIS tools, etc.
251 if( $target != 'newbies' ) {
252 $message = IP::isIPAddress( $target )
253 ? 'sp-contributions-footer-anon'
254 : 'sp-contributions-footer';
255
256
257 $text = wfMsg( $message, $target );
258 if( !wfEmptyMsg( $message, $text ) && $text != '-' ) {
259 $wgOut->addHtml( '<div class="mw-contributions-footer">' );
260 $wgOut->addWikiText( $text );
261 $wgOut->addHtml( '</div>' );
262 }
263 }
264 }
265
266 /**
267 * Generates the subheading with links
268 * @param Title $nt Title object for the target
269 * @param integer $id User ID for the target
270 * @return String: appropriately-escaped HTML to be output literally
271 */
272 function contributionsSub( $nt, $id ) {
273 global $wgSysopUserBans, $wgLang, $wgUser;
274
275 $sk = $wgUser->getSkin();
276
277 if ( 0 == $id ) {
278 $user = $nt->getText();
279 } else {
280 $user = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
281 }
282 $talk = $nt->getTalkPage();
283 if( $talk ) {
284 # Talk page link
285 $tools[] = $sk->makeLinkObj( $talk, wfMsgHtml( 'talkpagelinktext' ) );
286 if( ( $id != 0 && $wgSysopUserBans ) || ( $id == 0 && User::isIP( $nt->getText() ) ) ) {
287 # Block link
288 if( $wgUser->isAllowed( 'block' ) )
289 $tools[] = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'Blockip', $nt->getDBkey() ), wfMsgHtml( 'blocklink' ) );
290 # Block log link
291 $tools[] = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'Log' ), wfMsgHtml( 'sp-contributions-blocklog' ), 'type=block&page=' . $nt->getPrefixedUrl() );
292 }
293 # Other logs link
294 $tools[] = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'Log' ), wfMsgHtml( 'log' ), 'user=' . $nt->getPartialUrl() );
295 $links = implode( ' | ', $tools );
296 }
297
298 // Old message 'contribsub' had one parameter, but that doesn't work for
299 // languages that want to put the "for" bit right after $user but before
300 // $links. If 'contribsub' is around, use it for reverse compatibility,
301 // otherwise use 'contribsub2'.
302 if( wfEmptyMsg( 'contribsub', wfMsg( 'contribsub' ) ) ) {
303 return wfMsgHtml( 'contribsub2', $user, $links );
304 } else {
305 return wfMsgHtml( 'contribsub', "$user ($links)" );
306 }
307 }
308
309 /**
310 * Generates the namespace selector form with hidden attributes.
311 * @param $options Array: the options to be included.
312 */
313 function contributionsForm( $options ) {
314 global $wgScript, $wgTitle, $wgRequest;
315
316 $options['title'] = $wgTitle->getPrefixedText();
317 if ( !isset( $options['target'] ) ) {
318 $options['target'] = '';
319 } else {
320 $options['target'] = str_replace( '_' , ' ' , $options['target'] );
321 }
322
323 if ( !isset( $options['namespace'] ) ) {
324 $options['namespace'] = '';
325 }
326
327 if ( !isset( $options['contribs'] ) ) {
328 $options['contribs'] = 'user';
329 }
330
331 if ( $options['contribs'] == 'newbie' ) {
332 $options['target'] = '';
333 }
334
335 $f = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
336
337 foreach ( $options as $name => $value ) {
338 if ( in_array( $name, array( 'namespace', 'target', 'contribs' ) ) ) {
339 continue;
340 }
341 $f .= "\t" . Xml::hidden( $name, $value ) . "\n";
342 }
343
344 $f .= '<fieldset>' .
345 Xml::element( 'legend', array(), wfMsg( 'sp-contributions-search' ) ) .
346 Xml::radioLabel( wfMsgExt( 'sp-contributions-newbies', array( 'parseinline' ) ), 'contribs' , 'newbie' , 'newbie', $options['contribs'] == 'newbie' ? true : false ) . '<br />' .
347 Xml::radioLabel( wfMsgExt( 'sp-contributions-username', array( 'parseinline' ) ), 'contribs' , 'user', 'user', $options['contribs'] == 'user' ? true : false ) . ' ' .
348 Xml::input( 'target', 20, $options['target']) . ' '.
349 Xml::label( wfMsg( 'namespace' ), 'namespace' ) .
350 Xml::namespaceSelector( $options['namespace'], '' ) .
351 Xml::submitButton( wfMsg( 'sp-contributions-submit' ) ) .
352 '</fieldset>' .
353 Xml::closeElement( 'form' );
354 return $f;
355 }
356
357
358 ?>