4 use Psr\Log\LoggerInterface;
19 private $enableUploads;
24 private $shouldCreateNullRevision =
true;
32 LoggerInterface $logger
34 $this->enableUploads = $enableUploads;
35 $this->logger = $logger;
45 $this->shouldCreateNullRevision = $shouldCreateNullRevision;
51 private function newNotOkStatus() {
53 $statusValue->setOK(
false );
60 $archiveName = $importableRevision->getArchiveName();
61 $localRepo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo();
63 $this->logger->debug( __METHOD__ .
": Importing archived file as $archiveName" );
65 $localRepo, $archiveName );
67 $file = $localRepo->newFile( $importableRevision->getTitle() );
68 $file->load( File::READ_LATEST );
69 $this->logger->debug( __METHOD__ .
': Importing new file as ' .
$file->getName() );
70 if (
$file->exists() &&
$file->getTimestamp() > $importableRevision->getTimestamp() ) {
71 $archiveName = $importableRevision->getTimestamp() .
'!' .
$file->getName();
73 $localRepo, $archiveName );
74 $this->logger->debug( __METHOD__ .
": File already exists; importing as $archiveName" );
78 $this->logger->debug( __METHOD__ .
': Bad file for ' . $importableRevision->getTitle() );
79 return $this->newNotOkStatus();
82 # Get the file source or download if necessary
83 $source = $importableRevision->getFileSrc();
84 $autoDeleteSource = $importableRevision->isTempSrc();
87 $autoDeleteSource =
true;
90 $this->logger->debug( __METHOD__ .
": Could not fetch remote file." );
91 return $this->newNotOkStatus();
95 if ( $autoDeleteSource ) {
96 $tmpFile->autocollect();
99 $sha1File = ltrim( sha1_file(
$source ),
'0' );
100 $sha1 = $importableRevision->getSha1();
101 if ( $sha1 && ( $sha1 !== $sha1File ) ) {
102 $this->logger->debug( __METHOD__ .
": Corrupt file $source." );
103 return $this->newNotOkStatus();
106 $user = $importableRevision->getUserObj()
109 # Do the actual upload
111 $status =
$file->uploadOld(
113 $importableRevision->getTimestamp(),
114 $importableRevision->getComment(),
119 $status =
$file->upload(
121 $importableRevision->getComment(),
122 $importableRevision->getComment(),
125 $importableRevision->getTimestamp(),
128 $this->shouldCreateNullRevision
132 if ( $status->isGood() ) {
133 $this->logger->debug( __METHOD__ .
": Successful" );
135 $this->logger->debug( __METHOD__ .
': failed: ' . $status->getHTML() );
152 if ( !$this->enableUploads ) {
156 $tempo = tempnam(
wfTempDir(),
'download' );
157 $f = fopen( $tempo,
'wb' );
159 $this->logger->debug(
"IMPORT: couldn't write to temp file $tempo" );
164 $src = $wikiRevision->
getSrc();
165 $data = MediaWikiServices::getInstance()->getHttpRequestFactory()->
166 get( $src, [], __METHOD__ );
168 $this->logger->debug(
"IMPORT: couldn't fetch source $src" );
wfTempDir()
Tries to get the system directory for temporary files.
downloadSource(ImportableUploadRevision $wikiRevision)
setNullRevisionCreation( $shouldCreateNullRevision)
Setting this to false will deactivate the creation of a null revision as part of the upload process l...
__construct( $enableUploads, LoggerInterface $logger)
Old file in the oldimage table.
static newFromArchiveName( $title, $repo, $archiveName)
Generic operation result class Has warning/error list, boolean status and arbitrary value.
This class is used to hold the location and do limited manipulation of files stored temporarily (this...
static newFromName( $name, $validate='valid')
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.