Merge pull request #18742 from owncloud/mimetype-updatedb
Introduce mimetype DB update occ commandremotes/origin/db-empty-migrate
commit
24f5f50b20
@ -0,0 +1,96 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Robin McCorkell <rmccorkell@owncloud.com>
|
||||
*
|
||||
* @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 <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
|
||||
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');
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,165 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Robin McCorkell <rmccorkell@owncloud.com>
|
||||
*
|
||||
* @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 <http://www.gnu.org/licenses/>
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OC\Files\Type;
|
||||
|
||||
use OCP\Files\IMimeTypeLoader;
|
||||
use OCP\IDBConnection;
|
||||
|
||||
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
|
||||
|
||||
/**
|
||||
* Mimetype database loader
|
||||
*
|
||||
* @package OC\Files\Type
|
||||
*/
|
||||
class Loader implements IMimeTypeLoader {
|
||||
|
||||
/** @var IDBConnection */
|
||||
private $dbConnection;
|
||||
|
||||
/** @var array [id => mimetype] */
|
||||
protected $mimetypes;
|
||||
|
||||
/** @var array [mimetype => id] */
|
||||
protected $mimetypeIds;
|
||||
|
||||
/**
|
||||
* @param IDBConnection $dbConnection
|
||||
*/
|
||||
public function __construct(IDBConnection $dbConnection) {
|
||||
$this->dbConnection = $dbConnection;
|
||||
$this->mimetypes = [];
|
||||
$this->mimetypeIds = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a mimetype from its ID
|
||||
*
|
||||
* @param int $id
|
||||
* @return string|null
|
||||
*/
|
||||
public function getMimetypeById($id) {
|
||||
if (!$this->mimetypes) {
|
||||
$this->loadMimetypes();
|
||||
}
|
||||
if (isset($this->mimetypes[$id])) {
|
||||
return $this->mimetypes[$id];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a mimetype ID, adding the mimetype to the DB if it does not exist
|
||||
*
|
||||
* @param string $mimetype
|
||||
* @return int
|
||||
*/
|
||||
public function getId($mimetype) {
|
||||
if (!$this->mimetypeIds) {
|
||||
$this->loadMimetypes();
|
||||
}
|
||||
if (isset($this->mimetypeIds[$mimetype])) {
|
||||
return $this->mimetypeIds[$mimetype];
|
||||
}
|
||||
return $this->store($mimetype);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if a mimetype exists in the database
|
||||
*
|
||||
* @param string $mimetype
|
||||
* @return bool
|
||||
*/
|
||||
public function exists($mimetype) {
|
||||
if (!$this->mimetypeIds) {
|
||||
$this->loadMimetypes();
|
||||
}
|
||||
return isset($this->mimetypeIds[$mimetype]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a mimetype in the DB
|
||||
*
|
||||
* @param string $mimetype
|
||||
* @param int inserted ID
|
||||
*/
|
||||
protected function store($mimetype) {
|
||||
try {
|
||||
$qb = $this->dbConnection->getQueryBuilder();
|
||||
$qb->insert('mimetypes')
|
||||
->values([
|
||||
'mimetype' => $qb->createNamedParameter($mimetype)
|
||||
]);
|
||||
$qb->execute();
|
||||
} catch (UniqueConstraintViolationException $e) {
|
||||
// something inserted it before us
|
||||
}
|
||||
|
||||
$fetch = $this->dbConnection->getQueryBuilder();
|
||||
$fetch->select('id')
|
||||
->from('mimetypes')
|
||||
->where(
|
||||
$fetch->expr()->eq('mimetype', $fetch->createNamedParameter($mimetype)
|
||||
));
|
||||
$row = $fetch->execute()->fetch();
|
||||
|
||||
$this->mimetypes[$row['id']] = $mimetype;
|
||||
$this->mimetypeIds[$mimetype] = $row['id'];
|
||||
return $row['id'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Load all mimetypes from DB
|
||||
*/
|
||||
private function loadMimetypes() {
|
||||
$qb = $this->dbConnection->getQueryBuilder();
|
||||
$qb->select('id', 'mimetype')
|
||||
->from('mimetypes');
|
||||
$results = $qb->execute()->fetchAll();
|
||||
|
||||
foreach ($results as $row) {
|
||||
$this->mimetypes[$row['id']] = $row['mimetype'];
|
||||
$this->mimetypeIds[$row['mimetype']] = $row['id'];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update filecache mimetype based on file extension
|
||||
*
|
||||
* @param string $ext file extension
|
||||
* @param int $mimetypeId
|
||||
* @return int number of changed rows
|
||||
*/
|
||||
public function updateFilecache($ext, $mimetypeId) {
|
||||
$update = $this->dbConnection->getQueryBuilder();
|
||||
$update->update('filecache')
|
||||
->set('mimetype', $update->createNamedParameter($mimetypeId))
|
||||
->where($update->expr()->neq(
|
||||
'mimetype', $update->createNamedParameter($mimetypeId)
|
||||
))
|
||||
->andWhere($update->expr()->like(
|
||||
$update->createFunction('LOWER(`name`)'), $update->createNamedParameter($ext)
|
||||
));
|
||||
return $update->execute();
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,59 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Robin McCorkell <rmccorkell@owncloud.com>
|
||||
*
|
||||
* @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 <http://www.gnu.org/licenses/>
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCP\Files;
|
||||
|
||||
/**
|
||||
* Interface IMimeTypeLoader
|
||||
* @package OCP\Files
|
||||
* @since 8.2.0
|
||||
*
|
||||
* Interface to load mimetypes
|
||||
**/
|
||||
interface IMimeTypeLoader {
|
||||
|
||||
/**
|
||||
* Get a mimetype from its ID
|
||||
*
|
||||
* @param int $id
|
||||
* @return string|null
|
||||
* @since 8.2.0
|
||||
*/
|
||||
public function getMimetypeById($id);
|
||||
|
||||
/**
|
||||
* Get a mimetype ID, adding the mimetype to the DB if it does not exist
|
||||
*
|
||||
* @param string $mimetype
|
||||
* @return int
|
||||
* @since 8.2.0
|
||||
*/
|
||||
public function getId($mimetype);
|
||||
|
||||
/**
|
||||
* Test if a mimetype exists in the database
|
||||
*
|
||||
* @param string $mimetype
|
||||
* @return bool
|
||||
* @since 8.2.0
|
||||
*/
|
||||
public function exists($mimetype);
|
||||
}
|
||||
@ -0,0 +1,184 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Robin McCorkell <rmccorkell@owncloud.com>
|
||||
*
|
||||
* @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 <http://www.gnu.org/licenses/>
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Tests\Core\Command\Maintenance\Mimetype;
|
||||
|
||||
use OC\Core\Command\Maintenance\Mimetype\UpdateDB;
|
||||
use Test\TestCase;
|
||||
use OCP\Files\IMimeTypeDetector;
|
||||
use OCP\Files\IMimeTypeLoader;
|
||||
|
||||
class UpdateDBTest extends TestCase {
|
||||
/** @var IMimeTypeDetector */
|
||||
protected $detector;
|
||||
/** @var IMimeTypeLoader */
|
||||
protected $loader;
|
||||
|
||||
/** @var \PHPUnit_Framework_MockObject_MockObject */
|
||||
protected $consoleInput;
|
||||
/** @var \PHPUnit_Framework_MockObject_MockObject */
|
||||
protected $consoleOutput;
|
||||
|
||||
/** @var \Symfony\Component\Console\Command\Command */
|
||||
protected $command;
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->detector = $this->getMockBuilder('OC\Files\Type\Detection')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$this->loader = $this->getMockBuilder('OC\Files\Type\Loader')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$this->consoleInput = $this->getMock('Symfony\Component\Console\Input\InputInterface');
|
||||
$this->consoleOutput = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
|
||||
|
||||
$this->command = new UpdateDB($this->detector, $this->loader);
|
||||
}
|
||||
|
||||
public function testNoop() {
|
||||
$this->consoleInput->method('getOption')
|
||||
->with('repair-filecache')
|
||||
->willReturn(false);
|
||||
|
||||
$this->detector->expects($this->once())
|
||||
->method('getAllMappings')
|
||||
->willReturn([
|
||||
'ext' => ['testing/existingmimetype']
|
||||
]);
|
||||
$this->loader->expects($this->once())
|
||||
->method('exists')
|
||||
->with('testing/existingmimetype')
|
||||
->willReturn(true);
|
||||
|
||||
$this->loader->expects($this->never())
|
||||
->method('updateFilecache');
|
||||
|
||||
$this->consoleOutput->expects($this->at(0))
|
||||
->method('writeln')
|
||||
->with('Added 0 new mimetypes');
|
||||
$this->consoleOutput->expects($this->at(1))
|
||||
->method('writeln')
|
||||
->with('Updated 0 filecache rows');
|
||||
|
||||
self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
|
||||
}
|
||||
|
||||
public function testAddMimetype() {
|
||||
$this->consoleInput->method('getOption')
|
||||
->with('repair-filecache')
|
||||
->willReturn(false);
|
||||
|
||||
$this->detector->expects($this->once())
|
||||
->method('getAllMappings')
|
||||
->willReturn([
|
||||
'ext' => ['testing/existingmimetype'],
|
||||
'new' => ['testing/newmimetype']
|
||||
]);
|
||||
$this->loader->expects($this->exactly(2))
|
||||
->method('exists')
|
||||
->will($this->returnValueMap([
|
||||
['testing/existingmimetype', true],
|
||||
['testing/newmimetype', false],
|
||||
]));
|
||||
$this->loader->expects($this->exactly(2))
|
||||
->method('getId')
|
||||
->will($this->returnValueMap([
|
||||
['testing/existingmimetype', 1],
|
||||
['testing/newmimetype', 2],
|
||||
]));
|
||||
|
||||
$this->loader->expects($this->once())
|
||||
->method('updateFilecache')
|
||||
->with('new', 2)
|
||||
->willReturn(3);
|
||||
|
||||
$this->consoleOutput->expects($this->at(0))
|
||||
->method('writeln')
|
||||
->with('Added mimetype "testing/newmimetype" to database');
|
||||
$this->consoleOutput->expects($this->at(1))
|
||||
->method('writeln')
|
||||
->with('Updated 3 filecache rows for mimetype "testing/newmimetype"');
|
||||
|
||||
$this->consoleOutput->expects($this->at(2))
|
||||
->method('writeln')
|
||||
->with('Added 1 new mimetypes');
|
||||
$this->consoleOutput->expects($this->at(3))
|
||||
->method('writeln')
|
||||
->with('Updated 3 filecache rows');
|
||||
|
||||
self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
|
||||
}
|
||||
|
||||
public function testSkipComments() {
|
||||
$this->detector->expects($this->once())
|
||||
->method('getAllMappings')
|
||||
->willReturn([
|
||||
'_comment' => 'some comment in the JSON'
|
||||
]);
|
||||
$this->loader->expects($this->never())
|
||||
->method('exists');
|
||||
|
||||
self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
|
||||
}
|
||||
|
||||
public function testRepairFilecache() {
|
||||
$this->consoleInput->method('getOption')
|
||||
->with('repair-filecache')
|
||||
->willReturn(true);
|
||||
|
||||
$this->detector->expects($this->once())
|
||||
->method('getAllMappings')
|
||||
->willReturn([
|
||||
'ext' => ['testing/existingmimetype'],
|
||||
]);
|
||||
$this->loader->expects($this->exactly(1))
|
||||
->method('exists')
|
||||
->will($this->returnValueMap([
|
||||
['testing/existingmimetype', true],
|
||||
]));
|
||||
$this->loader->expects($this->exactly(1))
|
||||
->method('getId')
|
||||
->will($this->returnValueMap([
|
||||
['testing/existingmimetype', 1],
|
||||
]));
|
||||
|
||||
$this->loader->expects($this->once())
|
||||
->method('updateFilecache')
|
||||
->with('ext', 1)
|
||||
->willReturn(3);
|
||||
|
||||
$this->consoleOutput->expects($this->at(0))
|
||||
->method('writeln')
|
||||
->with('Updated 3 filecache rows for mimetype "testing/existingmimetype"');
|
||||
|
||||
$this->consoleOutput->expects($this->at(1))
|
||||
->method('writeln')
|
||||
->with('Added 0 new mimetypes');
|
||||
$this->consoleOutput->expects($this->at(2))
|
||||
->method('writeln')
|
||||
->with('Updated 3 filecache rows');
|
||||
|
||||
self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,93 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Robin McCorkell <rmccorkell@owncloud.com>
|
||||
*
|
||||
* @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 <http://www.gnu.org/licenses/>
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OC\Files\Type;
|
||||
|
||||
use \OC\Files\Type\Loader;
|
||||
use \OCP\IDBConnection;
|
||||
|
||||
class LoaderTest extends \Test\TestCase {
|
||||
/** @var IDBConnection */
|
||||
protected $db;
|
||||
/** @var Loader */
|
||||
protected $loader;
|
||||
|
||||
protected function setUp() {
|
||||
$this->db = \OC::$server->getDatabaseConnection();
|
||||
$this->loader = new Loader($this->db);
|
||||
}
|
||||
|
||||
protected function tearDown() {
|
||||
$deleteMimetypes = $this->db->getQueryBuilder();
|
||||
$deleteMimetypes->delete('mimetypes')
|
||||
->where($deleteMimetypes->expr()->like(
|
||||
'mimetype', $deleteMimetypes->createPositionalParameter('testing/%')
|
||||
));
|
||||
$deleteMimetypes->execute();
|
||||
}
|
||||
|
||||
|
||||
public function testGetMimetype() {
|
||||
$qb = $this->db->getQueryBuilder();
|
||||
$qb->insert('mimetypes')
|
||||
->values([
|
||||
'mimetype' => $qb->createPositionalParameter('testing/mymimetype')
|
||||
]);
|
||||
$qb->execute();
|
||||
|
||||
$this->assertTrue($this->loader->exists('testing/mymimetype'));
|
||||
$mimetypeId = $this->loader->getId('testing/mymimetype');
|
||||
$this->assertNotNull($mimetypeId);
|
||||
|
||||
$mimetype = $this->loader->getMimetypeById($mimetypeId);
|
||||
$this->assertEquals('testing/mymimetype', $mimetype);
|
||||
}
|
||||
|
||||
public function testGetNonexistentMimetype() {
|
||||
$this->assertFalse($this->loader->exists('testing/nonexistent'));
|
||||
// hopefully this ID doesn't exist
|
||||
$this->assertNull($this->loader->getMimetypeById(12345));
|
||||
}
|
||||
|
||||
public function testStore() {
|
||||
$this->assertFalse($this->loader->exists('testing/mymimetype'));
|
||||
$mimetypeId = $this->loader->getId('testing/mymimetype');
|
||||
|
||||
$qb = $this->db->getQueryBuilder();
|
||||
$qb->select('mimetype')
|
||||
->from('mimetypes')
|
||||
->where($qb->expr()->eq('id', $qb->createPositionalParameter($mimetypeId)));
|
||||
|
||||
$mimetype = $qb->execute()->fetch();
|
||||
$this->assertEquals('testing/mymimetype', $mimetype['mimetype']);
|
||||
|
||||
$this->assertEquals('testing/mymimetype', $this->loader->getMimetypeById($mimetypeId));
|
||||
$this->assertEquals($mimetypeId, $this->loader->getId('testing/mymimetype'));
|
||||
}
|
||||
|
||||
public function testStoreExists() {
|
||||
$mimetypeId = $this->loader->getId('testing/mymimetype');
|
||||
$mimetypeId2 = $this->loader->getId('testing/mymimetype');
|
||||
|
||||
$this->assertEquals($mimetypeId, $mimetypeId2);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue