1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
<?php
use MediaWiki\MediaWikiServices;
use Psr\Log\LoggerInterface;
/**
* @since 1.31
*/
class ImportableUploadRevisionImporter implements UploadRevisionImporter {
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var bool
*/
private $enableUploads;
/**
* @var bool
*/
private $shouldCreateNullRevision = true;
/**
* @param bool $enableUploads
* @param LoggerInterface $logger
*/
public function __construct(
$enableUploads,
LoggerInterface $logger
) {
$this->enableUploads = $enableUploads;
$this->logger = $logger;
}
/**
* Setting this to false will deactivate the creation of a null revision as part of the upload
* process logging in LocalFile::recordUpload2, see T193621
*
* @param bool $shouldCreateNullRevision
*/
public function setNullRevisionCreation( $shouldCreateNullRevision ) {
$this->shouldCreateNullRevision = $shouldCreateNullRevision;
}
/**
* @return StatusValue
*/
private function newNotOkStatus() {
$statusValue = new StatusValue();
$statusValue->setOK( false );
return $statusValue;
}
public function import( ImportableUploadRevision $importableRevision ) {
# Construct a file
$archiveName = $importableRevision->getArchiveName();
$localRepo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo();
if ( $archiveName ) {
$this->logger->debug( __METHOD__ . "Importing archived file as $archiveName" );
$file = OldLocalFile::newFromArchiveName( $importableRevision->getTitle(),
$localRepo, $archiveName );
} else {
$file = $localRepo->newFile( $importableRevision->getTitle() );
$file->load( File::READ_LATEST );
$this->logger->debug( __METHOD__ . 'Importing new file as ' . $file->getName() );
if ( $file->exists() && $file->getTimestamp() > $importableRevision->getTimestamp() ) {
$archiveName = $importableRevision->getTimestamp() . '!' . $file->getName();
$file = OldLocalFile::newFromArchiveName( $importableRevision->getTitle(),
$localRepo, $archiveName );
$this->logger->debug( __METHOD__ . "File already exists; importing as $archiveName" );
}
}
if ( !$file ) {
$this->logger->debug( __METHOD__ . ': Bad file for ' . $importableRevision->getTitle() );
return $this->newNotOkStatus();
}
# Get the file source or download if necessary
$source = $importableRevision->getFileSrc();
$autoDeleteSource = $importableRevision->isTempSrc();
if ( !strlen( $source ) ) {
$source = $this->downloadSource( $importableRevision );
$autoDeleteSource = true;
}
if ( !strlen( $source ) ) {
$this->logger->debug( __METHOD__ . ": Could not fetch remote file." );
return $this->newNotOkStatus();
}
$tmpFile = new TempFSFile( $source );
if ( $autoDeleteSource ) {
$tmpFile->autocollect();
}
$sha1File = ltrim( sha1_file( $source ), '0' );
$sha1 = $importableRevision->getSha1();
if ( $sha1 && ( $sha1 !== $sha1File ) ) {
$this->logger->debug( __METHOD__ . ": Corrupt file $source." );
return $this->newNotOkStatus();
}
$user = $importableRevision->getUserObj()
?: User::newFromName( $importableRevision->getUser(), false );
# Do the actual upload
if ( $file instanceof OldLocalFile ) {
$status = $file->uploadOld(
$source,
$importableRevision->getTimestamp(),
$importableRevision->getComment(),
$user
);
} else {
$flags = 0;
$status = $file->upload(
$source,
$importableRevision->getComment(),
$importableRevision->getComment(),
$flags,
false,
$importableRevision->getTimestamp(),
$user,
[],
$this->shouldCreateNullRevision
);
}
if ( $status->isGood() ) {
$this->logger->debug( __METHOD__ . ": Successful" );
} else {
$this->logger->debug( __METHOD__ . ': failed: ' . $status->getHTML() );
}
return $status;
}
/**
* @deprecated DO NOT CALL ME.
* This method was introduced when factoring (Importable)UploadRevisionImporter out of
* WikiRevision. It only has 1 use by the deprecated downloadSource method in WikiRevision.
* Do not use this in new code, it will be made private soon.
*
* @param ImportableUploadRevision $wikiRevision
*
* @return bool|string
*/
public function downloadSource( ImportableUploadRevision $wikiRevision ) {
if ( !$this->enableUploads ) {
return false;
}
$tempo = tempnam( wfTempDir(), 'download' );
$f = fopen( $tempo, 'wb' );
if ( !$f ) {
$this->logger->debug( "IMPORT: couldn't write to temp file $tempo" );
return false;
}
// @todo FIXME!
$src = $wikiRevision->getSrc();
$data = MediaWikiServices::getInstance()->getHttpRequestFactory()->
get( $src, [], __METHOD__ );
if ( !$data ) {
$this->logger->debug( "IMPORT: couldn't fetch source $src" );
fclose( $f );
unlink( $tempo );
return false;
}
fwrite( $f, $data );
fclose( $f );
return $tempo;
}
}
|