protected function getFileDuplicate($filepath) { $duplicates = RepoGroup::singleton()->findBySha1(FSFile::getSha1Base36FromPath($filepath)); if (count($duplicates) > 0) { return $duplicates[0]; } return null; }
/** * Helper function -- given a file on the filesystem, find matching * content in the db (and associated articles) and remove them. * * @param string $filePath Path to file on the filesystem * * @return bool */ public function deleteFileByContent($filePath) { $hash = FSFile::getSha1Base36FromPath($filePath); $dupes = RepoGroup::singleton()->findBySha1($hash); $success = true; foreach ($dupes as $dupe) { $success &= $this->deleteFileByTitle($dupe->getTitle()); } return $success; }
/** * Get the base 36 SHA1 of the file * @return string */ public function getTempFileSha1Base36() { return FSFile::getSha1Base36FromPath($this->mTempPath); }
# Check existence $image = wfLocalFile($title); if ($image->exists()) { if (isset($options['overwrite'])) { echo "{$base} exists, overwriting..."; $svar = 'overwritten'; } else { echo "{$base} exists, skipping\n"; $skipped++; continue; } } else { if (isset($options['skip-dupes'])) { $repo = $image->getRepo(); # XXX: we end up calculating this again when actually uploading. that sucks. $sha1 = FSFile::getSha1Base36FromPath($file); $dupes = $repo->findBySha1($sha1); if ($dupes) { echo "{$base} already exists as " . $dupes[0]->getName() . ", skipping\n"; $skipped++; continue; } } echo "Importing {$base}..."; $svar = 'added'; } if (isset($options['source-wiki-url'])) { /* find comment text directly from source wiki, through MW's API */ $real_comment = getFileCommentFromSourceWiki($options['source-wiki-url'], $base); if ($real_comment === false) { $commentText = $comment;
/** * Move or copy a file to a specified location. Returns a FileRepoStatus * object with the archive name in the "value" member on success. * * The archive name should be passed through to recordUpload for database * registration. * * @param string|FSFile $src Local filesystem path or virtual URL to the source image * @param string $dstRel Target relative path * @param int $flags A bitwise combination of: * File::DELETE_SOURCE Delete the source file, i.e. move rather than copy * @param array $options Optional additional parameters * @return FileRepoStatus On success, the value member contains the * archive name, or an empty string if it was a new file. */ function publishTo($src, $dstRel, $flags = 0, array $options = []) { $srcPath = $src instanceof FSFile ? $src->getPath() : $src; $repo = $this->getRepo(); if ($repo->getReadOnlyReason() !== false) { return $this->readOnlyFatalStatus(); } $this->lock(); // begin $archiveName = wfTimestamp(TS_MW) . '!' . $this->getName(); $archiveRel = 'archive/' . $this->getHashPath() . $archiveName; if ($repo->hasSha1Storage()) { $sha1 = $repo->isVirtualUrl($srcPath) ? $repo->getFileSha1($srcPath) : FSFile::getSha1Base36FromPath($srcPath); $dst = $repo->getBackend()->getPathForSHA1($sha1); $status = $repo->quickImport($src, $dst); if ($flags & File::DELETE_SOURCE) { unlink($srcPath); } if ($this->exists()) { $status->value = $archiveName; } } else { $flags = $flags & File::DELETE_SOURCE ? LocalRepo::DELETE_SOURCE : 0; $status = $repo->publish($srcPath, $dstRel, $archiveRel, $flags, $options); if ($status->value == 'new') { $status->value = ''; } else { $status->value = $archiveName; } } $this->unlock(); // done return $status; }
/** * Check for non fatal problems with the file * * @return Array of warnings */ public function checkWarnings() { global $wgLang; $warnings = array(); $localFile = $this->getLocalFile(); $filename = $localFile->getName(); /** * Check whether the resulting filename is different from the desired one, * but ignore things like ucfirst() and spaces/underscore things */ $comparableName = str_replace(' ', '_', $this->mDesiredDestName); $comparableName = Title::capitalize($comparableName, NS_FILE); if ($this->mDesiredDestName != $filename && $comparableName != $filename) { $warnings['badfilename'] = $filename; } // Check whether the file extension is on the unwanted list global $wgCheckFileExtensions, $wgFileExtensions; if ($wgCheckFileExtensions) { if (!$this->checkFileExtension($this->mFinalExtension, $wgFileExtensions)) { $warnings['filetype-unwanted-type'] = array($this->mFinalExtension, $wgLang->commaList($wgFileExtensions), count($wgFileExtensions)); } } global $wgUploadSizeWarning; if ($wgUploadSizeWarning && $this->mFileSize > $wgUploadSizeWarning) { $warnings['large-file'] = $wgUploadSizeWarning; } if ($this->mFileSize == 0) { $warnings['emptyfile'] = true; } $exists = self::getExistsWarning($localFile); if ($exists !== false) { $warnings['exists'] = $exists; } // Check dupes against existing files $hash = FSFile::getSha1Base36FromPath($this->mTempPath); $dupes = RepoGroup::singleton()->findBySha1($hash); $title = $this->getTitle(); // Remove all matches against self foreach ($dupes as $key => $dupe) { if ($title->equals($dupe->getTitle())) { unset($dupes[$key]); } } if ($dupes) { $warnings['duplicate'] = $dupes; } // Check dupes against archives $archivedImage = new ArchivedFile(null, 0, "{$hash}.{$this->mFinalExtension}"); if ($archivedImage->getID() > 0) { $warnings['duplicate-archive'] = $archivedImage->getName(); } return $warnings; }