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
24 * @ingroup SpecialPage
28 * Represents a revision, log entry or upload during the import process.
29 * This class sticks closely to the structure of the XML dump.
33 * @ingroup SpecialPage
39 * @deprecated in 1.29. Unused.
40 * @note Introduced in 9b3128eb2b654761f21fd4ca1d5a1a4b796dc912, unused there, unused now.
42 public $importer = null;
60 public $timestamp = "20010115000000";
65 * @deprecated in 1.29. Unused.
66 * @note Introduced in 436a028086fb3f01c4605c5ad2964d56f9306aca, unused there, unused now.
74 public $user_text = "";
80 public $userObj = null;
92 public $format = null;
110 public $content = null;
114 * @var ContentHandler
116 protected $contentHandler = null;
122 public $comment = "";
128 public $minor = false;
152 public $fileSrc = '';
158 public $sha1base36 = false;
164 public $archiveName = '';
182 public $isTemp = false;
186 * @deprecated 1.29 use Wikirevision::isTempSrc()
187 * First written to in 43d5d3b682cc1733ad01a837d11af4a402d57e6a
188 * Actually introduced in 52cd34acf590e5be946b7885ffdc13a157c1c6cf
193 private $mNoUpdates = false;
195 /** @var Config $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." );
214 throw new MWException( "WikiRevision given non-object title in import." );
222 public function setID( $id ) {
230 public function setTimestamp( $ts ) {
231 # 2003-08-05T18:30:02Z
232 $this->timestamp
= wfTimestamp( TS_MW
, $ts );
237 * @param string $user
239 public function setUsername( $user ) {
240 $this->user_text
= $user;
247 public function setUserObj( $user ) {
248 $this->userObj
= $user;
255 public function setUserIP( $ip ) {
256 $this->user_text
= $ip;
261 * @param string $model
263 public function setModel( $model ) {
264 $this->model
= $model;
269 * @param string $format
271 public function setFormat( $format ) {
272 $this->format
= $format;
277 * @param string $text
279 public function setText( $text ) {
285 * @param string $text
287 public function setComment( $text ) {
288 $this->comment
= $text;
295 public function setMinor( $minor ) {
296 $this->minor
= (bool)$minor;
303 public function setSrc( $src ) {
310 * @param bool $isTemp
312 public function setFileSrc( $src, $isTemp ) {
313 $this->fileSrc
= $src;
314 $this->fileIsTemp
= $isTemp;
315 $this->isTemp
= $isTemp;
320 * @param string $sha1base36
322 public function setSha1Base36( $sha1base36 ) {
323 $this->sha1base36
= $sha1base36;
328 * @param string $filename
330 public function setFilename( $filename ) {
331 $this->filename
= $filename;
336 * @param string $archiveName
338 public function setArchiveName( $archiveName ) {
339 $this->archiveName
= $archiveName;
346 public function setSize( $size ) {
347 $this->size
= intval( $size );
352 * @param string $type
354 public function setType( $type ) {
360 * @param string $action
362 public function setAction( $action ) {
363 $this->action
= $action;
368 * @param array $params
370 public function setParams( $params ) {
371 $this->params
= $params;
376 * @param bool $noupdates
378 public function setNoUpdates( $noupdates ) {
379 $this->mNoUpdates
= $noupdates;
386 public function getTitle() {
394 public function getID() {
402 public function getTimestamp() {
403 return $this->timestamp
;
410 public function getUser() {
411 return $this->user_text
;
418 public function getUserObj() {
419 return $this->userObj
;
426 public function getText() {
432 * @return ContentHandler
434 public function getContentHandler() {
435 if ( is_null( $this->contentHandler
) ) {
436 $this->contentHandler
= ContentHandler
::getForModelID( $this->getModel() );
439 return $this->contentHandler
;
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
;
459 public function getModel() {
460 if ( is_null( $this->model
) ) {
461 $this->model
= $this->getTitle()->getContentModel();
471 public function getFormat() {
472 if ( is_null( $this->format
) ) {
473 $this->format
= $this->getContentHandler()->getDefaultFormat();
476 return $this->format
;
483 public function getComment() {
484 return $this->comment
;
491 public function getMinor() {
499 public function getSrc() {
505 * @return bool|string
507 public function getSha1() {
508 if ( $this->sha1base36
) {
509 return Wikimedia\base_convert
( $this->sha1base36
, 36, 16 );
518 public function getFileSrc() {
519 return $this->fileSrc
;
526 public function isTempSrc() {
527 return $this->isTemp
;
534 public function getFilename() {
535 return $this->filename
;
542 public function getArchiveName() {
543 return $this->archiveName
;
550 public function getSize() {
558 public function getType() {
566 public function getAction() {
567 return $this->action
;
574 public function getParams() {
575 return $this->params
;
582 public function importOldRevision() {
583 $dbw = wfGetDB( DB_MASTER
);
585 # Sneak a single revision into place
586 $user = $this->getUserObj() ?
: User
::newFromName( $this->getUser() );
588 $userId = intval( $user->getId() );
589 $userText = $user->getName();
592 $userText = $this->getUser();
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 );
605 $oldcountable = null;
607 $pageId = $page->getId();
610 // Note: sha1 has been in XML dumps since 2012. If you have an
611 // older dump, the duplicate detection here won't work.
612 $prior = $dbw->selectField( 'revision', '1',
613 [ 'rev_page' => $pageId,
614 'rev_timestamp' => $dbw->timestamp( $this->timestamp
),
615 'rev_sha1' => $this->sha1base36
],
619 // @todo FIXME: This could fail slightly for multiple matches :P
620 wfDebug( __METHOD__
. ": skipping existing revision for [[" .
621 $this->title
->getPrefixedText() . "]], timestamp " . $this->timestamp
. "\n" );
627 // This seems to happen if two clients simultaneously try to import the
629 wfDebug( __METHOD__
. ': got invalid $pageId when importing revision of [[' .
630 $this->title
->getPrefixedText() . ']], timestamp ' . $this->timestamp
. "\n" );
634 // Select previous version to make size diffs correct
635 // @todo This assumes that multiple revisions of the same page are imported
636 // in order from oldest to newest.
637 $prevId = $dbw->selectField( 'revision', 'rev_id',
639 'rev_page' => $pageId,
640 'rev_timestamp <= ' . $dbw->addQuotes( $dbw->timestamp( $this->timestamp
) ),
644 'rev_timestamp DESC',
645 'rev_id DESC', // timestamp is not unique per page
650 # @todo FIXME: Use original rev_id optionally (better for backups)
652 $revision = new Revision( [
653 'title' => $this->title
,
655 'content_model' => $this->getModel(),
656 'content_format' => $this->getFormat(),
657 // XXX: just set 'content' => $this->getContent()?
658 'text' => $this->getContent()->serialize( $this->getFormat() ),
659 'comment' => $this->getComment(),
661 'user_text' => $userText,
662 'timestamp' => $this->timestamp
,
663 'minor_edit' => $this->minor
,
664 'parent_id' => $prevId,
666 $revision->insertOn( $dbw );
667 $changed = $page->updateIfNewerOn( $dbw, $revision );
669 if ( $changed !== false && !$this->mNoUpdates
) {
670 wfDebug( __METHOD__
. ": running updates\n" );
671 // countable/oldcountable stuff is handled in WikiImporter::finishImportPage
672 $page->doEditUpdates(
675 [ 'created' => $created, 'oldcountable' => 'no-change' ]
686 public function importLogItem() {
687 $dbw = wfGetDB( DB_MASTER
);
689 $user = $this->getUserObj() ?
: User
::newFromName( $this->getUser() );
691 $userId = intval( $user->getId() );
692 $userText = $user->getName();
695 $userText = $this->getUser();
698 # @todo FIXME: This will not record autoblocks
699 if ( !$this->getTitle() ) {
700 wfDebug( __METHOD__
. ": skipping invalid {$this->type}/{$this->action} log time, timestamp " .
701 $this->timestamp
. "\n" );
704 # Check if it exists already
705 // @todo FIXME: Use original log ID (better for backups)
706 $prior = $dbw->selectField( 'logging', '1',
707 [ 'log_type' => $this->getType(),
708 'log_action' => $this->getAction(),
709 'log_timestamp' => $dbw->timestamp( $this->timestamp
),
710 'log_namespace' => $this->getTitle()->getNamespace(),
711 'log_title' => $this->getTitle()->getDBkey(),
712 # 'log_user_text' => $this->user_text,
713 'log_params' => $this->params
],
716 // @todo FIXME: This could fail slightly for multiple matches :P
719 . ": skipping existing item for Log:{$this->type}/{$this->action}, timestamp "
720 . $this->timestamp
. "\n" );
723 $log_id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
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_params' => $this->params
734 ] + CommentStore
::newKey( 'log_comment' )->insert( $dbw, $this->getComment() );
735 $dbw->insert( 'logging', $data, __METHOD__
);
744 public function importUpload() {
746 $archiveName = $this->getArchiveName();
747 if ( $archiveName ) {
748 wfDebug( __METHOD__
. "Importing archived file as $archiveName\n" );
749 $file = OldLocalFile
::newFromArchiveName( $this->getTitle(),
750 RepoGroup
::singleton()->getLocalRepo(), $archiveName );
752 $file = wfLocalFile( $this->getTitle() );
753 $file->load( File
::READ_LATEST
);
754 wfDebug( __METHOD__
. 'Importing new file as ' . $file->getName() . "\n" );
755 if ( $file->exists() && $file->getTimestamp() > $this->getTimestamp() ) {
756 $archiveName = $file->getTimestamp() . '!' . $file->getName();
757 $file = OldLocalFile
::newFromArchiveName( $this->getTitle(),
758 RepoGroup
::singleton()->getLocalRepo(), $archiveName );
759 wfDebug( __METHOD__
. "File already exists; importing as $archiveName\n" );
763 wfDebug( __METHOD__
. ': Bad file for ' . $this->getTitle() . "\n" );
767 # Get the file source or download if necessary
768 $source = $this->getFileSrc();
769 $autoDeleteSource = $this->isTempSrc();
770 if ( !strlen( $source ) ) {
771 $source = $this->downloadSource();
772 $autoDeleteSource = true;
774 if ( !strlen( $source ) ) {
775 wfDebug( __METHOD__
. ": Could not fetch remote file.\n" );
779 $tmpFile = new TempFSFile( $source );
780 if ( $autoDeleteSource ) {
781 $tmpFile->autocollect();
784 $sha1File = ltrim( sha1_file( $source ), '0' );
785 $sha1 = $this->getSha1();
786 if ( $sha1 && ( $sha1 !== $sha1File ) ) {
787 wfDebug( __METHOD__
. ": Corrupt file $source.\n" );
791 $user = $this->getUserObj() ?
: User
::newFromName( $this->getUser() );
793 # Do the actual upload
794 if ( $archiveName ) {
795 $status = $file->uploadOld( $source, $archiveName,
796 $this->getTimestamp(), $this->getComment(), $user );
799 $status = $file->upload( $source, $this->getComment(), $this->getComment(),
800 $flags, false, $this->getTimestamp(), $user );
803 if ( $status->isGood() ) {
804 wfDebug( __METHOD__
. ": Successful\n" );
807 wfDebug( __METHOD__
. ': failed: ' . $status->getHTML() . "\n" );
814 * @return bool|string
816 public function downloadSource() {
817 if ( !$this->config
->get( 'EnableUploads' ) ) {
821 $tempo = tempnam( wfTempDir(), 'download' );
822 $f = fopen( $tempo, 'wb' );
824 wfDebug( "IMPORT: couldn't write to temp file $tempo\n" );
829 $src = $this->getSrc();
830 $data = Http
::get( $src, [], __METHOD__
);
832 wfDebug( "IMPORT: couldn't fetch source $src\n" );