Start splitting back-end functions from the Article user-interface class.
[lhc/web/wiklou.git] / includes / SpecialUndelete.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 * @subpackage SpecialPage
6 */
7
8 require_once( 'Revision.php' );
9
10 /**
11 *
12 */
13 function wfSpecialUndelete( $par ) {
14 global $wgRequest;
15
16 $form = new UndeleteForm( $wgRequest, $par );
17 $form->execute();
18 }
19
20 /**
21 *
22 * @package MediaWiki
23 * @subpackage SpecialPage
24 */
25 class PageArchive {
26 var $title;
27
28 function PageArchive( &$title ) {
29 if( is_null( $title ) ) {
30 wfDebugDieBacktrace( 'Archiver() given a null title.');
31 }
32 $this->title =& $title;
33 }
34
35 /**
36 * List all deleted pages recorded in the archive table. Returns result
37 * wrapper with (ar_namespace, ar_title, count) fields, ordered by page
38 * namespace/title. Can be called staticaly.
39 *
40 * @return ResultWrapper
41 */
42 /* static */ function &listAllPages() {
43 $dbr =& wfGetDB( DB_SLAVE );
44 $archive = $dbr->tableName( 'archive' );
45
46 $sql = "SELECT ar_namespace,ar_title, COUNT(*) AS count FROM $archive " .
47 "GROUP BY ar_namespace,ar_title ORDER BY ar_namespace,ar_title";
48
49 return $dbr->resultObject( $dbr->query( $sql, 'PageArchive::listAllPages' ) );
50 }
51
52 /**
53 * List the revisions of the given page. Returns result wrapper with
54 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
55 *
56 * @return ResultWrapper
57 */
58 function &listRevisions() {
59 $dbr =& wfGetDB( DB_SLAVE );
60 return $dbr->resultObject( $dbr->select( 'archive',
61 array( 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text', 'ar_comment' ),
62 array( 'ar_namespace' => $this->title->getNamespace(),
63 'ar_title' => $this->title->getDBkey() ),
64 'PageArchive::listRevisions',
65 array( 'ORDER BY' => 'ar_timestamp DESC' ) ) );
66 }
67
68 /**
69 * Fetch (and decompress if necessary) the stored text for the deleted
70 * revision of the page with the given timestamp.
71 *
72 * @return string
73 */
74 function getRevisionText( $timestamp ) {
75 $dbr =& wfGetDB( DB_SLAVE );
76 $row = $dbr->selectRow( 'archive',
77 array( 'ar_text', 'ar_flags' ),
78 array( 'ar_namespace' => $this->title->getNamespace(),
79 'ar_title' => $this->title->getDbkey(),
80 'ar_timestamp' => $dbr->timestamp( $timestamp ) ) );
81 return Revision::getRevisionText( $row, "ar_" );
82 }
83
84 /**
85 * Fetch (and decompress if necessary) the stored text of the most
86 * recently edited deleted revision of the page.
87 *
88 * If there are no archived revisions for the page, returns NULL.
89 *
90 * @return string
91 */
92 function getLastRevisionText() {
93 $dbr =& wfGetDB( DB_SLAVE );
94 $row = $dbr->selectRow( 'archive',
95 array( 'ar_text', 'ar_flags' ),
96 array( 'ar_namespace' => $this->title->getNamespace(),
97 'ar_title' => $this->title->getDBkey() ),
98 'PageArchive::getLastRevisionText',
99 'ORDER BY ar_timestamp DESC LIMIT 1' );
100 if( $row ) {
101 return Revision::getRevisionText( $row, "ar_" );
102 } else {
103 return NULL;
104 }
105 }
106
107 /**
108 * Quick check if any archived revisions are present for the page.
109 * @return bool
110 */
111 function isDeleted() {
112 $dbr =& wfGetDB( DB_SLAVE );
113 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
114 array( 'ar_namespace' => $this->title->getNamespace(),
115 'ar_title' => $this->title->getDBkey() ) );
116 return ($n > 0);
117 }
118
119 /**
120 * This is the meaty bit -- restores archived revisions of the given page
121 * to the cur/old tables. If the page currently exists, all revisions will
122 * be stuffed into old, otherwise the most recent will go into cur.
123 * The deletion log will be updated with an undeletion notice.
124 *
125 * Returns true on success.
126 *
127 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
128 * @return bool
129 */
130 function undelete( $timestamps ) {
131 global $wgUser, $wgOut, $wgLang, $wgDeferredUpdateList;
132 global $wgUseSquid, $wgInternalServer, $wgLinkCache;
133
134 $fname = "doUndeleteArticle";
135 $restoreAll = empty( $timestamps );
136 $restoreRevisions = count( $timestamps );
137
138 $dbw =& wfGetDB( DB_MASTER );
139 extract( $dbw->tableNames( 'cur', 'archive', 'old' ) );
140 $namespace = $this->title->getNamespace();
141 $ttl = $this->title->getDBkey();
142 $t = $dbw->strencode( $ttl );
143
144 # Move article and history from the "archive" table
145 $sql = "SELECT COUNT(*) AS count FROM $cur WHERE cur_namespace={$namespace} AND cur_title='{$t}'";
146 global $wgDBtype;
147 if( $wgDBtype != 'PostgreSQL' ) { # HACKHACKHACKHACK
148 $sql .= ' FOR UPDATE';
149 }
150 $res = $dbw->query( $sql, $fname );
151 $row = $dbw->fetchObject( $res );
152 $now = wfTimestampNow();
153
154 if( $row->count == 0) {
155 # Have to create new article...
156 $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}' ";
157 if( !$restoreAll ) {
158 $max = $dbw->addQuotes( $dbw->timestamp( array_shift( $timestamps ) ) );
159 $sql .= "AND ar_timestamp={$max} ";
160 }
161 $sql .= "ORDER BY ar_timestamp DESC LIMIT 1 FOR UPDATE";
162 $res = $dbw->query( $sql, $fname );
163 $s = $dbw->fetchObject( $res );
164 if( $restoreAll ) {
165 $max = $s->ar_timestamp;
166 }
167 $text = Revision::getRevisionText( $s, "ar_" );
168
169 $redirect = MagicWord::get( MAG_REDIRECT );
170 $redir = $redirect->matchStart( $text ) ? 1 : 0;
171
172 $rand = wfRandom();
173 $dbw->insert( 'cur', array(
174 'cur_id' => $dbw->nextSequenceValue( 'cur_cur_id_seq' ),
175 'cur_namespace' => $namespace,
176 'cur_title' => $ttl,
177 'cur_text' => $text,
178 'cur_comment' => $s->ar_comment,
179 'cur_user' => $s->ar_user,
180 'cur_timestamp' => $s->ar_timestamp,
181 'cur_minor_edit' => $s->ar_minor_edit,
182 'cur_user_text' => $s->ar_user_text,
183 'cur_is_redirect' => $redir,
184 'cur_random' => $rand,
185 'cur_touched' => $dbw->timestamp( $now ),
186 'inverse_timestamp' => wfInvertTimestamp( wfTimestamp( TS_MW, $s->ar_timestamp ) ),
187 ), $fname );
188
189 $newid = $dbw->insertId();
190 if( $restoreAll ) {
191 $oldones = "AND ar_timestamp<" . $dbw->addQuotes( $dbw->timestamp( $max ) );
192 }
193 } else {
194 # If already exists, put history entirely into old table
195 $oldones = "";
196 $newid = 0;
197
198 # But to make the history list show up right, we need to touch it.
199 $sql = "UPDATE $cur SET cur_touched='{$now}' WHERE cur_namespace={$namespace} AND cur_title='{$t}'";
200 $dbw->query( $sql, $fname );
201
202 # FIXME: Sometimes restored entries will be _newer_ than the current version.
203 # We should merge.
204 }
205
206 if( !$restoreAll ) {
207 $oldts = array();
208 foreach( $timestamps as $ts ) {
209 array_push( $oldts, $dbw->addQuotes( $dbw->timestamp( $ts ) ) );
210 }
211 $oldts = join( ",", $oldts );
212 $oldones = "AND ar_timestamp IN ( {$oldts} )";
213 }
214 $sql = "INSERT INTO $old (old_namespace,old_title,old_text," .
215 "old_comment,old_user,old_user_text,old_timestamp,inverse_timestamp,old_minor_edit," .
216 "old_flags) SELECT ar_namespace,ar_title,ar_text,ar_comment," .
217 "ar_user,ar_user_text,ar_timestamp,99999999999999-ar_timestamp,ar_minor_edit,ar_flags " .
218 "FROM $archive WHERE ar_namespace={$namespace} AND ar_title='{$t}' {$oldones}";
219 if( $restoreAll || !empty( $oldts ) ) {
220 $dbw->query( $sql, $fname );
221 }
222
223 # Finally, clean up the link tables
224 if( $newid ) {
225 $wgLinkCache = new LinkCache();
226 # Select for update
227 $wgLinkCache->forUpdate( true );
228 # Create a dummy OutputPage to update the outgoing links
229 $dummyOut = new OutputPage();
230 $dummyOut->addWikiText( $text );
231
232 $u = new LinksUpdate( $newid, $this->title->getPrefixedDBkey() );
233 array_push( $wgDeferredUpdateList, $u );
234
235 Article::onArticleCreate( $this->title );
236
237 #TODO: SearchUpdate, etc.
238 }
239
240 # Now that it's safely stored, take it out of the archive
241 $sql = "DELETE FROM $archive WHERE ar_namespace={$namespace} AND " .
242 "ar_title='{$t}'";
243 if( !$restoreAll ) {
244 $sql .= " AND ar_timestamp IN ( {$oldts}";
245 if( $newid ) {
246 if( !empty( $oldts ) ) $sql .= ",";
247 $sql .= $max;
248 }
249 $sql .= ")";
250 }
251 $dbw->query( $sql, $fname );
252
253
254 # Touch the log?
255 $log = new LogPage( 'delete' );
256 if( $restoreAll ) {
257 $reason = "";
258 } else {
259 $reason = wfMsg( 'undeletedrevisions', $restoreRevisions );
260 }
261 $log->addEntry( 'restore', $this->title, $reason );
262
263 return true;
264 }
265 }
266
267 /**
268 *
269 * @package MediaWiki
270 * @subpackage SpecialPage
271 */
272 class UndeleteForm {
273 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
274 var $mTargetTimestamp;
275
276 function UndeleteForm( &$request, $par = "" ) {
277 $this->mAction = $request->getText( 'action' );
278 $this->mTarget = $request->getText( 'target' );
279 $this->mTimestamp = $request->getText( 'timestamp' );
280 $this->mRestore = $request->getCheck( 'restore' ) && $request->wasPosted();
281 if( $par != "" ) {
282 $this->mTarget = $par;
283 }
284 if ( $this->mTarget !== "" ) {
285 $this->mTargetObj = Title::newFromURL( $this->mTarget );
286 } else {
287 $this->mTargetObj = NULL;
288 }
289 if( $this->mRestore ) {
290 $timestamps = array();
291 foreach( $_REQUEST as $key => $val ) {
292 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
293 array_push( $timestamps, $matches[1] );
294 }
295 }
296 rsort( $timestamps );
297 $this->mTargetTimestamp = $timestamps;
298 }
299 }
300
301 function execute() {
302 if( is_null( $this->mTargetObj ) ) {
303 return $this->showList();
304 }
305 if( $this->mTimestamp !== "" ) {
306 return $this->showRevision( $this->mTimestamp );
307 }
308 if( $this->mRestore && $this->mAction == "submit" ) {
309 return $this->undelete();
310 }
311 return $this->showHistory();
312 }
313
314 /* private */ function showList() {
315 global $wgLang, $wgContLang, $wgUser, $wgOut;
316 $fname = "UndeleteForm::showList";
317
318 # List undeletable articles
319 $result = PageArchive::listAllPages();
320
321 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
322 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
323
324 $sk = $wgUser->getSkin();
325 $undelete =& Title::makeTitle( NS_SPECIAL, 'Undelete' );
326 $wgOut->addHTML( "<ul>\n" );
327 while( $row = $result->fetchObject() ) {
328 $n = ($row->ar_namespace ?
329 ($wgContLang->getNsText( $row->ar_namespace ) . ":") : "").
330 $row->ar_title;
331 $link = $sk->makeKnownLinkObj( $undelete,
332 htmlspecialchars( $n ), "target=" . urlencode( $n ) );
333 $revisions = htmlspecialchars( wfMsg( "undeleterevisions",
334 $wgLang->formatNum( $row->count ) ) );
335 $wgOut->addHTML( "<li>$link $revisions</li>\n" );
336 }
337 $result->free();
338 $wgOut->addHTML( "</ul>\n" );
339
340 return true;
341 }
342
343 /* private */ function showRevision( $timestamp ) {
344 global $wgLang, $wgUser, $wgOut;
345 $fname = "UndeleteForm::showRevision";
346
347 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
348
349 $archive =& new PageArchive( $this->mTargetObj );
350 $text = $archive->getRevisionText( $timestamp );
351
352 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
353 $wgOut->addWikiText( "(" . wfMsg( "undeleterevision",
354 $wgLang->date( $timestamp ) ) . ")\n<hr />\n" . $text );
355 }
356
357 /* private */ function showHistory() {
358 global $wgLang, $wgUser, $wgOut;
359
360 $sk = $wgUser->getSkin();
361 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
362
363 $archive = new PageArchive( $this->mTargetObj );
364 $text = $archive->getLastRevisionText();
365 if( is_null( $text ) ) {
366 $wgOut->addWikiText( wfMsg( "nohistory" ) );
367 return;
368 }
369 $wgOut->addWikiText( wfMsg( "undeletehistory" ) . "\n----\n" . $text );
370
371 # List all stored revisions
372 $revisions = $archive->listRevisions();
373
374 $titleObj = Title::makeTitle( NS_SPECIAL, "Undelete" );
375 $action = $titleObj->escapeLocalURL( "action=submit" );
376 $encTarget = htmlspecialchars( $this->mTarget );
377 $button = htmlspecialchars( wfMsg("undeletebtn") );
378
379 $wgOut->addHTML("
380 <form id=\"undelete\" method=\"post\" action=\"{$action}\">
381 <input type=\"hidden\" name=\"target\" value=\"{$encTarget}\" />
382 <input type=\"submit\" name=\"restore\" value=\"{$button}\" />
383 ");
384
385 # Show relevant lines from the deletion log:
386 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
387 require_once( 'SpecialLog.php' );
388 $logViewer =& new LogViewer(
389 new LogReader(
390 new FauxRequest(
391 array( 'page' => $this->mTargetObj->getPrefixedText(),
392 'type' => 'delete' ) ) ) );
393 $logViewer->showList( $wgOut );
394
395 # The page's stored (deleted) history:
396 $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
397 $wgOut->addHTML("<ul>");
398 $target = urlencode( $this->mTarget );
399 while( $row = $revisions->fetchObject() ) {
400 $ts = wfTimestamp( TS_MW, $row->ar_timestamp );
401 $checkBox = "<input type=\"checkbox\" name=\"ts$ts\" value=\"1\" />";
402 $pageLink = $sk->makeKnownLinkObj( $titleObj,
403 $wgLang->timeanddate( $row->ar_timestamp, true ),
404 "target=$target&timestamp=$ts" );
405 $userLink = htmlspecialchars( $row->ar_user_text );
406 if( $row->ar_user ) {
407 $userLink = $sk->makeKnownLinkObj(
408 Title::makeTitle( NS_USER, $row->ar_user_text ),
409 $userLink );
410 }
411 $comment = $sk->formatComment( $row->ar_comment );
412 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink (<i>$comment</i>)</li>\n" );
413
414 }
415 $revisions->free();
416 $wgOut->addHTML("</ul>\n</form>");
417
418 return true;
419 }
420
421 function undelete() {
422 global $wgOut;
423 if( !is_null( $this->mTargetObj ) ) {
424 $archive = new PageArchive( $this->mTargetObj );
425 if( $archive->undelete( $this->mTargetTimestamp ) ) {
426 $wgOut->addWikiText( wfMsg( "undeletedtext", $this->mTarget ) );
427 return true;
428 }
429 }
430 $wgOut->fatalError( wfMsg( "cannotundelete" ) );
431 return false;
432 }
433 }
434
435 ?>