UpdateDB.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Robin McCorkell <robin@mccorkell.me.uk>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\Core\Command\Maintenance\Mimetype;
  26. use OCP\Files\IMimeTypeDetector;
  27. use OCP\Files\IMimeTypeLoader;
  28. use Symfony\Component\Console\Command\Command;
  29. use Symfony\Component\Console\Input\InputInterface;
  30. use Symfony\Component\Console\Input\InputOption;
  31. use Symfony\Component\Console\Output\OutputInterface;
  32. class UpdateDB extends Command {
  33. public const DEFAULT_MIMETYPE = 'application/octet-stream';
  34. protected IMimeTypeDetector $mimetypeDetector;
  35. protected IMimeTypeLoader $mimetypeLoader;
  36. public function __construct(
  37. IMimeTypeDetector $mimetypeDetector,
  38. IMimeTypeLoader $mimetypeLoader
  39. ) {
  40. parent::__construct();
  41. $this->mimetypeDetector = $mimetypeDetector;
  42. $this->mimetypeLoader = $mimetypeLoader;
  43. }
  44. protected function configure() {
  45. $this
  46. ->setName('maintenance:mimetype:update-db')
  47. ->setDescription('Update database mimetypes and update filecache')
  48. ->addOption(
  49. 'repair-filecache',
  50. null,
  51. InputOption::VALUE_NONE,
  52. 'Repair filecache for all mimetypes, not just new ones'
  53. )
  54. ;
  55. }
  56. protected function execute(InputInterface $input, OutputInterface $output): int {
  57. $mappings = $this->mimetypeDetector->getAllMappings();
  58. $totalFilecacheUpdates = 0;
  59. $totalNewMimetypes = 0;
  60. foreach ($mappings as $ext => $mimetypes) {
  61. if ($ext[0] === '_') {
  62. // comment
  63. continue;
  64. }
  65. $mimetype = $mimetypes[0];
  66. $existing = $this->mimetypeLoader->exists($mimetype);
  67. // this will add the mimetype if it didn't exist
  68. $mimetypeId = $this->mimetypeLoader->getId($mimetype);
  69. if (!$existing) {
  70. $output->writeln('Added mimetype "'.$mimetype.'" to database');
  71. $totalNewMimetypes++;
  72. }
  73. if (!$existing || $input->getOption('repair-filecache')) {
  74. $touchedFilecacheRows = $this->mimetypeLoader->updateFilecache($ext, $mimetypeId);
  75. if ($touchedFilecacheRows > 0) {
  76. $output->writeln('Updated '.$touchedFilecacheRows.' filecache rows for mimetype "'.$mimetype.'"');
  77. }
  78. $totalFilecacheUpdates += $touchedFilecacheRows;
  79. }
  80. }
  81. $output->writeln('Added '.$totalNewMimetypes.' new mimetypes');
  82. $output->writeln('Updated '.$totalFilecacheUpdates.' filecache rows');
  83. return 0;
  84. }
  85. }