1eae5b904cac789a6936dfe3237306224e055a11
[lhc/web/wiklou.git] / includes / SpecialDeletedcontribs.php
1 <?php
2 /**
3 * @package MediaWiki
4 * @subpackage SpecialPage
5 */
6
7 /** @package MediaWiki */
8 class DeletedContribsFinder extends ContribsFinder {
9 var $username, $offset, $limit, $namespace;
10 var $dbr;
11
12 function getEditLimit( $dir ) {
13 list( $index, $usercond ) = $this->getUserCond();
14 $nscond = $this->getNamespaceCond();
15 $use_index = $this->dbr->useIndexClause( $index );
16 list( $archive ) = $this->dbr->tableNamesN( 'archive' );
17 $sql = "SELECT ar_timestamp " .
18 " FROM $archive $use_index " .
19 " WHERE $usercond $nscond" .
20 " ORDER BY ar_timestamp $dir LIMIT 1";
21
22 $res = $this->dbr->query( $sql, __METHOD__ );
23 $row = $this->dbr->fetchObject( $res );
24 if ( $row ) {
25 return $row->ar_timestamp;
26 } else {
27 return false;
28 }
29 }
30
31 function getUserCond() {
32 $condition = ' ar_user_text=' . $this->dbr->addQuotes( $this->username );
33 $index = 'usertext_timestamp';
34 return array( $index, $condition );
35 }
36
37 function getNamespaceCond() {
38 if ( $this->namespace !== false )
39 return ' AND ar_namespace = ' . (int)$this->namespace;
40 return '';
41 }
42
43 # The following functions share a lot of code with each other and with
44 # their counterparts in ContribsFinder. Some refactoring should help.
45
46 function getPreviousOffsetForPaging() {
47 list( $index, $usercond ) = $this->getUserCond();
48 $nscond = $this->getNamespaceCond();
49
50 $use_index = $this->dbr->useIndexClause( $index );
51 list( $archive ) = $this->dbr->tableNamesN( 'archive' );
52
53 $sql = "SELECT ar_timestamp FROM $archive $use_index " .
54 "WHERE ar_timestamp > '" . $this->offset . "' AND " .
55 $usercond . $nscond;
56 $sql .= " ORDER BY ar_timestamp ASC";
57 $sql = $this->dbr->limitResult( $sql, $this->limit, 0 );
58 $res = $this->dbr->query( $sql );
59
60 $numRows = $this->dbr->numRows( $res );
61 if ( $numRows ) {
62 $this->dbr->dataSeek( $res, $numRows - 1 );
63 $row = $this->dbr->fetchObject( $res );
64 $offset = $row->ar_timestamp;
65 } else {
66 $offset = false;
67 }
68 $this->dbr->freeResult( $res );
69 return $offset;
70 }
71
72 function getFirstOffsetForPaging() {
73 list( $index, $usercond ) = $this->getUserCond();
74 $use_index = $this->dbr->useIndexClause( $index );
75 list( $archive ) = $this->dbr->tableNamesN( 'archive' );
76 $nscond = $this->getNamespaceCond();
77 $sql = "SELECT ar_timestamp FROM $archive $use_index " .
78 "WHERE " .
79 $usercond . $nscond;
80 $sql .= " ORDER BY ar_timestamp ASC";
81 $sql = $this->dbr->limitResult( $sql, $this->limit, 0 );
82 $res = $this->dbr->query( $sql );
83
84 $numRows = $this->dbr->numRows( $res );
85 if ( $numRows ) {
86 $this->dbr->dataSeek( $res, $numRows - 1 );
87 $row = $this->dbr->fetchObject( $res );
88 $offset = $row->ar_timestamp;
89 } else {
90 $offset = false;
91 }
92 $this->dbr->freeResult( $res );
93 return $offset;
94 }
95
96 /* private */ function makeSql() {
97 $offsetQuery = '';
98
99 list( $archive ) = $this->dbr->tableNamesN( 'archive' );
100 list( $index, $userCond ) = $this->getUserCond();
101
102 if ( $this->offset )
103 $offsetQuery = "AND ar_timestamp <= '{$this->offset}'";
104
105 $nscond = $this->getNamespaceCond();
106 $use_index = $this->dbr->useIndexClause( $index );
107 $sql = "SELECT
108 ar_namespace,ar_title,
109 ar_rev_id,ar_text_id,ar_timestamp,ar_comment,ar_minor_edit,ar_user,ar_user_text
110 FROM $archive $use_index
111 WHERE $userCond $nscond $offsetQuery
112 ORDER BY ar_timestamp DESC";
113 $sql = $this->dbr->limitResult( $sql, $this->limit, 0 );
114 return $sql;
115 }
116 };
117
118 /**
119 * Special page "deleted contributions".
120 * Shows a list of the deleted contributions of a user.
121 *
122 * XXX This is almost and exact duplicate of the code in
123 * XXX SpecialContributions.php and should be merged with it.
124 *
125 * @return none
126 * @param $par String: (optional) user name of the user for which to show the contributions
127 */
128 function wfSpecialDeletedcontribs( $par = null ) {
129 global $wgUser, $wgOut, $wgLang, $wgRequest;
130
131 $target = isset( $par ) ? $par : $wgRequest->getVal( 'target' );
132 if ( !strlen( $target ) ) {
133 $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
134 return;
135 }
136
137 $nt = Title::newFromURL( $target );
138 if ( !$nt ) {
139 $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
140 return;
141 }
142
143 $options = array();
144
145 list( $options['limit'], $options['offset']) = wfCheckLimits();
146 $options['offset'] = $wgRequest->getVal( 'offset' );
147 /* Offset must be an integral. */
148 if ( !strlen( $options['offset'] ) || !preg_match( '/^[0-9]+$/', $options['offset'] ) )
149 $options['offset'] = '';
150
151 $title = SpecialPage::getTitleFor( 'Deletedcontribs' );
152 $options['target'] = $target;
153
154 $nt =& Title::makeTitle( NS_USER, $nt->getDBkey() );
155 $finder = new DeletedContribsFinder( $nt->getText() );
156 $finder->setLimit( $options['limit'] );
157 $finder->setOffset( $options['offset'] );
158
159 if ( ( $ns = $wgRequest->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
160 $options['namespace'] = intval( $ns );
161 $finder->setNamespace( $options['namespace'] );
162 } else {
163 $options['namespace'] = '';
164 }
165
166 if ( $wgRequest->getText( 'go' ) == 'prev' ) {
167 $offset = $finder->getPreviousOffsetForPaging();
168 if ( $offset !== false ) {
169 $options['offset'] = $offset;
170 $prevurl = $title->getLocalURL( wfArrayToCGI( $options ) );
171 $wgOut->redirect( $prevurl );
172 return;
173 }
174 }
175
176 if ( $wgRequest->getText( 'go' ) == 'first' ) {
177 $offset = $finder->getFirstOffsetForPaging();
178 if ( $offset !== false ) {
179 $options['offset'] = $offset;
180 $prevurl = $title->getLocalURL( wfArrayToCGI( $options ) );
181 $wgOut->redirect( $prevurl );
182 return;
183 }
184 }
185
186 $wgOut->setSubtitle( wfMsgHtml( 'contribsub', contributionsSub( $nt ) ) );
187
188 $id = User::idFromName( $nt->getText() );
189 wfRunHooks( 'SpecialDeletedcontribsBeforeMainOutput', $id );
190
191 $wgOut->addHTML( contributionsForm( $options) );
192
193 $contribs = $finder->find();
194
195 if ( count( $contribs ) == 0) {
196 $wgOut->addWikiText( wfMsg( 'nocontribs' ) );
197 return;
198 }
199
200 list( $early, $late ) = $finder->getEditLimits();
201 $lastts = count( $contribs ) ? $contribs[count( $contribs ) - 1]->ar_timestamp : 0;
202 $atstart = ( !count( $contribs ) || $late == $contribs[0]->ar_timestamp );
203 $atend = ( !count( $contribs ) || $early == $lastts );
204
205 // These four are defaults
206 $newestlink = wfMsgHtml( 'sp-contributions-newest' );
207 $oldestlink = wfMsgHtml( 'sp-contributions-oldest' );
208 $newerlink = wfMsgHtml( 'sp-contributions-newer', $options['limit'] );
209 $olderlink = wfMsgHtml( 'sp-contributions-older', $options['limit'] );
210
211 if ( !$atstart ) {
212 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'offset' => '' ), $options ) );
213 $newestlink = "<a href=\"$stuff\">$newestlink</a>";
214 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'go' => 'prev' ), $options ) );
215 $newerlink = "<a href=\"$stuff\">$newerlink</a>";
216 }
217
218 if ( !$atend ) {
219 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'go' => 'first' ), $options ) );
220 $oldestlink = "<a href=\"$stuff\">$oldestlink</a>";
221 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'offset' => $lastts ), $options ) );
222 $olderlink = "<a href=\"$stuff\">$olderlink</a>";
223 }
224
225 $firstlast = "($newestlink | $oldestlink)";
226
227 $urls = array();
228 foreach ( array( 20, 50, 100, 250, 500 ) as $num ) {
229 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'limit' => $num ), $options ) );
230 $urls[] = "<a href=\"$stuff\">".$wgLang->formatNum( $num )."</a>";
231 }
232 $bits = implode( $urls, ' | ' );
233
234 $prevnextbits = $firstlast .' '. wfMsgHtml( 'viewprevnext', $newerlink, $olderlink, $bits );
235
236 $wgOut->addHTML( "<p>{$prevnextbits}</p>\n" );
237
238 $wgOut->addHTML( "<ul>\n" );
239
240 $sk = $wgUser->getSkin();
241 foreach ( $contribs as $contrib )
242 $wgOut->addHTML( dcListEdit( $sk, $contrib ) );
243
244 $wgOut->addHTML( "</ul>\n" );
245 $wgOut->addHTML( "<p>{$prevnextbits}</p>\n" );
246 }
247
248
249 /**
250 * Generates each row in the deleted contributions list.
251 *
252 * @todo This would probably look a lot nicer in a table.
253 */
254 function dcListEdit( $sk, $row ) {
255 $fname = 'dcListEdit';
256 wfProfileIn( $fname );
257
258 global $wgLang, $wgUser, $wgRequest;
259 static $messages, $undelete;
260 if( !isset( $messages ) ) {
261 foreach( explode( ' ', 'undel minoreditletter' ) as $msg ) {
262 $messages[$msg] = wfMsgExt( $msg, array( 'escape') );
263 }
264 }
265 if( !isset( $undelete ) ) {
266 $undelete =& SpecialPage::getTitleFor( 'Undelete' );
267 }
268
269 $page = Title::makeTitle( $row->ar_namespace, $row->ar_title )->getPrefixedText();
270
271 $ts = wfTimestamp( TS_MW, $row->ar_timestamp );
272 $d = $wgLang->timeanddate( $ts, true );
273
274 $link = $sk->makeKnownLinkObj( $undelete, $page, "target=$page&timestamp=$ts" );
275 $undellink = '(' . $sk->makeKnownLinkObj( $undelete, $messages['undel'], "target=$page" ) . ')';
276
277 if( $row->ar_minor_edit ) {
278 $mflag = '<span class="minor">' . $messages['minoreditletter'] . '</span> ';
279 } else {
280 $mflag = '';
281 }
282
283 $comment = $sk->commentBlock( $row->ar_comment );
284
285 $ret = "<li>{$d}{$mflag} {$link} {$undellink} {$comment}</li>";
286 wfProfileOut( $fname );
287 return $ret;
288 }
289
290 ?>