rdbms: Avoid selectDB() call in LoadMonitor new connections
[mediawiki.git] / includes / import / WikiRevision.php
blobf6becb9c927fc1d65e71ce3400eb94792ad9c74e
1 <?php
2 /**
3 * MediaWiki page data importer.
5 * Copyright © 2003,2005 Brion Vibber <brion@pobox.com>
6 * https://www.mediawiki.org/
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
23 * @file
24 * @ingroup SpecialPage
27 /**
28 * Represents a revision, log entry or upload during the import process.
29 * This class sticks closely to the structure of the XML dump.
31 * @since 1.2
33 * @ingroup SpecialPage
35 class WikiRevision {
37 /**
38 * @since 1.17
39 * @deprecated in 1.29. Unused.
40 * @note Introduced in 9b3128eb2b654761f21fd4ca1d5a1a4b796dc912, unused there, unused now.
42 public $importer = null;
44 /**
45 * @since 1.2
46 * @var Title
48 public $title = null;
50 /**
51 * @since 1.6.4
52 * @var int
54 public $id = 0;
56 /**
57 * @since 1.2
58 * @var string
60 public $timestamp = "20010115000000";
62 /**
63 * @since 1.2
64 * @var int
65 * @deprecated in 1.29. Unused.
66 * @note Introduced in 436a028086fb3f01c4605c5ad2964d56f9306aca, unused there, unused now.
68 public $user = 0;
70 /**
71 * @since 1.2
72 * @var string
74 public $user_text = "";
76 /**
77 * @since 1.27
78 * @var User
80 public $userObj = null;
82 /**
83 * @since 1.21
84 * @var string
86 public $model = null;
88 /**
89 * @since 1.21
90 * @var string
92 public $format = null;
94 /**
95 * @since 1.2
96 * @var string
98 public $text = "";
101 * @since 1.12.2
102 * @var int
104 protected $size;
107 * @since 1.21
108 * @var Content
110 public $content = null;
113 * @since 1.24
114 * @var ContentHandler
116 protected $contentHandler = null;
119 * @since 1.2.6
120 * @var string
122 public $comment = "";
125 * @since 1.5.7
126 * @var bool
128 public $minor = false;
131 * @since 1.12.2
132 * @var string
134 public $type = "";
137 * @since 1.12.2
138 * @var string
140 public $action = "";
143 * @since 1.12.2
144 * @var string
146 public $params = "";
149 * @since 1.17
150 * @var string
152 public $fileSrc = '';
155 * @since 1.17
156 * @var bool|string
158 public $sha1base36 = false;
161 * @since 1.17
162 * @var string
164 public $archiveName = '';
167 * @since 1.12.2
169 protected $filename;
172 * @since 1.12.2
173 * @var mixed
175 protected $src;
178 * @since 1.18
179 * @var bool
180 * @todo Unused?
182 public $isTemp = false;
185 * @since 1.18
186 * @deprecated 1.29 use Wikirevision::isTempSrc()
187 * First written to in 43d5d3b682cc1733ad01a837d11af4a402d57e6a
188 * Actually introduced in 52cd34acf590e5be946b7885ffdc13a157c1c6cf
190 public $fileIsTemp;
192 /** @var bool */
193 private $mNoUpdates = false;
195 /** @var Config $config */
196 private $config;
198 public function __construct( Config $config ) {
199 $this->config = $config;
203 * @since 1.7 taking a Title object (string before)
204 * @param Title $title
205 * @throws MWException
207 public function setTitle( $title ) {
208 if ( is_object( $title ) ) {
209 $this->title = $title;
210 } elseif ( is_null( $title ) ) {
211 throw new MWException( "WikiRevision given a null title in import. "
212 . "You may need to adjust \$wgLegalTitleChars." );
213 } else {
214 throw new MWException( "WikiRevision given non-object title in import." );
219 * @since 1.6.4
220 * @param int $id
222 public function setID( $id ) {
223 $this->id = $id;
227 * @since 1.2
228 * @param string $ts
230 public function setTimestamp( $ts ) {
231 # 2003-08-05T18:30:02Z
232 $this->timestamp = wfTimestamp( TS_MW, $ts );
236 * @since 1.2
237 * @param string $user
239 public function setUsername( $user ) {
240 $this->user_text = $user;
244 * @since 1.27
245 * @param User $user
247 public function setUserObj( $user ) {
248 $this->userObj = $user;
252 * @since 1.2
253 * @param string $ip
255 public function setUserIP( $ip ) {
256 $this->user_text = $ip;
260 * @since 1.21
261 * @param string $model
263 public function setModel( $model ) {
264 $this->model = $model;
268 * @since 1.21
269 * @param string $format
271 public function setFormat( $format ) {
272 $this->format = $format;
276 * @since 1.2
277 * @param string $text
279 public function setText( $text ) {
280 $this->text = $text;
284 * @since 1.2.6
285 * @param string $text
287 public function setComment( $text ) {
288 $this->comment = $text;
292 * @since 1.5.7
293 * @param bool $minor
295 public function setMinor( $minor ) {
296 $this->minor = (bool)$minor;
300 * @since 1.12.2
301 * @param mixed $src
303 public function setSrc( $src ) {
304 $this->src = $src;
308 * @since 1.17
309 * @param string $src
310 * @param bool $isTemp
312 public function setFileSrc( $src, $isTemp ) {
313 $this->fileSrc = $src;
314 $this->fileIsTemp = $isTemp;
315 $this->isTemp = $isTemp;
319 * @since 1.17
320 * @param string $sha1base36
322 public function setSha1Base36( $sha1base36 ) {
323 $this->sha1base36 = $sha1base36;
327 * @since 1.12.2
328 * @param string $filename
330 public function setFilename( $filename ) {
331 $this->filename = $filename;
335 * @since 1.17
336 * @param string $archiveName
338 public function setArchiveName( $archiveName ) {
339 $this->archiveName = $archiveName;
343 * @since 1.12.2
344 * @param int $size
346 public function setSize( $size ) {
347 $this->size = intval( $size );
351 * @since 1.12.2
352 * @param string $type
354 public function setType( $type ) {
355 $this->type = $type;
359 * @since 1.12.2
360 * @param string $action
362 public function setAction( $action ) {
363 $this->action = $action;
367 * @since 1.12.2
368 * @param array $params
370 public function setParams( $params ) {
371 $this->params = $params;
375 * @since 1.18
376 * @param bool $noupdates
378 public function setNoUpdates( $noupdates ) {
379 $this->mNoUpdates = $noupdates;
383 * @since 1.2
384 * @return Title
386 public function getTitle() {
387 return $this->title;
391 * @since 1.6.4
392 * @return int
394 public function getID() {
395 return $this->id;
399 * @since 1.2
400 * @return string
402 public function getTimestamp() {
403 return $this->timestamp;
407 * @since 1.2
408 * @return string
410 public function getUser() {
411 return $this->user_text;
415 * @since 1.27
416 * @return User
418 public function getUserObj() {
419 return $this->userObj;
423 * @since 1.2
424 * @return string
426 public function getText() {
427 return $this->text;
431 * @since 1.24
432 * @return ContentHandler
434 public function getContentHandler() {
435 if ( is_null( $this->contentHandler ) ) {
436 $this->contentHandler = ContentHandler::getForModelID( $this->getModel() );
439 return $this->contentHandler;
443 * @since 1.21
444 * @return Content
446 public function getContent() {
447 if ( is_null( $this->content ) ) {
448 $handler = $this->getContentHandler();
449 $this->content = $handler->unserializeContent( $this->text, $this->getFormat() );
452 return $this->content;
456 * @since 1.21
457 * @return string
459 public function getModel() {
460 if ( is_null( $this->model ) ) {
461 $this->model = $this->getTitle()->getContentModel();
464 return $this->model;
468 * @since 1.21
469 * @return string
471 public function getFormat() {
472 if ( is_null( $this->format ) ) {
473 $this->format = $this->getContentHandler()->getDefaultFormat();
476 return $this->format;
480 * @since 1.2.6
481 * @return string
483 public function getComment() {
484 return $this->comment;
488 * @since 1.5.7
489 * @return bool
491 public function getMinor() {
492 return $this->minor;
496 * @since 1.12.2
497 * @return mixed
499 public function getSrc() {
500 return $this->src;
504 * @since 1.17
505 * @return bool|string
507 public function getSha1() {
508 if ( $this->sha1base36 ) {
509 return Wikimedia\base_convert( $this->sha1base36, 36, 16 );
511 return false;
515 * @since 1.17
516 * @return string
518 public function getFileSrc() {
519 return $this->fileSrc;
523 * @since 1.17
524 * @return bool
526 public function isTempSrc() {
527 return $this->isTemp;
531 * @since 1.12.2
532 * @return mixed
534 public function getFilename() {
535 return $this->filename;
539 * @since 1.17
540 * @return string
542 public function getArchiveName() {
543 return $this->archiveName;
547 * @since 1.12.2
548 * @return mixed
550 public function getSize() {
551 return $this->size;
555 * @since 1.12.2
556 * @return string
558 public function getType() {
559 return $this->type;
563 * @since 1.12.2
564 * @return string
566 public function getAction() {
567 return $this->action;
571 * @since 1.12.2
572 * @return string
574 public function getParams() {
575 return $this->params;
579 * @since 1.4.1
580 * @return bool
582 public function importOldRevision() {
583 $dbw = wfGetDB( DB_MASTER );
585 # Sneak a single revision into place
586 $user = $this->getUserObj() ?: User::newFromName( $this->getUser() );
587 if ( $user ) {
588 $userId = intval( $user->getId() );
589 $userText = $user->getName();
590 } else {
591 $userId = 0;
592 $userText = $this->getUser();
593 $user = new User;
596 // avoid memory leak...?
597 Title::clearCaches();
599 $page = WikiPage::factory( $this->title );
600 $page->loadPageData( 'fromdbmaster' );
601 if ( !$page->exists() ) {
602 // must create the page...
603 $pageId = $page->insertOn( $dbw );
604 $created = true;
605 $oldcountable = null;
606 } else {
607 $pageId = $page->getId();
608 $created = false;
610 $prior = $dbw->selectField( 'revision', '1',
611 [ 'rev_page' => $pageId,
612 'rev_timestamp' => $dbw->timestamp( $this->timestamp ),
613 'rev_user_text' => $userText,
614 'rev_comment' => $this->getComment() ],
615 __METHOD__
617 if ( $prior ) {
618 // @todo FIXME: This could fail slightly for multiple matches :P
619 wfDebug( __METHOD__ . ": skipping existing revision for [[" .
620 $this->title->getPrefixedText() . "]], timestamp " . $this->timestamp . "\n" );
621 return false;
625 if ( !$pageId ) {
626 // This seems to happen if two clients simultaneously try to import the
627 // same page
628 wfDebug( __METHOD__ . ': got invalid $pageId when importing revision of [[' .
629 $this->title->getPrefixedText() . ']], timestamp ' . $this->timestamp . "\n" );
630 return false;
633 // Select previous version to make size diffs correct
634 // @todo This assumes that multiple revisions of the same page are imported
635 // in order from oldest to newest.
636 $prevId = $dbw->selectField( 'revision', 'rev_id',
638 'rev_page' => $pageId,
639 'rev_timestamp <= ' . $dbw->addQuotes( $dbw->timestamp( $this->timestamp ) ),
641 __METHOD__,
642 [ 'ORDER BY' => [
643 'rev_timestamp DESC',
644 'rev_id DESC', // timestamp is not unique per page
649 # @todo FIXME: Use original rev_id optionally (better for backups)
650 # Insert the row
651 $revision = new Revision( [
652 'title' => $this->title,
653 'page' => $pageId,
654 'content_model' => $this->getModel(),
655 'content_format' => $this->getFormat(),
656 // XXX: just set 'content' => $this->getContent()?
657 'text' => $this->getContent()->serialize( $this->getFormat() ),
658 'comment' => $this->getComment(),
659 'user' => $userId,
660 'user_text' => $userText,
661 'timestamp' => $this->timestamp,
662 'minor_edit' => $this->minor,
663 'parent_id' => $prevId,
664 ] );
665 $revision->insertOn( $dbw );
666 $changed = $page->updateIfNewerOn( $dbw, $revision );
668 if ( $changed !== false && !$this->mNoUpdates ) {
669 wfDebug( __METHOD__ . ": running updates\n" );
670 // countable/oldcountable stuff is handled in WikiImporter::finishImportPage
671 $page->doEditUpdates(
672 $revision,
673 $user,
674 [ 'created' => $created, 'oldcountable' => 'no-change' ]
678 return true;
682 * @since 1.12.2
683 * @return bool
685 public function importLogItem() {
686 $dbw = wfGetDB( DB_MASTER );
688 $user = $this->getUserObj() ?: User::newFromName( $this->getUser() );
689 if ( $user ) {
690 $userId = intval( $user->getId() );
691 $userText = $user->getName();
692 } else {
693 $userId = 0;
694 $userText = $this->getUser();
697 # @todo FIXME: This will not record autoblocks
698 if ( !$this->getTitle() ) {
699 wfDebug( __METHOD__ . ": skipping invalid {$this->type}/{$this->action} log time, timestamp " .
700 $this->timestamp . "\n" );
701 return false;
703 # Check if it exists already
704 // @todo FIXME: Use original log ID (better for backups)
705 $prior = $dbw->selectField( 'logging', '1',
706 [ 'log_type' => $this->getType(),
707 'log_action' => $this->getAction(),
708 'log_timestamp' => $dbw->timestamp( $this->timestamp ),
709 'log_namespace' => $this->getTitle()->getNamespace(),
710 'log_title' => $this->getTitle()->getDBkey(),
711 'log_comment' => $this->getComment(),
712 # 'log_user_text' => $this->user_text,
713 'log_params' => $this->params ],
714 __METHOD__
716 // @todo FIXME: This could fail slightly for multiple matches :P
717 if ( $prior ) {
718 wfDebug( __METHOD__
719 . ": skipping existing item for Log:{$this->type}/{$this->action}, timestamp "
720 . $this->timestamp . "\n" );
721 return false;
723 $log_id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
724 $data = [
725 'log_id' => $log_id,
726 'log_type' => $this->type,
727 'log_action' => $this->action,
728 'log_timestamp' => $dbw->timestamp( $this->timestamp ),
729 'log_user' => $userId,
730 'log_user_text' => $userText,
731 'log_namespace' => $this->getTitle()->getNamespace(),
732 'log_title' => $this->getTitle()->getDBkey(),
733 'log_comment' => $this->getComment(),
734 'log_params' => $this->params
736 $dbw->insert( 'logging', $data, __METHOD__ );
738 return true;
742 * @since 1.12.2
743 * @return bool
745 public function importUpload() {
746 # Construct a file
747 $archiveName = $this->getArchiveName();
748 if ( $archiveName ) {
749 wfDebug( __METHOD__ . "Importing archived file as $archiveName\n" );
750 $file = OldLocalFile::newFromArchiveName( $this->getTitle(),
751 RepoGroup::singleton()->getLocalRepo(), $archiveName );
752 } else {
753 $file = wfLocalFile( $this->getTitle() );
754 $file->load( File::READ_LATEST );
755 wfDebug( __METHOD__ . 'Importing new file as ' . $file->getName() . "\n" );
756 if ( $file->exists() && $file->getTimestamp() > $this->getTimestamp() ) {
757 $archiveName = $file->getTimestamp() . '!' . $file->getName();
758 $file = OldLocalFile::newFromArchiveName( $this->getTitle(),
759 RepoGroup::singleton()->getLocalRepo(), $archiveName );
760 wfDebug( __METHOD__ . "File already exists; importing as $archiveName\n" );
763 if ( !$file ) {
764 wfDebug( __METHOD__ . ': Bad file for ' . $this->getTitle() . "\n" );
765 return false;
768 # Get the file source or download if necessary
769 $source = $this->getFileSrc();
770 $autoDeleteSource = $this->isTempSrc();
771 if ( !strlen( $source ) ) {
772 $source = $this->downloadSource();
773 $autoDeleteSource = true;
775 if ( !strlen( $source ) ) {
776 wfDebug( __METHOD__ . ": Could not fetch remote file.\n" );
777 return false;
780 $tmpFile = new TempFSFile( $source );
781 if ( $autoDeleteSource ) {
782 $tmpFile->autocollect();
785 $sha1File = ltrim( sha1_file( $source ), '0' );
786 $sha1 = $this->getSha1();
787 if ( $sha1 && ( $sha1 !== $sha1File ) ) {
788 wfDebug( __METHOD__ . ": Corrupt file $source.\n" );
789 return false;
792 $user = $this->getUserObj() ?: User::newFromName( $this->getUser() );
794 # Do the actual upload
795 if ( $archiveName ) {
796 $status = $file->uploadOld( $source, $archiveName,
797 $this->getTimestamp(), $this->getComment(), $user );
798 } else {
799 $flags = 0;
800 $status = $file->upload( $source, $this->getComment(), $this->getComment(),
801 $flags, false, $this->getTimestamp(), $user );
804 if ( $status->isGood() ) {
805 wfDebug( __METHOD__ . ": Successful\n" );
806 return true;
807 } else {
808 wfDebug( __METHOD__ . ': failed: ' . $status->getHTML() . "\n" );
809 return false;
814 * @since 1.12.2
815 * @return bool|string
817 public function downloadSource() {
818 if ( !$this->config->get( 'EnableUploads' ) ) {
819 return false;
822 $tempo = tempnam( wfTempDir(), 'download' );
823 $f = fopen( $tempo, 'wb' );
824 if ( !$f ) {
825 wfDebug( "IMPORT: couldn't write to temp file $tempo\n" );
826 return false;
829 // @todo FIXME!
830 $src = $this->getSrc();
831 $data = Http::get( $src, [], __METHOD__ );
832 if ( !$data ) {
833 wfDebug( "IMPORT: couldn't fetch source $src\n" );
834 fclose( $f );
835 unlink( $tempo );
836 return false;
839 fwrite( $f, $data );
840 fclose( $f );
842 return $tempo;