1f577b6cfa84cffe421920425a335f32b92430ca
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;
98 public $archiveName = '';
109 public $isTemp = false;
113 * @deprecated 1.29 use Wikirevision::isTempSrc()
114 * First written to in 43d5d3b682cc1733ad01a837d11af4a402d57e6a
115 * Actually introduced in 52cd34acf590e5be946b7885ffdc13a157c1c6cf
120 private $mNoUpdates = false;
122 /** @var Config $config */
125 public function __construct( Config
$config ) {
126 $this->config
= $config;
130 * @param Title $title
131 * @throws MWException
133 public function setTitle( $title ) {
134 if ( is_object( $title ) ) {
135 $this->title
= $title;
136 } elseif ( is_null( $title ) ) {
137 throw new MWException( "WikiRevision given a null title in import. "
138 . "You may need to adjust \$wgLegalTitleChars." );
140 throw new MWException( "WikiRevision given non-object title in import." );
147 public function setID( $id ) {
154 public function setTimestamp( $ts ) {
155 # 2003-08-05T18:30:02Z
156 $this->timestamp
= wfTimestamp( TS_MW
, $ts );
160 * @param string $user
162 public function setUsername( $user ) {
163 $this->user_text
= $user;
169 public function setUserObj( $user ) {
170 $this->userObj
= $user;
176 public function setUserIP( $ip ) {
177 $this->user_text
= $ip;
181 * @param string $model
183 public function setModel( $model ) {
184 $this->model
= $model;
188 * @param string $format
190 public function setFormat( $format ) {
191 $this->format
= $format;
195 * @param string $text
197 public function setText( $text ) {
202 * @param string $text
204 public function setComment( $text ) {
205 $this->comment
= $text;
211 public function setMinor( $minor ) {
212 $this->minor
= (bool)$minor;
218 public function setSrc( $src ) {
224 * @param bool $isTemp
226 public function setFileSrc( $src, $isTemp ) {
227 $this->fileSrc
= $src;
228 $this->fileIsTemp
= $isTemp;
229 $this->isTemp
= $isTemp;
233 * @param string $sha1base36
235 public function setSha1Base36( $sha1base36 ) {
236 $this->sha1base36
= $sha1base36;
240 * @param string $filename
242 public function setFilename( $filename ) {
243 $this->filename
= $filename;
247 * @param string $archiveName
249 public function setArchiveName( $archiveName ) {
250 $this->archiveName
= $archiveName;
256 public function setSize( $size ) {
257 $this->size
= intval( $size );
261 * @param string $type
263 public function setType( $type ) {
268 * @param string $action
270 public function setAction( $action ) {
271 $this->action
= $action;
275 * @param array $params
277 public function setParams( $params ) {
278 $this->params
= $params;
282 * @param bool $noupdates
284 public function setNoUpdates( $noupdates ) {
285 $this->mNoUpdates
= $noupdates;
291 public function getTitle() {
298 public function getID() {
305 public function getTimestamp() {
306 return $this->timestamp
;
312 public function getUser() {
313 return $this->user_text
;
319 public function getUserObj() {
320 return $this->userObj
;
326 public function getText() {
331 * @return ContentHandler
333 public function getContentHandler() {
334 if ( is_null( $this->contentHandler
) ) {
335 $this->contentHandler
= ContentHandler
::getForModelID( $this->getModel() );
338 return $this->contentHandler
;
344 public function getContent() {
345 if ( is_null( $this->content
) ) {
346 $handler = $this->getContentHandler();
347 $this->content
= $handler->unserializeContent( $this->text
, $this->getFormat() );
350 return $this->content
;
356 public function getModel() {
357 if ( is_null( $this->model
) ) {
358 $this->model
= $this->getTitle()->getContentModel();
367 public function getFormat() {
368 if ( is_null( $this->format
) ) {
369 $this->format
= $this->getContentHandler()->getDefaultFormat();
372 return $this->format
;
378 public function getComment() {
379 return $this->comment
;
385 public function getMinor() {
392 public function getSrc() {
397 * @return bool|string
399 public function getSha1() {
400 if ( $this->sha1base36
) {
401 return Wikimedia\base_convert
( $this->sha1base36
, 36, 16 );
409 public function getFileSrc() {
410 return $this->fileSrc
;
416 public function isTempSrc() {
417 return $this->isTemp
;
423 public function getFilename() {
424 return $this->filename
;
430 public function getArchiveName() {
431 return $this->archiveName
;
437 public function getSize() {
444 public function getType() {
451 public function getAction() {
452 return $this->action
;
458 public function getParams() {
459 return $this->params
;
465 public function importOldRevision() {
466 $dbw = wfGetDB( DB_MASTER
);
468 # Sneak a single revision into place
469 $user = $this->getUserObj() ?
: User
::newFromName( $this->getUser() );
471 $userId = intval( $user->getId() );
472 $userText = $user->getName();
475 $userText = $this->getUser();
479 // avoid memory leak...?
480 Title
::clearCaches();
482 $page = WikiPage
::factory( $this->title
);
483 $page->loadPageData( 'fromdbmaster' );
484 if ( !$page->exists() ) {
485 // must create the page...
486 $pageId = $page->insertOn( $dbw );
488 $oldcountable = null;
490 $pageId = $page->getId();
493 $prior = $dbw->selectField( 'revision', '1',
494 [ 'rev_page' => $pageId,
495 'rev_timestamp' => $dbw->timestamp( $this->timestamp
),
496 'rev_user_text' => $userText,
497 'rev_comment' => $this->getComment() ],
501 // @todo FIXME: This could fail slightly for multiple matches :P
502 wfDebug( __METHOD__
. ": skipping existing revision for [[" .
503 $this->title
->getPrefixedText() . "]], timestamp " . $this->timestamp
. "\n" );
509 // This seems to happen if two clients simultaneously try to import the
511 wfDebug( __METHOD__
. ': got invalid $pageId when importing revision of [[' .
512 $this->title
->getPrefixedText() . ']], timestamp ' . $this->timestamp
. "\n" );
516 // Select previous version to make size diffs correct
517 // @todo This assumes that multiple revisions of the same page are imported
518 // in order from oldest to newest.
519 $prevId = $dbw->selectField( 'revision', 'rev_id',
521 'rev_page' => $pageId,
522 'rev_timestamp <= ' . $dbw->addQuotes( $dbw->timestamp( $this->timestamp
) ),
526 'rev_timestamp DESC',
527 'rev_id DESC', // timestamp is not unique per page
532 # @todo FIXME: Use original rev_id optionally (better for backups)
534 $revision = new Revision( [
535 'title' => $this->title
,
537 'content_model' => $this->getModel(),
538 'content_format' => $this->getFormat(),
539 // XXX: just set 'content' => $this->getContent()?
540 'text' => $this->getContent()->serialize( $this->getFormat() ),
541 'comment' => $this->getComment(),
543 'user_text' => $userText,
544 'timestamp' => $this->timestamp
,
545 'minor_edit' => $this->minor
,
546 'parent_id' => $prevId,
548 $revision->insertOn( $dbw );
549 $changed = $page->updateIfNewerOn( $dbw, $revision );
551 if ( $changed !== false && !$this->mNoUpdates
) {
552 wfDebug( __METHOD__
. ": running updates\n" );
553 // countable/oldcountable stuff is handled in WikiImporter::finishImportPage
554 $page->doEditUpdates(
557 [ 'created' => $created, 'oldcountable' => 'no-change' ]
564 public function importLogItem() {
565 $dbw = wfGetDB( DB_MASTER
);
567 $user = $this->getUserObj() ?
: User
::newFromName( $this->getUser() );
569 $userId = intval( $user->getId() );
570 $userText = $user->getName();
573 $userText = $this->getUser();
576 # @todo FIXME: This will not record autoblocks
577 if ( !$this->getTitle() ) {
578 wfDebug( __METHOD__
. ": skipping invalid {$this->type}/{$this->action} log time, timestamp " .
579 $this->timestamp
. "\n" );
582 # Check if it exists already
583 // @todo FIXME: Use original log ID (better for backups)
584 $prior = $dbw->selectField( 'logging', '1',
585 [ 'log_type' => $this->getType(),
586 'log_action' => $this->getAction(),
587 'log_timestamp' => $dbw->timestamp( $this->timestamp
),
588 'log_namespace' => $this->getTitle()->getNamespace(),
589 'log_title' => $this->getTitle()->getDBkey(),
590 'log_comment' => $this->getComment(),
591 # 'log_user_text' => $this->user_text,
592 'log_params' => $this->params
],
595 // @todo FIXME: This could fail slightly for multiple matches :P
598 . ": skipping existing item for Log:{$this->type}/{$this->action}, timestamp "
599 . $this->timestamp
. "\n" );
602 $log_id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
605 'log_type' => $this->type
,
606 'log_action' => $this->action
,
607 'log_timestamp' => $dbw->timestamp( $this->timestamp
),
608 'log_user' => $userId,
609 'log_user_text' => $userText,
610 'log_namespace' => $this->getTitle()->getNamespace(),
611 'log_title' => $this->getTitle()->getDBkey(),
612 'log_comment' => $this->getComment(),
613 'log_params' => $this->params
615 $dbw->insert( 'logging', $data, __METHOD__
);
623 public function importUpload() {
625 $archiveName = $this->getArchiveName();
626 if ( $archiveName ) {
627 wfDebug( __METHOD__
. "Importing archived file as $archiveName\n" );
628 $file = OldLocalFile
::newFromArchiveName( $this->getTitle(),
629 RepoGroup
::singleton()->getLocalRepo(), $archiveName );
631 $file = wfLocalFile( $this->getTitle() );
632 $file->load( File
::READ_LATEST
);
633 wfDebug( __METHOD__
. 'Importing new file as ' . $file->getName() . "\n" );
634 if ( $file->exists() && $file->getTimestamp() > $this->getTimestamp() ) {
635 $archiveName = $file->getTimestamp() . '!' . $file->getName();
636 $file = OldLocalFile
::newFromArchiveName( $this->getTitle(),
637 RepoGroup
::singleton()->getLocalRepo(), $archiveName );
638 wfDebug( __METHOD__
. "File already exists; importing as $archiveName\n" );
642 wfDebug( __METHOD__
. ': Bad file for ' . $this->getTitle() . "\n" );
646 # Get the file source or download if necessary
647 $source = $this->getFileSrc();
648 $autoDeleteSource = $this->isTempSrc();
649 if ( !strlen( $source ) ) {
650 $source = $this->downloadSource();
651 $autoDeleteSource = true;
653 if ( !strlen( $source ) ) {
654 wfDebug( __METHOD__
. ": Could not fetch remote file.\n" );
658 $tmpFile = new TempFSFile( $source );
659 if ( $autoDeleteSource ) {
660 $tmpFile->autocollect();
663 $sha1File = ltrim( sha1_file( $source ), '0' );
664 $sha1 = $this->getSha1();
665 if ( $sha1 && ( $sha1 !== $sha1File ) ) {
666 wfDebug( __METHOD__
. ": Corrupt file $source.\n" );
670 $user = $this->getUserObj() ?
: User
::newFromName( $this->getUser() );
672 # Do the actual upload
673 if ( $archiveName ) {
674 $status = $file->uploadOld( $source, $archiveName,
675 $this->getTimestamp(), $this->getComment(), $user );
678 $status = $file->upload( $source, $this->getComment(), $this->getComment(),
679 $flags, false, $this->getTimestamp(), $user );
682 if ( $status->isGood() ) {
683 wfDebug( __METHOD__
. ": Successful\n" );
686 wfDebug( __METHOD__
. ': failed: ' . $status->getHTML() . "\n" );
692 * @return bool|string
694 public function downloadSource() {
695 if ( !$this->config
->get( 'EnableUploads' ) ) {
699 $tempo = tempnam( wfTempDir(), 'download' );
700 $f = fopen( $tempo, 'wb' );
702 wfDebug( "IMPORT: couldn't write to temp file $tempo\n" );
707 $src = $this->getSrc();
708 $data = Http
::get( $src, [], __METHOD__
);
710 wfDebug( "IMPORT: couldn't fetch source $src\n" );