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