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