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 * @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 = $importableRevision->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()
89 ?: User::newFromName( $importableRevision->getUser(), false );
90
91 # Do the actual upload
92 if ( $archiveName ) {
93 $status = $file->uploadOld( $source, $archiveName,
94 $importableRevision->getTimestamp(), $importableRevision->getComment(), $user );
95 } else {
96 $flags = 0;
97 $status = $file->upload(
98 $source,
99 $importableRevision->getComment(),
100 $importableRevision->getComment(),
101 $flags,
102 false,
103 $importableRevision->getTimestamp(),
104 $user
105 );
106 }
107
108 if ( $status->isGood() ) {
109 $this->logger->debug( __METHOD__ . ": Successful\n" );
110 } else {
111 $this->logger->debug( __METHOD__ . ': failed: ' . $status->getHTML() . "\n" );
112 }
113
114 return $status;
115 }
116
117 /**
118 * @deprecated DO NOT CALL ME.
119 * This method was introduced when factoring UploadImporter out of WikiRevision.
120 * It only has 1 use by the deprecated downloadSource method in WikiRevision.
121 * Do not use this in new code.
122 *
123 * @param ImportableUploadRevision $wikiRevision
124 *
125 * @return bool|string
126 */
127 public function downloadSource( ImportableUploadRevision $wikiRevision ) {
128 if ( !$this->enableUploads ) {
129 return false;
130 }
131
132 $tempo = tempnam( wfTempDir(), 'download' );
133 $f = fopen( $tempo, 'wb' );
134 if ( !$f ) {
135 $this->logger->debug( "IMPORT: couldn't write to temp file $tempo\n" );
136 return false;
137 }
138
139 // @todo FIXME!
140 $src = $wikiRevision->getSrc();
141 $data = Http::get( $src, [], __METHOD__ );
142 if ( !$data ) {
143 $this->logger->debug( "IMPORT: couldn't fetch source $src\n" );
144 fclose( $f );
145 unlink( $tempo );
146 return false;
147 }
148
149 fwrite( $f, $data );
150 fclose( $f );
151
152 return $tempo;
153 }
154
155 }