3 use Psr\Log\LoggerInterface
;
8 class ImportableUploadRevisionImporter
implements UploadRevisionImporter
{
11 * @var LoggerInterface
18 private $enableUploads;
23 private $shouldCreateNullRevision = true;
26 * @param bool $enableUploads
27 * @param LoggerInterface $logger
29 public function __construct(
31 LoggerInterface
$logger
33 $this->enableUploads
= $enableUploads;
34 $this->logger
= $logger;
38 * Setting this to false will deactivate the creation of a null revision as part of the upload
39 * process logging in LocalFile::recordUpload2, see T193621
41 * @param bool $shouldCreateNullRevision
43 public function setNullRevisionCreation( $shouldCreateNullRevision ) {
44 $this->shouldCreateNullRevision
= $shouldCreateNullRevision;
50 private function newNotOkStatus() {
51 $statusValue = new StatusValue();
52 $statusValue->setOK( false );
56 public function import( ImportableUploadRevision
$importableRevision ) {
58 $archiveName = $importableRevision->getArchiveName();
60 $this->logger
->debug( __METHOD__
. "Importing archived file as $archiveName\n" );
61 $file = OldLocalFile
::newFromArchiveName( $importableRevision->getTitle(),
62 RepoGroup
::singleton()->getLocalRepo(), $archiveName );
64 $file = wfLocalFile( $importableRevision->getTitle() );
65 $file->load( File
::READ_LATEST
);
66 $this->logger
->debug( __METHOD__
. 'Importing new file as ' . $file->getName() . "\n" );
67 if ( $file->exists() && $file->getTimestamp() > $importableRevision->getTimestamp() ) {
68 $archiveName = $importableRevision->getTimestamp() . '!' . $file->getName();
69 $file = OldLocalFile
::newFromArchiveName( $importableRevision->getTitle(),
70 RepoGroup
::singleton()->getLocalRepo(), $archiveName );
71 $this->logger
->debug( __METHOD__
. "File already exists; importing as $archiveName\n" );
75 $this->logger
->debug( __METHOD__
. ': Bad file for ' . $importableRevision->getTitle() . "\n" );
76 return $this->newNotOkStatus();
79 # Get the file source or download if necessary
80 $source = $importableRevision->getFileSrc();
81 $autoDeleteSource = $importableRevision->isTempSrc();
82 if ( !strlen( $source ) ) {
83 $source = $this->downloadSource( $importableRevision );
84 $autoDeleteSource = true;
86 if ( !strlen( $source ) ) {
87 $this->logger
->debug( __METHOD__
. ": Could not fetch remote file.\n" );
88 return $this->newNotOkStatus();
91 $tmpFile = new TempFSFile( $source );
92 if ( $autoDeleteSource ) {
93 $tmpFile->autocollect();
96 $sha1File = ltrim( sha1_file( $source ), '0' );
97 $sha1 = $importableRevision->getSha1();
98 if ( $sha1 && ( $sha1 !== $sha1File ) ) {
99 $this->logger
->debug( __METHOD__
. ": Corrupt file $source.\n" );
100 return $this->newNotOkStatus();
103 $user = $importableRevision->getUserObj() ?
: User
::newFromName( $importableRevision->getUser() );
105 # Do the actual upload
106 if ( $archiveName ) {
107 $status = $file->uploadOld( $source, $archiveName,
108 $importableRevision->getTimestamp(), $importableRevision->getComment(), $user );
111 $status = $file->upload(
113 $importableRevision->getComment(),
114 $importableRevision->getComment(),
117 $importableRevision->getTimestamp(),
120 $this->shouldCreateNullRevision
124 if ( $status->isGood() ) {
125 $this->logger
->debug( __METHOD__
. ": Successful\n" );
127 $this->logger
->debug( __METHOD__
. ': failed: ' . $status->getHTML() . "\n" );
134 * @deprecated DO NOT CALL ME.
135 * This method was introduced when factoring UploadImporter out of WikiRevision.
136 * It only has 1 use by the deprecated downloadSource method in WikiRevision.
137 * Do not use this in new code.
139 * @param ImportableUploadRevision $wikiRevision
141 * @return bool|string
143 public function downloadSource( ImportableUploadRevision
$wikiRevision ) {
144 if ( !$this->enableUploads
) {
148 $tempo = tempnam( wfTempDir(), 'download' );
149 $f = fopen( $tempo, 'wb' );
151 $this->logger
->debug( "IMPORT: couldn't write to temp file $tempo\n" );
156 $src = $wikiRevision->getSrc();
157 $data = Http
::get( $src, [], __METHOD__
);
159 $this->logger
->debug( "IMPORT: couldn't fetch source $src\n" );