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 if( is_null( $row->ar_text_id
) ) {
87 // An old row from MediaWiki 1.4 or previous.
88 // Text is embedded in this row in classic compression format.
89 return Revision
::getRevisionText( $row, "ar_" );
91 // New-style: keyed to the text storage backend.
92 $text = $dbr->selectRow( 'text',
93 array( 'old_text', 'old_flags' ),
94 array( 'old_id' => $row->ar_text_id
),
96 return Revision
::getRevisionText( $text );
101 * Fetch (and decompress if necessary) the stored text of the most
102 * recently edited deleted revision of the page.
104 * If there are no archived revisions for the page, returns NULL.
108 function getLastRevisionText() {
109 $dbr =& wfGetDB( DB_SLAVE
);
110 $row = $dbr->selectRow( 'archive',
111 array( 'ar_text', 'ar_flags' ),
112 array( 'ar_namespace' => $this->title
->getNamespace(),
113 'ar_title' => $this->title
->getDBkey() ),
114 'PageArchive::getLastRevisionText',
115 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
117 return Revision
::getRevisionText( $row, "ar_" );
124 * Quick check if any archived revisions are present for the page.
127 function isDeleted() {
128 $dbr =& wfGetDB( DB_SLAVE
);
129 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
130 array( 'ar_namespace' => $this->title
->getNamespace(),
131 'ar_title' => $this->title
->getDBkey() ) );
136 * This is the meaty bit -- restores archived revisions of the given page
137 * to the cur/old tables. If the page currently exists, all revisions will
138 * be stuffed into old, otherwise the most recent will go into cur.
139 * The deletion log will be updated with an undeletion notice.
141 * Returns true on success.
143 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
146 function undelete( $timestamps ) {
147 global $wgUser, $wgOut, $wgLang, $wgDeferredUpdateList;
148 global $wgUseSquid, $wgInternalServer, $wgLinkCache;
151 $fname = "doUndeleteArticle";
152 $restoreAll = empty( $timestamps );
153 $restoreRevisions = count( $timestamps );
155 $dbw =& wfGetDB( DB_MASTER
);
156 extract( $dbw->tableNames( 'page', 'archive' ) );
158 # Does this page already exist? We'll have to update it...
159 $article = new Article( $this->title
);
160 $options = ( $wgDBtype == 'PostgreSQL' )
161 ?
'' // pg doesn't support this?
163 $page = $dbw->selectRow( 'page',
164 array( 'page_id', 'page_latest' ),
165 array( 'page_namespace' => $this->title
->getNamespace(),
166 'page_title' => $this->title
->getDBkey() ),
170 # Page already exists. Import the history, and if necessary
171 # we'll update the latest revision field in the record.
173 $pageId = $page->page_id
;
174 $previousRevId = $page->page_latest
;
175 $previousTimestamp = $page->rev_timestamp
;
177 # Have to create a new article...
178 $newid = $article->insertOn( $dbw );
181 $previousTimestamp = 0;
185 $oldones = '1 = 1'; # All revisions...
187 $oldts = implode( ',',
188 array_map( array( &$dbw, 'addQuotes' ),
189 array_map( array( &$dbw, 'timestamp' ),
192 $oldones = "ar_timestamp IN ( {$oldts} )";
196 * Restore each revision...
198 $result = $dbw->select( 'archive',
210 'ar_namespace' => $this->title
->getNamespace(),
211 'ar_title' => $this->title
->getDBkey(),
215 'ORDER BY' => 'ar_timestamp' )
218 while( $row = $dbw->fetchObject( $result ) ) {
219 $revision = new Revision( array(
221 'id' => $row->ar_rev_id
,
222 'text' => Revision
::getRevisionText( $row, 'ar_' ),
223 'comment' => $row->ar_comment
,
224 'user' => $row->ar_user
,
225 'user_text' => $row->ar_user_text
,
226 'timestamp' => $row->ar_timestamp
,
227 'minor_edit' => $row->ar_minor_edit
,
228 'text_id' => $row->ar_text_id
,
230 $revision->insertOn( $dbw );
234 # FIXME: Update latest if newer as well...
236 # FIXME: update article count if changed...
237 $article->updateRevisionOn( $dbw, $revision, $previousRevId );
239 # Finally, clean up the link tables
240 $wgLinkCache = new LinkCache();
242 $wgLinkCache->forUpdate( true );
244 # Create a dummy OutputPage to update the outgoing links
245 $dummyOut = new OutputPage();
246 $dummyOut->addWikiText( $revision->getText() );
248 $u = new LinksUpdate( $newid, $this->title
->getPrefixedDBkey() );
249 array_push( $wgDeferredUpdateList, $u );
251 #TODO: SearchUpdate, etc.
255 Article
::onArticleCreate( $this->title
);
257 Article
::onArticleEdit( $this->title
);
260 # Something went terribly worong!
263 # Now that it's safely stored, take it out of the archive
264 $dbw->delete( 'archive',
266 'ar_namespace' => $this->title
->getNamespace(),
267 'ar_title' => $this->title
->getDBkey(),
272 $log = new LogPage( 'delete' );
276 $reason = wfMsgForContent( 'undeletedrevisions', $restoreRevisions );
278 $log->addEntry( 'restore', $this->title
, $reason );
287 * @subpackage SpecialPage
290 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
291 var $mTargetTimestamp;
293 function UndeleteForm( &$request, $par = "" ) {
295 $this->mAction
= $request->getText( 'action' );
296 $this->mTarget
= $request->getText( 'target' );
297 $this->mTimestamp
= $request->getText( 'timestamp' );
298 $this->mRestore
= $request->getCheck( 'restore' ) &&
299 $request->wasPosted() &&
300 $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
302 $this->mTarget
= $par;
304 if ( $this->mTarget
!== "" ) {
305 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
307 $this->mTargetObj
= NULL;
309 if( $this->mRestore
) {
310 $timestamps = array();
311 foreach( $_REQUEST as $key => $val ) {
312 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
313 array_push( $timestamps, $matches[1] );
316 rsort( $timestamps );
317 $this->mTargetTimestamp
= $timestamps;
322 if( is_null( $this->mTargetObj
) ) {
323 return $this->showList();
325 if( $this->mTimestamp
!== "" ) {
326 return $this->showRevision( $this->mTimestamp
);
328 if( $this->mRestore
&& $this->mAction
== "submit" ) {
329 return $this->undelete();
331 return $this->showHistory();
334 /* private */ function showList() {
335 global $wgLang, $wgContLang, $wgUser, $wgOut;
336 $fname = "UndeleteForm::showList";
338 # List undeletable articles
339 $result = PageArchive
::listAllPages();
341 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
342 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
344 $sk = $wgUser->getSkin();
345 $undelete =& Title
::makeTitle( NS_SPECIAL
, 'Undelete' );
346 $wgOut->addHTML( "<ul>\n" );
347 while( $row = $result->fetchObject() ) {
348 $n = ($row->ar_namespace ?
349 ($wgContLang->getNsText( $row->ar_namespace
) . ":") : "").
351 $link = $sk->makeKnownLinkObj( $undelete,
352 htmlspecialchars( $n ), "target=" . urlencode( $n ) );
353 $revisions = htmlspecialchars( wfMsg( "undeleterevisions",
354 $wgLang->formatNum( $row->count
) ) );
355 $wgOut->addHTML( "<li>$link $revisions</li>\n" );
358 $wgOut->addHTML( "</ul>\n" );
363 /* private */ function showRevision( $timestamp ) {
364 global $wgLang, $wgUser, $wgOut;
365 $fname = "UndeleteForm::showRevision";
367 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
369 $archive =& new PageArchive( $this->mTargetObj
);
370 $text = $archive->getRevisionText( $timestamp );
372 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
373 $wgOut->addWikiText( "(" . wfMsg( "undeleterevision",
374 $wgLang->date( $timestamp ) ) . ")\n<hr />\n" . $text );
377 /* private */ function showHistory() {
378 global $wgLang, $wgUser, $wgOut;
380 $sk = $wgUser->getSkin();
381 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
383 $archive = new PageArchive( $this->mTargetObj
);
384 $text = $archive->getLastRevisionText();
385 if( is_null( $text ) ) {
386 $wgOut->addWikiText( wfMsg( "nohistory" ) );
389 $wgOut->addWikiText( wfMsg( "undeletehistory" ) . "\n----\n" . $text );
391 # List all stored revisions
392 $revisions = $archive->listRevisions();
394 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Undelete" );
395 $action = $titleObj->escapeLocalURL( "action=submit" );
396 $encTarget = htmlspecialchars( $this->mTarget
);
397 $button = htmlspecialchars( wfMsg("undeletebtn") );
398 $token = htmlspecialchars( $wgUser->editToken() );
401 <form id=\"undelete\" method=\"post\" action=\"{$action}\">
402 <input type=\"hidden\" name=\"target\" value=\"{$encTarget}\" />
403 <input type=\"submit\" name=\"restore\" value=\"{$button}\" />
404 <input type='hidden' name='wpEditToken' value=\"{$token}\" />
407 # Show relevant lines from the deletion log:
408 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
409 require_once( 'SpecialLog.php' );
410 $logViewer =& new LogViewer(
413 array( 'page' => $this->mTargetObj
->getPrefixedText(),
414 'type' => 'delete' ) ) ) );
415 $logViewer->showList( $wgOut );
417 # The page's stored (deleted) history:
418 $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
419 $wgOut->addHTML("<ul>");
420 $target = urlencode( $this->mTarget
);
421 while( $row = $revisions->fetchObject() ) {
422 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
423 $checkBox = "<input type=\"checkbox\" name=\"ts$ts\" value=\"1\" />";
424 $pageLink = $sk->makeKnownLinkObj( $titleObj,
425 $wgLang->timeanddate( $ts, true ),
426 "target=$target×tamp=$ts" );
427 $userLink = htmlspecialchars( $row->ar_user_text
);
428 if( $row->ar_user
) {
429 $userLink = $sk->makeKnownLinkObj(
430 Title
::makeTitle( NS_USER
, $row->ar_user_text
),
433 $userLink = $sk->makeKnownLinkObj(
434 Title
::makeTitle( NS_SPECIAL
, 'Contributions' ),
435 $userLink, 'target=' . $row->ar_user_text
);
437 $comment = $sk->commentBlock( $row->ar_comment
);
438 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink $comment</li>\n" );
442 $wgOut->addHTML("</ul>\n</form>");
447 function undelete() {
449 if( !is_null( $this->mTargetObj
) ) {
450 $archive = new PageArchive( $this->mTargetObj
);
451 if( $archive->undelete( $this->mTargetTimestamp
) ) {
452 $wgOut->addWikiText( wfMsg( "undeletedtext", $this->mTarget
) );
454 if (NS_IMAGE
== $this->mTargetObj
->getNamespace()) {
455 /* refresh image metadata cache */
456 new Image( $this->mTargetObj
);
462 $wgOut->fatalError( wfMsg( "cannotundelete" ) );