3 use Psr\Log\LoggerInterface
;
8 class ImportableUploadRevisionImporter
implements UploadRevisionImporter
{
11 * @var LoggerInterface
18 private $enableUploads;
21 * @param bool $enableUploads
22 * @param LoggerInterface $logger
24 public function __construct(
26 LoggerInterface
$logger
28 $this->enableUploads
= $enableUploads;
29 $this->logger
= $logger;
35 private function newNotOkStatus() {
36 $statusValue = new StatusValue();
37 $statusValue->setOK( false );
41 public function import( ImportableUploadRevision
$importableRevision ) {
43 $archiveName = $importableRevision->getArchiveName();
45 $this->logger
->debug( __METHOD__
. "Importing archived file as $archiveName\n" );
46 $file = OldLocalFile
::newFromArchiveName( $importableRevision->getTitle(),
47 RepoGroup
::singleton()->getLocalRepo(), $archiveName );
49 $file = wfLocalFile( $importableRevision->getTitle() );
50 $file->load( File
::READ_LATEST
);
51 $this->logger
->debug( __METHOD__
. 'Importing new file as ' . $file->getName() . "\n" );
52 if ( $file->exists() && $file->getTimestamp() > $importableRevision->getTimestamp() ) {
53 $archiveName = $file->getTimestamp() . '!' . $file->getName();
54 $file = OldLocalFile
::newFromArchiveName( $importableRevision->getTitle(),
55 RepoGroup
::singleton()->getLocalRepo(), $archiveName );
56 $this->logger
->debug( __METHOD__
. "File already exists; importing as $archiveName\n" );
60 $this->logger
->debug( __METHOD__
. ': Bad file for ' . $importableRevision->getTitle() . "\n" );
61 return $this->newNotOkStatus();
64 # Get the file source or download if necessary
65 $source = $importableRevision->getFileSrc();
66 $autoDeleteSource = $importableRevision->isTempSrc();
67 if ( !strlen( $source ) ) {
68 $source = $this->downloadSource( $importableRevision );
69 $autoDeleteSource = true;
71 if ( !strlen( $source ) ) {
72 $this->logger
->debug( __METHOD__
. ": Could not fetch remote file.\n" );
73 return $this->newNotOkStatus();
76 $tmpFile = new TempFSFile( $source );
77 if ( $autoDeleteSource ) {
78 $tmpFile->autocollect();
81 $sha1File = ltrim( sha1_file( $source ), '0' );
82 $sha1 = $importableRevision->getSha1();
83 if ( $sha1 && ( $sha1 !== $sha1File ) ) {
84 $this->logger
->debug( __METHOD__
. ": Corrupt file $source.\n" );
85 return $this->newNotOkStatus();
88 $user = $importableRevision->getUserObj() ?
: User
::newFromName( $importableRevision->getUser() );
90 # Do the actual upload
92 $status = $file->uploadOld( $source, $archiveName,
93 $importableRevision->getTimestamp(), $importableRevision->getComment(), $user );
96 $status = $file->upload(
98 $importableRevision->getComment(),
99 $importableRevision->getComment(),
102 $importableRevision->getTimestamp(),
107 if ( $status->isGood() ) {
108 $this->logger
->debug( __METHOD__
. ": Successful\n" );
110 $this->logger
->debug( __METHOD__
. ': failed: ' . $status->getHTML() . "\n" );
117 * @deprecated DO NOT CALL ME.
118 * This method was introduced when factoring UploadImporter out of WikiRevision.
119 * It only has 1 use by the deprecated downloadSource method in WikiRevision.
120 * Do not use this in new code.
122 * @param ImportableUploadRevision $wikiRevision
124 * @return bool|string
126 public function downloadSource( ImportableUploadRevision
$wikiRevision ) {
127 if ( !$this->enableUploads
) {
131 $tempo = tempnam( wfTempDir(), 'download' );
132 $f = fopen( $tempo, 'wb' );
134 $this->logger
->debug( "IMPORT: couldn't write to temp file $tempo\n" );
139 $src = $wikiRevision->getSrc();
140 $data = Http
::get( $src, [], __METHOD__
);
142 $this->logger
->debug( "IMPORT: couldn't fetch source $src\n" );