9d874d4f8f694258ecc4ea3f7b184cd051fe1211
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;
135 $fname = "doUndeleteArticle";
136 $restoreAll = empty( $timestamps );
137 $restoreRevisions = count( $timestamps );
139 $dbw =& wfGetDB( DB_MASTER
);
140 extract( $dbw->tableNames( 'cur', 'archive', 'old' ) );
141 $namespace = $this->title
->getNamespace();
142 $ttl = $this->title
->getDBkey();
143 $t = $dbw->strencode( $ttl );
145 # Move article and history from the "archive" table
146 $sql = "SELECT COUNT(*) AS count FROM $cur WHERE cur_namespace={$namespace} AND cur_title='{$t}'";
148 if( $wgDBtype != 'PostgreSQL' ) { # HACKHACKHACKHACK
149 $sql .= ' FOR UPDATE';
151 $res = $dbw->query( $sql, $fname );
152 $row = $dbw->fetchObject( $res );
153 $now = wfTimestampNow();
155 if( $row->count
== 0) {
156 # Have to create new article...
157 $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}' ";
159 $max = $dbw->addQuotes( $dbw->timestamp( array_shift( $timestamps ) ) );
160 $sql .= "AND ar_timestamp={$max} ";
162 $sql .= "ORDER BY ar_timestamp DESC LIMIT 1 FOR UPDATE";
163 $res = $dbw->query( $sql, $fname );
164 $s = $dbw->fetchObject( $res );
166 $max = $s->ar_timestamp
;
168 $text = Revision
::getRevisionText( $s, "ar_" );
170 $redirect = MagicWord
::get( MAG_REDIRECT
);
171 $redir = $redirect->matchStart( $text ) ?
1 : 0;
174 $dbw->insert( 'cur', array(
175 'cur_id' => $dbw->nextSequenceValue( 'cur_cur_id_seq' ),
176 'cur_namespace' => $namespace,
179 'cur_comment' => $s->ar_comment
,
180 'cur_user' => $s->ar_user
,
181 'cur_timestamp' => $s->ar_timestamp
,
182 'cur_minor_edit' => $s->ar_minor_edit
,
183 'cur_user_text' => $s->ar_user_text
,
184 'cur_is_redirect' => $redir,
185 'cur_random' => $rand,
186 'cur_touched' => $dbw->timestamp( $now ),
187 'inverse_timestamp' => wfInvertTimestamp( wfTimestamp( TS_MW
, $s->ar_timestamp
) ),
190 $newid = $dbw->insertId();
192 $oldones = "AND ar_timestamp<" . $dbw->addQuotes( $dbw->timestamp( $max ) );
195 # If already exists, put history entirely into old table
199 # But to make the history list show up right, we need to touch it.
200 $sql = "UPDATE $cur SET cur_touched='{$now}' WHERE cur_namespace={$namespace} AND cur_title='{$t}'";
201 $dbw->query( $sql, $fname );
203 # FIXME: Sometimes restored entries will be _newer_ than the current version.
209 foreach( $timestamps as $ts ) {
210 array_push( $oldts, $dbw->addQuotes( $dbw->timestamp( $ts ) ) );
212 $oldts = join( ",", $oldts );
213 $oldones = "AND ar_timestamp IN ( {$oldts} )";
215 $sql = "INSERT INTO $old (old_namespace,old_title,old_text," .
216 "old_comment,old_user,old_user_text,old_timestamp,inverse_timestamp,old_minor_edit," .
217 "old_flags) SELECT ar_namespace,ar_title,ar_text,ar_comment," .
218 "ar_user,ar_user_text,ar_timestamp,99999999999999-ar_timestamp,ar_minor_edit,ar_flags " .
219 "FROM $archive WHERE ar_namespace={$namespace} AND ar_title='{$t}' {$oldones}";
220 if( $restoreAll ||
!empty( $oldts ) ) {
221 $dbw->query( $sql, $fname );
224 # Finally, clean up the link tables
226 $wgLinkCache = new LinkCache();
228 $wgLinkCache->forUpdate( true );
229 # Create a dummy OutputPage to update the outgoing links
230 $dummyOut = new OutputPage();
231 $dummyOut->addWikiText( $text );
233 $u = new LinksUpdate( $newid, $this->title
->getPrefixedDBkey() );
234 array_push( $wgDeferredUpdateList, $u );
236 Article
::onArticleCreate( $this->title
);
238 #TODO: SearchUpdate, etc.
241 # Now that it's safely stored, take it out of the archive
242 $sql = "DELETE FROM $archive WHERE ar_namespace={$namespace} AND " .
245 $sql .= " AND ar_timestamp IN ( {$oldts}";
247 if( !empty( $oldts ) ) $sql .= ",";
252 $dbw->query( $sql, $fname );
256 $log = new LogPage( 'delete' );
260 $reason = wfMsg( 'undeletedrevisions', $restoreRevisions );
262 $log->addEntry( 'restore', $this->title
, $reason );
271 * @subpackage SpecialPage
274 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
275 var $mTargetTimestamp;
277 function UndeleteForm( &$request, $par = "" ) {
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' ) );
286 $this->mTarget
= $par;
288 if ( $this->mTarget
!== "" ) {
289 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
291 $this->mTargetObj
= NULL;
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] );
300 rsort( $timestamps );
301 $this->mTargetTimestamp
= $timestamps;
306 if( is_null( $this->mTargetObj
) ) {
307 return $this->showList();
309 if( $this->mTimestamp
!== "" ) {
310 return $this->showRevision( $this->mTimestamp
);
312 if( $this->mRestore
&& $this->mAction
== "submit" ) {
313 return $this->undelete();
315 return $this->showHistory();
318 /* private */ function showList() {
319 global $wgLang, $wgContLang, $wgUser, $wgOut;
320 $fname = "UndeleteForm::showList";
322 # List undeletable articles
323 $result = PageArchive
::listAllPages();
325 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
326 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
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
) . ":") : "").
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" );
342 $wgOut->addHTML( "</ul>\n" );
347 /* private */ function showRevision( $timestamp ) {
348 global $wgLang, $wgUser, $wgOut;
349 $fname = "UndeleteForm::showRevision";
351 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
353 $archive =& new PageArchive( $this->mTargetObj
);
354 $text = $archive->getRevisionText( $timestamp );
356 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
357 $wgOut->addWikiText( "(" . wfMsg( "undeleterevision",
358 $wgLang->date( $timestamp ) ) . ")\n<hr />\n" . $text );
361 /* private */ function showHistory() {
362 global $wgLang, $wgUser, $wgOut;
364 $sk = $wgUser->getSkin();
365 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
367 $archive = new PageArchive( $this->mTargetObj
);
368 $text = $archive->getLastRevisionText();
369 if( is_null( $text ) ) {
370 $wgOut->addWikiText( wfMsg( "nohistory" ) );
373 $wgOut->addWikiText( wfMsg( "undeletehistory" ) . "\n----\n" . $text );
375 # List all stored revisions
376 $revisions = $archive->listRevisions();
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() );
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}\" />
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(
397 array( 'page' => $this->mTargetObj
->getPrefixedText(),
398 'type' => 'delete' ) ) ) );
399 $logViewer->showList( $wgOut );
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×tamp=$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
),
417 $comment = $sk->formatComment( $row->ar_comment
);
418 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink (<i>$comment</i>)</li>\n" );
422 $wgOut->addHTML("</ul>\n</form>");
427 function undelete() {
429 if( !is_null( $this->mTargetObj
) ) {
430 $archive = new PageArchive( $this->mTargetObj
);
431 if( $archive->undelete( $this->mTargetTimestamp
) ) {
432 $wgOut->addWikiText( wfMsg( "undeletedtext", $this->mTarget
) );
436 $wgOut->fatalError( wfMsg( "cannotundelete" ) );