/** * Validate the user parameters and set $this->archiveName and $this->file. * Throws an error if validation fails */ protected function validateParameters() { // Validate the input title $title = Title::makeTitleSafe(NS_FILE, $this->params['filename']); if (is_null($title)) { $this->dieUsageMsg(array('invalidtitle', $this->params['filename'])); } $localRepo = RepoGroup::singleton()->getLocalRepo(); // Check if the file really exists $this->file = $localRepo->newFile($title); if (!$this->file->exists()) { $this->dieUsageMsg('notanarticle'); } // Check if the archivename is valid for this file $this->archiveName = $this->params['archivename']; $oldFile = $localRepo->newFromArchiveName($title, $this->archiveName); if (!$oldFile->exists()) { $this->dieUsageMsg('filerevert-badversion'); } }
protected function purgeFromArchiveTable(LocalRepo $repo, LocalFile $file) { $dbr = $repo->getSlaveDB(); $res = $dbr->select('filearchive', array('fa_archive_name'), array('fa_name' => $file->getName()), __METHOD__); foreach ($res as $row) { if ($row->fa_archive_name === null) { // Was not an old version (current version names checked already) continue; } $ofile = $repo->newFromArchiveName($file->getTitle(), $row->fa_archive_name); // If there is an orphaned storage file still there...delete it if (!$file->exists() && $repo->fileExists($ofile->getPath())) { $dpath = $this->getDeletedPath($repo, $ofile); if ($repo->fileExists($dpath)) { // Sanity check to avoid data loss $repo->getBackend()->delete(array('src' => $ofile->getPath())); $this->output("Deleted orphan file: {$ofile->getPath()}.\n"); } else { $this->error("File was not deleted: {$ofile->getPath()}.\n"); } } $file->purgeOldThumbnails($row->fa_archive_name); } }
/** * If archive name is an empty string, then file does not "exist" * * This is the case for a couple files on Wikimedia servers where * the old version is "lost". */ public function exists() { $archiveName = $this->getArchiveName(); if ($archiveName === '' || !is_string($archiveName)) { return false; } return parent::exists(); }
protected function insertImage($name, $mwname, $result) { global $wgRequest, $wgImageMagickConvertCommand, $wgServer; if (!$result) { $result = array(); } elseif ($result['error']) { return $result; } $fromPage = $wgRequest->getVal('viapage'); if (!empty($mwname) && !empty($name)) { $name = trim(urldecode($name)); $dateTime = new DateTime(); $mwDate = wfTimestamp(TS_MW); // Mediawiki timestamp: 'YmdHis' list($first, $ext) = self::splitFilenameExt($name); $ext = strtolower($ext); $validExts = array('GIF', 'JPG', 'JPEG', 'PNG'); if (!in_array(strtoupper($ext), $validExts)) { $result['error'] = 'Error: Invalid file extension ' . strtoupper($ext) . '. Valid extensions are:'; foreach ($validExts as $validExt) { $result['error'] .= ' ' . strtoupper($validExt); } $result['error'] .= '.'; return $result; } $saveName = false; $titleExists = false; $suffixNum = 1; while (!$saveName || $titleExists) { $saveName = 'User Completed Image ' . $fromPage . ' ' . $dateTime->format('Y.m.d H.i.s') . ' ' . $suffixNum . '.' . $ext; $title = Title::makeTitleSafe(NS_IMAGE, $saveName); $newFile = true; $titleExists = $title->exists(); $suffixNum++; } $temp_file = new TempLocalImageFile(Title::newFromText($mwname, NS_IMAGE), RepoGroup::singleton()->getLocalRepo()); if (!$temp_file || !$temp_file->exists()) { $result['error'] = 'Error: A server error has occurred. Please try again.'; return $result; } // Image orientation is a bit wonky on some mobile devices; use ImageMagick's auto-orient to try fixing it. $tempFilePath = $temp_file->getPath(); $cmd = $wgImageMagickConvertCommand . ' ' . $tempFilePath . ' -auto-orient ' . $tempFilePath; exec($cmd); // Use a CC license $comment = '{{Self}}'; $file = new LocalFile($title, RepoGroup::singleton()->getLocalRepo()); $file->upload($tempFilePath, $comment, $comment); if (!$file || !$file->exists()) { $result['error'] = 'Error: A server error has occurred. Please try again.'; return $result; } $temp_file->delete(''); $fileTitle = $file->getTitle(); $fileURL = $file->url; $thumbURL = ''; $thumb = $file->getThumbnail(200, -1, true, true); if (!$thumb) { $result['error'] = 'Error: A server error has occurred. Please try again.'; $file->delete(''); return $result; } $thumbURL = $thumb->url; $result['titleText'] = $fileTitle->getText(); $result['titleDBkey'] = substr($fileTitle->getDBkey(), 21); // Only keep important info $result['titlePreText'] = '/' . $fileTitle->getPrefixedText(); $result['titleArtID'] = $fileTitle->getArticleID(); $result['timestamp'] = $mwDate; $result['fromPage'] = $wgRequest->getVal('viapage'); $result['thumbURL'] = $thumbURL; $result['fileURL'] = $wgServer . $fileURL; } return $result; }
protected function deleteImageHelper($imageName) { $title = Title::newFromText($imageName, NS_FILE); $file = new LocalFile($title, RepoGroup::singleton()->getLocalRepo()); $visualization = new CityVisualization(); $visualization->removeImageFromReview($this->wg->cityId, $title->getArticleId(), $this->wg->contLang->getCode()); if ($file->exists()) { $file->delete('no longer needed'); } }