MediaWiki 1.41.2
ImportableUploadRevisionImporter.php
Go to the documentation of this file.
1<?php
2
5use Psr\Log\LoggerInterface;
6
11
15 private $logger;
16
20 private $enableUploads;
21
25 private $shouldCreateNullRevision = true;
26
31 public function __construct(
32 $enableUploads,
33 LoggerInterface $logger
34 ) {
35 $this->enableUploads = $enableUploads;
36 $this->logger = $logger;
37 }
38
45 public function setNullRevisionCreation( $shouldCreateNullRevision ) {
46 $this->shouldCreateNullRevision = $shouldCreateNullRevision;
47 }
48
52 private function newNotOkStatus() {
53 $statusValue = new StatusValue();
54 $statusValue->setOK( false );
55 return $statusValue;
56 }
57
59 public function import( ImportableUploadRevision $importableRevision ) {
60 # Construct a file
61 $archiveName = $importableRevision->getArchiveName();
62 $localRepo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo();
63 if ( $archiveName ) {
64 $this->logger->debug( __METHOD__ . ": Importing archived file as $archiveName" );
65 $file = OldLocalFile::newFromArchiveName( $importableRevision->getTitle(),
66 $localRepo, $archiveName );
67 } else {
68 $file = $localRepo->newFile( $importableRevision->getTitle() );
69 $file->load( File::READ_LATEST );
70 $this->logger->debug( __METHOD__ . ': Importing new file as ' . $file->getName() );
71 if ( $file->exists() && $file->getTimestamp() > $importableRevision->getTimestamp() ) {
72 $archiveName = $importableRevision->getTimestamp() . '!' . $file->getName();
73 $file = OldLocalFile::newFromArchiveName( $importableRevision->getTitle(),
74 $localRepo, $archiveName );
75 $this->logger->debug( __METHOD__ . ": File already exists; importing as $archiveName" );
76 }
77 }
78 if ( !$file ) {
79 $this->logger->debug( __METHOD__ . ': Bad file for ' . $importableRevision->getTitle() );
80 return $this->newNotOkStatus();
81 }
82
83 # Get the file source or download if necessary
84 $source = $importableRevision->getFileSrc();
85 $autoDeleteSource = $importableRevision->isTempSrc();
86 if ( !strlen( $source ) ) {
87 $source = $this->downloadSource( $importableRevision );
88 $autoDeleteSource = true;
89 }
90 if ( !strlen( $source ) ) {
91 $this->logger->debug( __METHOD__ . ": Could not fetch remote file." );
92 return $this->newNotOkStatus();
93 }
94
95 $tmpFile = new TempFSFile( $source );
96 if ( $autoDeleteSource ) {
97 $tmpFile->autocollect();
98 }
99
100 $sha1File = ltrim( sha1_file( $source ), '0' );
101 $sha1 = $importableRevision->getSha1();
102 if ( $sha1 && ( $sha1 !== $sha1File ) ) {
103 $this->logger->debug( __METHOD__ . ": Corrupt file $source." );
104 return $this->newNotOkStatus();
105 }
106
107 $user = $importableRevision->getUserObj()
108 ?: User::newFromName( $importableRevision->getUser(), false );
109
110 # Do the actual upload
111 if ( $file instanceof OldLocalFile ) {
112 $status = $file->uploadOld(
113 $source,
114 $importableRevision->getTimestamp(),
115 $importableRevision->getComment(),
116 $user
117 );
118 } else {
119 $flags = 0;
120 $status = $file->upload(
121 $source,
122 $importableRevision->getComment(),
123 $importableRevision->getComment(),
124 $flags,
125 false,
126 $importableRevision->getTimestamp(),
127 $user,
128 [],
129 $this->shouldCreateNullRevision
130 );
131 }
132
133 if ( $status->isGood() ) {
134 $this->logger->debug( __METHOD__ . ": Successful" );
135 } else {
136 $this->logger->debug( __METHOD__ . ': failed: ' . $status->getHTML() );
137 }
138
139 return $status;
140 }
141
152 public function downloadSource( ImportableUploadRevision $wikiRevision ) {
153 if ( !$this->enableUploads ) {
154 return false;
155 }
156
157 $tempo = tempnam( wfTempDir(), 'download' );
158 $f = fopen( $tempo, 'wb' );
159 if ( !$f ) {
160 $this->logger->debug( "IMPORT: couldn't write to temp file $tempo" );
161 return false;
162 }
163
164 // @todo FIXME!
165 $src = $wikiRevision->getSrc();
166 $data = MediaWikiServices::getInstance()->getHttpRequestFactory()->
167 get( $src, [], __METHOD__ );
168 if ( !$data ) {
169 $this->logger->debug( "IMPORT: couldn't fetch source $src" );
170 fclose( $f );
171 unlink( $tempo );
172 return false;
173 }
174
175 fwrite( $f, $data );
176 fclose( $f );
177
178 return $tempo;
179 }
180
181}
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)
Service locator for MediaWiki core services.
internal since 1.36
Definition User.php:98
Old file in the oldimage table.
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...
$source
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42