5 * @subpackage SpecialPage
11 function wfSpecialUndelete( $par ) {
14 $form = new UndeleteForm( $wgRequest, $par );
21 * @subpackage SpecialPage
26 function PageArchive( &$title ) {
27 if( is_null( $title ) ) {
28 wfDebugDieBacktrace( 'Archiver() given a null title.');
30 $this->title
=& $title;
33 /* static */ function &listAllPages() {
34 $dbr =& wfGetDB( DB_SLAVE
);
35 $archive = $dbr->tableName( 'archive' );
37 $sql = "SELECT ar_namespace,ar_title, COUNT(*) AS count FROM $archive " .
38 "GROUP BY ar_namespace,ar_title ORDER BY ar_namespace,ar_title";
40 return $dbr->resultObject( $dbr->query( $sql, 'PageArchive::listAllPages' ) );
43 function &listRevisions() {
44 $dbr =& wfGetDB( DB_SLAVE
);
45 $sql = "SELECT ar_minor_edit,ar_timestamp,ar_user,ar_user_text,ar_comment
46 FROM archive WHERE ar_namespace=" . $this->title
->getNamespace() .
47 " AND ar_title='" . $dbr->strencode( $this->title
->getDBkey() ) .
48 "' ORDER BY ar_timestamp DESC";
49 return $dbr->resultObject( $dbr->query( $sql ) );
52 function getRevisionText( $timestamp ) {
53 $dbr =& wfGetDB( DB_SLAVE
);
54 $row = $dbr->selectRow( 'archive',
55 array( 'ar_text', 'ar_flags' ),
56 array( 'ar_namespace' => $this->title
->getNamespace(),
57 'ar_title' => $this->title
->getDbkey(),
58 'ar_timestamp' => $timestamp ) );
59 return Article
::getRevisionText( $row, "ar_" );
62 function getLastRevisionText() {
63 $dbr =& wfGetDB( DB_SLAVE
);
64 $archive = $dbr->tableName( 'archive' );
66 # Get text of first revision
67 $sql = "SELECT ar_text,ar_flags FROM $archive WHERE ar_namespace=" . $this->title
->getNamespace() . " AND ar_title='" .
68 $dbr->strencode( $this->title
->getDBkey() ) . "' ORDER BY ar_timestamp DESC LIMIT 1";
69 $ret = $dbr->query( $sql );
70 $row = $dbr->fetchObject( $ret );
71 $dbr->freeResult( $ret );
73 return Article
::getRevisionText( $row, "ar_" );
79 function isDeleted() {
80 $dbr =& wfGetDB( DB_SLAVE
);
81 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
82 array( 'ar_namespace' => $this->title
->getNamespace(),
83 'ar_title' => $this->title
->getDBkey() ) );
87 function undelete( $timestamps ) {
88 global $wgUser, $wgOut, $wgLang, $wgDeferredUpdateList;
89 global $wgUseSquid, $wgInternalServer, $wgLinkCache;
91 $fname = "doUndeleteArticle";
92 $restoreAll = empty( $timestamps );
93 $restoreRevisions = count( $timestamps );
95 $dbw =& wfGetDB( DB_MASTER
);
96 extract( $dbw->tableNames( 'cur', 'archive', 'old' ) );
97 $namespace = $this->title
->getNamespace();
98 $ttl = $this->title
->getDBkey();
99 $t = $dbw->strencode( $ttl );
101 # Move article and history from the "archive" table
102 $sql = "SELECT COUNT(*) AS count FROM $cur WHERE cur_namespace={$namespace} AND cur_title='{$t}' FOR UPDATE";
103 $res = $dbw->query( $sql, $fname );
104 $row = $dbw->fetchObject( $res );
105 $now = wfTimestampNow();
107 if( $row->count
== 0) {
108 # Have to create new article...
109 $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}' ";
111 $max = $dbw->addQuotes( $dbw->timestamp( array_shift( $timestamps ) ) );
112 $sql .= "AND ar_timestamp={$max} ";
114 $sql .= "ORDER BY ar_timestamp DESC LIMIT 1 FOR UPDATE";
115 $res = $dbw->query( $sql, $fname );
116 $s = $dbw->fetchObject( $res );
118 $max = $s->ar_timestamp
;
120 $text = Article
::getRevisionText( $s, "ar_" );
122 $redirect = MagicWord
::get( MAG_REDIRECT
);
123 $redir = $redirect->matchStart( $text ) ?
1 : 0;
125 $rand = number_format( mt_rand() / mt_getrandmax(), 12, '.', '' );
126 $dbw->insertArray( 'cur', array(
127 'cur_id' => $dbw->nextSequenceValue( 'cur_cur_id_seq' ),
128 'cur_namespace' => $namespace,
131 'cur_comment' => $s->ar_comment
,
132 'cur_user' => $s->ar_user
,
133 'cur_timestamp' => $s->ar_timestamp
,
134 'cur_minor_edit' => $s->ar_minor_edit
,
135 'cur_user_text' => $s->ar_user_text
,
136 'cur_is_redirect' => $redir,
137 'cur_random' => $rand,
138 'cur_touched' => $dbw->timestamp( $now ),
139 'inverse_timestamp' => wfInvertTimestamp( wfTimestamp( TS_MW
, $s->ar_timestamp
) ),
142 $newid = $dbw->insertId();
144 $oldones = "AND ar_timestamp<{$max}";
147 # If already exists, put history entirely into old table
151 # But to make the history list show up right, we need to touch it.
152 $sql = "UPDATE $cur SET cur_touched='{$now}' WHERE cur_namespace={$namespace} AND cur_title='{$t}'";
153 $dbw->query( $sql, $fname );
155 # FIXME: Sometimes restored entries will be _newer_ than the current version.
161 foreach( $timestamps as $ts ) {
162 array_push( $oldts, $dbw->addQuotes( $dbw->timestamp( $ts ) ) );
164 $oldts = join( ",", $oldts );
165 $oldones = "AND ar_timestamp IN ( {$oldts} )";
167 $sql = "INSERT INTO $old (old_namespace,old_title,old_text," .
168 "old_comment,old_user,old_user_text,old_timestamp,inverse_timestamp,old_minor_edit," .
169 "old_flags) SELECT ar_namespace,ar_title,ar_text,ar_comment," .
170 "ar_user,ar_user_text,ar_timestamp,99999999999999-ar_timestamp,ar_minor_edit,ar_flags " .
171 "FROM $archive WHERE ar_namespace={$namespace} AND ar_title='{$t}' {$oldones}";
172 if( $restoreAll ||
!empty( $oldts ) ) {
173 $dbw->query( $sql, $fname );
176 # Finally, clean up the link tables
178 $wgLinkCache = new LinkCache();
180 $wgLinkCache->forUpdate( true );
181 # Create a dummy OutputPage to update the outgoing links
182 $dummyOut = new OutputPage();
183 $dummyOut->addWikiText( $text );
185 $u = new LinksUpdate( $newid, $this->title
->getPrefixedDBkey() );
186 array_push( $wgDeferredUpdateList, $u );
188 Article
::onArticleCreate( $this->title
);
190 #TODO: SearchUpdate, etc.
193 # Now that it's safely stored, take it out of the archive
194 $sql = "DELETE FROM $archive WHERE ar_namespace={$namespace} AND " .
197 $sql .= " AND ar_timestamp IN ( {$oldts}";
199 if( !empty( $oldts ) ) $sql .= ",";
204 $dbw->query( $sql, $fname );
208 $log = new LogPage( 'delete' );
212 $reason = wfMsg( 'undeletedrevisions', $restoreRevisions );
214 $log->addEntry( 'restore', $this->title
, $reason );
223 * @subpackage SpecialPage
226 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
227 var $mTargetTimestamp;
229 function UndeleteForm( &$request, $par = "" ) {
230 $this->mAction
= $request->getText( 'action' );
231 $this->mTarget
= $request->getText( 'target' );
232 $this->mTimestamp
= $request->getText( 'timestamp' );
233 $this->mRestore
= $request->getCheck( 'restore' ) && $request->wasPosted();
235 $this->mTarget
= $par;
237 if ( $this->mTarget
!== "" ) {
238 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
240 $this->mTargetObj
= NULL;
242 if( $this->mRestore
) {
243 $timestamps = array();
244 foreach( $_REQUEST as $key => $val ) {
245 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
246 array_push( $timestamps, $matches[1] );
249 rsort( $timestamps );
250 $this->mTargetTimestamp
= $timestamps;
255 if( is_null( $this->mTargetObj
) ) {
256 return $this->showList();
258 if( $this->mTimestamp
!== "" ) {
259 return $this->showRevision( $this->mTimestamp
);
261 if( $this->mRestore
&& $this->mAction
== "submit" ) {
262 return $this->undelete();
264 return $this->showHistory();
267 /* private */ function showList() {
268 global $wgLang, $wgContLang, $wgUser, $wgOut;
269 $fname = "UndeleteForm::showList";
271 # List undeletable articles
272 $result = PageArchive
::listAllPages();
274 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
275 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
277 $sk = $wgUser->getSkin();
278 $undelete =& Title
::makeTitle( NS_SPECIAL
, 'Undelete' );
279 $wgOut->addHTML( "<ul>\n" );
280 while( $row = $result->fetchObject() ) {
281 $n = ($row->ar_namespace ?
282 ($wgContLang->getNsText( $row->ar_namespace
) . ":") : "").
284 $link = $sk->makeKnownLinkObj( $undelete,
285 htmlspecialchars( $n ), "target=" . urlencode( $n ) );
286 $revisions = htmlspecialchars( wfMsg( "undeleterevisions",
287 $wgLang->formatNum( $row->count
) ) );
288 $wgOut->addHTML( "<li>$link $revisions</li>\n" );
291 $wgOut->addHTML( "</ul>\n" );
296 /* private */ function showRevision( $timestamp ) {
297 global $wgLang, $wgUser, $wgOut;
298 $fname = "UndeleteForm::showRevision";
300 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
302 $archive =& new PageArchive( $this->mTargetObj
);
303 $text = $archive->getRevisionText( $timestamp );
305 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
306 $wgOut->addWikiText( "(" . wfMsg( "undeleterevision",
307 $wgLang->date( $timestamp ) ) . ")\n<hr />\n" . $text );
310 /* private */ function showHistory() {
311 global $wgLang, $wgUser, $wgOut;
313 $sk = $wgUser->getSkin();
314 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
316 $archive = new PageArchive( $this->mTargetObj
);
317 $text = $archive->getLastRevisionText();
318 if( is_null( $text ) ) {
319 $wgOut->addWikiText( wfMsg( "nohistory" ) );
322 $wgOut->addWikiText( wfMsg( "undeletehistory" ) . "\n----\n" . $text );
324 # List all stored revisions
325 $revisions = $archive->listRevisions();
327 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Undelete" );
328 $action = $titleObj->escapeLocalURL( "action=submit" );
329 $encTarget = htmlspecialchars( $this->mTarget
);
330 $button = htmlspecialchars( wfMsg("undeletebtn") );
333 <form id=\"undelete\" method=\"post\" action=\"{$action}\">
334 <input type=\"hidden\" name=\"target\" value=\"{$encTarget}\" />
335 <input type=\"submit\" name=\"restore\" value=\"{$button}\" />
338 # Show relevant lines from the deletion log:
339 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
340 require_once( 'SpecialLog.php' );
341 $logViewer =& new LogViewer(
344 array( 'page' => $this->mTargetObj
->getPrefixedText(),
345 'type' => 'delete' ) ) ) );
346 $logViewer->showList( $wgOut );
348 # The page's stored (deleted) history:
349 $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
350 $wgOut->addHTML("<ul>");
351 $target = urlencode( $this->mTarget
);
352 while( $row = $revisions->fetchObject() ) {
353 $checkBox = "<input type=\"checkbox\" name=\"ts{$row->ar_timestamp}\" value=\"1\" />";
354 $pageLink = $sk->makeKnownLinkObj( $titleObj,
355 $wgLang->timeanddate( $row->ar_timestamp
, true ),
356 "target=$target×tamp={$row->ar_timestamp}" );
357 $userLink = htmlspecialchars( $row->ar_user_text
);
358 if( $row->ar_user
) {
359 $userLink = $sk->makeKnownLinkObj(
360 Title
::makeTitle( NS_USER
, $row->ar_user_text
),
363 $comment = $sk->formatComment( $row->ar_comment
);
364 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink (<i>$comment</i>)</li>\n" );
368 $wgOut->addHTML("</ul>\n</form>");
373 function undelete() {
375 if( !is_null( $this->mTargetObj
) ) {
376 $archive = new PageArchive( $this->mTargetObj
);
377 if( $archive->undelete( $this->mTargetTimestamp
) ) {
378 $wgOut->addWikiText( wfMsg( "undeletedtext", $this->mTarget
) );
382 $wgOut->fatalError( wfMsg( "cannotundelete" ) );