ba81557f144345f5e61ad18c1411908dcb8af4ec
3 function wfSpecialUndelete( $par )
7 $form = new UndeleteForm( $wgRequest, $par );
14 function PageArchive( &$title ) {
15 if( is_null( $title ) ) {
16 wfDebugDieBacktrace( 'Archiver() given a null title.');
18 $this->title
=& $title;
21 /* static */ function &listAllPages() {
22 $dbr =& wfGetDB( DB_SLAVE
);
23 $archive = $dbr->tableName( 'archive' );
25 $sql = "SELECT ar_namespace,ar_title, COUNT(*) AS count FROM $archive " .
26 "GROUP BY ar_namespace,ar_title ORDER BY ar_namespace,ar_title";
28 return $dbr->resultObject( $dbr->query( $sql, 'PageArchive::listAllPages' ) );
31 function &listRevisions() {
32 $dbr =& wfGetDB( DB_SLAVE
);
33 $sql = "SELECT ar_minor_edit,ar_timestamp,ar_user,ar_user_text,ar_comment
34 FROM archive WHERE ar_namespace=" . $this->title
->getNamespace() .
35 " AND ar_title='" . $dbr->strencode( $this->title
->getDBkey() ) .
36 "' ORDER BY ar_timestamp DESC";
37 return $dbr->resultObject( $dbr->query( $sql ) );
40 function getRevisionText( $timestamp ) {
41 $dbr =& wfGetDB( DB_SLAVE
);
42 $row = $dbr->selectRow( 'archive',
43 array( 'ar_text', 'ar_flags' ),
44 array( 'ar_namespace' => $this->title
->getNamespace(),
45 'ar_title' => $this->title
->getDbkey(),
46 'ar_timestamp' => $timestamp ) );
47 return Article
::getRevisionText( $row, "ar_" );
50 function getLastRevisionText() {
51 $dbr =& wfGetDB( DB_SLAVE
);
52 $archive = $dbr->tableName( 'archive' );
54 # Get text of first revision
55 $sql = "SELECT ar_text,ar_flags FROM $archive WHERE ar_namespace=" . $this->title
->getNamespace() . " AND ar_title='" .
56 $dbr->strencode( $this->title
->getDBkey() ) . "' ORDER BY ar_timestamp DESC LIMIT 1";
57 $ret = $dbr->query( $sql );
58 $row = $dbr->fetchObject( $ret );
59 $dbr->freeResult( $ret );
61 return Article
::getRevisionText( $row, "ar_" );
67 function isDeleted() {
68 $dbr =& wfGetDB( DB_SLAVE
);
69 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
70 array( 'ar_namespace' => $this->title
->getNamespace(),
71 'ar_title' => $this->title
->getDBkey() ) );
76 global $wgUser, $wgOut, $wgLang, $wgDeferredUpdateList;
77 global $wgUseSquid, $wgInternalServer, $wgLinkCache;
79 $fname = "doUndeleteArticle";
81 $dbw =& wfGetDB( DB_MASTER
);
82 extract( $dbw->tableNames( 'cur', 'archive', 'old' ) );
83 $namespace = $this->title
->getNamespace();
84 $t = $dbw->strencode( $this->title
->getDBkey() );
86 # Move article and history from the "archive" table
87 $sql = "SELECT COUNT(*) AS count FROM $cur WHERE cur_namespace={$namespace} AND cur_title='{$t}' FOR UPDATE";
88 $res = $dbw->query( $sql, $fname );
89 $row = $dbw->fetchObject( $res );
90 $now = wfTimestampNow();
92 if( $row->count
== 0) {
93 # Have to create new article...
94 $sql = "SELECT ar_text,ar_timestamp,ar_flags FROM $archive WHERE ar_namespace={$namespace} AND ar_title='{$t}' " .
95 "ORDER BY ar_timestamp DESC LIMIT 1 FOR UPDATE";
96 $res = $dbw->query( $sql, $fname );
97 $s = $dbw->fetchObject( $res );
98 $max = $s->ar_timestamp
;
99 $redirect = MagicWord
::get( MAG_REDIRECT
);
100 $redir = $redirect->matchStart( $s->ar_text
) ?
1 : 0;
102 $seqVal = $dbw->addQuotes( $dbw->nextSequenceValue( 'cur_cur_id_seq' ) );
104 $sql = "INSERT INTO $cur (cur_id,cur_namespace,cur_title,cur_text," .
105 "cur_comment,cur_user,cur_user_text,cur_timestamp,inverse_timestamp,cur_minor_edit,cur_is_redirect,cur_random,cur_touched)" .
106 "SELECT $seqVal,ar_namespace,ar_title,ar_text,ar_comment," .
107 "ar_user,ar_user_text,ar_timestamp,99999999999999-ar_timestamp,ar_minor_edit,{$redir},RAND(),'{$now}' FROM $archive " .
108 "WHERE ar_namespace={$namespace} AND ar_title='{$t}' AND ar_timestamp={$max}";
109 $dbw->query( $sql, $fname );
110 $newid = $dbw->insertId();
111 $oldones = "AND ar_timestamp<{$max}";
113 # If already exists, put history entirely into old table
117 # But to make the history list show up right, we need to touch it.
118 $sql = "UPDATE $cur SET cur_touched='{$now}' WHERE cur_namespace={$namespace} AND cur_title='{$t}'";
119 $dbw->query( $sql, $fname );
121 # FIXME: Sometimes restored entries will be _newer_ than the current version.
125 $sql = "INSERT INTO $old (old_namespace,old_title,old_text," .
126 "old_comment,old_user,old_user_text,old_timestamp,inverse_timestamp,old_minor_edit," .
127 "old_flags) SELECT ar_namespace,ar_title,ar_text,ar_comment," .
128 "ar_user,ar_user_text,ar_timestamp,99999999999999-ar_timestamp,ar_minor_edit,ar_flags " .
129 "FROM $archive WHERE ar_namespace={$namespace} AND ar_title='{$t}' {$oldones}";
130 $dbw->query( $sql, $fname );
132 # Finally, clean up the link tables
134 $wgLinkCache = new LinkCache();
136 $wgLinkCache->forUpdate( true );
137 # Create a dummy OutputPage to update the outgoing links
138 $dummyOut = new OutputPage();
140 $text = $dbw->selectField( 'cur', 'cur_text',
141 array( 'cur_id' => $newid, 'cur_namespace' => $namespace ),
144 $dummyOut->addWikiText( $text );
146 $u = new LinksUpdate( $newid, $this->title
->getPrefixedDBkey() );
147 array_push( $wgDeferredUpdateList, $u );
149 Article
::onArticleCreate( $this->title
);
151 #TODO: SearchUpdate, etc.
154 # Now that it's safely stored, take it out of the archive
155 $sql = "DELETE FROM $archive WHERE ar_namespace={$namespace} AND " .
157 $dbw->query( $sql, $fname );
161 $log = new LogPage( 'delete' );
162 $log->addEntry( 'restore', $this->title
, "" );
169 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
171 function UndeleteForm( &$request, $par = "" ) {
172 $this->mAction
= $request->getText( 'action' );
173 $this->mTarget
= $request->getText( 'target' );
174 $this->mTimestamp
= $request->getText( 'timestamp' );
175 $this->mRestore
= $request->getCheck( 'restore' ) && $request->wasPosted();
177 $this->mTarget
= $par;
179 if ( $this->mTarget
!== "" ) {
180 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
182 $this->mTargetObj
= NULL;
187 if( is_null( $this->mTargetObj
) ) {
188 return $this->showList();
190 if( $this->mTimestamp
!== "" ) {
191 return $this->showRevision( $this->mTimestamp
);
193 if( $this->mRestore
&& $this->mAction
== "submit" ) {
194 return $this->undelete();
196 return $this->showHistory();
199 /* private */ function showList() {
200 global $wgLang, $wgUser, $wgOut;
201 $fname = "UndeleteForm::showList";
203 # List undeletable articles
204 $result = PageArchive
::listAllPages();
206 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
207 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
209 $sk = $wgUser->getSkin();
210 $undelete =& Title
::makeTitle( NS_SPECIAL
, 'Undelete' );
211 $wgOut->addHTML( "<ul>\n" );
212 while( $row = $result->fetchObject() ) {
213 $n = ($row->ar_namespace ?
214 ($wgLang->getNsText( $row->ar_namespace
) . ":") : "").
216 $link = $sk->makeKnownLinkObj( $undelete,
217 htmlspecialchars( $n ), "target=" . urlencode( $n ) );
218 $revisions = htmlspecialchars( wfMsg( "undeleterevisions",
219 $wgLang->formatNum( $row->count
) ) );
220 $wgOut->addHTML( "<li>$link $revisions</li>\n" );
223 $wgOut->addHTML( "</ul>\n" );
228 /* private */ function showRevision( $timestamp ) {
229 global $wgLang, $wgUser, $wgOut;
230 $fname = "UndeleteForm::showRevision";
232 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
234 $archive =& new PageArchive( $this->mTargetObj
);
235 $text = $archive->getRevisionText( $timestamp );
237 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
238 $wgOut->addWikiText( "(" . wfMsg( "undeleterevision",
239 $wgLang->date( $timestamp ) ) . ")\n<hr />\n" . $text );
242 /* private */ function showHistory() {
243 global $wgLang, $wgUser, $wgOut;
245 $sk = $wgUser->getSkin();
246 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
248 $archive = new PageArchive( $this->mTargetObj
);
249 $text = $archive->getLastRevisionText();
250 if( is_null( $text ) ) {
251 $wgOut->addWikiText( wfMsg( "nohistory" ) );
254 $wgOut->addWikiText( wfMsg( "undeletehistory" ) . "\n----\n" . $text );
256 # List all stored revisions
257 $revisions = $archive->listRevisions();
259 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Undelete" );
260 $action = $titleObj->escapeLocalURL( "action=submit" );
261 $encTarget = htmlspecialchars( $this->mTarget
);
262 $button = htmlspecialchars( wfMsg("undeletebtn") );
265 <form id=\"undelete\" method=\"post\" action=\"{$action}\">
266 <input type=\"hidden\" name=\"target\" value=\"{$encTarget}\" />
267 <input type=\"submit\" name=\"restore\" value=\"{$button}\" />
270 # Show relevant lines from the deletion log:
271 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
272 require_once( 'SpecialLog.php' );
273 $logViewer =& new LogViewer(
276 array( 'page', $this->mTargetObj
->getPrefixedText() ) ) ) );
277 $logViewer->showList( $wgOut );
279 # The page's stored (deleted) history:
280 $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
281 $wgOut->addHTML("<ul>");
282 $target = urlencode( $this->mTarget
);
283 while( $row = $revisions->fetchObject() ) {
284 $pageLink = $sk->makeKnownLinkObj( $titleObj,
285 $wgLang->timeanddate( $row->ar_timestamp
, true ),
286 "target=$target×tamp={$row->ar_timestamp}" );
287 $userLink = htmlspecialchars( $row->ar_user_text
);
288 if( $row->ar_user
) {
289 $userLink = $sk->makeKnownLinkObj(
290 Title
::makeTitle( NS_USER
, $row->ar_user_text
),
293 $comment = $sk->formatComment( $row->ar_comment
);
294 $wgOut->addHTML( "<li>$pageLink . . $userLink (<i>$comment</i>)</li>\n" );
298 $wgOut->addHTML("</ul>");
303 function undelete() {
305 if( !is_null( $this->mTargetObj
) ) {
306 $archive = new PageArchive( $this->mTargetObj
);
307 if( $archive->undelete() ) {
308 $wgOut->addWikiText( wfMsg( "undeletedtext", $this->mTarget
) );
312 $wgOut->fatalError( wfMsg( "cannotundelete" ) );