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 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' ) ) );
70 * Fetch (and decompress if necessary) the stored text for the deleted
71 * revision of the page with the given timestamp.
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_" );
86 * Fetch (and decompress if necessary) the stored text of the most
87 * recently edited deleted revision of the page.
89 * If there are no archived revisions for the page, returns NULL.
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 'ORDER BY ar_timestamp DESC LIMIT 1' );
102 return Revision
::getRevisionText( $row, "ar_" );
109 * Quick check if any archived revisions are present for the page.
112 function isDeleted() {
113 $dbr =& wfGetDB( DB_SLAVE
);
114 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
115 array( 'ar_namespace' => $this->title
->getNamespace(),
116 'ar_title' => $this->title
->getDBkey() ) );
121 * This is the meaty bit -- restores archived revisions of the given page
122 * to the cur/old tables. If the page currently exists, all revisions will
123 * be stuffed into old, otherwise the most recent will go into cur.
124 * The deletion log will be updated with an undeletion notice.
126 * Returns true on success.
128 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
131 function undelete( $timestamps ) {
132 global $wgUser, $wgOut, $wgLang, $wgDeferredUpdateList;
133 global $wgUseSquid, $wgInternalServer, $wgLinkCache;
136 $fname = "doUndeleteArticle";
137 $restoreAll = empty( $timestamps );
138 $restoreRevisions = count( $timestamps );
140 $dbw =& wfGetDB( DB_MASTER
);
141 extract( $dbw->tableNames( 'page', 'archive' ) );
143 # Does this page already exist? We'll have to update it...
144 $article = new Article( $this->title
);
145 $options = ( $wgDBtype == 'PostgreSQL' )
146 ?
'' // pg doesn't support this?
148 $page = $dbw->selectRow( 'page',
149 array( 'page_id', 'page_latest' ),
150 array( 'page_namespace' => $this->title
->getNamespace(),
151 'page_title' => $this->title
->getDBkey() ),
155 # Page already exists. Import the history, and if necessary
156 # we'll update the latest revision field in the record.
158 $pageId = $page->page_id
;
159 $previousRevId = $page->page_latest
;
160 $previousTimestamp = $page->rev_timestamp
;
162 # Have to create a new article...
163 $newid = $article->insertOn( $dbw );
166 $previousTimestamp = 0;
170 $oldones = '1'; # All revisions...
172 $oldts = implode( ',',
173 array_map( array( &$dbw, 'addQuotes' ),
174 array_map( array( &$dbw, 'timestamp' ),
177 $oldones = "ar_timestamp IN ( {$oldts} )";
181 * Restore each revision...
183 $result = $dbw->select( 'archive',
194 'ar_namespace' => $this->title
->getNamespace(),
195 'ar_title' => $this->title
->getDBkey(),
199 'ORDER BY' => 'ar_timestamp' )
202 while( $row = $dbw->fetchObject( $result ) ) {
203 $revision = new Revision( array(
205 'id' => $row->ar_rev_id
,
206 'text' => Revision
::getRevisionText( $row, 'ar_' ),
207 'comment' => $row->ar_comment
,
208 'user' => $row->ar_user
,
209 'user_text' => $row->ar_user_text
,
210 'timestamp' => $row->ar_timestamp
,
211 'minor_edit' => $row->ar_minor_edit
,
213 $revision->insertOn( $dbw );
217 # FIXME: Update latest if newer as well...
219 # FIXME: update article count if changed...
220 $article->updateRevisionOn( $dbw, $revision, $previousRevId );
222 # Finally, clean up the link tables
223 $wgLinkCache = new LinkCache();
225 $wgLinkCache->forUpdate( true );
227 # Create a dummy OutputPage to update the outgoing links
228 $dummyOut = new OutputPage();
229 $dummyOut->addWikiText( $revision->getText() );
231 $u = new LinksUpdate( $newid, $this->title
->getPrefixedDBkey() );
232 array_push( $wgDeferredUpdateList, $u );
234 #TODO: SearchUpdate, etc.
238 Article
::onArticleCreate( $this->title
);
240 Article
::onArticleEdit( $this->title
);
243 # Something went terribly worong!
246 # Now that it's safely stored, take it out of the archive
247 $dbw->delete( 'archive',
249 'ar_namespace' => $this->title
->getNamespace(),
250 'ar_title' => $this->title
->getDBkey(),
255 $log = new LogPage( 'delete' );
259 $reason = wfMsg( 'undeletedrevisions', $restoreRevisions );
261 $log->addEntry( 'restore', $this->title
, $reason );
270 * @subpackage SpecialPage
273 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
274 var $mTargetTimestamp;
276 function UndeleteForm( &$request, $par = "" ) {
278 $this->mAction
= $request->getText( 'action' );
279 $this->mTarget
= $request->getText( 'target' );
280 $this->mTimestamp
= $request->getText( 'timestamp' );
281 $this->mRestore
= $request->getCheck( 'restore' ) &&
282 $request->wasPosted() &&
283 $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
285 $this->mTarget
= $par;
287 if ( $this->mTarget
!== "" ) {
288 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
290 $this->mTargetObj
= NULL;
292 if( $this->mRestore
) {
293 $timestamps = array();
294 foreach( $_REQUEST as $key => $val ) {
295 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
296 array_push( $timestamps, $matches[1] );
299 rsort( $timestamps );
300 $this->mTargetTimestamp
= $timestamps;
305 if( is_null( $this->mTargetObj
) ) {
306 return $this->showList();
308 if( $this->mTimestamp
!== "" ) {
309 return $this->showRevision( $this->mTimestamp
);
311 if( $this->mRestore
&& $this->mAction
== "submit" ) {
312 return $this->undelete();
314 return $this->showHistory();
317 /* private */ function showList() {
318 global $wgLang, $wgContLang, $wgUser, $wgOut;
319 $fname = "UndeleteForm::showList";
321 # List undeletable articles
322 $result = PageArchive
::listAllPages();
324 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
325 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
327 $sk = $wgUser->getSkin();
328 $undelete =& Title
::makeTitle( NS_SPECIAL
, 'Undelete' );
329 $wgOut->addHTML( "<ul>\n" );
330 while( $row = $result->fetchObject() ) {
331 $n = ($row->ar_namespace ?
332 ($wgContLang->getNsText( $row->ar_namespace
) . ":") : "").
334 $link = $sk->makeKnownLinkObj( $undelete,
335 htmlspecialchars( $n ), "target=" . urlencode( $n ) );
336 $revisions = htmlspecialchars( wfMsg( "undeleterevisions",
337 $wgLang->formatNum( $row->count
) ) );
338 $wgOut->addHTML( "<li>$link $revisions</li>\n" );
341 $wgOut->addHTML( "</ul>\n" );
346 /* private */ function showRevision( $timestamp ) {
347 global $wgLang, $wgUser, $wgOut;
348 $fname = "UndeleteForm::showRevision";
350 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
352 $archive =& new PageArchive( $this->mTargetObj
);
353 $text = $archive->getRevisionText( $timestamp );
355 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
356 $wgOut->addWikiText( "(" . wfMsg( "undeleterevision",
357 $wgLang->date( $timestamp ) ) . ")\n<hr />\n" . $text );
360 /* private */ function showHistory() {
361 global $wgLang, $wgUser, $wgOut;
363 $sk = $wgUser->getSkin();
364 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
366 $archive = new PageArchive( $this->mTargetObj
);
367 $text = $archive->getLastRevisionText();
368 if( is_null( $text ) ) {
369 $wgOut->addWikiText( wfMsg( "nohistory" ) );
372 $wgOut->addWikiText( wfMsg( "undeletehistory" ) . "\n----\n" . $text );
374 # List all stored revisions
375 $revisions = $archive->listRevisions();
377 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Undelete" );
378 $action = $titleObj->escapeLocalURL( "action=submit" );
379 $encTarget = htmlspecialchars( $this->mTarget
);
380 $button = htmlspecialchars( wfMsg("undeletebtn") );
381 $token = htmlspecialchars( $wgUser->editToken() );
384 <form id=\"undelete\" method=\"post\" action=\"{$action}\">
385 <input type=\"hidden\" name=\"target\" value=\"{$encTarget}\" />
386 <input type=\"submit\" name=\"restore\" value=\"{$button}\" />
387 <input type='hidden' name='wpEditToken' value=\"{$token}\" />
390 # Show relevant lines from the deletion log:
391 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
392 require_once( 'SpecialLog.php' );
393 $logViewer =& new LogViewer(
396 array( 'page' => $this->mTargetObj
->getPrefixedText(),
397 'type' => 'delete' ) ) ) );
398 $logViewer->showList( $wgOut );
400 # The page's stored (deleted) history:
401 $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
402 $wgOut->addHTML("<ul>");
403 $target = urlencode( $this->mTarget
);
404 while( $row = $revisions->fetchObject() ) {
405 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
406 $checkBox = "<input type=\"checkbox\" name=\"ts$ts\" value=\"1\" />";
407 $pageLink = $sk->makeKnownLinkObj( $titleObj,
408 $wgLang->timeanddate( $row->ar_timestamp
, true ),
409 "target=$target×tamp=$ts" );
410 $userLink = htmlspecialchars( $row->ar_user_text
);
411 if( $row->ar_user
) {
412 $userLink = $sk->makeKnownLinkObj(
413 Title
::makeTitle( NS_USER
, $row->ar_user_text
),
416 $userLink = $sk->makeKnownLinkObj(
417 Title
::makeTitle( NS_SPECIAL
, 'Contributions' ),
418 $userLink, 'target=' . $row->ar_user_text
);
420 $comment = $sk->commentBlock( $row->ar_comment
);
421 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink $comment</li>\n" );
425 $wgOut->addHTML("</ul>\n</form>");
430 function undelete() {
432 if( !is_null( $this->mTargetObj
) ) {
433 $archive = new PageArchive( $this->mTargetObj
);
434 if( $archive->undelete( $this->mTargetTimestamp
) ) {
435 $wgOut->addWikiText( wfMsg( "undeletedtext", $this->mTarget
) );
437 if (NS_IMAGE
== $this->mTargetObj
->getNamespace()) {
438 /* refresh image metadata cache */
439 new Image( $this->mTargetObj
->getText(), true );
445 $wgOut->fatalError( wfMsg( "cannotundelete" ) );