* Refactor to separate the database bits from the UI bits
[lhc/web/wiklou.git] / includes / SpecialUndelete.php
1 <?php
2
3 function wfSpecialUndelete( $par )
4 {
5 global $wgRequest;
6
7 $form = new UndeleteForm( $wgRequest, $par );
8 $form->execute();
9 }
10
11 class PageArchive {
12 var $title;
13
14 function PageArchive( &$title ) {
15 if( is_null( $title ) ) {
16 wfDebugDieBacktrace( 'Archiver() given a null title.');
17 }
18 $this->title =& $title;
19 }
20
21 /* static */ function &listAllPages() {
22 $dbr =& wfGetDB( DB_SLAVE );
23 $archive = $dbr->tableName( 'archive' );
24
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";
27
28 return $dbr->resultObject( $dbr->query( $sql, 'PageArchive::listAllPages' ) );
29 }
30
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 ) );
38 }
39
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_" );
48 }
49
50 function getLastRevisionText() {
51 $dbr =& wfGetDB( DB_SLAVE );
52 $archive = $dbr->tableName( 'archive' );
53
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 );
60 if( $row ) {
61 return Article::getRevisionText( $row, "ar_" );
62 } else {
63 return NULL;
64 }
65 }
66
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() ) );
72 return ($n > 0);
73 }
74
75 function undelete() {
76 global $wgUser, $wgOut, $wgLang, $wgDeferredUpdateList;
77 global $wgUseSquid, $wgInternalServer, $wgLinkCache;
78
79 $fname = "doUndeleteArticle";
80
81 $dbw =& wfGetDB( DB_MASTER );
82 extract( $dbw->tableNames( 'cur', 'archive', 'old' ) );
83 $namespace = $this->title->getNamespace();
84 $t = $dbw->strencode( $this->title->getDBkey() );
85
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();
91
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;
101
102 $seqVal = $dbw->addQuotes( $dbw->nextSequenceValue( 'cur_cur_id_seq' ) );
103
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}";
112 } else {
113 # If already exists, put history entirely into old table
114 $oldones = "";
115 $newid = 0;
116
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 );
120
121 # FIXME: Sometimes restored entries will be _newer_ than the current version.
122 # We should merge.
123 }
124
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 );
131
132 # Finally, clean up the link tables
133 if( $newid ) {
134 $wgLinkCache = new LinkCache();
135 # Select for update
136 $wgLinkCache->forUpdate( true );
137 # Create a dummy OutputPage to update the outgoing links
138 $dummyOut = new OutputPage();
139 # Get the text
140 $text = $dbw->selectField( 'cur', 'cur_text',
141 array( 'cur_id' => $newid, 'cur_namespace' => $namespace ),
142 $fname, 'FOR UPDATE'
143 );
144 $dummyOut->addWikiText( $text );
145
146 $u = new LinksUpdate( $newid, $this->title->getPrefixedDBkey() );
147 array_push( $wgDeferredUpdateList, $u );
148
149 Article::onArticleCreate( $this->title );
150
151 #TODO: SearchUpdate, etc.
152 }
153
154 # Now that it's safely stored, take it out of the archive
155 $sql = "DELETE FROM $archive WHERE ar_namespace={$namespace} AND " .
156 "ar_title='{$t}'";
157 $dbw->query( $sql, $fname );
158
159
160 # Touch the log?
161 $log = new LogPage( 'delete' );
162 $log->addEntry( 'restore', $this->title, "" );
163
164 return true;
165 }
166 }
167
168 class UndeleteForm {
169 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
170
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();
176 if( $par != "" ) {
177 $this->mTarget = $par;
178 }
179 if ( $this->mTarget !== "" ) {
180 $this->mTargetObj = Title::newFromURL( $this->mTarget );
181 } else {
182 $this->mTargetObj = NULL;
183 }
184 }
185
186 function execute() {
187 if( is_null( $this->mTargetObj ) ) {
188 return $this->showList();
189 }
190 if( $this->mTimestamp !== "" ) {
191 return $this->showRevision( $this->mTimestamp );
192 }
193 if( $this->mRestore && $this->mAction == "submit" ) {
194 return $this->undelete();
195 }
196 return $this->showHistory();
197 }
198
199 /* private */ function showList() {
200 global $wgLang, $wgUser, $wgOut;
201 $fname = "UndeleteForm::showList";
202
203 # List undeletable articles
204 $result = PageArchive::listAllPages();
205
206 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
207 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
208
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 ) . ":") : "").
215 $row->ar_title;
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" );
221 }
222 $result->free();
223 $wgOut->addHTML( "</ul>\n" );
224
225 return true;
226 }
227
228 /* private */ function showRevision( $timestamp ) {
229 global $wgLang, $wgUser, $wgOut;
230 $fname = "UndeleteForm::showRevision";
231
232 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
233
234 $archive =& new PageArchive( $this->mTargetObj );
235 $text = $archive->getRevisionText( $timestamp );
236
237 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
238 $wgOut->addWikiText( "(" . wfMsg( "undeleterevision",
239 $wgLang->date( $timestamp ) ) . ")\n<hr />\n" . $text );
240 }
241
242 /* private */ function showHistory() {
243 global $wgLang, $wgUser, $wgOut;
244
245 $sk = $wgUser->getSkin();
246 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
247
248 $archive = new PageArchive( $this->mTargetObj );
249 $text = $archive->getLastRevisionText();
250 if( is_null( $text ) ) {
251 $wgOut->addWikiText( wfMsg( "nohistory" ) );
252 return;
253 }
254 $wgOut->addWikiText( wfMsg( "undeletehistory" ) . "\n----\n" . $text );
255
256 # List all stored revisions
257 $revisions = $archive->listRevisions();
258
259 $titleObj = Title::makeTitle( NS_SPECIAL, "Undelete" );
260 $action = $titleObj->escapeLocalURL( "action=submit" );
261 $encTarget = htmlspecialchars( $this->mTarget );
262 $button = htmlspecialchars( wfMsg("undeletebtn") );
263
264 $wgOut->addHTML("
265 <form id=\"undelete\" method=\"post\" action=\"{$action}\">
266 <input type=\"hidden\" name=\"target\" value=\"{$encTarget}\" />
267 <input type=\"submit\" name=\"restore\" value=\"{$button}\" />
268 </form>");
269
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(
274 new LogReader(
275 new FauxRequest(
276 array( 'page', $this->mTargetObj->getPrefixedText() ) ) ) );
277 $logViewer->showList( $wgOut );
278
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&timestamp={$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 ),
291 $userLink );
292 }
293 $comment = $sk->formatComment( $row->ar_comment );
294 $wgOut->addHTML( "<li>$pageLink . . $userLink (<i>$comment</i>)</li>\n" );
295
296 }
297 $revisions->free();
298 $wgOut->addHTML("</ul>");
299
300 return true;
301 }
302
303 function undelete() {
304 global $wgOut;
305 if( !is_null( $this->mTargetObj ) ) {
306 $archive = new PageArchive( $this->mTargetObj );
307 if( $archive->undelete() ) {
308 $wgOut->addWikiText( wfMsg( "undeletedtext", $this->mTarget ) );
309 return true;
310 }
311 }
312 $wgOut->fatalError( wfMsg( "cannotundelete" ) );
313 return false;
314 }
315 }
316
317 ?>