Tietew's patch for optionally selecting revisions on undeletion.
[lhc/web/wiklou.git] / includes / SpecialUndelete.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 * @subpackage SpecialPage
6 */
7
8 /**
9 *
10 */
11 function wfSpecialUndelete( $par ) {
12 global $wgRequest;
13
14 $form = new UndeleteForm( $wgRequest, $par );
15 $form->execute();
16 }
17
18 /**
19 *
20 * @package MediaWiki
21 * @subpackage SpecialPage
22 */
23 class PageArchive {
24 var $title;
25
26 function PageArchive( &$title ) {
27 if( is_null( $title ) ) {
28 wfDebugDieBacktrace( 'Archiver() given a null title.');
29 }
30 $this->title =& $title;
31 }
32
33 /* static */ function &listAllPages() {
34 $dbr =& wfGetDB( DB_SLAVE );
35 $archive = $dbr->tableName( 'archive' );
36
37 $sql = "SELECT ar_namespace,ar_title, COUNT(*) AS count FROM $archive " .
38 "GROUP BY ar_namespace,ar_title ORDER BY ar_namespace,ar_title";
39
40 return $dbr->resultObject( $dbr->query( $sql, 'PageArchive::listAllPages' ) );
41 }
42
43 function &listRevisions() {
44 $dbr =& wfGetDB( DB_SLAVE );
45 $sql = "SELECT ar_minor_edit,ar_timestamp,ar_user,ar_user_text,ar_comment
46 FROM archive WHERE ar_namespace=" . $this->title->getNamespace() .
47 " AND ar_title='" . $dbr->strencode( $this->title->getDBkey() ) .
48 "' ORDER BY ar_timestamp DESC";
49 return $dbr->resultObject( $dbr->query( $sql ) );
50 }
51
52 function getRevisionText( $timestamp ) {
53 $dbr =& wfGetDB( DB_SLAVE );
54 $row = $dbr->selectRow( 'archive',
55 array( 'ar_text', 'ar_flags' ),
56 array( 'ar_namespace' => $this->title->getNamespace(),
57 'ar_title' => $this->title->getDbkey(),
58 'ar_timestamp' => $timestamp ) );
59 return Article::getRevisionText( $row, "ar_" );
60 }
61
62 function getLastRevisionText() {
63 $dbr =& wfGetDB( DB_SLAVE );
64 $archive = $dbr->tableName( 'archive' );
65
66 # Get text of first revision
67 $sql = "SELECT ar_text,ar_flags FROM $archive WHERE ar_namespace=" . $this->title->getNamespace() . " AND ar_title='" .
68 $dbr->strencode( $this->title->getDBkey() ) . "' ORDER BY ar_timestamp DESC LIMIT 1";
69 $ret = $dbr->query( $sql );
70 $row = $dbr->fetchObject( $ret );
71 $dbr->freeResult( $ret );
72 if( $row ) {
73 return Article::getRevisionText( $row, "ar_" );
74 } else {
75 return NULL;
76 }
77 }
78
79 function isDeleted() {
80 $dbr =& wfGetDB( DB_SLAVE );
81 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
82 array( 'ar_namespace' => $this->title->getNamespace(),
83 'ar_title' => $this->title->getDBkey() ) );
84 return ($n > 0);
85 }
86
87 function undelete( $timestamps ) {
88 global $wgUser, $wgOut, $wgLang, $wgDeferredUpdateList;
89 global $wgUseSquid, $wgInternalServer, $wgLinkCache;
90
91 $fname = "doUndeleteArticle";
92 $restoreAll = empty( $timestamps );
93 $restoreRevisions = count( $timestamps );
94
95 $dbw =& wfGetDB( DB_MASTER );
96 extract( $dbw->tableNames( 'cur', 'archive', 'old' ) );
97 $namespace = $this->title->getNamespace();
98 $ttl = $this->title->getDBkey();
99 $t = $dbw->strencode( $ttl );
100
101 # Move article and history from the "archive" table
102 $sql = "SELECT COUNT(*) AS count FROM $cur WHERE cur_namespace={$namespace} AND cur_title='{$t}' FOR UPDATE";
103 $res = $dbw->query( $sql, $fname );
104 $row = $dbw->fetchObject( $res );
105 $now = wfTimestampNow();
106
107 if( $row->count == 0) {
108 # Have to create new article...
109 $sql = "SELECT ar_text,ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit,ar_flags FROM $archive WHERE ar_namespace={$namespace} AND ar_title='{$t}' ";
110 if( !$restoreAll ) {
111 $max = $dbw->addQuotes( $dbw->timestamp( array_shift( $timestamps ) ) );
112 $sql .= "AND ar_timestamp={$max} ";
113 }
114 $sql .= "ORDER BY ar_timestamp DESC LIMIT 1 FOR UPDATE";
115 $res = $dbw->query( $sql, $fname );
116 $s = $dbw->fetchObject( $res );
117 if( $restoreAll ) {
118 $max = $s->ar_timestamp;
119 }
120 $text = Article::getRevisionText( $s, "ar_" );
121
122 $redirect = MagicWord::get( MAG_REDIRECT );
123 $redir = $redirect->matchStart( $text ) ? 1 : 0;
124
125 $rand = number_format( mt_rand() / mt_getrandmax(), 12, '.', '' );
126 $dbw->insertArray( 'cur', array(
127 'cur_id' => $dbw->nextSequenceValue( 'cur_cur_id_seq' ),
128 'cur_namespace' => $namespace,
129 'cur_title' => $ttl,
130 'cur_text' => $text,
131 'cur_comment' => $s->ar_comment,
132 'cur_user' => $s->ar_user,
133 'cur_timestamp' => $s->ar_timestamp,
134 'cur_minor_edit' => $s->ar_minor_edit,
135 'cur_user_text' => $s->ar_user_text,
136 'cur_is_redirect' => $redir,
137 'cur_random' => $rand,
138 'cur_touched' => $dbw->timestamp( $now ),
139 'inverse_timestamp' => wfInvertTimestamp( wfTimestamp( TS_MW, $s->ar_timestamp ) ),
140 ), $fname );
141
142 $newid = $dbw->insertId();
143 if( $restoreAll ) {
144 $oldones = "AND ar_timestamp<{$max}";
145 }
146 } else {
147 # If already exists, put history entirely into old table
148 $oldones = "";
149 $newid = 0;
150
151 # But to make the history list show up right, we need to touch it.
152 $sql = "UPDATE $cur SET cur_touched='{$now}' WHERE cur_namespace={$namespace} AND cur_title='{$t}'";
153 $dbw->query( $sql, $fname );
154
155 # FIXME: Sometimes restored entries will be _newer_ than the current version.
156 # We should merge.
157 }
158
159 if( !$restoreAll ) {
160 $oldts = array();
161 foreach( $timestamps as $ts ) {
162 array_push( $oldts, $dbw->addQuotes( $dbw->timestamp( $ts ) ) );
163 }
164 $oldts = join( ",", $oldts );
165 $oldones = "AND ar_timestamp IN ( {$oldts} )";
166 }
167 $sql = "INSERT INTO $old (old_namespace,old_title,old_text," .
168 "old_comment,old_user,old_user_text,old_timestamp,inverse_timestamp,old_minor_edit," .
169 "old_flags) SELECT ar_namespace,ar_title,ar_text,ar_comment," .
170 "ar_user,ar_user_text,ar_timestamp,99999999999999-ar_timestamp,ar_minor_edit,ar_flags " .
171 "FROM $archive WHERE ar_namespace={$namespace} AND ar_title='{$t}' {$oldones}";
172 if( $restoreAll || !empty( $oldts ) ) {
173 $dbw->query( $sql, $fname );
174 }
175
176 # Finally, clean up the link tables
177 if( $newid ) {
178 $wgLinkCache = new LinkCache();
179 # Select for update
180 $wgLinkCache->forUpdate( true );
181 # Create a dummy OutputPage to update the outgoing links
182 $dummyOut = new OutputPage();
183 $dummyOut->addWikiText( $text );
184
185 $u = new LinksUpdate( $newid, $this->title->getPrefixedDBkey() );
186 array_push( $wgDeferredUpdateList, $u );
187
188 Article::onArticleCreate( $this->title );
189
190 #TODO: SearchUpdate, etc.
191 }
192
193 # Now that it's safely stored, take it out of the archive
194 $sql = "DELETE FROM $archive WHERE ar_namespace={$namespace} AND " .
195 "ar_title='{$t}'";
196 if( !$restoreAll ) {
197 $sql .= " AND ar_timestamp IN ( {$oldts}";
198 if( $newid ) {
199 if( !empty( $oldts ) ) $sql .= ",";
200 $sql .= $max;
201 }
202 $sql .= ")";
203 }
204 $dbw->query( $sql, $fname );
205
206
207 # Touch the log?
208 $log = new LogPage( 'delete' );
209 if( $restoreAll ) {
210 $reason = "";
211 } else {
212 $reason = wfMsg( 'undeletedrevisions', $restoreRevisions );
213 }
214 $log->addEntry( 'restore', $this->title, $reason );
215
216 return true;
217 }
218 }
219
220 /**
221 *
222 * @package MediaWiki
223 * @subpackage SpecialPage
224 */
225 class UndeleteForm {
226 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
227 var $mTargetTimestamp;
228
229 function UndeleteForm( &$request, $par = "" ) {
230 $this->mAction = $request->getText( 'action' );
231 $this->mTarget = $request->getText( 'target' );
232 $this->mTimestamp = $request->getText( 'timestamp' );
233 $this->mRestore = $request->getCheck( 'restore' ) && $request->wasPosted();
234 if( $par != "" ) {
235 $this->mTarget = $par;
236 }
237 if ( $this->mTarget !== "" ) {
238 $this->mTargetObj = Title::newFromURL( $this->mTarget );
239 } else {
240 $this->mTargetObj = NULL;
241 }
242 if( $this->mRestore ) {
243 $timestamps = array();
244 foreach( $_REQUEST as $key => $val ) {
245 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
246 array_push( $timestamps, $matches[1] );
247 }
248 }
249 rsort( $timestamps );
250 $this->mTargetTimestamp = $timestamps;
251 }
252 }
253
254 function execute() {
255 if( is_null( $this->mTargetObj ) ) {
256 return $this->showList();
257 }
258 if( $this->mTimestamp !== "" ) {
259 return $this->showRevision( $this->mTimestamp );
260 }
261 if( $this->mRestore && $this->mAction == "submit" ) {
262 return $this->undelete();
263 }
264 return $this->showHistory();
265 }
266
267 /* private */ function showList() {
268 global $wgLang, $wgContLang, $wgUser, $wgOut;
269 $fname = "UndeleteForm::showList";
270
271 # List undeletable articles
272 $result = PageArchive::listAllPages();
273
274 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
275 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
276
277 $sk = $wgUser->getSkin();
278 $undelete =& Title::makeTitle( NS_SPECIAL, 'Undelete' );
279 $wgOut->addHTML( "<ul>\n" );
280 while( $row = $result->fetchObject() ) {
281 $n = ($row->ar_namespace ?
282 ($wgContLang->getNsText( $row->ar_namespace ) . ":") : "").
283 $row->ar_title;
284 $link = $sk->makeKnownLinkObj( $undelete,
285 htmlspecialchars( $n ), "target=" . urlencode( $n ) );
286 $revisions = htmlspecialchars( wfMsg( "undeleterevisions",
287 $wgLang->formatNum( $row->count ) ) );
288 $wgOut->addHTML( "<li>$link $revisions</li>\n" );
289 }
290 $result->free();
291 $wgOut->addHTML( "</ul>\n" );
292
293 return true;
294 }
295
296 /* private */ function showRevision( $timestamp ) {
297 global $wgLang, $wgUser, $wgOut;
298 $fname = "UndeleteForm::showRevision";
299
300 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
301
302 $archive =& new PageArchive( $this->mTargetObj );
303 $text = $archive->getRevisionText( $timestamp );
304
305 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
306 $wgOut->addWikiText( "(" . wfMsg( "undeleterevision",
307 $wgLang->date( $timestamp ) ) . ")\n<hr />\n" . $text );
308 }
309
310 /* private */ function showHistory() {
311 global $wgLang, $wgUser, $wgOut;
312
313 $sk = $wgUser->getSkin();
314 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
315
316 $archive = new PageArchive( $this->mTargetObj );
317 $text = $archive->getLastRevisionText();
318 if( is_null( $text ) ) {
319 $wgOut->addWikiText( wfMsg( "nohistory" ) );
320 return;
321 }
322 $wgOut->addWikiText( wfMsg( "undeletehistory" ) . "\n----\n" . $text );
323
324 # List all stored revisions
325 $revisions = $archive->listRevisions();
326
327 $titleObj = Title::makeTitle( NS_SPECIAL, "Undelete" );
328 $action = $titleObj->escapeLocalURL( "action=submit" );
329 $encTarget = htmlspecialchars( $this->mTarget );
330 $button = htmlspecialchars( wfMsg("undeletebtn") );
331
332 $wgOut->addHTML("
333 <form id=\"undelete\" method=\"post\" action=\"{$action}\">
334 <input type=\"hidden\" name=\"target\" value=\"{$encTarget}\" />
335 <input type=\"submit\" name=\"restore\" value=\"{$button}\" />
336 ");
337
338 # Show relevant lines from the deletion log:
339 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
340 require_once( 'SpecialLog.php' );
341 $logViewer =& new LogViewer(
342 new LogReader(
343 new FauxRequest(
344 array( 'page' => $this->mTargetObj->getPrefixedText(),
345 'type' => 'delete' ) ) ) );
346 $logViewer->showList( $wgOut );
347
348 # The page's stored (deleted) history:
349 $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
350 $wgOut->addHTML("<ul>");
351 $target = urlencode( $this->mTarget );
352 while( $row = $revisions->fetchObject() ) {
353 $checkBox = "<input type=\"checkbox\" name=\"ts{$row->ar_timestamp}\" value=\"1\" />";
354 $pageLink = $sk->makeKnownLinkObj( $titleObj,
355 $wgLang->timeanddate( $row->ar_timestamp, true ),
356 "target=$target&timestamp={$row->ar_timestamp}" );
357 $userLink = htmlspecialchars( $row->ar_user_text );
358 if( $row->ar_user ) {
359 $userLink = $sk->makeKnownLinkObj(
360 Title::makeTitle( NS_USER, $row->ar_user_text ),
361 $userLink );
362 }
363 $comment = $sk->formatComment( $row->ar_comment );
364 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink (<i>$comment</i>)</li>\n" );
365
366 }
367 $revisions->free();
368 $wgOut->addHTML("</ul>\n</form>");
369
370 return true;
371 }
372
373 function undelete() {
374 global $wgOut;
375 if( !is_null( $this->mTargetObj ) ) {
376 $archive = new PageArchive( $this->mTargetObj );
377 if( $archive->undelete( $this->mTargetTimestamp ) ) {
378 $wgOut->addWikiText( wfMsg( "undeletedtext", $this->mTarget ) );
379 return true;
380 }
381 }
382 $wgOut->fatalError( wfMsg( "cannotundelete" ) );
383 return false;
384 }
385 }
386
387 ?>