From 79fceeff338d262828cc977839b171999da0a7eb Mon Sep 17 00:00:00 2001 From: Robin McCorkell Date: Tue, 1 Sep 2015 14:09:15 +0100 Subject: Move maintenance:mimetypesjs to sublocation, cleanup code --- core/command/maintenance/mimetype/updatejs.php | 124 +++++++++++++++++++++++++ core/command/maintenance/mimetypesjs.php | 117 ----------------------- core/register_command.php | 2 +- 3 files changed, 125 insertions(+), 118 deletions(-) create mode 100644 core/command/maintenance/mimetype/updatejs.php delete mode 100644 core/command/maintenance/mimetypesjs.php (limited to 'core') diff --git a/core/command/maintenance/mimetype/updatejs.php b/core/command/maintenance/mimetype/updatejs.php new file mode 100644 index 00000000000..5de75d53a3f --- /dev/null +++ b/core/command/maintenance/mimetype/updatejs.php @@ -0,0 +1,124 @@ + + * + */ + +namespace OC\Core\Command\Maintenance\Mimetype; + +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +use OCP\Files\IMimeTypeDetector; + +class UpdateJS extends Command { + + /** @var IMimeTypeDetector */ + protected $mimetypeDetector; + + public function __construct( + IMimeTypeDetector $mimetypeDetector + ) { + parent::__construct(); + $this->mimetypeDetector = $mimetypeDetector; + } + + protected function configure() { + $this + ->setName('maintenance:mimetype:update-js') + ->setDescription('Update mimetypelist.js'); + } + + protected function execute(InputInterface $input, OutputInterface $output) { + // Fetch all the aliases + $aliases = $this->mimetypeDetector->getAllAliases(); + + // Remove comments + $keys = array_filter(array_keys($aliases), function($k) { + return $k[0] === '_'; + }); + foreach($keys as $key) { + unset($aliases[$key]); + } + + // Fetch all files + $dir = new \DirectoryIterator(\OC::$SERVERROOT.'/core/img/filetypes'); + + $files = []; + foreach($dir as $fileInfo) { + if ($fileInfo->isFile()) { + $file = preg_replace('/.[^.]*$/', '', $fileInfo->getFilename()); + $files[] = $file; + } + } + + //Remove duplicates + $files = array_values(array_unique($files)); + + // Fetch all themes! + $themes = []; + $dirs = new \DirectoryIterator(\OC::$SERVERROOT.'/themes/'); + foreach($dirs as $dir) { + //Valid theme dir + if ($dir->isFile() || $dir->isDot()) { + continue; + } + + $theme = $dir->getFilename(); + $themeDir = $dir->getPath() . '/' . $theme . '/core/img/filetypes/'; + // Check if this theme has its own filetype icons + if (!file_exists($themeDir)) { + continue; + } + + $themes[$theme] = []; + // Fetch all the theme icons! + $themeIt = new \DirectoryIterator($themeDir); + foreach ($themeIt as $fileInfo) { + if ($fileInfo->isFile()) { + $file = preg_replace('/.[^.]*$/', '', $fileInfo->getFilename()); + $themes[$theme][] = $file; + } + } + + //Remove Duplicates + $themes[$theme] = array_values(array_unique($themes[$theme])); + } + + //Generate the JS + $js = '/** +* This file is automatically generated +* DO NOT EDIT MANUALLY! +* +* You can update the list of MimeType Aliases in config/mimetypealiases.json +* The list of files is fetched from core/img/filetypes +* To regenerate this file run ./occ maintenance:mimetypesjs +*/ +OC.MimeTypeList={ + aliases: ' . json_encode($aliases, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . ', + files: ' . json_encode($files, JSON_PRETTY_PRINT) . ', + themes: ' . json_encode($themes, JSON_PRETTY_PRINT) . ' +}; +'; + + //Output the JS + file_put_contents(\OC::$SERVERROOT.'/core/js/mimetypelist.js', $js); + + $output->writeln('mimetypelist.js is updated'); + } +} diff --git a/core/command/maintenance/mimetypesjs.php b/core/command/maintenance/mimetypesjs.php deleted file mode 100644 index 8b01f0acf79..00000000000 --- a/core/command/maintenance/mimetypesjs.php +++ /dev/null @@ -1,117 +0,0 @@ - - * - */ - -namespace OC\Core\Command\Maintenance; - -use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Output\OutputInterface; - -class MimeTypesJS extends Command { - protected function configure() { - $this - ->setName('maintenance:mimetypesjs') - ->setDescription('Update mimetypelist.js'); - } - - protected function execute(InputInterface $input, OutputInterface $output) { - // Fetch all the aliases - $aliases = json_decode(file_get_contents(\OC::$SERVERROOT . '/config/mimetypealiases.dist.json'), true); - - if (file_exists(\OC::$SERVERROOT . '/config/mimetypealiases.json')) { - $custom = get_object_vars(json_decode(file_get_contents(\OC::$SERVERROOT . '/config/mimetypealiases.json'))); - $aliases = array_merge($aliases, $custom); - } - - // Remove comments - $keys = array_filter(array_keys($aliases), function($k) { - return $k[0] === '_'; - }); - foreach($keys as $key) { - unset($aliases[$key]); - } - - // Fetch all files - $dir = new \DirectoryIterator(dirname(__DIR__) . '/../img/filetypes'); - - $files = []; - foreach($dir as $fileInfo) { - if ($fileInfo->isFile()) { - $file = preg_replace('/.[^.]*$/', '', $fileInfo->getFilename()); - $files[] = $file; - } - } - - //Remove duplicates - $files = array_values(array_unique($files)); - - - // Fetch all themes! - $themes = []; - $dirs = new \DirectoryIterator(dirname(__DIR__) . '/../../themes/'); - foreach($dirs as $dir) { - //Valid theme dir - if ($dir->isFile() || $dir->isDot()) { - continue; - } - - $theme = $dir->getFilename(); - $themeDir = $dir->getPath() . '/' . $theme . '/core/img/filetypes/'; - // Check if this theme has its own filetype icons - if (!file_exists($themeDir)) { - continue; - } - - $themes[$theme] = []; - // Fetch all the theme icons! - $themeIt = new \DirectoryIterator($themeDir); - foreach ($themeIt as $fileInfo) { - if ($fileInfo->isFile()) { - $file = preg_replace('/.[^.]*$/', '', $fileInfo->getFilename()); - $themes[$theme][] = $file; - } - } - - //Remove Duplicates - $themes[$theme] = array_values(array_unique($themes[$theme])); - } - - //Generate the JS - $js = '/** -* This file is automatically generated -* DO NOT EDIT MANUALLY! -* -* You can update the list of MimeType Aliases in config/mimetypealiases.json -* The list of files is fetched from core/img/filetypes -* To regenerate this file run ./occ maintenance:mimetypesjs -*/ -OC.MimeTypeList={ - aliases: ' . json_encode($aliases, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . ', - files: ' . json_encode($files, JSON_PRETTY_PRINT) . ', - themes: ' . json_encode($themes, JSON_PRETTY_PRINT) . ' -}; -'; - - //Output the JS - file_put_contents(dirname(__DIR__) . '/../js/mimetypelist.js', $js); - - $output->writeln('mimetypelist.js is updated'); - } -} diff --git a/core/register_command.php b/core/register_command.php index 72c7b28e9ae..b79c69c8fd5 100644 --- a/core/register_command.php +++ b/core/register_command.php @@ -79,10 +79,10 @@ if (\OC::$server->getConfig()->getSystemValue('installed', false)) { ); $application->add(new OC\Core\Command\Encryption\ShowKeyStorageRoot($util)); - $application->add(new OC\Core\Command\Maintenance\MimeTypesJS()); $application->add(new OC\Core\Command\Maintenance\Mode(\OC::$server->getConfig())); $application->add(new OC\Core\Command\Maintenance\Repair(new \OC\Repair(\OC\Repair::getRepairSteps()), \OC::$server->getConfig())); $application->add(new OC\Core\Command\Maintenance\SingleUser(\OC::$server->getConfig())); + $application->add(new OC\Core\Command\Maintenance\Mimetype\UpdateJS(\OC::$server->getMimeTypeDetector())); $application->add(new OC\Core\Command\Upgrade(\OC::$server->getConfig())); -- cgit v1.2.3 From 19830e6c2455dc67ba0d8f463b46166aa9713064 Mon Sep 17 00:00:00 2001 From: Robin McCorkell Date: Tue, 1 Sep 2015 14:12:52 +0100 Subject: Introduce mimetype DB update occ command --- core/command/maintenance/mimetype/updatedb.php | 96 ++++++++++++++++++++++++++ core/register_command.php | 3 +- 2 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 core/command/maintenance/mimetype/updatedb.php (limited to 'core') diff --git a/core/command/maintenance/mimetype/updatedb.php b/core/command/maintenance/mimetype/updatedb.php new file mode 100644 index 00000000000..37c401c0338 --- /dev/null +++ b/core/command/maintenance/mimetype/updatedb.php @@ -0,0 +1,96 @@ + + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + */ + +namespace OC\Core\Command\Maintenance\Mimetype; + +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Input\InputOption; + +use OCP\Files\IMimeTypeDetector; +use OCP\Files\IMimeTypeLoader; + +class UpdateDB extends Command { + + const DEFAULT_MIMETYPE = 'application/octet-stream'; + + /** @var IMimeTypeDetector */ + protected $mimetypeDetector; + + /** @var IMimeTypeLoader */ + protected $mimetypeLoader; + + public function __construct( + IMimeTypeDetector $mimetypeDetector, + IMimeTypeLoader $mimetypeLoader + ) { + parent::__construct(); + $this->mimetypeDetector = $mimetypeDetector; + $this->mimetypeLoader = $mimetypeLoader; + } + + protected function configure() { + $this + ->setName('maintenance:mimetype:update-db') + ->setDescription('Update database mimetypes and update filecache') + ->addOption( + 'repair-filecache', + null, + InputOption::VALUE_NONE, + 'Repair filecache for all mimetypes, not just new ones' + ) + ; + } + + protected function execute(InputInterface $input, OutputInterface $output) { + $mappings = $this->mimetypeDetector->getAllMappings(); + + $totalFilecacheUpdates = 0; + $totalNewMimetypes = 0; + + foreach ($mappings as $ext => $mimetypes) { + if ($ext[0] === '_') { + // comment + continue; + } + $mimetype = $mimetypes[0]; + $existing = $this->mimetypeLoader->exists($mimetype); + // this will add the mimetype if it didn't exist + $mimetypeId = $this->mimetypeLoader->getId($mimetype); + + if (!$existing) { + $output->writeln('Added mimetype "'.$mimetype.'" to database'); + $totalNewMimetypes++; + } + + if (!$existing || $input->getOption('repair-filecache')) { + $touchedFilecacheRows = $this->mimetypeLoader->updateFilecache($ext, $mimetypeId); + if ($touchedFilecacheRows > 0) { + $output->writeln('Updated '.$touchedFilecacheRows.' filecache rows for mimetype "'.$mimetype.'"'); + } + $totalFilecacheUpdates += $touchedFilecacheRows; + } + } + + $output->writeln('Added '.$totalNewMimetypes.' new mimetypes'); + $output->writeln('Updated '.$totalFilecacheUpdates.' filecache rows'); + } +} diff --git a/core/register_command.php b/core/register_command.php index b79c69c8fd5..d3c04ad0671 100644 --- a/core/register_command.php +++ b/core/register_command.php @@ -79,10 +79,11 @@ if (\OC::$server->getConfig()->getSystemValue('installed', false)) { ); $application->add(new OC\Core\Command\Encryption\ShowKeyStorageRoot($util)); + $application->add(new OC\Core\Command\Maintenance\Mimetype\UpdateDB(\OC::$server->getMimeTypeDetector(), \OC::$server->getMimeTypeLoader())); + $application->add(new OC\Core\Command\Maintenance\Mimetype\UpdateJS(\OC::$server->getMimeTypeDetector())); $application->add(new OC\Core\Command\Maintenance\Mode(\OC::$server->getConfig())); $application->add(new OC\Core\Command\Maintenance\Repair(new \OC\Repair(\OC\Repair::getRepairSteps()), \OC::$server->getConfig())); $application->add(new OC\Core\Command\Maintenance\SingleUser(\OC::$server->getConfig())); - $application->add(new OC\Core\Command\Maintenance\Mimetype\UpdateJS(\OC::$server->getMimeTypeDetector())); $application->add(new OC\Core\Command\Upgrade(\OC::$server->getConfig())); -- cgit v1.2.3