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