Merge "Clean up X-Content-Dimensions"
[lhc/web/wiklou.git] / includes / page / PageArchive.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 use MediaWiki\MediaWikiServices;
22 use Wikimedia\Rdbms\ResultWrapper;
23 use Wikimedia\Rdbms\IDatabase;
24
25 /**
26 * Used to show archived pages and eventually restore them.
27 */
28 class PageArchive {
29 /** @var Title */
30 protected $title;
31
32 /** @var Status */
33 protected $fileStatus;
34
35 /** @var Status */
36 protected $revisionStatus;
37
38 /** @var Config */
39 protected $config;
40
41 public function __construct( $title, Config $config = null ) {
42 if ( is_null( $title ) ) {
43 throw new MWException( __METHOD__ . ' given a null title.' );
44 }
45 $this->title = $title;
46 if ( $config === null ) {
47 wfDebug( __METHOD__ . ' did not have a Config object passed to it' );
48 $config = MediaWikiServices::getInstance()->getMainConfig();
49 }
50 $this->config = $config;
51 }
52
53 public function doesWrites() {
54 return true;
55 }
56
57 /**
58 * List all deleted pages recorded in the archive table. Returns result
59 * wrapper with (ar_namespace, ar_title, count) fields, ordered by page
60 * namespace/title.
61 *
62 * @return ResultWrapper
63 */
64 public static function listAllPages() {
65 $dbr = wfGetDB( DB_REPLICA );
66
67 return self::listPages( $dbr, '' );
68 }
69
70 /**
71 * List deleted pages recorded in the archive matching the
72 * given term, using search engine archive.
73 * Returns result wrapper with (ar_namespace, ar_title, count) fields.
74 *
75 * @param string $term Search term
76 * @return ResultWrapper
77 */
78 public static function listPagesBySearch( $term ) {
79 $title = Title::newFromText( $term );
80 if ( $title ) {
81 $ns = $title->getNamespace();
82 $termMain = $title->getText();
83 $termDb = $title->getDBkey();
84 } else {
85 // Prolly won't work too good
86 // @todo handle bare namespace names cleanly?
87 $ns = 0;
88 $termMain = $termDb = $term;
89 }
90
91 // Try search engine first
92 $engine = MediaWikiServices::getInstance()->newSearchEngine();
93 $engine->setLimitOffset( 100 );
94 $engine->setNamespaces( [ $ns ] );
95 $results = $engine->searchArchiveTitle( $termMain );
96 if ( !$results->isOK() ) {
97 $results = [];
98 } else {
99 $results = $results->getValue();
100 }
101
102 if ( !$results ) {
103 // Fall back to regular prefix search
104 return self::listPagesByPrefix( $term );
105 }
106
107 $dbr = wfGetDB( DB_REPLICA );
108 $condTitles = array_unique( array_map( function ( Title $t ) {
109 return $t->getDBkey();
110 }, $results ) );
111 $conds = [
112 'ar_namespace' => $ns,
113 $dbr->makeList( [ 'ar_title' => $condTitles ], LIST_OR ) . " OR ar_title " .
114 $dbr->buildLike( $termDb, $dbr->anyString() )
115 ];
116
117 return self::listPages( $dbr, $conds );
118 }
119
120 /**
121 * List deleted pages recorded in the archive table matching the
122 * given title prefix.
123 * Returns result wrapper with (ar_namespace, ar_title, count) fields.
124 *
125 * @param string $prefix Title prefix
126 * @return ResultWrapper
127 */
128 public static function listPagesByPrefix( $prefix ) {
129 $dbr = wfGetDB( DB_REPLICA );
130
131 $title = Title::newFromText( $prefix );
132 if ( $title ) {
133 $ns = $title->getNamespace();
134 $prefix = $title->getDBkey();
135 } else {
136 // Prolly won't work too good
137 // @todo handle bare namespace names cleanly?
138 $ns = 0;
139 }
140
141 $conds = [
142 'ar_namespace' => $ns,
143 'ar_title' . $dbr->buildLike( $prefix, $dbr->anyString() ),
144 ];
145
146 return self::listPages( $dbr, $conds );
147 }
148
149 /**
150 * @param IDatabase $dbr
151 * @param string|array $condition
152 * @return bool|ResultWrapper
153 */
154 protected static function listPages( $dbr, $condition ) {
155 return $dbr->select(
156 [ 'archive' ],
157 [
158 'ar_namespace',
159 'ar_title',
160 'count' => 'COUNT(*)'
161 ],
162 $condition,
163 __METHOD__,
164 [
165 'GROUP BY' => [ 'ar_namespace', 'ar_title' ],
166 'ORDER BY' => [ 'ar_namespace', 'ar_title' ],
167 'LIMIT' => 100,
168 ]
169 );
170 }
171
172 /**
173 * List the revisions of the given page. Returns result wrapper with
174 * various archive table fields.
175 *
176 * @return ResultWrapper
177 */
178 public function listRevisions() {
179 $dbr = wfGetDB( DB_REPLICA );
180 $commentQuery = CommentStore::newKey( 'ar_comment' )->getJoin();
181
182 $tables = [ 'archive' ] + $commentQuery['tables'];
183
184 $fields = [
185 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text',
186 'ar_len', 'ar_deleted', 'ar_rev_id', 'ar_sha1',
187 'ar_page_id'
188 ] + $commentQuery['fields'];
189
190 if ( $this->config->get( 'ContentHandlerUseDB' ) ) {
191 $fields[] = 'ar_content_format';
192 $fields[] = 'ar_content_model';
193 }
194
195 $conds = [ 'ar_namespace' => $this->title->getNamespace(),
196 'ar_title' => $this->title->getDBkey() ];
197
198 $options = [ 'ORDER BY' => 'ar_timestamp DESC' ];
199
200 $join_conds = [] + $commentQuery['joins'];
201
202 ChangeTags::modifyDisplayQuery(
203 $tables,
204 $fields,
205 $conds,
206 $join_conds,
207 $options,
208 ''
209 );
210
211 return $dbr->select( $tables,
212 $fields,
213 $conds,
214 __METHOD__,
215 $options,
216 $join_conds
217 );
218 }
219
220 /**
221 * List the deleted file revisions for this page, if it's a file page.
222 * Returns a result wrapper with various filearchive fields, or null
223 * if not a file page.
224 *
225 * @return ResultWrapper
226 * @todo Does this belong in Image for fuller encapsulation?
227 */
228 public function listFiles() {
229 if ( $this->title->getNamespace() != NS_FILE ) {
230 return null;
231 }
232
233 $dbr = wfGetDB( DB_REPLICA );
234 return $dbr->select(
235 'filearchive',
236 ArchivedFile::selectFields(),
237 [ 'fa_name' => $this->title->getDBkey() ],
238 __METHOD__,
239 [ 'ORDER BY' => 'fa_timestamp DESC' ]
240 );
241 }
242
243 /**
244 * Return a Revision object containing data for the deleted revision.
245 * Note that the result *may* or *may not* have a null page ID.
246 *
247 * @param string $timestamp
248 * @return Revision|null
249 */
250 public function getRevision( $timestamp ) {
251 $dbr = wfGetDB( DB_REPLICA );
252 $commentQuery = CommentStore::newKey( 'ar_comment' )->getJoin();
253
254 $tables = [ 'archive' ] + $commentQuery['tables'];
255
256 $fields = [
257 'ar_rev_id',
258 'ar_text',
259 'ar_user',
260 'ar_user_text',
261 'ar_timestamp',
262 'ar_minor_edit',
263 'ar_flags',
264 'ar_text_id',
265 'ar_deleted',
266 'ar_len',
267 'ar_sha1',
268 ] + $commentQuery['fields'];
269
270 if ( $this->config->get( 'ContentHandlerUseDB' ) ) {
271 $fields[] = 'ar_content_format';
272 $fields[] = 'ar_content_model';
273 }
274
275 $join_conds = [] + $commentQuery['joins'];
276
277 $row = $dbr->selectRow(
278 $tables,
279 $fields,
280 [
281 'ar_namespace' => $this->title->getNamespace(),
282 'ar_title' => $this->title->getDBkey(),
283 'ar_timestamp' => $dbr->timestamp( $timestamp )
284 ],
285 __METHOD__,
286 [],
287 $join_conds
288 );
289
290 if ( $row ) {
291 return Revision::newFromArchiveRow( $row, [ 'title' => $this->title ] );
292 }
293
294 return null;
295 }
296
297 /**
298 * Return the most-previous revision, either live or deleted, against
299 * the deleted revision given by timestamp.
300 *
301 * May produce unexpected results in case of history merges or other
302 * unusual time issues.
303 *
304 * @param string $timestamp
305 * @return Revision|null Null when there is no previous revision
306 */
307 public function getPreviousRevision( $timestamp ) {
308 $dbr = wfGetDB( DB_REPLICA );
309
310 // Check the previous deleted revision...
311 $row = $dbr->selectRow( 'archive',
312 'ar_timestamp',
313 [ 'ar_namespace' => $this->title->getNamespace(),
314 'ar_title' => $this->title->getDBkey(),
315 'ar_timestamp < ' .
316 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ],
317 __METHOD__,
318 [
319 'ORDER BY' => 'ar_timestamp DESC',
320 'LIMIT' => 1 ] );
321 $prevDeleted = $row ? wfTimestamp( TS_MW, $row->ar_timestamp ) : false;
322
323 $row = $dbr->selectRow( [ 'page', 'revision' ],
324 [ 'rev_id', 'rev_timestamp' ],
325 [
326 'page_namespace' => $this->title->getNamespace(),
327 'page_title' => $this->title->getDBkey(),
328 'page_id = rev_page',
329 'rev_timestamp < ' .
330 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ],
331 __METHOD__,
332 [
333 'ORDER BY' => 'rev_timestamp DESC',
334 'LIMIT' => 1 ] );
335 $prevLive = $row ? wfTimestamp( TS_MW, $row->rev_timestamp ) : false;
336 $prevLiveId = $row ? intval( $row->rev_id ) : null;
337
338 if ( $prevLive && $prevLive > $prevDeleted ) {
339 // Most prior revision was live
340 return Revision::newFromId( $prevLiveId );
341 } elseif ( $prevDeleted ) {
342 // Most prior revision was deleted
343 return $this->getRevision( $prevDeleted );
344 }
345
346 // No prior revision on this page.
347 return null;
348 }
349
350 /**
351 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
352 *
353 * @param object $row Database row
354 * @return string
355 */
356 public function getTextFromRow( $row ) {
357 if ( is_null( $row->ar_text_id ) ) {
358 // An old row from MediaWiki 1.4 or previous.
359 // Text is embedded in this row in classic compression format.
360 return Revision::getRevisionText( $row, 'ar_' );
361 }
362
363 // New-style: keyed to the text storage backend.
364 $dbr = wfGetDB( DB_REPLICA );
365 $text = $dbr->selectRow( 'text',
366 [ 'old_text', 'old_flags' ],
367 [ 'old_id' => $row->ar_text_id ],
368 __METHOD__ );
369
370 return Revision::getRevisionText( $text );
371 }
372
373 /**
374 * Fetch (and decompress if necessary) the stored text of the most
375 * recently edited deleted revision of the page.
376 *
377 * If there are no archived revisions for the page, returns NULL.
378 *
379 * @return string|null
380 */
381 public function getLastRevisionText() {
382 $dbr = wfGetDB( DB_REPLICA );
383 $row = $dbr->selectRow( 'archive',
384 [ 'ar_text', 'ar_flags', 'ar_text_id' ],
385 [ 'ar_namespace' => $this->title->getNamespace(),
386 'ar_title' => $this->title->getDBkey() ],
387 __METHOD__,
388 [ 'ORDER BY' => 'ar_timestamp DESC' ] );
389
390 if ( $row ) {
391 return $this->getTextFromRow( $row );
392 }
393
394 return null;
395 }
396
397 /**
398 * Quick check if any archived revisions are present for the page.
399 *
400 * @return bool
401 */
402 public function isDeleted() {
403 $dbr = wfGetDB( DB_REPLICA );
404 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
405 [ 'ar_namespace' => $this->title->getNamespace(),
406 'ar_title' => $this->title->getDBkey() ],
407 __METHOD__
408 );
409
410 return ( $n > 0 );
411 }
412
413 /**
414 * Restore the given (or all) text and file revisions for the page.
415 * Once restored, the items will be removed from the archive tables.
416 * The deletion log will be updated with an undeletion notice.
417 *
418 * This also sets Status objects, $this->fileStatus and $this->revisionStatus
419 * (depending what operations are attempted).
420 *
421 * @param array $timestamps Pass an empty array to restore all revisions,
422 * otherwise list the ones to undelete.
423 * @param string $comment
424 * @param array $fileVersions
425 * @param bool $unsuppress
426 * @param User $user User performing the action, or null to use $wgUser
427 * @param string|string[] $tags Change tags to add to log entry
428 * ($user should be able to add the specified tags before this is called)
429 * @return array|bool array(number of file revisions restored, number of image revisions
430 * restored, log message) on success, false on failure.
431 */
432 public function undelete( $timestamps, $comment = '', $fileVersions = [],
433 $unsuppress = false, User $user = null, $tags = null
434 ) {
435 // If both the set of text revisions and file revisions are empty,
436 // restore everything. Otherwise, just restore the requested items.
437 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
438
439 $restoreText = $restoreAll || !empty( $timestamps );
440 $restoreFiles = $restoreAll || !empty( $fileVersions );
441
442 if ( $restoreFiles && $this->title->getNamespace() == NS_FILE ) {
443 $img = wfLocalFile( $this->title );
444 $img->load( File::READ_LATEST );
445 $this->fileStatus = $img->restore( $fileVersions, $unsuppress );
446 if ( !$this->fileStatus->isOK() ) {
447 return false;
448 }
449 $filesRestored = $this->fileStatus->successCount;
450 } else {
451 $filesRestored = 0;
452 }
453
454 if ( $restoreText ) {
455 $this->revisionStatus = $this->undeleteRevisions( $timestamps, $unsuppress, $comment );
456 if ( !$this->revisionStatus->isOK() ) {
457 return false;
458 }
459
460 $textRestored = $this->revisionStatus->getValue();
461 } else {
462 $textRestored = 0;
463 }
464
465 // Touch the log!
466
467 if ( !$textRestored && !$filesRestored ) {
468 wfDebug( "Undelete: nothing undeleted...\n" );
469
470 return false;
471 }
472
473 if ( $user === null ) {
474 global $wgUser;
475 $user = $wgUser;
476 }
477
478 $logEntry = new ManualLogEntry( 'delete', 'restore' );
479 $logEntry->setPerformer( $user );
480 $logEntry->setTarget( $this->title );
481 $logEntry->setComment( $comment );
482 $logEntry->setTags( $tags );
483 $logEntry->setParameters( [
484 ':assoc:count' => [
485 'revisions' => $textRestored,
486 'files' => $filesRestored,
487 ],
488 ] );
489
490 Hooks::run( 'ArticleUndeleteLogEntry', [ $this, &$logEntry, $user ] );
491
492 $logid = $logEntry->insert();
493 $logEntry->publish( $logid );
494
495 return [ $textRestored, $filesRestored, $comment ];
496 }
497
498 /**
499 * This is the meaty bit -- It restores archived revisions of the given page
500 * to the revision table.
501 *
502 * @param array $timestamps Pass an empty array to restore all revisions,
503 * otherwise list the ones to undelete.
504 * @param bool $unsuppress Remove all ar_deleted/fa_deleted restrictions of seletected revs
505 * @param string $comment
506 * @throws ReadOnlyError
507 * @return Status Status object containing the number of revisions restored on success
508 */
509 private function undeleteRevisions( $timestamps, $unsuppress = false, $comment = '' ) {
510 if ( wfReadOnly() ) {
511 throw new ReadOnlyError();
512 }
513
514 $dbw = wfGetDB( DB_MASTER );
515 $dbw->startAtomic( __METHOD__ );
516
517 $restoreAll = empty( $timestamps );
518
519 # Does this page already exist? We'll have to update it...
520 $article = WikiPage::factory( $this->title );
521 # Load latest data for the current page (T33179)
522 $article->loadPageData( 'fromdbmaster' );
523 $oldcountable = $article->isCountable();
524
525 $page = $dbw->selectRow( 'page',
526 [ 'page_id', 'page_latest' ],
527 [ 'page_namespace' => $this->title->getNamespace(),
528 'page_title' => $this->title->getDBkey() ],
529 __METHOD__,
530 [ 'FOR UPDATE' ] // lock page
531 );
532
533 if ( $page ) {
534 $makepage = false;
535 # Page already exists. Import the history, and if necessary
536 # we'll update the latest revision field in the record.
537
538 # Get the time span of this page
539 $previousTimestamp = $dbw->selectField( 'revision', 'rev_timestamp',
540 [ 'rev_id' => $page->page_latest ],
541 __METHOD__ );
542
543 if ( $previousTimestamp === false ) {
544 wfDebug( __METHOD__ . ": existing page refers to a page_latest that does not exist\n" );
545
546 $status = Status::newGood( 0 );
547 $status->warning( 'undeleterevision-missing' );
548 $dbw->endAtomic( __METHOD__ );
549
550 return $status;
551 }
552 } else {
553 # Have to create a new article...
554 $makepage = true;
555 $previousTimestamp = 0;
556 }
557
558 $oldWhere = [
559 'ar_namespace' => $this->title->getNamespace(),
560 'ar_title' => $this->title->getDBkey(),
561 ];
562 if ( !$restoreAll ) {
563 $oldWhere['ar_timestamp'] = array_map( [ &$dbw, 'timestamp' ], $timestamps );
564 }
565
566 $commentQuery = CommentStore::newKey( 'ar_comment' )->getJoin();
567
568 $tables = [ 'archive', 'revision' ] + $commentQuery['tables'];
569
570 $fields = [
571 'ar_id',
572 'ar_rev_id',
573 'rev_id',
574 'ar_text',
575 'ar_user',
576 'ar_user_text',
577 'ar_timestamp',
578 'ar_minor_edit',
579 'ar_flags',
580 'ar_text_id',
581 'ar_deleted',
582 'ar_page_id',
583 'ar_len',
584 'ar_sha1'
585 ] + $commentQuery['fields'];
586
587 if ( $this->config->get( 'ContentHandlerUseDB' ) ) {
588 $fields[] = 'ar_content_format';
589 $fields[] = 'ar_content_model';
590 }
591
592 $join_conds = [
593 'revision' => [ 'LEFT JOIN', 'ar_rev_id=rev_id' ],
594 ] + $commentQuery['joins'];
595
596 /**
597 * Select each archived revision...
598 */
599 $result = $dbw->select(
600 $tables,
601 $fields,
602 $oldWhere,
603 __METHOD__,
604 /* options */
605 [ 'ORDER BY' => 'ar_timestamp' ],
606 $join_conds
607 );
608
609 $rev_count = $result->numRows();
610 if ( !$rev_count ) {
611 wfDebug( __METHOD__ . ": no revisions to restore\n" );
612
613 $status = Status::newGood( 0 );
614 $status->warning( "undelete-no-results" );
615 $dbw->endAtomic( __METHOD__ );
616
617 return $status;
618 }
619
620 // We use ar_id because there can be duplicate ar_rev_id even for the same
621 // page. In this case, we may be able to restore the first one.
622 $restoreFailedArIds = [];
623
624 // Map rev_id to the ar_id that is allowed to use it. When checking later,
625 // if it doesn't match, the current ar_id can not be restored.
626
627 // Value can be an ar_id or -1 (-1 means no ar_id can use it, since the
628 // rev_id is taken before we even start the restore).
629 $allowedRevIdToArIdMap = [];
630
631 $latestRestorableRow = null;
632
633 foreach ( $result as $row ) {
634 if ( $row->ar_rev_id ) {
635 // rev_id is taken even before we start restoring.
636 if ( $row->ar_rev_id === $row->rev_id ) {
637 $restoreFailedArIds[] = $row->ar_id;
638 $allowedRevIdToArIdMap[$row->ar_rev_id] = -1;
639 } else {
640 // rev_id is not taken yet in the DB, but it might be taken
641 // by a prior revision in the same restore operation. If
642 // not, we need to reserve it.
643 if ( isset( $allowedRevIdToArIdMap[$row->ar_rev_id] ) ) {
644 $restoreFailedArIds[] = $row->ar_id;
645 } else {
646 $allowedRevIdToArIdMap[$row->ar_rev_id] = $row->ar_id;
647 $latestRestorableRow = $row;
648 }
649 }
650 } else {
651 // If ar_rev_id is null, there can't be a collision, and a
652 // rev_id will be chosen automatically.
653 $latestRestorableRow = $row;
654 }
655 }
656
657 $result->seek( 0 ); // move back
658
659 $oldPageId = 0;
660 if ( $latestRestorableRow !== null ) {
661 $oldPageId = (int)$latestRestorableRow->ar_page_id; // pass this to ArticleUndelete hook
662
663 // grab the content to check consistency with global state before restoring the page.
664 $revision = Revision::newFromArchiveRow( $latestRestorableRow,
665 [
666 'title' => $article->getTitle(), // used to derive default content model
667 ]
668 );
669 $user = User::newFromName( $revision->getUserText( Revision::RAW ), false );
670 $content = $revision->getContent( Revision::RAW );
671
672 // NOTE: article ID may not be known yet. prepareSave() should not modify the database.
673 $status = $content->prepareSave( $article, 0, -1, $user );
674 if ( !$status->isOK() ) {
675 $dbw->endAtomic( __METHOD__ );
676
677 return $status;
678 }
679 }
680
681 $newid = false; // newly created page ID
682 $restored = 0; // number of revisions restored
683 /** @var Revision $revision */
684 $revision = null;
685 $restoredPages = [];
686 // If there are no restorable revisions, we can skip most of the steps.
687 if ( $latestRestorableRow === null ) {
688 $failedRevisionCount = $rev_count;
689 } else {
690 if ( $makepage ) {
691 // Check the state of the newest to-be version...
692 if ( !$unsuppress
693 && ( $latestRestorableRow->ar_deleted & Revision::DELETED_TEXT )
694 ) {
695 $dbw->endAtomic( __METHOD__ );
696
697 return Status::newFatal( "undeleterevdel" );
698 }
699 // Safe to insert now...
700 $newid = $article->insertOn( $dbw, $latestRestorableRow->ar_page_id );
701 if ( $newid === false ) {
702 // The old ID is reserved; let's pick another
703 $newid = $article->insertOn( $dbw );
704 }
705 $pageId = $newid;
706 } else {
707 // Check if a deleted revision will become the current revision...
708 if ( $latestRestorableRow->ar_timestamp > $previousTimestamp ) {
709 // Check the state of the newest to-be version...
710 if ( !$unsuppress
711 && ( $latestRestorableRow->ar_deleted & Revision::DELETED_TEXT )
712 ) {
713 $dbw->endAtomic( __METHOD__ );
714
715 return Status::newFatal( "undeleterevdel" );
716 }
717 }
718
719 $newid = false;
720 $pageId = $article->getId();
721 }
722
723 foreach ( $result as $row ) {
724 // Check for key dupes due to needed archive integrity.
725 if ( $row->ar_rev_id && $allowedRevIdToArIdMap[$row->ar_rev_id] !== $row->ar_id ) {
726 continue;
727 }
728 // Insert one revision at a time...maintaining deletion status
729 // unless we are specifically removing all restrictions...
730 $revision = Revision::newFromArchiveRow( $row,
731 [
732 'page' => $pageId,
733 'title' => $this->title,
734 'deleted' => $unsuppress ? 0 : $row->ar_deleted
735 ] );
736
737 // This will also copy the revision to ip_changes if it was an IP edit.
738 $revision->insertOn( $dbw );
739
740 $restored++;
741
742 Hooks::run( 'ArticleRevisionUndeleted',
743 [ &$this->title, $revision, $row->ar_page_id ] );
744 $restoredPages[$row->ar_page_id] = true;
745 }
746
747 // Now that it's safely stored, take it out of the archive
748 // Don't delete rows that we failed to restore
749 $toDeleteConds = $oldWhere;
750 $failedRevisionCount = count( $restoreFailedArIds );
751 if ( $failedRevisionCount > 0 ) {
752 $toDeleteConds[] = 'ar_id NOT IN ( ' . $dbw->makeList( $restoreFailedArIds ) . ' )';
753 }
754
755 $dbw->delete( 'archive',
756 $toDeleteConds,
757 __METHOD__ );
758 }
759
760 $status = Status::newGood( $restored );
761
762 if ( $failedRevisionCount > 0 ) {
763 $status->warning(
764 wfMessage( 'undeleterevision-duplicate-revid', $failedRevisionCount ) );
765 }
766
767 // Was anything restored at all?
768 if ( $restored ) {
769 $created = (bool)$newid;
770 // Attach the latest revision to the page...
771 $wasnew = $article->updateIfNewerOn( $dbw, $revision );
772 if ( $created || $wasnew ) {
773 // Update site stats, link tables, etc
774 $article->doEditUpdates(
775 $revision,
776 User::newFromName( $revision->getUserText( Revision::RAW ), false ),
777 [
778 'created' => $created,
779 'oldcountable' => $oldcountable,
780 'restored' => true
781 ]
782 );
783 }
784
785 Hooks::run( 'ArticleUndelete',
786 [ &$this->title, $created, $comment, $oldPageId, $restoredPages ] );
787 if ( $this->title->getNamespace() == NS_FILE ) {
788 DeferredUpdates::addUpdate( new HTMLCacheUpdate( $this->title, 'imagelinks' ) );
789 }
790 }
791
792 $dbw->endAtomic( __METHOD__ );
793
794 return $status;
795 }
796
797 /**
798 * @return Status
799 */
800 public function getFileStatus() {
801 return $this->fileStatus;
802 }
803
804 /**
805 * @return Status
806 */
807 public function getRevisionStatus() {
808 return $this->revisionStatus;
809 }
810 }