Update ImportableUploadRevisionImporter for interwiki usernames
[lhc/web/wiklou.git] / includes / import / ImportableUploadRevisionImporter.php
1 <?php
2
3 use Psr\Log\LoggerInterface;
4
5 /**
6 * @since 1.31
7 */
8 class ImportableUploadRevisionImporter implements UploadRevisionImporter {
9
10 /**
11 * @var LoggerInterface
12 */
13 private $logger;
14
15 /**
16 * @var bool
17 */
18 private $enableUploads;
19
20 /**
21 * @var bool
22 */
23 private $shouldCreateNullRevision = true;
24
25 /**
26 * @param bool $enableUploads
27 * @param LoggerInterface $logger
28 */
29 public function __construct(
30 $enableUploads,
31 LoggerInterface $logger
32 ) {
33 $this->enableUploads = $enableUploads;
34 $this->logger = $logger;
35 }
36
37 /**
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
40 *
41 * @param bool $shouldCreateNullRevision
42 */
43 public function setNullRevisionCreation( $shouldCreateNullRevision ) {
44 $this->shouldCreateNullRevision = $shouldCreateNullRevision;
45 }
46
47 /**
48 * @return StatusValue
49 */
50 private function newNotOkStatus() {
51 $statusValue = new StatusValue();
52 $statusValue->setOK( false );
53 return $statusValue;
54 }
55
56 public function import( ImportableUploadRevision $importableRevision ) {
57 # Construct a file
58 $archiveName = $importableRevision->getArchiveName();
59 if ( $archiveName ) {
60 $this->logger->debug( __METHOD__ . "Importing archived file as $archiveName\n" );
61 $file = OldLocalFile::newFromArchiveName( $importableRevision->getTitle(),
62 RepoGroup::singleton()->getLocalRepo(), $archiveName );
63 } else {
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" );
72 }
73 }
74 if ( !$file ) {
75 $this->logger->debug( __METHOD__ . ': Bad file for ' . $importableRevision->getTitle() . "\n" );
76 return $this->newNotOkStatus();
77 }
78
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;
85 }
86 if ( !strlen( $source ) ) {
87 $this->logger->debug( __METHOD__ . ": Could not fetch remote file.\n" );
88 return $this->newNotOkStatus();
89 }
90
91 $tmpFile = new TempFSFile( $source );
92 if ( $autoDeleteSource ) {
93 $tmpFile->autocollect();
94 }
95
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();
101 }
102
103 $user = $importableRevision->getUserObj()
104 ?: User::newFromName( $importableRevision->getUser(), false );
105
106 # Do the actual upload
107 if ( $archiveName ) {
108 $status = $file->uploadOld( $source, $archiveName,
109 $importableRevision->getTimestamp(), $importableRevision->getComment(), $user );
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\n" );
127 } else {
128 $this->logger->debug( __METHOD__ . ': failed: ' . $status->getHTML() . "\n" );
129 }
130
131 return $status;
132 }
133
134 /**
135 * @deprecated DO NOT CALL ME.
136 * This method was introduced when factoring UploadImporter out of WikiRevision.
137 * It only has 1 use by the deprecated downloadSource method in WikiRevision.
138 * Do not use this in new code.
139 *
140 * @param ImportableUploadRevision $wikiRevision
141 *
142 * @return bool|string
143 */
144 public function downloadSource( ImportableUploadRevision $wikiRevision ) {
145 if ( !$this->enableUploads ) {
146 return false;
147 }
148
149 $tempo = tempnam( wfTempDir(), 'download' );
150 $f = fopen( $tempo, 'wb' );
151 if ( !$f ) {
152 $this->logger->debug( "IMPORT: couldn't write to temp file $tempo\n" );
153 return false;
154 }
155
156 // @todo FIXME!
157 $src = $wikiRevision->getSrc();
158 $data = Http::get( $src, [], __METHOD__ );
159 if ( !$data ) {
160 $this->logger->debug( "IMPORT: couldn't fetch source $src\n" );
161 fclose( $f );
162 unlink( $tempo );
163 return false;
164 }
165
166 fwrite( $f, $data );
167 fclose( $f );
168
169 return $tempo;
170 }
171
172 }