* (bug 1163) Special:Undelete showed oldest revision instead of newest
[lhc/web/wiklou.git] / includes / SpecialUndelete.php
1 <?php
2 /**
3 * @todo document
4 * @package MediaWiki
5 * @subpackage SpecialPage
6 */
7
8 /** */
9 require_once( 'Revision.php' );
10
11 /**
12 *
13 */
14 function wfSpecialUndelete( $par ) {
15 global $wgRequest;
16
17 $form = new UndeleteForm( $wgRequest, $par );
18 $form->execute();
19 }
20
21 /**
22 *
23 * @package MediaWiki
24 * @subpackage SpecialPage
25 */
26 class PageArchive {
27 var $title;
28
29 function PageArchive( &$title ) {
30 if( is_null( $title ) ) {
31 wfDebugDieBacktrace( 'Archiver() given a null title.');
32 }
33 $this->title =& $title;
34 }
35
36 /**
37 * List all deleted pages recorded in the archive table. Returns result
38 * wrapper with (ar_namespace, ar_title, count) fields, ordered by page
39 * namespace/title. Can be called staticaly.
40 *
41 * @return ResultWrapper
42 */
43 /* static */ function &listAllPages() {
44 $dbr =& wfGetDB( DB_SLAVE );
45 $archive = $dbr->tableName( 'archive' );
46
47 $sql = "SELECT ar_namespace,ar_title, COUNT(*) AS count FROM $archive " .
48 "GROUP BY ar_namespace,ar_title ORDER BY ar_namespace,ar_title";
49
50 return $dbr->resultObject( $dbr->query( $sql, 'PageArchive::listAllPages' ) );
51 }
52
53 /**
54 * List the revisions of the given page. Returns result wrapper with
55 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
56 *
57 * @return ResultWrapper
58 */
59 function &listRevisions() {
60 $dbr =& wfGetDB( DB_SLAVE );
61 return $dbr->resultObject( $dbr->select( 'archive',
62 array( 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text', 'ar_comment' ),
63 array( 'ar_namespace' => $this->title->getNamespace(),
64 'ar_title' => $this->title->getDBkey() ),
65 'PageArchive::listRevisions',
66 array( 'ORDER BY' => 'ar_timestamp DESC' ) ) );
67 }
68
69 /**
70 * Fetch (and decompress if necessary) the stored text for the deleted
71 * revision of the page with the given timestamp.
72 *
73 * @return string
74 */
75 function getRevisionText( $timestamp ) {
76 $dbr =& wfGetDB( DB_SLAVE );
77 $row = $dbr->selectRow( 'archive',
78 array( 'ar_text', 'ar_flags' ),
79 array( 'ar_namespace' => $this->title->getNamespace(),
80 'ar_title' => $this->title->getDbkey(),
81 'ar_timestamp' => $dbr->timestamp( $timestamp ) ) );
82 return Revision::getRevisionText( $row, "ar_" );
83 }
84
85 /**
86 * Fetch (and decompress if necessary) the stored text of the most
87 * recently edited deleted revision of the page.
88 *
89 * If there are no archived revisions for the page, returns NULL.
90 *
91 * @return string
92 */
93 function getLastRevisionText() {
94 $dbr =& wfGetDB( DB_SLAVE );
95 $row = $dbr->selectRow( 'archive',
96 array( 'ar_text', 'ar_flags' ),
97 array( 'ar_namespace' => $this->title->getNamespace(),
98 'ar_title' => $this->title->getDBkey() ),
99 'PageArchive::getLastRevisionText',
100 array( 'ORDER BY' => 'ar_timestamp DESC',
101 'LIMIT' => '1' ) );
102 if( $row ) {
103 return Revision::getRevisionText( $row, "ar_" );
104 } else {
105 return NULL;
106 }
107 }
108
109 /**
110 * Quick check if any archived revisions are present for the page.
111 * @return bool
112 */
113 function isDeleted() {
114 $dbr =& wfGetDB( DB_SLAVE );
115 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
116 array( 'ar_namespace' => $this->title->getNamespace(),
117 'ar_title' => $this->title->getDBkey() ) );
118 return ($n > 0);
119 }
120
121 /**
122 * This is the meaty bit -- restores archived revisions of the given page
123 * to the cur/old tables. If the page currently exists, all revisions will
124 * be stuffed into old, otherwise the most recent will go into cur.
125 * The deletion log will be updated with an undeletion notice.
126 *
127 * Returns true on success.
128 *
129 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
130 * @return bool
131 */
132 function undelete( $timestamps ) {
133 global $wgUser, $wgOut, $wgLang, $wgDeferredUpdateList;
134 global $wgUseSquid, $wgInternalServer, $wgLinkCache;
135 global $wgDBtype;
136
137 $fname = "doUndeleteArticle";
138 $restoreAll = empty( $timestamps );
139 $restoreRevisions = count( $timestamps );
140
141 $dbw =& wfGetDB( DB_MASTER );
142 extract( $dbw->tableNames( 'page', 'archive' ) );
143
144 # Does this page already exist? We'll have to update it...
145 $article = new Article( $this->title );
146 $options = ( $wgDBtype == 'PostgreSQL' )
147 ? '' // pg doesn't support this?
148 : 'FOR UPDATE';
149 $page = $dbw->selectRow( 'page',
150 array( 'page_id', 'page_latest' ),
151 array( 'page_namespace' => $this->title->getNamespace(),
152 'page_title' => $this->title->getDBkey() ),
153 $fname,
154 $options );
155 if( $page ) {
156 # Page already exists. Import the history, and if necessary
157 # we'll update the latest revision field in the record.
158 $newid = 0;
159 $pageId = $page->page_id;
160 $previousRevId = $page->page_latest;
161 $previousTimestamp = $page->rev_timestamp;
162 } else {
163 # Have to create a new article...
164 $newid = $article->insertOn( $dbw );
165 $pageId = $newid;
166 $previousRevId = 0;
167 $previousTimestamp = 0;
168 }
169
170 if( $restoreAll ) {
171 $oldones = '1'; # All revisions...
172 } else {
173 $oldts = implode( ',',
174 array_map( array( &$dbw, 'addQuotes' ),
175 array_map( array( &$dbw, 'timestamp' ),
176 $timestamps ) ) );
177
178 $oldones = "ar_timestamp IN ( {$oldts} )";
179 }
180
181 /**
182 * Restore each revision...
183 */
184 $result = $dbw->select( 'archive',
185 /* fields */ array(
186 'ar_rev_id',
187 'ar_text',
188 'ar_comment',
189 'ar_user',
190 'ar_user_text',
191 'ar_timestamp',
192 'ar_minor_edit',
193 'ar_flags' ),
194 /* WHERE */ array(
195 'ar_namespace' => $this->title->getNamespace(),
196 'ar_title' => $this->title->getDBkey(),
197 $oldones ),
198 $fname,
199 /* options */ array(
200 'ORDER BY' => 'ar_timestamp' )
201 );
202 $revision = null;
203 while( $row = $dbw->fetchObject( $result ) ) {
204 $revision = new Revision( array(
205 'page' => $pageId,
206 'id' => $row->ar_rev_id,
207 'text' => Revision::getRevisionText( $row, 'ar_' ),
208 'comment' => $row->ar_comment,
209 'user' => $row->ar_user,
210 'user_text' => $row->ar_user_text,
211 'timestamp' => $row->ar_timestamp,
212 'minor_edit' => $row->ar_minor_edit,
213 ) );
214 $revision->insertOn( $dbw );
215 }
216
217 if( $revision ) {
218 # FIXME: Update latest if newer as well...
219 if( $newid ) {
220 # FIXME: update article count if changed...
221 $article->updateRevisionOn( $dbw, $revision, $previousRevId );
222
223 # Finally, clean up the link tables
224 $wgLinkCache = new LinkCache();
225 # Select for update
226 $wgLinkCache->forUpdate( true );
227
228 # Create a dummy OutputPage to update the outgoing links
229 $dummyOut = new OutputPage();
230 $dummyOut->addWikiText( $revision->getText() );
231
232 $u = new LinksUpdate( $newid, $this->title->getPrefixedDBkey() );
233 array_push( $wgDeferredUpdateList, $u );
234
235 #TODO: SearchUpdate, etc.
236 }
237
238 if( $newid ) {
239 Article::onArticleCreate( $this->title );
240 } else {
241 Article::onArticleEdit( $this->title );
242 }
243 } else {
244 # Something went terribly worong!
245 }
246
247 # Now that it's safely stored, take it out of the archive
248 $dbw->delete( 'archive',
249 /* WHERE */ array(
250 'ar_namespace' => $this->title->getNamespace(),
251 'ar_title' => $this->title->getDBkey(),
252 $oldones ),
253 $fname );
254
255 # Touch the log!
256 $log = new LogPage( 'delete' );
257 if( $restoreAll ) {
258 $reason = "";
259 } else {
260 $reason = wfMsg( 'undeletedrevisions', $restoreRevisions );
261 }
262 $log->addEntry( 'restore', $this->title, $reason );
263
264 return true;
265 }
266 }
267
268 /**
269 *
270 * @package MediaWiki
271 * @subpackage SpecialPage
272 */
273 class UndeleteForm {
274 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
275 var $mTargetTimestamp;
276
277 function UndeleteForm( &$request, $par = "" ) {
278 global $wgUser;
279 $this->mAction = $request->getText( 'action' );
280 $this->mTarget = $request->getText( 'target' );
281 $this->mTimestamp = $request->getText( 'timestamp' );
282 $this->mRestore = $request->getCheck( 'restore' ) &&
283 $request->wasPosted() &&
284 $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
285 if( $par != "" ) {
286 $this->mTarget = $par;
287 }
288 if ( $this->mTarget !== "" ) {
289 $this->mTargetObj = Title::newFromURL( $this->mTarget );
290 } else {
291 $this->mTargetObj = NULL;
292 }
293 if( $this->mRestore ) {
294 $timestamps = array();
295 foreach( $_REQUEST as $key => $val ) {
296 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
297 array_push( $timestamps, $matches[1] );
298 }
299 }
300 rsort( $timestamps );
301 $this->mTargetTimestamp = $timestamps;
302 }
303 }
304
305 function execute() {
306 if( is_null( $this->mTargetObj ) ) {
307 return $this->showList();
308 }
309 if( $this->mTimestamp !== "" ) {
310 return $this->showRevision( $this->mTimestamp );
311 }
312 if( $this->mRestore && $this->mAction == "submit" ) {
313 return $this->undelete();
314 }
315 return $this->showHistory();
316 }
317
318 /* private */ function showList() {
319 global $wgLang, $wgContLang, $wgUser, $wgOut;
320 $fname = "UndeleteForm::showList";
321
322 # List undeletable articles
323 $result = PageArchive::listAllPages();
324
325 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
326 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
327
328 $sk = $wgUser->getSkin();
329 $undelete =& Title::makeTitle( NS_SPECIAL, 'Undelete' );
330 $wgOut->addHTML( "<ul>\n" );
331 while( $row = $result->fetchObject() ) {
332 $n = ($row->ar_namespace ?
333 ($wgContLang->getNsText( $row->ar_namespace ) . ":") : "").
334 $row->ar_title;
335 $link = $sk->makeKnownLinkObj( $undelete,
336 htmlspecialchars( $n ), "target=" . urlencode( $n ) );
337 $revisions = htmlspecialchars( wfMsg( "undeleterevisions",
338 $wgLang->formatNum( $row->count ) ) );
339 $wgOut->addHTML( "<li>$link $revisions</li>\n" );
340 }
341 $result->free();
342 $wgOut->addHTML( "</ul>\n" );
343
344 return true;
345 }
346
347 /* private */ function showRevision( $timestamp ) {
348 global $wgLang, $wgUser, $wgOut;
349 $fname = "UndeleteForm::showRevision";
350
351 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
352
353 $archive =& new PageArchive( $this->mTargetObj );
354 $text = $archive->getRevisionText( $timestamp );
355
356 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
357 $wgOut->addWikiText( "(" . wfMsg( "undeleterevision",
358 $wgLang->date( $timestamp ) ) . ")\n<hr />\n" . $text );
359 }
360
361 /* private */ function showHistory() {
362 global $wgLang, $wgUser, $wgOut;
363
364 $sk = $wgUser->getSkin();
365 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
366
367 $archive = new PageArchive( $this->mTargetObj );
368 $text = $archive->getLastRevisionText();
369 if( is_null( $text ) ) {
370 $wgOut->addWikiText( wfMsg( "nohistory" ) );
371 return;
372 }
373 $wgOut->addWikiText( wfMsg( "undeletehistory" ) . "\n----\n" . $text );
374
375 # List all stored revisions
376 $revisions = $archive->listRevisions();
377
378 $titleObj = Title::makeTitle( NS_SPECIAL, "Undelete" );
379 $action = $titleObj->escapeLocalURL( "action=submit" );
380 $encTarget = htmlspecialchars( $this->mTarget );
381 $button = htmlspecialchars( wfMsg("undeletebtn") );
382 $token = htmlspecialchars( $wgUser->editToken() );
383
384 $wgOut->addHTML("
385 <form id=\"undelete\" method=\"post\" action=\"{$action}\">
386 <input type=\"hidden\" name=\"target\" value=\"{$encTarget}\" />
387 <input type=\"submit\" name=\"restore\" value=\"{$button}\" />
388 <input type='hidden' name='wpEditToken' value=\"{$token}\" />
389 ");
390
391 # Show relevant lines from the deletion log:
392 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
393 require_once( 'SpecialLog.php' );
394 $logViewer =& new LogViewer(
395 new LogReader(
396 new FauxRequest(
397 array( 'page' => $this->mTargetObj->getPrefixedText(),
398 'type' => 'delete' ) ) ) );
399 $logViewer->showList( $wgOut );
400
401 # The page's stored (deleted) history:
402 $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
403 $wgOut->addHTML("<ul>");
404 $target = urlencode( $this->mTarget );
405 while( $row = $revisions->fetchObject() ) {
406 $ts = wfTimestamp( TS_MW, $row->ar_timestamp );
407 $checkBox = "<input type=\"checkbox\" name=\"ts$ts\" value=\"1\" />";
408 $pageLink = $sk->makeKnownLinkObj( $titleObj,
409 $wgLang->timeanddate( $row->ar_timestamp, true ),
410 "target=$target&timestamp=$ts" );
411 $userLink = htmlspecialchars( $row->ar_user_text );
412 if( $row->ar_user ) {
413 $userLink = $sk->makeKnownLinkObj(
414 Title::makeTitle( NS_USER, $row->ar_user_text ),
415 $userLink );
416 } else {
417 $userLink = $sk->makeKnownLinkObj(
418 Title::makeTitle( NS_SPECIAL, 'Contributions' ),
419 $userLink, 'target=' . $row->ar_user_text );
420 }
421 $comment = $sk->commentBlock( $row->ar_comment );
422 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink $comment</li>\n" );
423
424 }
425 $revisions->free();
426 $wgOut->addHTML("</ul>\n</form>");
427
428 return true;
429 }
430
431 function undelete() {
432 global $wgOut;
433 if( !is_null( $this->mTargetObj ) ) {
434 $archive = new PageArchive( $this->mTargetObj );
435 if( $archive->undelete( $this->mTargetTimestamp ) ) {
436 $wgOut->addWikiText( wfMsg( "undeletedtext", $this->mTarget ) );
437
438 if (NS_IMAGE == $this->mTargetObj->getNamespace()) {
439 /* refresh image metadata cache */
440 new Image( $this->mTargetObj->getText(), true );
441 }
442
443 return true;
444 }
445 }
446 $wgOut->fatalError( wfMsg( "cannotundelete" ) );
447 return false;
448 }
449 }
450
451 ?>