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.
31 * @ingroup SpecialPage
37 * @deprecated in 1.29. Unused.
38 * @note Introduced in 9b3128eb2b654761f21fd4ca1d5a1a4b796dc912, unused there, unused now.
40 public $importer = null;
49 public $timestamp = "20010115000000";
53 * @deprecated in 1.29. Unused.
54 * @note Introduced in 436a028086fb3f01c4605c5ad2964d56f9306aca, unused there, unused now.
59 public $user_text = "";
62 public $userObj = null;
68 public $format = null;
77 public $content = null;
79 /** @var ContentHandler */
80 protected $contentHandler = null;
86 public $minor = false;
100 /** @var bool|string */
101 public $sha1base36 = false;
105 public $archiveName = '';
116 public $isTemp = false;
120 * @deprecated 1.29 use Wikirevision::isTempSrc()
121 * First written to in 43d5d3b682cc1733ad01a837d11af4a402d57e6a
122 * Actually introduced in 52cd34acf590e5be946b7885ffdc13a157c1c6cf
127 private $mNoUpdates = false;
129 /** @var Config $config */
132 public function __construct( Config
$config ) {
133 $this->config
= $config;
137 * @param Title $title
138 * @throws MWException
140 public function setTitle( $title ) {
141 if ( is_object( $title ) ) {
142 $this->title
= $title;
143 } elseif ( is_null( $title ) ) {
144 throw new MWException( "WikiRevision given a null title in import. "
145 . "You may need to adjust \$wgLegalTitleChars." );
147 throw new MWException( "WikiRevision given non-object title in import." );
154 public function setID( $id ) {
161 public function setTimestamp( $ts ) {
162 # 2003-08-05T18:30:02Z
163 $this->timestamp
= wfTimestamp( TS_MW
, $ts );
167 * @param string $user
169 public function setUsername( $user ) {
170 $this->user_text
= $user;
176 public function setUserObj( $user ) {
177 $this->userObj
= $user;
183 public function setUserIP( $ip ) {
184 $this->user_text
= $ip;
188 * @param string $model
190 public function setModel( $model ) {
191 $this->model
= $model;
195 * @param string $format
197 public function setFormat( $format ) {
198 $this->format
= $format;
202 * @param string $text
204 public function setText( $text ) {
209 * @param string $text
211 public function setComment( $text ) {
212 $this->comment
= $text;
218 public function setMinor( $minor ) {
219 $this->minor
= (bool)$minor;
225 public function setSrc( $src ) {
231 * @param bool $isTemp
233 public function setFileSrc( $src, $isTemp ) {
234 $this->fileSrc
= $src;
235 $this->fileIsTemp
= $isTemp;
236 $this->isTemp
= $isTemp;
240 * @param string $sha1base36
242 public function setSha1Base36( $sha1base36 ) {
243 $this->sha1base36
= $sha1base36;
247 * @param string $filename
249 public function setFilename( $filename ) {
250 $this->filename
= $filename;
254 * @param string $archiveName
256 public function setArchiveName( $archiveName ) {
257 $this->archiveName
= $archiveName;
263 public function setSize( $size ) {
264 $this->size
= intval( $size );
268 * @param string $type
270 public function setType( $type ) {
275 * @param string $action
277 public function setAction( $action ) {
278 $this->action
= $action;
282 * @param array $params
284 public function setParams( $params ) {
285 $this->params
= $params;
289 * @param bool $noupdates
291 public function setNoUpdates( $noupdates ) {
292 $this->mNoUpdates
= $noupdates;
298 public function getTitle() {
305 public function getID() {
312 public function getTimestamp() {
313 return $this->timestamp
;
319 public function getUser() {
320 return $this->user_text
;
326 public function getUserObj() {
327 return $this->userObj
;
333 public function getText() {
338 * @return ContentHandler
340 public function getContentHandler() {
341 if ( is_null( $this->contentHandler
) ) {
342 $this->contentHandler
= ContentHandler
::getForModelID( $this->getModel() );
345 return $this->contentHandler
;
351 public function getContent() {
352 if ( is_null( $this->content
) ) {
353 $handler = $this->getContentHandler();
354 $this->content
= $handler->unserializeContent( $this->text
, $this->getFormat() );
357 return $this->content
;
363 public function getModel() {
364 if ( is_null( $this->model
) ) {
365 $this->model
= $this->getTitle()->getContentModel();
374 public function getFormat() {
375 if ( is_null( $this->format
) ) {
376 $this->format
= $this->getContentHandler()->getDefaultFormat();
379 return $this->format
;
385 public function getComment() {
386 return $this->comment
;
392 public function getMinor() {
399 public function getSrc() {
404 * @return bool|string
406 public function getSha1() {
407 if ( $this->sha1base36
) {
408 return Wikimedia\base_convert
( $this->sha1base36
, 36, 16 );
416 public function getFileSrc() {
417 return $this->fileSrc
;
423 public function isTempSrc() {
424 return $this->isTemp
;
430 public function getFilename() {
431 return $this->filename
;
437 public function getArchiveName() {
438 return $this->archiveName
;
444 public function getSize() {
451 public function getType() {
458 public function getAction() {
459 return $this->action
;
465 public function getParams() {
466 return $this->params
;
472 public function importOldRevision() {
473 $dbw = wfGetDB( DB_MASTER
);
475 # Sneak a single revision into place
476 $user = $this->getUserObj() ?
: User
::newFromName( $this->getUser() );
478 $userId = intval( $user->getId() );
479 $userText = $user->getName();
482 $userText = $this->getUser();
486 // avoid memory leak...?
487 Title
::clearCaches();
489 $page = WikiPage
::factory( $this->title
);
490 $page->loadPageData( 'fromdbmaster' );
491 if ( !$page->exists() ) {
492 // must create the page...
493 $pageId = $page->insertOn( $dbw );
495 $oldcountable = null;
497 $pageId = $page->getId();
500 $prior = $dbw->selectField( 'revision', '1',
501 [ 'rev_page' => $pageId,
502 'rev_timestamp' => $dbw->timestamp( $this->timestamp
),
503 'rev_user_text' => $userText,
504 'rev_comment' => $this->getComment() ],
508 // @todo FIXME: This could fail slightly for multiple matches :P
509 wfDebug( __METHOD__
. ": skipping existing revision for [[" .
510 $this->title
->getPrefixedText() . "]], timestamp " . $this->timestamp
. "\n" );
516 // This seems to happen if two clients simultaneously try to import the
518 wfDebug( __METHOD__
. ': got invalid $pageId when importing revision of [[' .
519 $this->title
->getPrefixedText() . ']], timestamp ' . $this->timestamp
. "\n" );
523 // Select previous version to make size diffs correct
524 // @todo This assumes that multiple revisions of the same page are imported
525 // in order from oldest to newest.
526 $prevId = $dbw->selectField( 'revision', 'rev_id',
528 'rev_page' => $pageId,
529 'rev_timestamp <= ' . $dbw->addQuotes( $dbw->timestamp( $this->timestamp
) ),
533 'rev_timestamp DESC',
534 'rev_id DESC', // timestamp is not unique per page
539 # @todo FIXME: Use original rev_id optionally (better for backups)
541 $revision = new Revision( [
542 'title' => $this->title
,
544 'content_model' => $this->getModel(),
545 'content_format' => $this->getFormat(),
546 // XXX: just set 'content' => $this->getContent()?
547 'text' => $this->getContent()->serialize( $this->getFormat() ),
548 'comment' => $this->getComment(),
550 'user_text' => $userText,
551 'timestamp' => $this->timestamp
,
552 'minor_edit' => $this->minor
,
553 'parent_id' => $prevId,
555 $revision->insertOn( $dbw );
556 $changed = $page->updateIfNewerOn( $dbw, $revision );
558 if ( $changed !== false && !$this->mNoUpdates
) {
559 wfDebug( __METHOD__
. ": running updates\n" );
560 // countable/oldcountable stuff is handled in WikiImporter::finishImportPage
561 $page->doEditUpdates(
564 [ 'created' => $created, 'oldcountable' => 'no-change' ]
571 public function importLogItem() {
572 $dbw = wfGetDB( DB_MASTER
);
574 $user = $this->getUserObj() ?
: User
::newFromName( $this->getUser() );
576 $userId = intval( $user->getId() );
577 $userText = $user->getName();
580 $userText = $this->getUser();
583 # @todo FIXME: This will not record autoblocks
584 if ( !$this->getTitle() ) {
585 wfDebug( __METHOD__
. ": skipping invalid {$this->type}/{$this->action} log time, timestamp " .
586 $this->timestamp
. "\n" );
589 # Check if it exists already
590 // @todo FIXME: Use original log ID (better for backups)
591 $prior = $dbw->selectField( 'logging', '1',
592 [ 'log_type' => $this->getType(),
593 'log_action' => $this->getAction(),
594 'log_timestamp' => $dbw->timestamp( $this->timestamp
),
595 'log_namespace' => $this->getTitle()->getNamespace(),
596 'log_title' => $this->getTitle()->getDBkey(),
597 'log_comment' => $this->getComment(),
598 # 'log_user_text' => $this->user_text,
599 'log_params' => $this->params
],
602 // @todo FIXME: This could fail slightly for multiple matches :P
605 . ": skipping existing item for Log:{$this->type}/{$this->action}, timestamp "
606 . $this->timestamp
. "\n" );
609 $log_id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
612 'log_type' => $this->type
,
613 'log_action' => $this->action
,
614 'log_timestamp' => $dbw->timestamp( $this->timestamp
),
615 'log_user' => $userId,
616 'log_user_text' => $userText,
617 'log_namespace' => $this->getTitle()->getNamespace(),
618 'log_title' => $this->getTitle()->getDBkey(),
619 'log_comment' => $this->getComment(),
620 'log_params' => $this->params
622 $dbw->insert( 'logging', $data, __METHOD__
);
630 public function importUpload() {
632 $archiveName = $this->getArchiveName();
633 if ( $archiveName ) {
634 wfDebug( __METHOD__
. "Importing archived file as $archiveName\n" );
635 $file = OldLocalFile
::newFromArchiveName( $this->getTitle(),
636 RepoGroup
::singleton()->getLocalRepo(), $archiveName );
638 $file = wfLocalFile( $this->getTitle() );
639 $file->load( File
::READ_LATEST
);
640 wfDebug( __METHOD__
. 'Importing new file as ' . $file->getName() . "\n" );
641 if ( $file->exists() && $file->getTimestamp() > $this->getTimestamp() ) {
642 $archiveName = $file->getTimestamp() . '!' . $file->getName();
643 $file = OldLocalFile
::newFromArchiveName( $this->getTitle(),
644 RepoGroup
::singleton()->getLocalRepo(), $archiveName );
645 wfDebug( __METHOD__
. "File already exists; importing as $archiveName\n" );
649 wfDebug( __METHOD__
. ': Bad file for ' . $this->getTitle() . "\n" );
653 # Get the file source or download if necessary
654 $source = $this->getFileSrc();
655 $autoDeleteSource = $this->isTempSrc();
656 if ( !strlen( $source ) ) {
657 $source = $this->downloadSource();
658 $autoDeleteSource = true;
660 if ( !strlen( $source ) ) {
661 wfDebug( __METHOD__
. ": Could not fetch remote file.\n" );
665 $tmpFile = new TempFSFile( $source );
666 if ( $autoDeleteSource ) {
667 $tmpFile->autocollect();
670 $sha1File = ltrim( sha1_file( $source ), '0' );
671 $sha1 = $this->getSha1();
672 if ( $sha1 && ( $sha1 !== $sha1File ) ) {
673 wfDebug( __METHOD__
. ": Corrupt file $source.\n" );
677 $user = $this->getUserObj() ?
: User
::newFromName( $this->getUser() );
679 # Do the actual upload
680 if ( $archiveName ) {
681 $status = $file->uploadOld( $source, $archiveName,
682 $this->getTimestamp(), $this->getComment(), $user );
685 $status = $file->upload( $source, $this->getComment(), $this->getComment(),
686 $flags, false, $this->getTimestamp(), $user );
689 if ( $status->isGood() ) {
690 wfDebug( __METHOD__
. ": Successful\n" );
693 wfDebug( __METHOD__
. ': failed: ' . $status->getHTML() . "\n" );
699 * @return bool|string
701 public function downloadSource() {
702 if ( !$this->config
->get( 'EnableUploads' ) ) {
706 $tempo = tempnam( wfTempDir(), 'download' );
707 $f = fopen( $tempo, 'wb' );
709 wfDebug( "IMPORT: couldn't write to temp file $tempo\n" );
714 $src = $this->getSrc();
715 $data = Http
::get( $src, [], __METHOD__
);
717 wfDebug( "IMPORT: couldn't fetch source $src\n" );