MediaWiki master
ImportableUploadRevisionImporter.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Import;
4
8use Psr\Log\LoggerInterface;
12
17
18 private bool $enableUploads;
19 private LoggerInterface $logger;
20
21 private bool $shouldCreateNullRevision = true;
22
23 public function __construct(
24 bool $enableUploads,
25 LoggerInterface $logger
26 ) {
27 $this->enableUploads = $enableUploads;
28 $this->logger = $logger;
29 }
30
37 public function setNullRevisionCreation( $shouldCreateNullRevision ) {
38 $this->shouldCreateNullRevision = $shouldCreateNullRevision;
39 }
40
44 private function newNotOkStatus() {
45 $statusValue = new StatusValue();
46 $statusValue->setOK( false );
47 return $statusValue;
48 }
49
51 public function import( ImportableUploadRevision $importableRevision ) {
52 # Construct a file
53 $archiveName = $importableRevision->getArchiveName();
54 $localRepo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo();
55 if ( $archiveName ) {
56 $this->logger->debug( __METHOD__ . ": Importing archived file as $archiveName" );
57 $file = OldLocalFile::newFromArchiveName( $importableRevision->getTitle(),
58 $localRepo, $archiveName );
59 } else {
60 $file = $localRepo->newFile( $importableRevision->getTitle() );
61 $file->load( IDBAccessObject::READ_LATEST );
62 $this->logger->debug( __METHOD__ . ': Importing new file as ' . $file->getName() );
63 if ( $file->exists() && $file->getTimestamp() > $importableRevision->getTimestamp() ) {
64 $archiveName = $importableRevision->getTimestamp() . '!' . $file->getName();
65 $file = OldLocalFile::newFromArchiveName( $importableRevision->getTitle(),
66 $localRepo, $archiveName );
67 $this->logger->debug( __METHOD__ . ": File already exists; importing as $archiveName" );
68 }
69 }
70 if ( !$file ) {
71 $this->logger->debug( __METHOD__ . ': Bad file for ' . $importableRevision->getTitle() );
72 return $this->newNotOkStatus();
73 }
74
75 # Get the file source or download if necessary
76 $source = $importableRevision->getFileSrc();
77 $autoDeleteSource = $importableRevision->isTempSrc();
78 if ( $source === '' ) {
79 $source = $this->downloadSource( $importableRevision );
80 $autoDeleteSource = true;
81 }
82 if ( $source === '' ) {
83 $this->logger->debug( __METHOD__ . ": Could not fetch remote file." );
84 return $this->newNotOkStatus();
85 }
86
87 $tmpFile = new TempFSFile( $source );
88 if ( $autoDeleteSource ) {
89 $tmpFile->autocollect();
90 }
91
92 $sha1File = ltrim( sha1_file( $source ), '0' );
93 $sha1 = $importableRevision->getSha1();
94 if ( $sha1 && ( $sha1 !== $sha1File ) ) {
95 $this->logger->debug( __METHOD__ . ": Corrupt file $source." );
96 return $this->newNotOkStatus();
97 }
98
99 $user = $importableRevision->getUserObj()
100 ?: User::newFromName( $importableRevision->getUser(), false );
101
102 # Do the actual upload
103 if ( $file instanceof OldLocalFile ) {
104 $status = $file->uploadOld(
105 $source,
106 $importableRevision->getTimestamp(),
107 $importableRevision->getComment(),
108 $user
109 );
110 } else {
111 $flags = 0;
112 $status = $file->upload(
113 $source,
114 $importableRevision->getComment(),
115 $importableRevision->getComment(),
116 $flags,
117 false,
118 $importableRevision->getTimestamp(),
119 $user,
120 [],
121 $this->shouldCreateNullRevision
122 );
123 }
124
125 if ( $status->isGood() ) {
126 $this->logger->debug( __METHOD__ . ": Successful" );
127 } else {
128 $this->logger->debug( __METHOD__ . ': failed: ' . $status->getHTML() );
129 }
130
131 return $status;
132 }
133
139 private function downloadSource( ImportableUploadRevision $wikiRevision ) {
140 if ( !$this->enableUploads ) {
141 return false;
142 }
143
144 $tempo = tempnam( wfTempDir(), 'download' );
145 $f = fopen( $tempo, 'wb' );
146 if ( !$f ) {
147 $this->logger->debug( "IMPORT: couldn't write to temp file $tempo" );
148 return false;
149 }
150
151 // @todo FIXME!
152 $src = $wikiRevision->getSrc();
153 $data = MediaWikiServices::getInstance()->getHttpRequestFactory()->
154 get( $src, [], __METHOD__ );
155 if ( !$data ) {
156 $this->logger->debug( "IMPORT: couldn't fetch source $src" );
157 fclose( $f );
158 unlink( $tempo );
159 return false;
160 }
161
162 fwrite( $f, $data );
163 fclose( $f );
164
165 return $tempo;
166 }
167
168}
169
171class_alias( ImportableUploadRevisionImporter::class, 'ImportableUploadRevisionImporter' );
wfTempDir()
Tries to get the system directory for temporary files.
Old file in the oldimage table.
setNullRevisionCreation( $shouldCreateNullRevision)
Setting this to false will deactivate the creation of a dummy revision as part of the upload process ...
__construct(bool $enableUploads, LoggerInterface $logger)
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
User class for the MediaWiki software.
Definition User.php:130
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...
Interface for database access objects.
$source