3 function wfSpecialUndelete( $par )
7 $form = new UndeleteForm( $wgRequest, $par );
12 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
14 function UndeleteForm( &$request, $par = "" ) {
15 $this->mAction
= $request->getText( 'action' );
16 $this->mTarget
= $request->getText( 'target' );
17 $this->mTimestamp
= $request->getText( 'timestamp' );
18 $this->mRestore
= $request->getCheck( 'restore' );
20 $this->mTarget
= $par;
22 if ( $this->mTarget
!== "" ) {
23 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
25 $this->mTargetObj
= NULL;
30 if( !is_null( $this->mTargetObj
) ) {
31 $title = $this->mTargetObj
->mDbkeyform
;
32 $namespace = $this->mTargetObj
->mNamespace
;
33 if( $this->mTimestamp
!== "" ) {
34 return $this->showRevision( $namespace, $title, $this->mTimestamp
);
36 if( $this->mRestore
and $this->mAction
== "submit" ) {
37 return $this->undelete( $namespace, $title );
39 return $this->showHistory( $namespace, $title );
41 return $this->showList();
45 /* private */ function showList() {
46 global $wgLang, $wgUser, $wgOut;
47 $fname = "UndeleteForm::showList";
49 # List undeletable articles
50 $dbr =& wfGetDB( DB_SLAVE
);
51 $archive = $dbr->tableName( 'archive' );
53 $sql = "SELECT ar_namespace,ar_title, COUNT(*) AS count FROM $archive " .
54 "GROUP BY ar_namespace,ar_title ORDER BY ar_namespace,ar_title";
56 $res = $dbr->query( $sql, $fname );
58 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
59 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
61 $special = $wgLang->getNsText( Namespace::getSpecial() );
62 $sk = $wgUser->getSkin();
63 $wgOut->addHTML( "<ul>\n" );
64 while ($row = $dbr->fetchObject( $res )) {
65 $n = ($row->ar_namespace ?
66 ($wgLang->getNsText( $row->ar_namespace
) . ":") : "").
69 $wgOut->addHTML( "<li>" .
70 $sk->makeKnownLink( $wgLang->specialPage( "Undelete" ),
71 $n, "target=" . urlencode($n) ) . " " .
72 wfMsg( "undeleterevisions", $row->count
) );
74 $wgOut->addHTML( "</ul>\n" );
79 /* private */ function showRevision( $namespace, $title, $timestamp ) {
80 global $wgLang, $wgUser, $wgOut;
81 $fname = "UndeleteForm::showRevision";
83 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
85 $dbr =& wfGetDB( DB_SLAVE
);
86 $archive = $dbr->tableName( 'archive' );
87 $sql = "SELECT ar_text,ar_flags FROM $archive ".
88 "WHERE ar_namespace={$namespace} AND ar_title='" .
89 $dbr->strencode( $title ) . "' AND ar_timestamp='" . $dbr->strencode( $timestamp ) ."'";
90 $ret = $dbr->query( $sql, $fname );
91 $row = $dbr->fetchObject( $ret );
93 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
94 $wgOut->addWikiText( "(" . wfMsg( "undeleterevision", $wgLang->date($timestamp, true) )
95 . ")\n<hr>\n" . Article
::getRevisionText( $row, "ar_" ) );
100 /* private */ function showHistory( $namespace, $title ) {
101 global $wgLang, $wgUser, $wgOut;
103 $sk = $wgUser->getSkin();
104 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
105 $dbr =& wfGetDB( DB_SLAVE
);
106 $archive = $dbr->tableName( 'archive' );
108 # Get text of first revision
109 $sql = "SELECT ar_text FROM $archive WHERE ar_namespace={$namespace} AND ar_title='" .
110 $dbr->strencode( $title ) . "' ORDER BY ar_timestamp DESC LIMIT 1";
111 $ret = $dbr->query( $sql );
113 if( $dbr->numRows( $ret ) == 0 ) {
114 $wgOut->addWikiText( wfMsg( "nohistory" ) );
117 $row = $dbr->fetchObject( $ret );
118 $wgOut->addWikiText( wfMsg( "undeletehistory" ) . "\n<hr>\n" . $row->ar_text
);
120 # List all stored revisions
121 $sql = "SELECT ar_minor_edit,ar_timestamp,ar_user,ar_user_text,ar_comment
122 FROM archive WHERE ar_namespace={$namespace} AND ar_title='" . $dbr->strencode( $title ) .
123 "' ORDER BY ar_timestamp DESC";
124 $ret = $dbr->query( $sql );
126 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Undelete" );
127 $action = $titleObj->escapeLocalURL( "action=submit" );
128 $encTarget = htmlspecialchars( $this->mTarget
);
131 <form id=\"undelete\" method=\"post\" action=\"{$action}\">
132 <input type=hidden name=\"target\" value=\"{$encTarget}\">
133 <input type=submit name=\"restore\" value=\"".wfMsg("undeletebtn")."\">
136 # Show relevant lines from the deletion log:
137 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
138 require_once( 'SpecialLog.php' );
139 $t = Title
::MakeTitle( $namespace, $title );
140 # TODO: make a pure, faux request for this.
142 $logReader = new LogReader( $wgRequest );
143 $logReader->limitTitle( $t->getPrefixedText() );
144 $logViewer =& new LogViewer( $logReader );
145 $logViewer->showList( $wgOut );
147 # The page's stored (deleted) history:
148 $special = $wgLang->getNsText( Namespace::getSpecial() );
149 $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
150 $wgOut->addHTML("<ul>");
151 while( $row = $dbr->fetchObject( $ret ) ) {
152 $wgOut->addHTML( "<li>" .
153 $sk->makeKnownLink( $wgLang->specialPage( "Undelete" ),
154 $wgLang->timeanddate( $row->ar_timestamp
, true ),
155 "target=" . urlencode($this->mTarget
) . "×tamp={$row->ar_timestamp}" ) . " " .
156 ". . " . htmlspecialchars( $row->ar_user_text
) .
157 " <i>(" . htmlspecialchars($row->ar_comment
) . "</i>)\n");
160 $wgOut->addHTML("</ul>");
165 /* private */ function undelete( $namespace, $title )
167 global $wgUser, $wgOut, $wgLang, $wgDeferredUpdateList;
168 global $wgUseSquid, $wgInternalServer, $wgLinkCache;
170 $fname = "doUndeleteArticle";
172 if ( "" == $title ) {
173 $wgOut->fatalError( wfMsg( "cannotundelete" ) );
176 $dbw =& wfGetDB( DB_MASTER
);
177 extract( $dbw->tableNames( 'cur', 'archive', 'old' ) );
178 $t = $dbw->strencode($title);
180 # Move article and history from the "archive" table
181 $sql = "SELECT COUNT(*) AS count FROM $cur WHERE cur_namespace={$namespace} AND cur_title='{$t}' FOR UPDATE";
182 $res = $dbw->query( $sql, $fname );
183 $row = $dbw->fetchObject( $res );
184 $now = wfTimestampNow();
186 if( $row->count
== 0) {
187 # Have to create new article...
188 $sql = "SELECT ar_text,ar_timestamp,ar_flags FROM $archive WHERE ar_namespace={$namespace} AND ar_title='{$t}' " .
189 "ORDER BY ar_timestamp DESC LIMIT 1 FOR UPDATE";
190 $res = $dbw->query( $sql, $fname );
191 $s = $dbw->fetchObject( $res );
192 $max = $s->ar_timestamp
;
193 $redirect = MagicWord
::get( MAG_REDIRECT
);
194 $redir = $redirect->matchStart( $s->ar_text
) ?
1 : 0;
196 $seqVal = $dbw->addQuotes( $dbw->nextSequenceValue( 'cur_cur_id_seq' ) );
198 $sql = "INSERT INTO $cur (cur_id,cur_namespace,cur_title,cur_text," .
199 "cur_comment,cur_user,cur_user_text,cur_timestamp,inverse_timestamp,cur_minor_edit,cur_is_redirect,cur_random,cur_touched)" .
200 "SELECT $seqVal,ar_namespace,ar_title,ar_text,ar_comment," .
201 "ar_user,ar_user_text,ar_timestamp,99999999999999-ar_timestamp,ar_minor_edit,{$redir},RAND(),'{$now}' FROM $archive " .
202 "WHERE ar_namespace={$namespace} AND ar_title='{$t}' AND ar_timestamp={$max}";
203 $dbw->query( $sql, $fname );
204 $newid = $dbw->insertId();
205 $oldones = "AND ar_timestamp<{$max}";
207 # If already exists, put history entirely into old table
211 # But to make the history list show up right, we need to touch it.
212 $sql = "UPDATE $cur SET cur_touched='{$now}' WHERE cur_namespace={$namespace} AND cur_title='{$t}'";
213 $dbw->query( $sql, $fname );
215 # FIXME: Sometimes restored entries will be _newer_ than the current version.
219 $sql = "INSERT INTO $old (old_namespace,old_title,old_text," .
220 "old_comment,old_user,old_user_text,old_timestamp,inverse_timestamp,old_minor_edit," .
221 "old_flags) SELECT ar_namespace,ar_title,ar_text,ar_comment," .
222 "ar_user,ar_user_text,ar_timestamp,99999999999999-ar_timestamp,ar_minor_edit,ar_flags " .
223 "FROM $archive WHERE ar_namespace={$namespace} AND ar_title='{$t}' {$oldones}";
224 $dbw->query( $sql, $fname );
226 # Finally, clean up the link tables
228 $wgLinkCache = new LinkCache();
230 $wgLinkCache->forUpdate( true );
231 # Create a dummy OutputPage to update the outgoing links
232 $dummyOut = new OutputPage();
234 $text = $dbw->selectField( 'cur', 'cur_text',
235 array( 'cur_id' => $newid, 'cur_namespace' => $namespace ),
238 $dummyOut->addWikiText( $text );
240 $u = new LinksUpdate( $newid, $this->mTargetObj
->getPrefixedDBkey() );
241 array_push( $wgDeferredUpdateList, $u );
243 Article
::onArticleCreate( $this->mTargetObj
);
245 #TODO: SearchUpdate, etc.
248 # Now that it's safely stored, take it out of the archive
249 $sql = "DELETE FROM $archive WHERE ar_namespace={$namespace} AND " .
251 $dbw->query( $sql, $fname );
255 $log = new LogPage( 'delete' );
256 $log->addEntry( 'restore', Title
::makeTitle( $namespace, $title ), "" );
258 $wgOut->addWikiText( wfMsg( "undeletedtext", $this->mTarget
) );