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
35 public $importer = null;
44 public $timestamp = "20010115000000";
48 * @todo Can't find any uses. Public, because that's suspicious. Get clarity. */
52 public $user_text = "";
55 public $userObj = null;
61 public $format = null;
70 public $content = null;
72 /** @var ContentHandler */
73 protected $contentHandler = null;
79 public $minor = false;
93 /** @var bool|string */
94 public $sha1base36 = false;
100 public $isTemp = false;
103 public $archiveName = '';
114 private $mNoUpdates = false;
116 /** @var Config $config */
119 public function __construct( Config
$config ) {
120 $this->config
= $config;
124 * @param Title $title
125 * @throws MWException
127 function setTitle( $title ) {
128 if ( is_object( $title ) ) {
129 $this->title
= $title;
130 } elseif ( is_null( $title ) ) {
131 throw new MWException( "WikiRevision given a null title in import. "
132 . "You may need to adjust \$wgLegalTitleChars." );
134 throw new MWException( "WikiRevision given non-object title in import." );
141 function setID( $id ) {
148 function setTimestamp( $ts ) {
149 # 2003-08-05T18:30:02Z
150 $this->timestamp
= wfTimestamp( TS_MW
, $ts );
154 * @param string $user
156 function setUsername( $user ) {
157 $this->user_text
= $user;
163 function setUserObj( $user ) {
164 $this->userObj
= $user;
170 function setUserIP( $ip ) {
171 $this->user_text
= $ip;
175 * @param string $model
177 function setModel( $model ) {
178 $this->model
= $model;
182 * @param string $format
184 function setFormat( $format ) {
185 $this->format
= $format;
189 * @param string $text
191 function setText( $text ) {
196 * @param string $text
198 function setComment( $text ) {
199 $this->comment
= $text;
205 function setMinor( $minor ) {
206 $this->minor
= (bool)$minor;
212 function setSrc( $src ) {
218 * @param bool $isTemp
220 function setFileSrc( $src, $isTemp ) {
221 $this->fileSrc
= $src;
222 $this->fileIsTemp
= $isTemp;
226 * @param string $sha1base36
228 function setSha1Base36( $sha1base36 ) {
229 $this->sha1base36
= $sha1base36;
233 * @param string $filename
235 function setFilename( $filename ) {
236 $this->filename
= $filename;
240 * @param string $archiveName
242 function setArchiveName( $archiveName ) {
243 $this->archiveName
= $archiveName;
249 function setSize( $size ) {
250 $this->size
= intval( $size );
254 * @param string $type
256 function setType( $type ) {
261 * @param string $action
263 function setAction( $action ) {
264 $this->action
= $action;
268 * @param array $params
270 function setParams( $params ) {
271 $this->params
= $params;
275 * @param bool $noupdates
277 public function setNoUpdates( $noupdates ) {
278 $this->mNoUpdates
= $noupdates;
284 function getTitle() {
298 function getTimestamp() {
299 return $this->timestamp
;
306 return $this->user_text
;
312 function getUserObj() {
313 return $this->userObj
;
319 * @deprecated Since 1.21, use getContent() instead.
322 ContentHandler
::deprecated( __METHOD__
, '1.21' );
328 * @return ContentHandler
330 function getContentHandler() {
331 if ( is_null( $this->contentHandler
) ) {
332 $this->contentHandler
= ContentHandler
::getForModelID( $this->getModel() );
335 return $this->contentHandler
;
341 function getContent() {
342 if ( is_null( $this->content
) ) {
343 $handler = $this->getContentHandler();
344 $this->content
= $handler->unserializeContent( $this->text
, $this->getFormat() );
347 return $this->content
;
353 function getModel() {
354 if ( is_null( $this->model
) ) {
355 $this->model
= $this->getTitle()->getContentModel();
364 function getFormat() {
365 if ( is_null( $this->format
) ) {
366 $this->format
= $this->getContentHandler()->getDefaultFormat();
369 return $this->format
;
375 function getComment() {
376 return $this->comment
;
382 function getMinor() {
394 * @return bool|string
397 if ( $this->sha1base36
) {
398 return Wikimedia\base_convert
( $this->sha1base36
, 36, 16 );
406 function getFileSrc() {
407 return $this->fileSrc
;
413 function isTempSrc() {
414 return $this->isTemp
;
420 function getFilename() {
421 return $this->filename
;
427 function getArchiveName() {
428 return $this->archiveName
;
448 function getAction() {
449 return $this->action
;
455 function getParams() {
456 return $this->params
;
462 function importOldRevision() {
463 $dbw = wfGetDB( DB_MASTER
);
465 # Sneak a single revision into place
466 $user = $this->getUserObj() ?
: User
::newFromName( $this->getUser() );
468 $userId = intval( $user->getId() );
469 $userText = $user->getName();
472 $userText = $this->getUser();
476 // avoid memory leak...?
477 Title
::clearCaches();
479 $page = WikiPage
::factory( $this->title
);
480 $page->loadPageData( 'fromdbmaster' );
481 if ( !$page->exists() ) {
482 // must create the page...
483 $pageId = $page->insertOn( $dbw );
485 $oldcountable = null;
487 $pageId = $page->getId();
490 $prior = $dbw->selectField( 'revision', '1',
491 [ 'rev_page' => $pageId,
492 'rev_timestamp' => $dbw->timestamp( $this->timestamp
),
493 'rev_user_text' => $userText,
494 'rev_comment' => $this->getComment() ],
498 // @todo FIXME: This could fail slightly for multiple matches :P
499 wfDebug( __METHOD__
. ": skipping existing revision for [[" .
500 $this->title
->getPrefixedText() . "]], timestamp " . $this->timestamp
. "\n" );
506 // This seems to happen if two clients simultaneously try to import the
508 wfDebug( __METHOD__
. ': got invalid $pageId when importing revision of [[' .
509 $this->title
->getPrefixedText() . ']], timestamp ' . $this->timestamp
. "\n" );
513 // Select previous version to make size diffs correct
514 // @todo This assumes that multiple revisions of the same page are imported
515 // in order from oldest to newest.
516 $prevId = $dbw->selectField( 'revision', 'rev_id',
518 'rev_page' => $pageId,
519 'rev_timestamp <= ' . $dbw->addQuotes( $dbw->timestamp( $this->timestamp
) ),
523 'rev_timestamp DESC',
524 'rev_id DESC', // timestamp is not unique per page
529 # @todo FIXME: Use original rev_id optionally (better for backups)
531 $revision = new Revision( [
532 'title' => $this->title
,
534 'content_model' => $this->getModel(),
535 'content_format' => $this->getFormat(),
536 // XXX: just set 'content' => $this->getContent()?
537 'text' => $this->getContent()->serialize( $this->getFormat() ),
538 'comment' => $this->getComment(),
540 'user_text' => $userText,
541 'timestamp' => $this->timestamp
,
542 'minor_edit' => $this->minor
,
543 'parent_id' => $prevId,
545 $revision->insertOn( $dbw );
546 $changed = $page->updateIfNewerOn( $dbw, $revision );
548 if ( $changed !== false && !$this->mNoUpdates
) {
549 wfDebug( __METHOD__
. ": running updates\n" );
550 // countable/oldcountable stuff is handled in WikiImporter::finishImportPage
551 $page->doEditUpdates(
554 [ 'created' => $created, 'oldcountable' => 'no-change' ]
561 function importLogItem() {
562 $dbw = wfGetDB( DB_MASTER
);
564 $user = $this->getUserObj() ?
: User
::newFromName( $this->getUser() );
566 $userId = intval( $user->getId() );
567 $userText = $user->getName();
570 $userText = $this->getUser();
573 # @todo FIXME: This will not record autoblocks
574 if ( !$this->getTitle() ) {
575 wfDebug( __METHOD__
. ": skipping invalid {$this->type}/{$this->action} log time, timestamp " .
576 $this->timestamp
. "\n" );
579 # Check if it exists already
580 // @todo FIXME: Use original log ID (better for backups)
581 $prior = $dbw->selectField( 'logging', '1',
582 [ 'log_type' => $this->getType(),
583 'log_action' => $this->getAction(),
584 'log_timestamp' => $dbw->timestamp( $this->timestamp
),
585 'log_namespace' => $this->getTitle()->getNamespace(),
586 'log_title' => $this->getTitle()->getDBkey(),
587 'log_comment' => $this->getComment(),
588 # 'log_user_text' => $this->user_text,
589 'log_params' => $this->params
],
592 // @todo FIXME: This could fail slightly for multiple matches :P
595 . ": skipping existing item for Log:{$this->type}/{$this->action}, timestamp "
596 . $this->timestamp
. "\n" );
599 $log_id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
602 'log_type' => $this->type
,
603 'log_action' => $this->action
,
604 'log_timestamp' => $dbw->timestamp( $this->timestamp
),
605 'log_user' => $userId,
606 'log_user_text' => $userText,
607 'log_namespace' => $this->getTitle()->getNamespace(),
608 'log_title' => $this->getTitle()->getDBkey(),
609 'log_comment' => $this->getComment(),
610 'log_params' => $this->params
612 $dbw->insert( 'logging', $data, __METHOD__
);
618 function importUpload() {
620 $archiveName = $this->getArchiveName();
621 if ( $archiveName ) {
622 wfDebug( __METHOD__
. "Importing archived file as $archiveName\n" );
623 $file = OldLocalFile
::newFromArchiveName( $this->getTitle(),
624 RepoGroup
::singleton()->getLocalRepo(), $archiveName );
626 $file = wfLocalFile( $this->getTitle() );
627 $file->load( File
::READ_LATEST
);
628 wfDebug( __METHOD__
. 'Importing new file as ' . $file->getName() . "\n" );
629 if ( $file->exists() && $file->getTimestamp() > $this->getTimestamp() ) {
630 $archiveName = $file->getTimestamp() . '!' . $file->getName();
631 $file = OldLocalFile
::newFromArchiveName( $this->getTitle(),
632 RepoGroup
::singleton()->getLocalRepo(), $archiveName );
633 wfDebug( __METHOD__
. "File already exists; importing as $archiveName\n" );
637 wfDebug( __METHOD__
. ': Bad file for ' . $this->getTitle() . "\n" );
641 # Get the file source or download if necessary
642 $source = $this->getFileSrc();
643 $autoDeleteSource = $this->isTempSrc();
644 if ( !strlen( $source ) ) {
645 $source = $this->downloadSource();
646 $autoDeleteSource = true;
648 if ( !strlen( $source ) ) {
649 wfDebug( __METHOD__
. ": Could not fetch remote file.\n" );
653 $tmpFile = new TempFSFile( $source );
654 if ( $autoDeleteSource ) {
655 $tmpFile->autocollect();
658 $sha1File = ltrim( sha1_file( $source ), '0' );
659 $sha1 = $this->getSha1();
660 if ( $sha1 && ( $sha1 !== $sha1File ) ) {
661 wfDebug( __METHOD__
. ": Corrupt file $source.\n" );
665 $user = $this->getUserObj() ?
: User
::newFromName( $this->getUser() );
667 # Do the actual upload
668 if ( $archiveName ) {
669 $status = $file->uploadOld( $source, $archiveName,
670 $this->getTimestamp(), $this->getComment(), $user );
673 $status = $file->upload( $source, $this->getComment(), $this->getComment(),
674 $flags, false, $this->getTimestamp(), $user );
677 if ( $status->isGood() ) {
678 wfDebug( __METHOD__
. ": Successful\n" );
681 wfDebug( __METHOD__
. ': failed: ' . $status->getHTML() . "\n" );
687 * @return bool|string
689 function downloadSource() {
690 if ( !$this->config
->get( 'EnableUploads' ) ) {
694 $tempo = tempnam( wfTempDir(), 'download' );
695 $f = fopen( $tempo, 'wb' );
697 wfDebug( "IMPORT: couldn't write to temp file $tempo\n" );
702 $src = $this->getSrc();
703 $data = Http
::get( $src, [], __METHOD__
);
705 wfDebug( "IMPORT: couldn't fetch source $src\n" );