- # Construct a file
- $archiveName = $this->getArchiveName();
- if ( $archiveName ) {
- wfDebug( __METHOD__ . "Importing archived file as $archiveName\n" );
- $file = OldLocalFile::newFromArchiveName( $this->getTitle(),
- RepoGroup::singleton()->getLocalRepo(), $archiveName );
- } else {
- $file = wfLocalFile( $this->getTitle() );
- $file->load( File::READ_LATEST );
- wfDebug( __METHOD__ . 'Importing new file as ' . $file->getName() . "\n" );
- if ( $file->exists() && $file->getTimestamp() > $this->getTimestamp() ) {
- $archiveName = $file->getTimestamp() . '!' . $file->getName();
- $file = OldLocalFile::newFromArchiveName( $this->getTitle(),
- RepoGroup::singleton()->getLocalRepo(), $archiveName );
- wfDebug( __METHOD__ . "File already exists; importing as $archiveName\n" );
- }
- }
- if ( !$file ) {
- wfDebug( __METHOD__ . ': Bad file for ' . $this->getTitle() . "\n" );
- return false;
- }
-
- # Get the file source or download if necessary
- $source = $this->getFileSrc();
- $autoDeleteSource = $this->isTempSrc();
- if ( !strlen( $source ) ) {
- $source = $this->downloadSource();
- $autoDeleteSource = true;
- }
- if ( !strlen( $source ) ) {
- wfDebug( __METHOD__ . ": Could not fetch remote file.\n" );
- return false;
- }
-
- $tmpFile = new TempFSFile( $source );
- if ( $autoDeleteSource ) {
- $tmpFile->autocollect();
- }
-
- $sha1File = ltrim( sha1_file( $source ), '0' );
- $sha1 = $this->getSha1();
- if ( $sha1 && ( $sha1 !== $sha1File ) ) {
- wfDebug( __METHOD__ . ": Corrupt file $source.\n" );
- return false;
- }
-
- $user = $this->getUserObj() ?: User::newFromName( $this->getUser() );
-
- # Do the actual upload
- if ( $archiveName ) {
- $status = $file->uploadOld( $source, $archiveName,
- $this->getTimestamp(), $this->getComment(), $user );
- } else {
- $flags = 0;
- $status = $file->upload( $source, $this->getComment(), $this->getComment(),
- $flags, false, $this->getTimestamp(), $user );
- }
-
- if ( $status->isGood() ) {
- wfDebug( __METHOD__ . ": Successful\n" );
- return true;
- } else {
- wfDebug( __METHOD__ . ': failed: ' . $status->getHTML() . "\n" );
- return false;
- }