Merge "Don't rely on magic __call in MWNamespaceTest"
[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 * @param bool $enableUploads
22 * @param LoggerInterface $logger
23 */
24 public function __construct(
25 $enableUploads,
26 LoggerInterface $logger
27 ) {
28 $this->enableUploads = $enableUploads;
29 $this->logger = $logger;
30 }
31
32 /**
33 * @return StatusValue
34 */
35 private function newNotOkStatus() {
36 $statusValue = new StatusValue();
37 $statusValue->setOK( false );
38 return $statusValue;
39 }
40
41 public function import( ImportableUploadRevision $importableRevision ) {
42 # Construct a file
43 $archiveName = $importableRevision->getArchiveName();
44 if ( $archiveName ) {
45 $this->logger->debug( __METHOD__ . "Importing archived file as $archiveName\n" );
46 $file = OldLocalFile::newFromArchiveName( $importableRevision->getTitle(),
47 RepoGroup::singleton()->getLocalRepo(), $archiveName );
48 } else {
49 $file = wfLocalFile( $importableRevision->getTitle() );
50 $file->load( File::READ_LATEST );
51 $this->logger->debug( __METHOD__ . 'Importing new file as ' . $file->getName() . "\n" );
52 if ( $file->exists() && $file->getTimestamp() > $importableRevision->getTimestamp() ) {
53 $archiveName = $file->getTimestamp() . '!' . $file->getName();
54 $file = OldLocalFile::newFromArchiveName( $importableRevision->getTitle(),
55 RepoGroup::singleton()->getLocalRepo(), $archiveName );
56 $this->logger->debug( __METHOD__ . "File already exists; importing as $archiveName\n" );
57 }
58 }
59 if ( !$file ) {
60 $this->logger->debug( __METHOD__ . ': Bad file for ' . $importableRevision->getTitle() . "\n" );
61 return $this->newNotOkStatus();
62 }
63
64 # Get the file source or download if necessary
65 $source = $importableRevision->getFileSrc();
66 $autoDeleteSource = $importableRevision->isTempSrc();
67 if ( !strlen( $source ) ) {
68 $source = $this->downloadSource( $importableRevision );
69 $autoDeleteSource = true;
70 }
71 if ( !strlen( $source ) ) {
72 $this->logger->debug( __METHOD__ . ": Could not fetch remote file.\n" );
73 return $this->newNotOkStatus();
74 }
75
76 $tmpFile = new TempFSFile( $source );
77 if ( $autoDeleteSource ) {
78 $tmpFile->autocollect();
79 }
80
81 $sha1File = ltrim( sha1_file( $source ), '0' );
82 $sha1 = $importableRevision->getSha1();
83 if ( $sha1 && ( $sha1 !== $sha1File ) ) {
84 $this->logger->debug( __METHOD__ . ": Corrupt file $source.\n" );
85 return $this->newNotOkStatus();
86 }
87
88 $user = $importableRevision->getUserObj() ?: User::newFromName( $importableRevision->getUser() );
89
90 # Do the actual upload
91 if ( $archiveName ) {
92 $status = $file->uploadOld( $source, $archiveName,
93 $importableRevision->getTimestamp(), $importableRevision->getComment(), $user );
94 } else {
95 $flags = 0;
96 $status = $file->upload(
97 $source,
98 $importableRevision->getComment(),
99 $importableRevision->getComment(),
100 $flags,
101 false,
102 $importableRevision->getTimestamp(),
103 $user
104 );
105 }
106
107 if ( $status->isGood() ) {
108 $this->logger->debug( __METHOD__ . ": Successful\n" );
109 } else {
110 $this->logger->debug( __METHOD__ . ': failed: ' . $status->getHTML() . "\n" );
111 }
112
113 return $status;
114 }
115
116 /**
117 * @deprecated DO NOT CALL ME.
118 * This method was introduced when factoring UploadImporter out of WikiRevision.
119 * It only has 1 use by the deprecated downloadSource method in WikiRevision.
120 * Do not use this in new code.
121 *
122 * @param ImportableUploadRevision $wikiRevision
123 *
124 * @return bool|string
125 */
126 public function downloadSource( ImportableUploadRevision $wikiRevision ) {
127 if ( !$this->enableUploads ) {
128 return false;
129 }
130
131 $tempo = tempnam( wfTempDir(), 'download' );
132 $f = fopen( $tempo, 'wb' );
133 if ( !$f ) {
134 $this->logger->debug( "IMPORT: couldn't write to temp file $tempo\n" );
135 return false;
136 }
137
138 // @todo FIXME!
139 $src = $wikiRevision->getSrc();
140 $data = Http::get( $src, [], __METHOD__ );
141 if ( !$data ) {
142 $this->logger->debug( "IMPORT: couldn't fetch source $src\n" );
143 fclose( $f );
144 unlink( $tempo );
145 return false;
146 }
147
148 fwrite( $f, $data );
149 fclose( $f );
150
151 return $tempo;
152 }
153
154 }