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 $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() ],
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" );
626 // This seems to happen if two clients simultaneously try to import the
628 wfDebug( __METHOD__
. ': got invalid $pageId when importing revision of [[' .
629 $this->title
->getPrefixedText() . ']], timestamp ' . $this->timestamp
. "\n" );
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
) ),
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)
651 $revision = new Revision( [
652 'title' => $this->title
,
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(),
660 'user_text' => $userText,
661 'timestamp' => $this->timestamp
,
662 'minor_edit' => $this->minor
,
663 'parent_id' => $prevId,
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(
674 [ 'created' => $created, 'oldcountable' => 'no-change' ]
685 public function importLogItem() {
686 $dbw = wfGetDB( DB_MASTER
);
688 $user = $this->getUserObj() ?
: User
::newFromName( $this->getUser() );
690 $userId = intval( $user->getId() );
691 $userText = $user->getName();
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" );
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
],
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_comment' => $this->getComment(),
734 'log_params' => $this->params
736 $dbw->insert( 'logging', $data, __METHOD__
);
745 public function importUpload() {
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 );
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" );
764 wfDebug( __METHOD__
. ': Bad file for ' . $this->getTitle() . "\n" );
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" );
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" );
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 );
800 $status = $file->upload( $source, $this->getComment(), $this->getComment(),
801 $flags, false, $this->getTimestamp(), $user );
804 if ( $status->isGood() ) {
805 wfDebug( __METHOD__
. ": Successful\n" );
808 wfDebug( __METHOD__
. ': failed: ' . $status->getHTML() . "\n" );
815 * @return bool|string
817 public function downloadSource() {
818 if ( !$this->config
->get( 'EnableUploads' ) ) {
822 $tempo = tempnam( wfTempDir(), 'download' );
823 $f = fopen( $tempo, 'wb' );
825 wfDebug( "IMPORT: couldn't write to temp file $tempo\n" );
830 $src = $this->getSrc();
831 $data = Http
::get( $src, [], __METHOD__
);
833 wfDebug( "IMPORT: couldn't fetch source $src\n" );