Add extension DeletedContributions to core
[lhc/web/wiklou.git] / includes / specials / SpecialDeletedContributions.php
1 <?php
2 /**
3 * Implements Special:DeletedContributions to display archived revisions
4 * @ingroup SpecialPage
5 */
6
7 class DeletedContribsPager 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( ' ', 'deletionlog undeletebtn minoreditletter diff' ) 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( $userCond, $this->getNamespaceCond() );
31
32 return array(
33 'tables' => array( 'archive' ),
34 'fields' => array(
35 'ar_rev_id', 'ar_namespace', 'ar_title', 'ar_timestamp', 'ar_comment', 'ar_minor_edit',
36 'ar_user', 'ar_user_text', 'ar_deleted'
37 ),
38 'conds' => $conds,
39 'options' => array( 'FORCE INDEX' => $index )
40 );
41 }
42
43 function getUserCond() {
44 $condition = array();
45
46 $condition['ar_user_text'] = $this->target;
47 $index = 'usertext_timestamp';
48
49 return array( $index, $condition );
50 }
51
52 function getIndexField() {
53 return 'ar_timestamp';
54 }
55
56 function getStartBody() {
57 return "<ul>\n";
58 }
59
60 function getEndBody() {
61 return "</ul>\n";
62 }
63
64 function getNavigationBar() {
65 if ( isset( $this->mNavigationBar ) ) {
66 return $this->mNavigationBar;
67 }
68 $linkTexts = array(
69 'prev' => wfMsgHtml( 'pager-newer-n', $this->mLimit ),
70 'next' => wfMsgHtml( 'pager-older-n', $this->mLimit ),
71 'first' => wfMsgHtml( 'page_first' ),
72 'last' => wfMsgHtml( 'page_last' )
73 );
74
75 $pagingLinks = $this->getPagingLinks( $linkTexts );
76 $limitLinks = $this->getLimitLinks();
77 $limits = implode( ' | ', $limitLinks );
78
79 $this->mNavigationBar = "({$pagingLinks['first']} | {$pagingLinks['last']}) " .
80 wfMsgExt( 'viewprevnext', array( 'parsemag' ), $pagingLinks['prev'], $pagingLinks['next'], $limits );
81 return $this->mNavigationBar;
82 }
83
84 function getNamespaceCond() {
85 if ( $this->namespace !== '' ) {
86 return array( 'ar_namespace' => (int)$this->namespace );
87 } else {
88 return array();
89 }
90 }
91
92 /**
93 * Generates each row in the contributions list.
94 *
95 * Contributions which are marked "top" are currently on top of the history.
96 * For these contributions, a [rollback] link is shown for users with sysop
97 * privileges. The rollback link restores the most recent version that was not
98 * written by the target user.
99 *
100 * @todo This would probably look a lot nicer in a table.
101 */
102 function formatRow( $row ) {
103 wfProfileIn( __METHOD__ );
104
105 global $wgLang, $wgUser;
106
107 $sk = $this->getSkin();
108
109 $rev = new Revision( array(
110 'id' => $row->ar_rev_id,
111 'comment' => $row->ar_comment,
112 'user' => $row->ar_user,
113 'user_text' => $row->ar_user_text,
114 'timestamp' => $row->ar_timestamp,
115 'minor_edit' => $row->ar_minor_edit,
116 'rev_deleted' => $row->ar_deleted,
117 ) );
118
119 $page = Title::makeTitle( $row->ar_namespace, $row->ar_title );
120
121 $undelete = SpecialPage::getTitleFor( 'Undelete' );
122
123 $logs = SpecialPage::getTitleFor( 'Log' );
124 $dellog = $sk->makeKnownLinkObj( $logs,
125 $this->messages['deletionlog'],
126 'type=delete&page=' . $page->getPrefixedUrl() );
127
128 $reviewlink = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'Undelete', $page->getPrefixedDBkey() ),
129 $this->messages['undeletebtn'] );
130
131 $link = $sk->makeKnownLinkObj( $undelete,
132 htmlspecialchars( $page->getPrefixedText() ),
133 'target=' . $page->getPrefixedUrl() .
134 '&timestamp=' . $rev->getTimestamp() );
135
136 $last = $sk->makeKnownLinkObj( $undelete,
137 $this->messages['diff'],
138 "target=" . $page->getPrefixedUrl() .
139 "&timestamp=" . $rev->getTimestamp() .
140 "&diff=prev" );
141
142 $comment = $sk->revComment( $rev );
143 $d = $wgLang->timeanddate( $rev->getTimestamp(), true );
144
145 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
146 $d = '<span class="history-deleted">' . $d . '</span>';
147 } else {
148 $link = $sk->makeKnownLinkObj( $undelete, $d,
149 'target=' . $page->getPrefixedUrl() .
150 '&timestamp=' . $rev->getTimestamp() );
151 }
152
153 $pagelink = $sk->makeLinkObj( $page );
154
155 if( $rev->isMinor() ) {
156 $mflag = '<span class="minor">' . $this->messages['minoreditletter'] . '</span> ';
157 } else {
158 $mflag = '';
159 }
160
161
162 $ret = "{$link} ($last) ({$dellog}) ({$reviewlink}) . . {$mflag} {$pagelink} {$comment}";
163 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
164 $ret .= ' ' . wfMsgHtml( 'deletedrev' );
165 }
166
167 $ret = "<li>$ret</li>\n";
168
169 wfProfileOut( __METHOD__ );
170 return $ret;
171 }
172
173 /**
174 * Get the Database object in use
175 *
176 * @return Database
177 */
178 public function getDatabase() {
179 return $this->mDb;
180 }
181 }
182
183 class DeletedContributionsPage extends SpecialPage {
184 function __construct() {
185 parent::__construct( 'DeletedContributions', 'deletedhistory',
186 /*listed*/ true, /*function*/ false, /*file*/ false );
187 }
188
189 /**
190 * Special page "deleted user contributions".
191 * Shows a list of the deleted contributions of a user.
192 *
193 * @return none
194 * @param $par String: (optional) user name of the user for which to show the contributions
195 */
196 function execute( $par ) {
197 global $wgUser;
198 $this->setHeaders();
199
200 if ( !$this->userCanExecute( $wgUser ) ) {
201 $this->displayRestrictionError();
202 return;
203 }
204
205 global $wgUser, $wgOut, $wgLang, $wgRequest;
206
207 $options = array();
208
209 if ( isset( $par ) ) {
210 $target = $par;
211 } else {
212 $target = $wgRequest->getVal( 'target' );
213 }
214
215 if ( !strlen( $target ) ) {
216 $wgOut->addHTML( $this->getForm( '' ) );
217 return;
218 }
219
220 $options['limit'] = $wgRequest->getInt( 'limit', 50 );
221 $options['target'] = $target;
222
223 $nt = Title::makeTitleSafe( NS_USER, $target );
224 if ( !$nt ) {
225 $wgOut->addHTML( $this->getForm( '' ) );
226 return;
227 }
228 $id = User::idFromName( $nt->getText() );
229
230 $target = $nt->getText();
231 $wgOut->setSubtitle( $this->getSubTitle( $nt, $id ) );
232
233 if ( ( $ns = $wgRequest->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
234 $options['namespace'] = intval( $ns );
235 } else {
236 $options['namespace'] = '';
237 }
238
239 $wgOut->addHTML( $this->getForm( $options ) );
240
241 $pager = new DeletedContribsPager( $target, $options['namespace'] );
242 if ( !$pager->getNumRows() ) {
243 $wgOut->addWikiText( wfMsg( 'nocontribs' ) );
244 return;
245 }
246
247 # Show a message about slave lag, if applicable
248 if( ( $lag = $pager->getDatabase()->getLag() ) > 0 )
249 $wgOut->showLagWarning( $lag );
250
251 $wgOut->addHTML(
252 '<p>' . $pager->getNavigationBar() . '</p>' .
253 $pager->getBody() .
254 '<p>' . $pager->getNavigationBar() . '</p>' );
255
256 # If there were contributions, and it was a valid user or IP, show
257 # the appropriate "footer" message - WHOIS tools, etc.
258 if( $target != 'newbies' ) {
259 $message = IP::isIPAddress( $target )
260 ? 'sp-contributions-footer-anon'
261 : 'sp-contributions-footer';
262
263
264 $text = wfMsgNoTrans( $message, $target );
265 if( !wfEmptyMsg( $message, $text ) && $text != '-' ) {
266 $wgOut->addHtml( '<div class="mw-contributions-footer">' );
267 $wgOut->addWikiText( $text );
268 $wgOut->addHtml( '</div>' );
269 }
270 }
271 }
272
273 /**
274 * Generates the subheading with links
275 * @param $nt @see Title object for the target
276 */
277 function getSubTitle( $nt, $id ) {
278 global $wgSysopUserBans, $wgLang, $wgUser;
279
280 $sk = $wgUser->getSkin();
281
282 if ( 0 == $id ) {
283 $user = $nt->getText();
284 } else {
285 $user = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
286 }
287 $talk = $nt->getTalkPage();
288 if( $talk ) {
289 # Talk page link
290 $tools[] = $sk->makeLinkObj( $talk, wfMsgHtml( 'talkpagelinktext' ) );
291 if( ( $id != 0 && $wgSysopUserBans ) || ( $id == 0 && User::isIP( $nt->getText() ) ) ) {
292 # Block link
293 if( $wgUser->isAllowed( 'block' ) )
294 $tools[] = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'Blockip', $nt->getDBkey() ),
295 wfMsgHtml( 'blocklink' ) );
296 # Block log link
297 $tools[] = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'Log' ),
298 wfMsgHtml( 'sp-contributions-blocklog' ), 'type=block&page=' . $nt->getPrefixedUrl() );
299 }
300 # Other logs link
301 $tools[] = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'Log' ),
302 wfMsgHtml( 'log' ), 'user=' . $nt->getPartialUrl() );
303 # Link to undeleted contributions
304 $tools[] = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'Contributions', $nt->getDBkey() ),
305 wfMsgHtml( 'contributions' ) );
306
307 $links = implode( ' | ', $tools );
308 }
309
310 // Old message 'contribsub' had one parameter, but that doesn't work for
311 // languages that want to put the "for" bit right after $user but before
312 // $links. If 'contribsub' is around, use it for reverse compatibility,
313 // otherwise use 'contribsub2'.
314 if( wfEmptyMsg( 'contribsub', wfMsg( 'contribsub' ) ) ) {
315 return wfMsgHtml( 'contribsub2', $user, $links );
316 } else {
317 return wfMsgHtml( 'contribsub', "$user ($links)" );
318 }
319 }
320
321 /**
322 * Generates the namespace selector form with hidden attributes.
323 * @param $options Array: the options to be included.
324 */
325 function getForm( $options ) {
326 global $wgScript, $wgTitle, $wgRequest;
327
328 $options['title'] = $wgTitle->getPrefixedText();
329 if ( !isset( $options['target'] ) ) {
330 $options['target'] = '';
331 } else {
332 $options['target'] = str_replace( '_' , ' ' , $options['target'] );
333 }
334
335 if ( !isset( $options['namespace'] ) ) {
336 $options['namespace'] = '';
337 }
338
339 if ( !isset( $options['contribs'] ) ) {
340 $options['contribs'] = 'user';
341 }
342
343 if ( $options['contribs'] == 'newbie' ) {
344 $options['target'] = '';
345 }
346
347 $f = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
348
349 foreach ( $options as $name => $value ) {
350 if ( in_array( $name, array( 'namespace', 'target', 'contribs' ) ) ) {
351 continue;
352 }
353 $f .= "\t" . Xml::hidden( $name, $value ) . "\n";
354 }
355
356 $f .= Xml::openElement( 'fieldset' ) .
357 Xml::element( 'legend', array(), wfMsg( 'sp-contributions-search' ) ) .
358 Xml::tags( 'label', array( 'for' => 'target' ), wfMsgExt( 'sp-contributions-username', 'parseinline' ) ) . ' ' .
359 Xml::input( 'target', 20, $options['target']) . ' '.
360 Xml::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
361 Xml::namespaceSelector( $options['namespace'], '' ) . ' ' .
362 Xml::submitButton( wfMsg( 'sp-contributions-submit' ) ) .
363 Xml::closeElement( 'fieldset' ) .
364 Xml::closeElement( 'form' );
365 return $f;
366 }
367 }