From cdeba4cfc7c697866d3ddeb02a3aa56e6edf5aba Mon Sep 17 00:00:00 2001 From: Krzysztof Zbudniewek Date: Mon, 28 Dec 2015 23:27:48 +0100 Subject: [PATCH] Split classes in Import.php into separate files Bug: T122532 Change-Id: Ic4463ab8d3a7b2779f43efb92cb790dbc1d88064 --- autoload.php | 12 +- includes/import/ImportSource.php | 51 + includes/import/ImportStreamSource.php | 172 +++ includes/import/ImportStringSource.php | 57 + includes/import/UploadSourceAdapter.php | 149 +++ .../{Import.php => import/WikiImporter.php} | 986 +----------------- includes/import/WikiRevision.php | 677 ++++++++++++ 7 files changed, 1113 insertions(+), 991 deletions(-) create mode 100644 includes/import/ImportSource.php create mode 100644 includes/import/ImportStreamSource.php create mode 100644 includes/import/ImportStringSource.php create mode 100644 includes/import/UploadSourceAdapter.php rename includes/{Import.php => import/WikiImporter.php} (59%) create mode 100644 includes/import/WikiRevision.php diff --git a/autoload.php b/autoload.php index 8c5ec81731..75466f52ae 100644 --- a/autoload.php +++ b/autoload.php @@ -571,9 +571,9 @@ $wgAutoloadLocalClasses = array( 'ImportReporter' => __DIR__ . '/includes/specials/SpecialImport.php', 'ImportSiteScripts' => __DIR__ . '/maintenance/importSiteScripts.php', 'ImportSites' => __DIR__ . '/maintenance/importSites.php', - 'ImportSource' => __DIR__ . '/includes/Import.php', - 'ImportStreamSource' => __DIR__ . '/includes/Import.php', - 'ImportStringSource' => __DIR__ . '/includes/Import.php', + 'ImportSource' => __DIR__ . '/includes/import/ImportSource.php', + 'ImportStreamSource' => __DIR__ . '/includes/import/ImportStreamSource.php', + 'ImportStringSource' => __DIR__ . '/includes/import/ImportStringSource.php', 'ImportTitleFactory' => __DIR__ . '/includes/title/ImportTitleFactory.php', 'IncludableSpecialPage' => __DIR__ . '/includes/specialpage/IncludableSpecialPage.php', 'IndexPager' => __DIR__ . '/includes/pager/IndexPager.php', @@ -1314,7 +1314,7 @@ $wgAutoloadLocalClasses = array( 'UploadFromUrl' => __DIR__ . '/includes/upload/UploadFromUrl.php', 'UploadFromUrlJob' => __DIR__ . '/includes/jobqueue/jobs/UploadFromUrlJob.php', 'UploadLogFormatter' => __DIR__ . '/includes/logging/UploadLogFormatter.php', - 'UploadSourceAdapter' => __DIR__ . '/includes/Import.php', + 'UploadSourceAdapter' => __DIR__ . '/includes/import/UploadSourceAdapter.php', 'UploadSourceField' => __DIR__ . '/includes/specials/SpecialUpload.php', 'UploadStash' => __DIR__ . '/includes/upload/UploadStash.php', 'UploadStashBadPathException' => __DIR__ . '/includes/upload/UploadStash.php', @@ -1388,11 +1388,11 @@ $wgAutoloadLocalClasses = array( 'WikiDiff3' => __DIR__ . '/includes/diff/WikiDiff3.php', 'WikiExporter' => __DIR__ . '/includes/Export.php', 'WikiFilePage' => __DIR__ . '/includes/page/WikiFilePage.php', - 'WikiImporter' => __DIR__ . '/includes/Import.php', + 'WikiImporter' => __DIR__ . '/includes/import/WikiImporter.php', 'WikiMap' => __DIR__ . '/includes/WikiMap.php', 'WikiPage' => __DIR__ . '/includes/page/WikiPage.php', 'WikiReference' => __DIR__ . '/includes/WikiMap.php', - 'WikiRevision' => __DIR__ . '/includes/Import.php', + 'WikiRevision' => __DIR__ . '/includes/import/WikiRevision.php', 'WikiStatsOutput' => __DIR__ . '/maintenance/language/StatOutputs.php', 'WikitextContent' => __DIR__ . '/includes/content/WikitextContent.php', 'WikitextContentHandler' => __DIR__ . '/includes/content/WikitextContentHandler.php', diff --git a/includes/import/ImportSource.php b/includes/import/ImportSource.php new file mode 100644 index 0000000000..75d20b4eaa --- /dev/null +++ b/includes/import/ImportSource.php @@ -0,0 +1,51 @@ + + * https://www.mediawiki.org/ + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html + * + * @file + * @ingroup SpecialPage + */ + +/** + * Source interface for XML import. + * + * @ingroup SpecialPage + */ +interface ImportSource { + + /** + * Indicates whether the end of the input has been reached. + * Will return true after a finite number of calls to readChunk. + * + * @return bool true if there is no more input, false otherwise. + */ + function atEnd(); + + /** + * Return a chunk of the input, as a (possibly empty) string. + * When the end of input is reached, readChunk() returns false. + * If atEnd() returns false, readChunk() will return a string. + * If atEnd() returns true, readChunk() will return false. + * + * @return bool|string + */ + function readChunk(); +} diff --git a/includes/import/ImportStreamSource.php b/includes/import/ImportStreamSource.php new file mode 100644 index 0000000000..0e03d9fbce --- /dev/null +++ b/includes/import/ImportStreamSource.php @@ -0,0 +1,172 @@ + + * https://www.mediawiki.org/ + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html + * + * @file + * @ingroup SpecialPage + */ + +/** + * Imports a XML dump from a file (either from file upload, files on disk, or HTTP) + * @ingroup SpecialPage + */ +class ImportStreamSource implements ImportSource { + function __construct( $handle ) { + $this->mHandle = $handle; + } + + /** + * @return bool + */ + function atEnd() { + return feof( $this->mHandle ); + } + + /** + * @return string + */ + function readChunk() { + return fread( $this->mHandle, 32768 ); + } + + /** + * @param string $filename + * @return Status + */ + static function newFromFile( $filename ) { + MediaWiki\suppressWarnings(); + $file = fopen( $filename, 'rt' ); + MediaWiki\restoreWarnings(); + if ( !$file ) { + return Status::newFatal( "importcantopen" ); + } + return Status::newGood( new ImportStreamSource( $file ) ); + } + + /** + * @param string $fieldname + * @return Status + */ + static function newFromUpload( $fieldname = "xmlimport" ) { + $upload =& $_FILES[$fieldname]; + + if ( $upload === null || !$upload['name'] ) { + return Status::newFatal( 'importnofile' ); + } + if ( !empty( $upload['error'] ) ) { + switch ( $upload['error'] ) { + case 1: + # The uploaded file exceeds the upload_max_filesize directive in php.ini. + return Status::newFatal( 'importuploaderrorsize' ); + case 2: + # The uploaded file exceeds the MAX_FILE_SIZE directive that + # was specified in the HTML form. + return Status::newFatal( 'importuploaderrorsize' ); + case 3: + # The uploaded file was only partially uploaded + return Status::newFatal( 'importuploaderrorpartial' ); + case 6: + # Missing a temporary folder. + return Status::newFatal( 'importuploaderrortemp' ); + # case else: # Currently impossible + } + + } + $fname = $upload['tmp_name']; + if ( is_uploaded_file( $fname ) ) { + return ImportStreamSource::newFromFile( $fname ); + } else { + return Status::newFatal( 'importnofile' ); + } + } + + /** + * @param string $url + * @param string $method + * @return Status + */ + static function newFromURL( $url, $method = 'GET' ) { + wfDebug( __METHOD__ . ": opening $url\n" ); + # Use the standard HTTP fetch function; it times out + # quicker and sorts out user-agent problems which might + # otherwise prevent importing from large sites, such + # as the Wikimedia cluster, etc. + $data = Http::request( $method, $url, array( 'followRedirects' => true ), __METHOD__ ); + if ( $data !== false ) { + $file = tmpfile(); + fwrite( $file, $data ); + fflush( $file ); + fseek( $file, 0 ); + return Status::newGood( new ImportStreamSource( $file ) ); + } else { + return Status::newFatal( 'importcantopen' ); + } + } + + /** + * @param string $interwiki + * @param string $page + * @param bool $history + * @param bool $templates + * @param int $pageLinkDepth + * @return Status + */ + public static function newFromInterwiki( $interwiki, $page, $history = false, + $templates = false, $pageLinkDepth = 0 + ) { + if ( $page == '' ) { + return Status::newFatal( 'import-noarticle' ); + } + + # Look up the first interwiki prefix, and let the foreign site handle + # subsequent interwiki prefixes + $firstIwPrefix = strtok( $interwiki, ':' ); + $firstIw = Interwiki::fetch( $firstIwPrefix ); + if ( !$firstIw ) { + return Status::newFatal( 'importbadinterwiki' ); + } + + $additionalIwPrefixes = strtok( '' ); + if ( $additionalIwPrefixes ) { + $additionalIwPrefixes .= ':'; + } + # Have to do a DB-key replacement ourselves; otherwise spaces get + # URL-encoded to +, which is wrong in this case. Similar to logic in + # Title::getLocalURL + $link = $firstIw->getURL( strtr( "${additionalIwPrefixes}Special:Export/$page", + ' ', '_' ) ); + + $params = array(); + if ( $history ) { + $params['history'] = 1; + } + if ( $templates ) { + $params['templates'] = 1; + } + if ( $pageLinkDepth ) { + $params['pagelink-depth'] = $pageLinkDepth; + } + + $url = wfAppendQuery( $link, $params ); + # For interwikis, use POST to avoid redirects. + return ImportStreamSource::newFromURL( $url, "POST" ); + } +} diff --git a/includes/import/ImportStringSource.php b/includes/import/ImportStringSource.php new file mode 100644 index 0000000000..85983b1a11 --- /dev/null +++ b/includes/import/ImportStringSource.php @@ -0,0 +1,57 @@ + + * https://www.mediawiki.org/ + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html + * + * @file + * @ingroup SpecialPage + */ + +/** + * Used for importing XML dumps where the content of the dump is in a string. + * This class is ineffecient, and should only be used for small dumps. + * For larger dumps, ImportStreamSource should be used instead. + * + * @ingroup SpecialPage + */ +class ImportStringSource implements ImportSource { + function __construct( $string ) { + $this->mString = $string; + $this->mRead = false; + } + + /** + * @return bool + */ + function atEnd() { + return $this->mRead; + } + + /** + * @return bool|string + */ + function readChunk() { + if ( $this->atEnd() ) { + return false; + } + $this->mRead = true; + return $this->mString; + } +} diff --git a/includes/import/UploadSourceAdapter.php b/includes/import/UploadSourceAdapter.php new file mode 100644 index 0000000000..17fbdfb43a --- /dev/null +++ b/includes/import/UploadSourceAdapter.php @@ -0,0 +1,149 @@ + + * https://www.mediawiki.org/ + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html + * + * @file + * @ingroup SpecialPage + */ + +/** + * This is a horrible hack used to keep source compatibility. + * @ingroup SpecialPage + */ +class UploadSourceAdapter { + /** @var array */ + public static $sourceRegistrations = array(); + + /** @var string */ + private $mSource; + + /** @var string */ + private $mBuffer; + + /** @var int */ + private $mPosition; + + /** + * @param ImportSource $source + * @return string + */ + static function registerSource( ImportSource $source ) { + $id = wfRandomString(); + + self::$sourceRegistrations[$id] = $source; + + return $id; + } + + /** + * @param string $path + * @param string $mode + * @param array $options + * @param string $opened_path + * @return bool + */ + function stream_open( $path, $mode, $options, &$opened_path ) { + $url = parse_url( $path ); + $id = $url['host']; + + if ( !isset( self::$sourceRegistrations[$id] ) ) { + return false; + } + + $this->mSource = self::$sourceRegistrations[$id]; + + return true; + } + + /** + * @param int $count + * @return string + */ + function stream_read( $count ) { + $return = ''; + $leave = false; + + while ( !$leave && !$this->mSource->atEnd() && + strlen( $this->mBuffer ) < $count ) { + $read = $this->mSource->readChunk(); + + if ( !strlen( $read ) ) { + $leave = true; + } + + $this->mBuffer .= $read; + } + + if ( strlen( $this->mBuffer ) ) { + $return = substr( $this->mBuffer, 0, $count ); + $this->mBuffer = substr( $this->mBuffer, $count ); + } + + $this->mPosition += strlen( $return ); + + return $return; + } + + /** + * @param string $data + * @return bool + */ + function stream_write( $data ) { + return false; + } + + /** + * @return mixed + */ + function stream_tell() { + return $this->mPosition; + } + + /** + * @return bool + */ + function stream_eof() { + return $this->mSource->atEnd(); + } + + /** + * @return array + */ + function url_stat() { + $result = array(); + + $result['dev'] = $result[0] = 0; + $result['ino'] = $result[1] = 0; + $result['mode'] = $result[2] = 0; + $result['nlink'] = $result[3] = 0; + $result['uid'] = $result[4] = 0; + $result['gid'] = $result[5] = 0; + $result['rdev'] = $result[6] = 0; + $result['size'] = $result[7] = 0; + $result['atime'] = $result[8] = 0; + $result['mtime'] = $result[9] = 0; + $result['ctime'] = $result[10] = 0; + $result['blksize'] = $result[11] = 0; + $result['blocks'] = $result[12] = 0; + + return $result; + } +} diff --git a/includes/Import.php b/includes/import/WikiImporter.php similarity index 59% rename from includes/Import.php rename to includes/import/WikiImporter.php index f59cf47d37..9bf9282aca 100644 --- a/includes/Import.php +++ b/includes/import/WikiImporter.php @@ -25,7 +25,7 @@ */ /** - * XML file reader for the page data importer + * XML file reader for the page data importer. * * implements Special:Import * @ingroup SpecialPage @@ -1068,987 +1068,3 @@ class WikiImporter { return array( $title, $foreignTitle ); } } - -/** This is a horrible hack used to keep source compatibility */ -class UploadSourceAdapter { - /** @var array */ - public static $sourceRegistrations = array(); - - /** @var string */ - private $mSource; - - /** @var string */ - private $mBuffer; - - /** @var int */ - private $mPosition; - - /** - * @param ImportSource $source - * @return string - */ - static function registerSource( ImportSource $source ) { - $id = wfRandomString(); - - self::$sourceRegistrations[$id] = $source; - - return $id; - } - - /** - * @param string $path - * @param string $mode - * @param array $options - * @param string $opened_path - * @return bool - */ - function stream_open( $path, $mode, $options, &$opened_path ) { - $url = parse_url( $path ); - $id = $url['host']; - - if ( !isset( self::$sourceRegistrations[$id] ) ) { - return false; - } - - $this->mSource = self::$sourceRegistrations[$id]; - - return true; - } - - /** - * @param int $count - * @return string - */ - function stream_read( $count ) { - $return = ''; - $leave = false; - - while ( !$leave && !$this->mSource->atEnd() && - strlen( $this->mBuffer ) < $count ) { - $read = $this->mSource->readChunk(); - - if ( !strlen( $read ) ) { - $leave = true; - } - - $this->mBuffer .= $read; - } - - if ( strlen( $this->mBuffer ) ) { - $return = substr( $this->mBuffer, 0, $count ); - $this->mBuffer = substr( $this->mBuffer, $count ); - } - - $this->mPosition += strlen( $return ); - - return $return; - } - - /** - * @param string $data - * @return bool - */ - function stream_write( $data ) { - return false; - } - - /** - * @return mixed - */ - function stream_tell() { - return $this->mPosition; - } - - /** - * @return bool - */ - function stream_eof() { - return $this->mSource->atEnd(); - } - - /** - * @return array - */ - function url_stat() { - $result = array(); - - $result['dev'] = $result[0] = 0; - $result['ino'] = $result[1] = 0; - $result['mode'] = $result[2] = 0; - $result['nlink'] = $result[3] = 0; - $result['uid'] = $result[4] = 0; - $result['gid'] = $result[5] = 0; - $result['rdev'] = $result[6] = 0; - $result['size'] = $result[7] = 0; - $result['atime'] = $result[8] = 0; - $result['mtime'] = $result[9] = 0; - $result['ctime'] = $result[10] = 0; - $result['blksize'] = $result[11] = 0; - $result['blocks'] = $result[12] = 0; - - return $result; - } -} - -/** - * @todo document (e.g. one-sentence class description). - * @ingroup SpecialPage - */ -class WikiRevision { - /** @todo Unused? */ - public $importer = null; - - /** @var Title */ - public $title = null; - - /** @var int */ - public $id = 0; - - /** @var string */ - public $timestamp = "20010115000000"; - - /** - * @var int - * @todo Can't find any uses. Public, because that's suspicious. Get clarity. */ - public $user = 0; - - /** @var string */ - public $user_text = ""; - - /** @var string */ - public $model = null; - - /** @var string */ - public $format = null; - - /** @var string */ - public $text = ""; - - /** @var int */ - protected $size; - - /** @var Content */ - public $content = null; - - /** @var ContentHandler */ - protected $contentHandler = null; - - /** @var string */ - public $comment = ""; - - /** @var bool */ - public $minor = false; - - /** @var string */ - public $type = ""; - - /** @var string */ - public $action = ""; - - /** @var string */ - public $params = ""; - - /** @var string */ - public $fileSrc = ''; - - /** @var bool|string */ - public $sha1base36 = false; - - /** - * @var bool - * @todo Unused? - */ - public $isTemp = false; - - /** @var string */ - public $archiveName = ''; - - protected $filename; - - /** @var mixed */ - protected $src; - - /** @todo Unused? */ - public $fileIsTemp; - - /** @var bool */ - private $mNoUpdates = false; - - /** @var Config $config */ - private $config; - - public function __construct( Config $config ) { - $this->config = $config; - } - - /** - * @param Title $title - * @throws MWException - */ - function setTitle( $title ) { - if ( is_object( $title ) ) { - $this->title = $title; - } elseif ( is_null( $title ) ) { - throw new MWException( "WikiRevision given a null title in import. " - . "You may need to adjust \$wgLegalTitleChars." ); - } else { - throw new MWException( "WikiRevision given non-object title in import." ); - } - } - - /** - * @param int $id - */ - function setID( $id ) { - $this->id = $id; - } - - /** - * @param string $ts - */ - function setTimestamp( $ts ) { - # 2003-08-05T18:30:02Z - $this->timestamp = wfTimestamp( TS_MW, $ts ); - } - - /** - * @param string $user - */ - function setUsername( $user ) { - $this->user_text = $user; - } - - /** - * @param string $ip - */ - function setUserIP( $ip ) { - $this->user_text = $ip; - } - - /** - * @param string $model - */ - function setModel( $model ) { - $this->model = $model; - } - - /** - * @param string $format - */ - function setFormat( $format ) { - $this->format = $format; - } - - /** - * @param string $text - */ - function setText( $text ) { - $this->text = $text; - } - - /** - * @param string $text - */ - function setComment( $text ) { - $this->comment = $text; - } - - /** - * @param bool $minor - */ - function setMinor( $minor ) { - $this->minor = (bool)$minor; - } - - /** - * @param mixed $src - */ - function setSrc( $src ) { - $this->src = $src; - } - - /** - * @param string $src - * @param bool $isTemp - */ - function setFileSrc( $src, $isTemp ) { - $this->fileSrc = $src; - $this->fileIsTemp = $isTemp; - } - - /** - * @param string $sha1base36 - */ - function setSha1Base36( $sha1base36 ) { - $this->sha1base36 = $sha1base36; - } - - /** - * @param string $filename - */ - function setFilename( $filename ) { - $this->filename = $filename; - } - - /** - * @param string $archiveName - */ - function setArchiveName( $archiveName ) { - $this->archiveName = $archiveName; - } - - /** - * @param int $size - */ - function setSize( $size ) { - $this->size = intval( $size ); - } - - /** - * @param string $type - */ - function setType( $type ) { - $this->type = $type; - } - - /** - * @param string $action - */ - function setAction( $action ) { - $this->action = $action; - } - - /** - * @param array $params - */ - function setParams( $params ) { - $this->params = $params; - } - - /** - * @param bool $noupdates - */ - public function setNoUpdates( $noupdates ) { - $this->mNoUpdates = $noupdates; - } - - /** - * @return Title - */ - function getTitle() { - return $this->title; - } - - /** - * @return int - */ - function getID() { - return $this->id; - } - - /** - * @return string - */ - function getTimestamp() { - return $this->timestamp; - } - - /** - * @return string - */ - function getUser() { - return $this->user_text; - } - - /** - * @return string - * - * @deprecated Since 1.21, use getContent() instead. - */ - function getText() { - ContentHandler::deprecated( __METHOD__, '1.21' ); - - return $this->text; - } - - /** - * @return ContentHandler - */ - function getContentHandler() { - if ( is_null( $this->contentHandler ) ) { - $this->contentHandler = ContentHandler::getForModelID( $this->getModel() ); - } - - return $this->contentHandler; - } - - /** - * @return Content - */ - function getContent() { - if ( is_null( $this->content ) ) { - $handler = $this->getContentHandler(); - $this->content = $handler->unserializeContent( $this->text, $this->getFormat() ); - } - - return $this->content; - } - - /** - * @return string - */ - function getModel() { - if ( is_null( $this->model ) ) { - $this->model = $this->getTitle()->getContentModel(); - } - - return $this->model; - } - - /** - * @return string - */ - function getFormat() { - if ( is_null( $this->format ) ) { - $this->format = $this->getContentHandler()->getDefaultFormat(); - } - - return $this->format; - } - - /** - * @return string - */ - function getComment() { - return $this->comment; - } - - /** - * @return bool - */ - function getMinor() { - return $this->minor; - } - - /** - * @return mixed - */ - function getSrc() { - return $this->src; - } - - /** - * @return bool|string - */ - function getSha1() { - if ( $this->sha1base36 ) { - return Wikimedia\base_convert( $this->sha1base36, 36, 16 ); - } - return false; - } - - /** - * @return string - */ - function getFileSrc() { - return $this->fileSrc; - } - - /** - * @return bool - */ - function isTempSrc() { - return $this->isTemp; - } - - /** - * @return mixed - */ - function getFilename() { - return $this->filename; - } - - /** - * @return string - */ - function getArchiveName() { - return $this->archiveName; - } - - /** - * @return mixed - */ - function getSize() { - return $this->size; - } - - /** - * @return string - */ - function getType() { - return $this->type; - } - - /** - * @return string - */ - function getAction() { - return $this->action; - } - - /** - * @return string - */ - function getParams() { - return $this->params; - } - - /** - * @return bool - */ - function importOldRevision() { - $dbw = wfGetDB( DB_MASTER ); - - # Sneak a single revision into place - $user = User::newFromName( $this->getUser() ); - if ( $user ) { - $userId = intval( $user->getId() ); - $userText = $user->getName(); - $userObj = $user; - } else { - $userId = 0; - $userText = $this->getUser(); - $userObj = new User; - } - - // avoid memory leak...? - Title::clearCaches(); - - $page = WikiPage::factory( $this->title ); - $page->loadPageData( 'fromdbmaster' ); - if ( !$page->exists() ) { - # must create the page... - $pageId = $page->insertOn( $dbw ); - $created = true; - $oldcountable = null; - } else { - $pageId = $page->getId(); - $created = false; - - $prior = $dbw->selectField( 'revision', '1', - array( 'rev_page' => $pageId, - 'rev_timestamp' => $dbw->timestamp( $this->timestamp ), - 'rev_user_text' => $userText, - 'rev_comment' => $this->getComment() ), - __METHOD__ - ); - if ( $prior ) { - // @todo FIXME: This could fail slightly for multiple matches :P - wfDebug( __METHOD__ . ": skipping existing revision for [[" . - $this->title->getPrefixedText() . "]], timestamp " . $this->timestamp . "\n" ); - return false; - } - } - - // Select previous version to make size diffs correct - $prevId = $dbw->selectField( 'revision', 'rev_id', - array( - 'rev_page' => $pageId, - 'rev_timestamp <= ' . $dbw->addQuotes( $dbw->timestamp( $this->timestamp ) ), - ), - __METHOD__, - array( 'ORDER BY' => array( - 'rev_timestamp DESC', - 'rev_id DESC', // timestamp is not unique per page - ) - ) - ); - - # @todo FIXME: Use original rev_id optionally (better for backups) - # Insert the row - $revision = new Revision( array( - 'title' => $this->title, - 'page' => $pageId, - 'content_model' => $this->getModel(), - 'content_format' => $this->getFormat(), - // XXX: just set 'content' => $this->getContent()? - 'text' => $this->getContent()->serialize( $this->getFormat() ), - 'comment' => $this->getComment(), - 'user' => $userId, - 'user_text' => $userText, - 'timestamp' => $this->timestamp, - 'minor_edit' => $this->minor, - 'parent_id' => $prevId, - ) ); - $revision->insertOn( $dbw ); - $changed = $page->updateIfNewerOn( $dbw, $revision ); - - if ( $changed !== false && !$this->mNoUpdates ) { - wfDebug( __METHOD__ . ": running updates\n" ); - // countable/oldcountable stuff is handled in WikiImporter::finishImportPage - $page->doEditUpdates( - $revision, - $userObj, - array( 'created' => $created, 'oldcountable' => 'no-change' ) - ); - } - - return true; - } - - function importLogItem() { - $dbw = wfGetDB( DB_MASTER ); - - $user = User::newFromName( $this->getUser() ); - if ( $user ) { - $userId = intval( $user->getId() ); - $userText = $user->getName(); - } else { - $userId = 0; - $userText = $this->getUser(); - } - - # @todo FIXME: This will not record autoblocks - if ( !$this->getTitle() ) { - wfDebug( __METHOD__ . ": skipping invalid {$this->type}/{$this->action} log time, timestamp " . - $this->timestamp . "\n" ); - return; - } - # Check if it exists already - // @todo FIXME: Use original log ID (better for backups) - $prior = $dbw->selectField( 'logging', '1', - array( 'log_type' => $this->getType(), - 'log_action' => $this->getAction(), - 'log_timestamp' => $dbw->timestamp( $this->timestamp ), - 'log_namespace' => $this->getTitle()->getNamespace(), - 'log_title' => $this->getTitle()->getDBkey(), - 'log_comment' => $this->getComment(), - # 'log_user_text' => $this->user_text, - 'log_params' => $this->params ), - __METHOD__ - ); - // @todo FIXME: This could fail slightly for multiple matches :P - if ( $prior ) { - wfDebug( __METHOD__ - . ": skipping existing item for Log:{$this->type}/{$this->action}, timestamp " - . $this->timestamp . "\n" ); - return; - } - $log_id = $dbw->nextSequenceValue( 'logging_log_id_seq' ); - $data = array( - 'log_id' => $log_id, - 'log_type' => $this->type, - 'log_action' => $this->action, - 'log_timestamp' => $dbw->timestamp( $this->timestamp ), - 'log_user' => $userId, - 'log_user_text' => $userText, - 'log_namespace' => $this->getTitle()->getNamespace(), - 'log_title' => $this->getTitle()->getDBkey(), - 'log_comment' => $this->getComment(), - 'log_params' => $this->params - ); - $dbw->insert( 'logging', $data, __METHOD__ ); - } - - /** - * @return bool - */ - function importUpload() { - # Construct a file - $archiveName = $this->getArchiveName(); - if ( $archiveName ) { - wfDebug( __METHOD__ . "Importing archived file as $archiveName\n" ); - $file = OldLocalFile::newFromArchiveName( $this->getTitle(), - RepoGroup::singleton()->getLocalRepo(), $archiveName ); - } else { - $file = wfLocalFile( $this->getTitle() ); - $file->load( File::READ_LATEST ); - wfDebug( __METHOD__ . 'Importing new file as ' . $file->getName() . "\n" ); - if ( $file->exists() && $file->getTimestamp() > $this->getTimestamp() ) { - $archiveName = $file->getTimestamp() . '!' . $file->getName(); - $file = OldLocalFile::newFromArchiveName( $this->getTitle(), - RepoGroup::singleton()->getLocalRepo(), $archiveName ); - wfDebug( __METHOD__ . "File already exists; importing as $archiveName\n" ); - } - } - if ( !$file ) { - wfDebug( __METHOD__ . ': Bad file for ' . $this->getTitle() . "\n" ); - return false; - } - - # Get the file source or download if necessary - $source = $this->getFileSrc(); - $flags = $this->isTempSrc() ? File::DELETE_SOURCE : 0; - if ( !$source ) { - $source = $this->downloadSource(); - $flags |= File::DELETE_SOURCE; - } - if ( !$source ) { - wfDebug( __METHOD__ . ": Could not fetch remote file.\n" ); - return false; - } - $sha1 = $this->getSha1(); - if ( $sha1 && ( $sha1 !== sha1_file( $source ) ) ) { - if ( $flags & File::DELETE_SOURCE ) { - # Broken file; delete it if it is a temporary file - unlink( $source ); - } - wfDebug( __METHOD__ . ": Corrupt file $source.\n" ); - return false; - } - - $user = User::newFromName( $this->user_text ); - - # Do the actual upload - if ( $archiveName ) { - $status = $file->uploadOld( $source, $archiveName, - $this->getTimestamp(), $this->getComment(), $user, $flags ); - } else { - $status = $file->upload( $source, $this->getComment(), $this->getComment(), - $flags, false, $this->getTimestamp(), $user ); - } - - if ( $status->isGood() ) { - wfDebug( __METHOD__ . ": Successful\n" ); - return true; - } else { - wfDebug( __METHOD__ . ': failed: ' . $status->getHTML() . "\n" ); - return false; - } - } - - /** - * @return bool|string - */ - function downloadSource() { - if ( !$this->config->get( 'EnableUploads' ) ) { - return false; - } - - $tempo = tempnam( wfTempDir(), 'download' ); - $f = fopen( $tempo, 'wb' ); - if ( !$f ) { - wfDebug( "IMPORT: couldn't write to temp file $tempo\n" ); - return false; - } - - // @todo FIXME! - $src = $this->getSrc(); - $data = Http::get( $src, array(), __METHOD__ ); - if ( !$data ) { - wfDebug( "IMPORT: couldn't fetch source $src\n" ); - fclose( $f ); - unlink( $tempo ); - return false; - } - - fwrite( $f, $data ); - fclose( $f ); - - return $tempo; - } - -} - -/** - * Source interface for XML import. - */ -interface ImportSource { - - /** - * Indicates whether the end of the input has been reached. - * Will return true after a finite number of calls to readChunk. - * - * @return bool true if there is no more input, false otherwise. - */ - function atEnd(); - - /** - * Return a chunk of the input, as a (possibly empty) string. - * When the end of input is reached, readChunk() returns false. - * If atEnd() returns false, readChunk() will return a string. - * If atEnd() returns true, readChunk() will return false. - * - * @return bool|string - */ - function readChunk(); -} - -/** - * Used for importing XML dumps where the content of the dump is in a string. - * This class is ineffecient, and should only be used for small dumps. - * For larger dumps, ImportStreamSource should be used instead. - * - * @ingroup SpecialPage - */ -class ImportStringSource implements ImportSource { - function __construct( $string ) { - $this->mString = $string; - $this->mRead = false; - } - - /** - * @return bool - */ - function atEnd() { - return $this->mRead; - } - - /** - * @return bool|string - */ - function readChunk() { - if ( $this->atEnd() ) { - return false; - } - $this->mRead = true; - return $this->mString; - } -} - -/** - * Imports a XML dump from a file (either from file upload, files on disk, or HTTP) - * @ingroup SpecialPage - */ -class ImportStreamSource implements ImportSource { - function __construct( $handle ) { - $this->mHandle = $handle; - } - - /** - * @return bool - */ - function atEnd() { - return feof( $this->mHandle ); - } - - /** - * @return string - */ - function readChunk() { - return fread( $this->mHandle, 32768 ); - } - - /** - * @param string $filename - * @return Status - */ - static function newFromFile( $filename ) { - MediaWiki\suppressWarnings(); - $file = fopen( $filename, 'rt' ); - MediaWiki\restoreWarnings(); - if ( !$file ) { - return Status::newFatal( "importcantopen" ); - } - return Status::newGood( new ImportStreamSource( $file ) ); - } - - /** - * @param string $fieldname - * @return Status - */ - static function newFromUpload( $fieldname = "xmlimport" ) { - $upload =& $_FILES[$fieldname]; - - if ( $upload === null || !$upload['name'] ) { - return Status::newFatal( 'importnofile' ); - } - if ( !empty( $upload['error'] ) ) { - switch ( $upload['error'] ) { - case 1: - # The uploaded file exceeds the upload_max_filesize directive in php.ini. - return Status::newFatal( 'importuploaderrorsize' ); - case 2: - # The uploaded file exceeds the MAX_FILE_SIZE directive that - # was specified in the HTML form. - return Status::newFatal( 'importuploaderrorsize' ); - case 3: - # The uploaded file was only partially uploaded - return Status::newFatal( 'importuploaderrorpartial' ); - case 6: - # Missing a temporary folder. - return Status::newFatal( 'importuploaderrortemp' ); - # case else: # Currently impossible - } - - } - $fname = $upload['tmp_name']; - if ( is_uploaded_file( $fname ) ) { - return ImportStreamSource::newFromFile( $fname ); - } else { - return Status::newFatal( 'importnofile' ); - } - } - - /** - * @param string $url - * @param string $method - * @return Status - */ - static function newFromURL( $url, $method = 'GET' ) { - wfDebug( __METHOD__ . ": opening $url\n" ); - # Use the standard HTTP fetch function; it times out - # quicker and sorts out user-agent problems which might - # otherwise prevent importing from large sites, such - # as the Wikimedia cluster, etc. - $data = Http::request( $method, $url, array( 'followRedirects' => true ), __METHOD__ ); - if ( $data !== false ) { - $file = tmpfile(); - fwrite( $file, $data ); - fflush( $file ); - fseek( $file, 0 ); - return Status::newGood( new ImportStreamSource( $file ) ); - } else { - return Status::newFatal( 'importcantopen' ); - } - } - - /** - * @param string $interwiki - * @param string $page - * @param bool $history - * @param bool $templates - * @param int $pageLinkDepth - * @return Status - */ - public static function newFromInterwiki( $interwiki, $page, $history = false, - $templates = false, $pageLinkDepth = 0 - ) { - if ( $page == '' ) { - return Status::newFatal( 'import-noarticle' ); - } - - # Look up the first interwiki prefix, and let the foreign site handle - # subsequent interwiki prefixes - $firstIwPrefix = strtok( $interwiki, ':' ); - $firstIw = Interwiki::fetch( $firstIwPrefix ); - if ( !$firstIw ) { - return Status::newFatal( 'importbadinterwiki' ); - } - - $additionalIwPrefixes = strtok( '' ); - if ( $additionalIwPrefixes ) { - $additionalIwPrefixes .= ':'; - } - # Have to do a DB-key replacement ourselves; otherwise spaces get - # URL-encoded to +, which is wrong in this case. Similar to logic in - # Title::getLocalURL - $link = $firstIw->getURL( strtr( "${additionalIwPrefixes}Special:Export/$page", - ' ', '_' ) ); - - $params = array(); - if ( $history ) { - $params['history'] = 1; - } - if ( $templates ) { - $params['templates'] = 1; - } - if ( $pageLinkDepth ) { - $params['pagelink-depth'] = $pageLinkDepth; - } - - $url = wfAppendQuery( $link, $params ); - # For interwikis, use POST to avoid redirects. - return ImportStreamSource::newFromURL( $url, "POST" ); - } -} diff --git a/includes/import/WikiRevision.php b/includes/import/WikiRevision.php new file mode 100644 index 0000000000..9b8c74c878 --- /dev/null +++ b/includes/import/WikiRevision.php @@ -0,0 +1,677 @@ + + * https://www.mediawiki.org/ + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html + * + * @file + * @ingroup SpecialPage + */ + +/** + * Represents a revision, log entry or upload during the import process. + * This class sticks closely to the structure of the XML dump. + * + * @ingroup SpecialPage + */ +class WikiRevision { + /** @todo Unused? */ + public $importer = null; + + /** @var Title */ + public $title = null; + + /** @var int */ + public $id = 0; + + /** @var string */ + public $timestamp = "20010115000000"; + + /** + * @var int + * @todo Can't find any uses. Public, because that's suspicious. Get clarity. */ + public $user = 0; + + /** @var string */ + public $user_text = ""; + + /** @var string */ + public $model = null; + + /** @var string */ + public $format = null; + + /** @var string */ + public $text = ""; + + /** @var int */ + protected $size; + + /** @var Content */ + public $content = null; + + /** @var ContentHandler */ + protected $contentHandler = null; + + /** @var string */ + public $comment = ""; + + /** @var bool */ + public $minor = false; + + /** @var string */ + public $type = ""; + + /** @var string */ + public $action = ""; + + /** @var string */ + public $params = ""; + + /** @var string */ + public $fileSrc = ''; + + /** @var bool|string */ + public $sha1base36 = false; + + /** + * @var bool + * @todo Unused? + */ + public $isTemp = false; + + /** @var string */ + public $archiveName = ''; + + protected $filename; + + /** @var mixed */ + protected $src; + + /** @todo Unused? */ + public $fileIsTemp; + + /** @var bool */ + private $mNoUpdates = false; + + /** @var Config $config */ + private $config; + + public function __construct( Config $config ) { + $this->config = $config; + } + + /** + * @param Title $title + * @throws MWException + */ + function setTitle( $title ) { + if ( is_object( $title ) ) { + $this->title = $title; + } elseif ( is_null( $title ) ) { + throw new MWException( "WikiRevision given a null title in import. " + . "You may need to adjust \$wgLegalTitleChars." ); + } else { + throw new MWException( "WikiRevision given non-object title in import." ); + } + } + + /** + * @param int $id + */ + function setID( $id ) { + $this->id = $id; + } + + /** + * @param string $ts + */ + function setTimestamp( $ts ) { + # 2003-08-05T18:30:02Z + $this->timestamp = wfTimestamp( TS_MW, $ts ); + } + + /** + * @param string $user + */ + function setUsername( $user ) { + $this->user_text = $user; + } + + /** + * @param string $ip + */ + function setUserIP( $ip ) { + $this->user_text = $ip; + } + + /** + * @param string $model + */ + function setModel( $model ) { + $this->model = $model; + } + + /** + * @param string $format + */ + function setFormat( $format ) { + $this->format = $format; + } + + /** + * @param string $text + */ + function setText( $text ) { + $this->text = $text; + } + + /** + * @param string $text + */ + function setComment( $text ) { + $this->comment = $text; + } + + /** + * @param bool $minor + */ + function setMinor( $minor ) { + $this->minor = (bool)$minor; + } + + /** + * @param mixed $src + */ + function setSrc( $src ) { + $this->src = $src; + } + + /** + * @param string $src + * @param bool $isTemp + */ + function setFileSrc( $src, $isTemp ) { + $this->fileSrc = $src; + $this->fileIsTemp = $isTemp; + } + + /** + * @param string $sha1base36 + */ + function setSha1Base36( $sha1base36 ) { + $this->sha1base36 = $sha1base36; + } + + /** + * @param string $filename + */ + function setFilename( $filename ) { + $this->filename = $filename; + } + + /** + * @param string $archiveName + */ + function setArchiveName( $archiveName ) { + $this->archiveName = $archiveName; + } + + /** + * @param int $size + */ + function setSize( $size ) { + $this->size = intval( $size ); + } + + /** + * @param string $type + */ + function setType( $type ) { + $this->type = $type; + } + + /** + * @param string $action + */ + function setAction( $action ) { + $this->action = $action; + } + + /** + * @param array $params + */ + function setParams( $params ) { + $this->params = $params; + } + + /** + * @param bool $noupdates + */ + public function setNoUpdates( $noupdates ) { + $this->mNoUpdates = $noupdates; + } + + /** + * @return Title + */ + function getTitle() { + return $this->title; + } + + /** + * @return int + */ + function getID() { + return $this->id; + } + + /** + * @return string + */ + function getTimestamp() { + return $this->timestamp; + } + + /** + * @return string + */ + function getUser() { + return $this->user_text; + } + + /** + * @return string + * + * @deprecated Since 1.21, use getContent() instead. + */ + function getText() { + ContentHandler::deprecated( __METHOD__, '1.21' ); + + return $this->text; + } + + /** + * @return ContentHandler + */ + function getContentHandler() { + if ( is_null( $this->contentHandler ) ) { + $this->contentHandler = ContentHandler::getForModelID( $this->getModel() ); + } + + return $this->contentHandler; + } + + /** + * @return Content + */ + function getContent() { + if ( is_null( $this->content ) ) { + $handler = $this->getContentHandler(); + $this->content = $handler->unserializeContent( $this->text, $this->getFormat() ); + } + + return $this->content; + } + + /** + * @return string + */ + function getModel() { + if ( is_null( $this->model ) ) { + $this->model = $this->getTitle()->getContentModel(); + } + + return $this->model; + } + + /** + * @return string + */ + function getFormat() { + if ( is_null( $this->format ) ) { + $this->format = $this->getContentHandler()->getDefaultFormat(); + } + + return $this->format; + } + + /** + * @return string + */ + function getComment() { + return $this->comment; + } + + /** + * @return bool + */ + function getMinor() { + return $this->minor; + } + + /** + * @return mixed + */ + function getSrc() { + return $this->src; + } + + /** + * @return bool|string + */ + function getSha1() { + if ( $this->sha1base36 ) { + return Wikimedia\base_convert( $this->sha1base36, 36, 16 ); + } + return false; + } + + /** + * @return string + */ + function getFileSrc() { + return $this->fileSrc; + } + + /** + * @return bool + */ + function isTempSrc() { + return $this->isTemp; + } + + /** + * @return mixed + */ + function getFilename() { + return $this->filename; + } + + /** + * @return string + */ + function getArchiveName() { + return $this->archiveName; + } + + /** + * @return mixed + */ + function getSize() { + return $this->size; + } + + /** + * @return string + */ + function getType() { + return $this->type; + } + + /** + * @return string + */ + function getAction() { + return $this->action; + } + + /** + * @return string + */ + function getParams() { + return $this->params; + } + + /** + * @return bool + */ + function importOldRevision() { + $dbw = wfGetDB( DB_MASTER ); + + # Sneak a single revision into place + $user = User::newFromName( $this->getUser() ); + if ( $user ) { + $userId = intval( $user->getId() ); + $userText = $user->getName(); + $userObj = $user; + } else { + $userId = 0; + $userText = $this->getUser(); + $userObj = new User; + } + + // avoid memory leak...? + Title::clearCaches(); + + $page = WikiPage::factory( $this->title ); + $page->loadPageData( 'fromdbmaster' ); + if ( !$page->exists() ) { + # must create the page... + $pageId = $page->insertOn( $dbw ); + $created = true; + $oldcountable = null; + } else { + $pageId = $page->getId(); + $created = false; + + $prior = $dbw->selectField( 'revision', '1', + array( 'rev_page' => $pageId, + 'rev_timestamp' => $dbw->timestamp( $this->timestamp ), + 'rev_user_text' => $userText, + 'rev_comment' => $this->getComment() ), + __METHOD__ + ); + if ( $prior ) { + // @todo FIXME: This could fail slightly for multiple matches :P + wfDebug( __METHOD__ . ": skipping existing revision for [[" . + $this->title->getPrefixedText() . "]], timestamp " . $this->timestamp . "\n" ); + return false; + } + } + + // Select previous version to make size diffs correct + $prevId = $dbw->selectField( 'revision', 'rev_id', + array( + 'rev_page' => $pageId, + 'rev_timestamp <= ' . $dbw->timestamp( $this->timestamp ), + ), + __METHOD__, + array( 'ORDER BY' => array( + 'rev_timestamp DESC', + 'rev_id DESC', // timestamp is not unique per page + ) + ) + ); + + # @todo FIXME: Use original rev_id optionally (better for backups) + # Insert the row + $revision = new Revision( array( + 'title' => $this->title, + 'page' => $pageId, + 'content_model' => $this->getModel(), + 'content_format' => $this->getFormat(), + // XXX: just set 'content' => $this->getContent()? + 'text' => $this->getContent()->serialize( $this->getFormat() ), + 'comment' => $this->getComment(), + 'user' => $userId, + 'user_text' => $userText, + 'timestamp' => $this->timestamp, + 'minor_edit' => $this->minor, + 'parent_id' => $prevId, + ) ); + $revision->insertOn( $dbw ); + $changed = $page->updateIfNewerOn( $dbw, $revision ); + + if ( $changed !== false && !$this->mNoUpdates ) { + wfDebug( __METHOD__ . ": running updates\n" ); + // countable/oldcountable stuff is handled in WikiImporter::finishImportPage + $page->doEditUpdates( + $revision, + $userObj, + array( 'created' => $created, 'oldcountable' => 'no-change' ) + ); + } + + return true; + } + + function importLogItem() { + $dbw = wfGetDB( DB_MASTER ); + # @todo FIXME: This will not record autoblocks + if ( !$this->getTitle() ) { + wfDebug( __METHOD__ . ": skipping invalid {$this->type}/{$this->action} log time, timestamp " . + $this->timestamp . "\n" ); + return; + } + # Check if it exists already + // @todo FIXME: Use original log ID (better for backups) + $prior = $dbw->selectField( 'logging', '1', + array( 'log_type' => $this->getType(), + 'log_action' => $this->getAction(), + 'log_timestamp' => $dbw->timestamp( $this->timestamp ), + 'log_namespace' => $this->getTitle()->getNamespace(), + 'log_title' => $this->getTitle()->getDBkey(), + 'log_comment' => $this->getComment(), + # 'log_user_text' => $this->user_text, + 'log_params' => $this->params ), + __METHOD__ + ); + // @todo FIXME: This could fail slightly for multiple matches :P + if ( $prior ) { + wfDebug( __METHOD__ + . ": skipping existing item for Log:{$this->type}/{$this->action}, timestamp " + . $this->timestamp . "\n" ); + return; + } + $log_id = $dbw->nextSequenceValue( 'logging_log_id_seq' ); + $data = array( + 'log_id' => $log_id, + 'log_type' => $this->type, + 'log_action' => $this->action, + 'log_timestamp' => $dbw->timestamp( $this->timestamp ), + 'log_user' => User::idFromName( $this->user_text ), + # 'log_user_text' => $this->user_text, + 'log_namespace' => $this->getTitle()->getNamespace(), + 'log_title' => $this->getTitle()->getDBkey(), + 'log_comment' => $this->getComment(), + 'log_params' => $this->params + ); + $dbw->insert( 'logging', $data, __METHOD__ ); + } + + /** + * @return bool + */ + function importUpload() { + # Construct a file + $archiveName = $this->getArchiveName(); + if ( $archiveName ) { + wfDebug( __METHOD__ . "Importing archived file as $archiveName\n" ); + $file = OldLocalFile::newFromArchiveName( $this->getTitle(), + RepoGroup::singleton()->getLocalRepo(), $archiveName ); + } else { + $file = wfLocalFile( $this->getTitle() ); + $file->load( File::READ_LATEST ); + wfDebug( __METHOD__ . 'Importing new file as ' . $file->getName() . "\n" ); + if ( $file->exists() && $file->getTimestamp() > $this->getTimestamp() ) { + $archiveName = $file->getTimestamp() . '!' . $file->getName(); + $file = OldLocalFile::newFromArchiveName( $this->getTitle(), + RepoGroup::singleton()->getLocalRepo(), $archiveName ); + wfDebug( __METHOD__ . "File already exists; importing as $archiveName\n" ); + } + } + if ( !$file ) { + wfDebug( __METHOD__ . ': Bad file for ' . $this->getTitle() . "\n" ); + return false; + } + + # Get the file source or download if necessary + $source = $this->getFileSrc(); + $flags = $this->isTempSrc() ? File::DELETE_SOURCE : 0; + if ( !$source ) { + $source = $this->downloadSource(); + $flags |= File::DELETE_SOURCE; + } + if ( !$source ) { + wfDebug( __METHOD__ . ": Could not fetch remote file.\n" ); + return false; + } + $sha1 = $this->getSha1(); + if ( $sha1 && ( $sha1 !== sha1_file( $source ) ) ) { + if ( $flags & File::DELETE_SOURCE ) { + # Broken file; delete it if it is a temporary file + unlink( $source ); + } + wfDebug( __METHOD__ . ": Corrupt file $source.\n" ); + return false; + } + + $user = User::newFromName( $this->user_text ); + + # Do the actual upload + if ( $archiveName ) { + $status = $file->uploadOld( $source, $archiveName, + $this->getTimestamp(), $this->getComment(), $user, $flags ); + } else { + $status = $file->upload( $source, $this->getComment(), $this->getComment(), + $flags, false, $this->getTimestamp(), $user ); + } + + if ( $status->isGood() ) { + wfDebug( __METHOD__ . ": Successful\n" ); + return true; + } else { + wfDebug( __METHOD__ . ': failed: ' . $status->getHTML() . "\n" ); + return false; + } + } + + /** + * @return bool|string + */ + function downloadSource() { + if ( !$this->config->get( 'EnableUploads' ) ) { + return false; + } + + $tempo = tempnam( wfTempDir(), 'download' ); + $f = fopen( $tempo, 'wb' ); + if ( !$f ) { + wfDebug( "IMPORT: couldn't write to temp file $tempo\n" ); + return false; + } + + // @todo FIXME! + $src = $this->getSrc(); + $data = Http::get( $src, array(), __METHOD__ ); + if ( !$data ) { + wfDebug( "IMPORT: couldn't fetch source $src\n" ); + fclose( $f ); + unlink( $tempo ); + return false; + } + + fwrite( $f, $data ); + fclose( $f ); + + return $tempo; + } + +} -- 2.20.1