5 * @subpackage SpecialPage
9 require_once( 'Revision.php' );
14 function wfSpecialUndelete( $par ) {
17 $form = new UndeleteForm( $wgRequest, $par );
24 * @subpackage SpecialPage
29 function PageArchive( &$title ) {
30 if( is_null( $title ) ) {
31 wfDebugDieBacktrace( 'Archiver() given a null title.');
33 $this->title
=& $title;
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.
41 * @return ResultWrapper
43 /* static */ function listAllPages() {
44 $dbr =& wfGetDB( DB_SLAVE
);
45 $archive = $dbr->tableName( 'archive' );
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";
50 return $dbr->resultObject( $dbr->query( $sql, 'PageArchive::listAllPages' ) );
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.
57 * @return ResultWrapper
59 function listRevisions() {
60 $dbr =& wfGetDB( DB_SLAVE
);
61 $res = $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 $ret = $dbr->resultObject( $res );
72 * Fetch (and decompress if necessary) the stored text for the deleted
73 * revision of the page with the given timestamp.
77 function getRevisionText( $timestamp ) {
78 $fname = 'PageArchive::getRevisionText';
79 $dbr =& wfGetDB( DB_SLAVE
);
80 $row = $dbr->selectRow( 'archive',
81 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
82 array( 'ar_namespace' => $this->title
->getNamespace(),
83 'ar_title' => $this->title
->getDbkey(),
84 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
86 return $this->getTextFromRow( $row );
90 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
92 function getTextFromRow( $row ) {
93 if( is_null( $row->ar_text_id
) ) {
94 // An old row from MediaWiki 1.4 or previous.
95 // Text is embedded in this row in classic compression format.
96 return Revision
::getRevisionText( $row, "ar_" );
98 // New-style: keyed to the text storage backend.
99 $dbr =& wfGetDB( DB_SLAVE
);
100 $text = $dbr->selectRow( 'text',
101 array( 'old_text', 'old_flags' ),
102 array( 'old_id' => $row->ar_text_id
),
104 return Revision
::getRevisionText( $text );
110 * Fetch (and decompress if necessary) the stored text of the most
111 * recently edited deleted revision of the page.
113 * If there are no archived revisions for the page, returns NULL.
117 function getLastRevisionText() {
118 $dbr =& wfGetDB( DB_SLAVE
);
119 $row = $dbr->selectRow( 'archive',
120 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
121 array( 'ar_namespace' => $this->title
->getNamespace(),
122 'ar_title' => $this->title
->getDBkey() ),
123 'PageArchive::getLastRevisionText',
124 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
126 return $this->getTextFromRow( $row );
133 * Quick check if any archived revisions are present for the page.
136 function isDeleted() {
137 $dbr =& wfGetDB( DB_SLAVE
);
138 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
139 array( 'ar_namespace' => $this->title
->getNamespace(),
140 'ar_title' => $this->title
->getDBkey() ) );
145 * This is the meaty bit -- restores archived revisions of the given page
146 * to the cur/old tables. If the page currently exists, all revisions will
147 * be stuffed into old, otherwise the most recent will go into cur.
148 * The deletion log will be updated with an undeletion notice.
150 * Returns true on success.
152 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
155 function undelete( $timestamps ) {
156 global $wgUser, $wgOut, $wgLang, $wgDeferredUpdateList;
157 global $wgUseSquid, $wgInternalServer, $wgLinkCache;
160 $fname = "doUndeleteArticle";
161 $restoreAll = empty( $timestamps );
162 $restoreRevisions = count( $timestamps );
164 $dbw =& wfGetDB( DB_MASTER
);
165 extract( $dbw->tableNames( 'page', 'archive' ) );
167 # Does this page already exist? We'll have to update it...
168 $article = new Article( $this->title
);
169 $options = ( $wgDBtype == 'PostgreSQL' )
170 ?
'' // pg doesn't support this?
172 $page = $dbw->selectRow( 'page',
173 array( 'page_id', 'page_latest' ),
174 array( 'page_namespace' => $this->title
->getNamespace(),
175 'page_title' => $this->title
->getDBkey() ),
179 # Page already exists. Import the history, and if necessary
180 # we'll update the latest revision field in the record.
182 $pageId = $page->page_id
;
183 $previousRevId = $page->page_latest
;
184 $previousTimestamp = $page->rev_timestamp
;
186 # Have to create a new article...
187 $newid = $article->insertOn( $dbw );
190 $previousTimestamp = 0;
194 $oldones = '1 = 1'; # All revisions...
196 $oldts = implode( ',',
197 array_map( array( &$dbw, 'addQuotes' ),
198 array_map( array( &$dbw, 'timestamp' ),
201 $oldones = "ar_timestamp IN ( {$oldts} )";
205 * Restore each revision...
207 $result = $dbw->select( 'archive',
219 'ar_namespace' => $this->title
->getNamespace(),
220 'ar_title' => $this->title
->getDBkey(),
224 'ORDER BY' => 'ar_timestamp' )
227 while( $row = $dbw->fetchObject( $result ) ) {
228 $revision = new Revision( array(
230 'id' => $row->ar_rev_id
,
231 'text' => Revision
::getRevisionText( $row, 'ar_' ),
232 'comment' => $row->ar_comment
,
233 'user' => $row->ar_user
,
234 'user_text' => $row->ar_user_text
,
235 'timestamp' => $row->ar_timestamp
,
236 'minor_edit' => $row->ar_minor_edit
,
237 'text_id' => $row->ar_text_id
,
239 $revision->insertOn( $dbw );
243 # FIXME: Update latest if newer as well...
245 # FIXME: update article count if changed...
246 $article->updateRevisionOn( $dbw, $revision, $previousRevId );
248 # Finally, clean up the link tables
249 $wgLinkCache = new LinkCache();
251 $wgLinkCache->forUpdate( true );
253 # Create a dummy OutputPage to update the outgoing links
254 $dummyOut = new OutputPage();
255 $dummyOut->addWikiText( $revision->getText() );
257 $u = new LinksUpdate( $newid, $this->title
->getPrefixedDBkey() );
258 array_push( $wgDeferredUpdateList, $u );
260 #TODO: SearchUpdate, etc.
264 Article
::onArticleCreate( $this->title
);
266 Article
::onArticleEdit( $this->title
);
269 # Something went terribly worong!
272 # Now that it's safely stored, take it out of the archive
273 $dbw->delete( 'archive',
275 'ar_namespace' => $this->title
->getNamespace(),
276 'ar_title' => $this->title
->getDBkey(),
281 $log = new LogPage( 'delete' );
285 $reason = wfMsgForContent( 'undeletedrevisions', $restoreRevisions );
287 $log->addEntry( 'restore', $this->title
, $reason );
296 * @subpackage SpecialPage
299 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
300 var $mTargetTimestamp;
302 function UndeleteForm( &$request, $par = "" ) {
304 $this->mAction
= $request->getText( 'action' );
305 $this->mTarget
= $request->getText( 'target' );
306 $this->mTimestamp
= $request->getText( 'timestamp' );
307 $this->mRestore
= $request->getCheck( 'restore' ) &&
308 $request->wasPosted() &&
309 $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
311 $this->mTarget
= $par;
313 if ( $this->mTarget
!== "" ) {
314 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
316 $this->mTargetObj
= NULL;
318 if( $this->mRestore
) {
319 $timestamps = array();
320 foreach( $_REQUEST as $key => $val ) {
321 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
322 array_push( $timestamps, $matches[1] );
325 rsort( $timestamps );
326 $this->mTargetTimestamp
= $timestamps;
331 if( is_null( $this->mTargetObj
) ) {
332 return $this->showList();
334 if( $this->mTimestamp
!== "" ) {
335 return $this->showRevision( $this->mTimestamp
);
337 if( $this->mRestore
&& $this->mAction
== "submit" ) {
338 return $this->undelete();
340 return $this->showHistory();
343 /* private */ function showList() {
344 global $wgLang, $wgContLang, $wgUser, $wgOut;
345 $fname = "UndeleteForm::showList";
347 # List undeletable articles
348 $result = PageArchive
::listAllPages();
350 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
351 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
353 $sk = $wgUser->getSkin();
354 $undelete =& Title
::makeTitle( NS_SPECIAL
, 'Undelete' );
355 $wgOut->addHTML( "<ul>\n" );
356 while( $row = $result->fetchObject() ) {
357 $n = ($row->ar_namespace ?
358 ($wgContLang->getNsText( $row->ar_namespace
) . ":") : "").
360 $link = $sk->makeKnownLinkObj( $undelete,
361 htmlspecialchars( $n ), "target=" . urlencode( $n ) );
362 $revisions = htmlspecialchars( wfMsg( "undeleterevisions",
363 $wgLang->formatNum( $row->count
) ) );
364 $wgOut->addHTML( "<li>$link $revisions</li>\n" );
367 $wgOut->addHTML( "</ul>\n" );
372 /* private */ function showRevision( $timestamp ) {
373 global $wgLang, $wgUser, $wgOut;
374 $fname = "UndeleteForm::showRevision";
376 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
378 $archive =& new PageArchive( $this->mTargetObj
);
379 $text = $archive->getRevisionText( $timestamp );
381 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
382 $wgOut->addWikiText( "(" . wfMsg( "undeleterevision",
383 $wgLang->date( $timestamp ) ) . ")\n<hr />\n" . $text );
386 /* private */ function showHistory() {
387 global $wgLang, $wgUser, $wgOut;
389 $sk = $wgUser->getSkin();
390 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
392 $archive = new PageArchive( $this->mTargetObj
);
393 $text = $archive->getLastRevisionText();
394 if( is_null( $text ) ) {
395 $wgOut->addWikiText( wfMsg( "nohistory" ) );
398 $wgOut->addWikiText( wfMsg( "undeletehistory" ) . "\n----\n" . $text );
400 # List all stored revisions
401 $revisions = $archive->listRevisions();
403 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Undelete" );
404 $action = $titleObj->escapeLocalURL( "action=submit" );
405 $encTarget = htmlspecialchars( $this->mTarget
);
406 $button = htmlspecialchars( wfMsg("undeletebtn") );
407 $token = htmlspecialchars( $wgUser->editToken() );
410 <form id=\"undelete\" method=\"post\" action=\"{$action}\">
411 <input type=\"hidden\" name=\"target\" value=\"{$encTarget}\" />
412 <input type=\"submit\" name=\"restore\" value=\"{$button}\" />
413 <input type='hidden' name='wpEditToken' value=\"{$token}\" />
416 # Show relevant lines from the deletion log:
417 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
418 require_once( 'SpecialLog.php' );
419 $logViewer =& new LogViewer(
422 array( 'page' => $this->mTargetObj
->getPrefixedText(),
423 'type' => 'delete' ) ) ) );
424 $logViewer->showList( $wgOut );
426 # The page's stored (deleted) history:
427 $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
428 $wgOut->addHTML("<ul>");
429 $target = urlencode( $this->mTarget
);
430 while( $row = $revisions->fetchObject() ) {
431 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
432 $checkBox = "<input type=\"checkbox\" name=\"ts$ts\" value=\"1\" />";
433 $pageLink = $sk->makeKnownLinkObj( $titleObj,
434 $wgLang->timeanddate( $ts, true ),
435 "target=$target×tamp=$ts" );
436 $userLink = htmlspecialchars( $row->ar_user_text
);
437 if( $row->ar_user
) {
438 $userLink = $sk->makeKnownLinkObj(
439 Title
::makeTitle( NS_USER
, $row->ar_user_text
),
442 $userLink = $sk->makeKnownLinkObj(
443 Title
::makeTitle( NS_SPECIAL
, 'Contributions' ),
444 $userLink, 'target=' . $row->ar_user_text
);
446 $comment = $sk->commentBlock( $row->ar_comment
);
447 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink $comment</li>\n" );
451 $wgOut->addHTML("</ul>\n</form>");
456 function undelete() {
458 if( !is_null( $this->mTargetObj
) ) {
459 $archive = new PageArchive( $this->mTargetObj
);
460 if( $archive->undelete( $this->mTargetTimestamp
) ) {
461 $wgOut->addWikiText( wfMsg( "undeletedtext", $this->mTarget
) );
463 if (NS_IMAGE
== $this->mTargetObj
->getNamespace()) {
464 /* refresh image metadata cache */
465 new Image( $this->mTargetObj
);
471 $wgOut->fatalError( wfMsg( "cannotundelete" ) );