Browse Source

Fixed mime type repair step

tags/v7.0.0RC1
Vincent Petry 10 years ago
parent
commit
67d0a3c15e
2 changed files with 133 additions and 41 deletions
  1. 67
    29
      lib/repair/repairmimetypes.php
  2. 66
    12
      tests/lib/repair/repairmimetypes.php

+ 67
- 29
lib/repair/repairmimetypes.php View File

'application/msexcel' => 'application/vnd.ms-excel', 'application/msexcel' => 'application/vnd.ms-excel',
); );


$stmt = \OC_DB::prepare('
UPDATE `*PREFIX*mimetypes`
SET `mimetype` = ?
$existsStmt = \OC_DB::prepare('
SELECT count(`mimetype`)
FROM `*PREFIX*mimetypes`
WHERE `mimetype` = ? WHERE `mimetype` = ?
'); ');


$getIdStmt = \OC_DB::prepare('
SELECT `id`
FROM `*PREFIX*mimetypes`
WHERE `mimetype` = ?
');

$insertStmt = \OC_DB::prepare('
INSERT INTO `*PREFIX*mimetypes` ( `mimetype` )
VALUES ( ? )
');

$updateWrongStmt = \OC_DB::prepare('
UPDATE `*PREFIX*filecache`
SET `mimetype` = (
SELECT `id`
FROM `*PREFIX*mimetypes`
WHERE `mimetype` = ?
) WHERE `mimetype` = ?
');

$deleteStmt = \OC_DB::prepare('
DELETE FROM `*PREFIX*mimetypes`
WHERE `id` = ?
');

foreach ($wrongMimetypes as $wrong => $correct) { foreach ($wrongMimetypes as $wrong => $correct) {
\OC_DB::executeAudited($stmt, array($wrong, $correct));


// do we need to remove a wrong mimetype?
$result = \OC_DB::executeAudited($getIdStmt, array($wrong));
$wrongId = $result->fetchOne();

if ($wrongId !== false) {

// do we need to insert the correct mimetype?
$result = \OC_DB::executeAudited($existsStmt, array($correct));
$exists = $result->fetchOne();

if ( ! $exists ) {
// insert mimetype
\OC_DB::executeAudited($insertStmt, array($correct));
}

// change wrong mimetype to correct mimetype in filecache
\OC_DB::executeAudited($updateWrongStmt, array($correct, $wrongId));

// delete wrong mimetype
\OC_DB::executeAudited($deleteStmt, array($wrongId));

}

} }


$updatedMimetypes = array( $updatedMimetypes = array(
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'xlsx' => 'application/vnd.ms-excel',
'pptx' => 'application/vnd.ms-powerpoint',
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
); );


$updateByNameStmt = \OC_DB::prepare('
UPDATE `*PREFIX*filecache`
SET `mimetype` = (
SELECT `id`
FROM `*PREFIX*mimetypes`
WHERE `mimetype` = ?
) WHERE `name` LIKE ?
');

// separate doc from docx etc // separate doc from docx etc
foreach ($updatedMimetypes as $extension => $mimetype ) { foreach ($updatedMimetypes as $extension => $mimetype ) {
$result = \OC_DB::executeAudited('
SELECT count(`mimetype`)
FROM `*PREFIX*mimetypes`
WHERE `mimetype` = ?
', array($mimetype)
);

$result = \OC_DB::executeAudited($existsStmt, array($mimetype));
$exists = $result->fetchOne(); $exists = $result->fetchOne();


if ( ! $exists ) { if ( ! $exists ) {
// insert mimetype // insert mimetype
\OC_DB::executeAudited('
INSERT INTO `*PREFIX*mimetypes` ( `mimetype` )
VALUES ( ? )
', array($mimetype)
);
\OC_DB::executeAudited($insertStmt, array($mimetype));
} }


// change mimetype for files with x extension // change mimetype for files with x extension
\OC_DB::executeAudited('
UPDATE `*PREFIX*filecache`
SET `mimetype` = (
SELECT `id`
FROM `*PREFIX*mimetypes`
WHERE `mimetype` = ?
) WHERE `name` LIKE ?
', array($mimetype, '%.'.$extension)
);
\OC_DB::executeAudited($updateByNameStmt, array($mimetype, '%.'.$extension));
} }
return true;
} }


/** /**
* Fix mime types * Fix mime types
*/ */
public function run() { public function run() {
// TODO: check precondition to avoid running the fix every time
if ($this->fixOfficeMimeTypes()) { if ($this->fixOfficeMimeTypes()) {
$this->emit('\OC\Repair', 'info', array('Fixed office mime types')); $this->emit('\OC\Repair', 'info', array('Fixed office mime types'));
} }

+ 66
- 12
tests/lib/repair/repairmimetypes.php View File

} }
} }


/**
* Returns the id of a given mime type or null
* if it does not exist.
*/
private function getMimeTypeIdFromDB($mimeType) {
$sql = 'SELECT `id` FROM `*PREFIX*mimetypes` WHERE mimetype = ?';
$results = \OC_DB::executeAudited($sql, array($mimeType));
$result = $results->fetchOne();
if ($result) {
return $result['id'];
}
return null;
}

/** /**
* Test renaming and splitting old office mime types * Test renaming and splitting old office mime types
*/ */
$this->repair->run(); $this->repair->run();


// force mimetype reload // force mimetype reload
DummyFileCache::clearCachedMimeTypes();
$this->storage->getCache()->loadMimeTypes(); $this->storage->getCache()->loadMimeTypes();


$this->checkEntries( $this->checkEntries(
array( array(
array('test.doc', 'application/msword'), array('test.doc', 'application/msword'),
array('test.docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'), array('test.docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'),
array('test.xls', 'application/msexcel'),
array('test.xlsx', 'application/vnd.ms-excel'),
array('test.ppt', 'application/mspowerpoint'),
array('test.pptx', 'application/vnd.ms-powerpoint'),
array('test.xls', 'application/vnd.ms-excel'),
array('test.xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'),
array('test.ppt', 'application/vnd.ms-powerpoint'),
array('test.pptx', 'application/vnd.openxmlformats-officedocument.presentationml.presentation'),
) )
); );
} }
$this->repair->run(); $this->repair->run();


// force mimetype reload // force mimetype reload
DummyFileCache::clearCachedMimeTypes();
$this->storage->getCache()->loadMimeTypes(); $this->storage->getCache()->loadMimeTypes();


$this->checkEntries( $this->checkEntries(
array( array(
array('test.doc', 'application/msword'), array('test.doc', 'application/msword'),
array('test.docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'), array('test.docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'),
array('test.xls', 'application/msexcel'),
array('test.xlsx', 'application/vnd.ms-excel'),
array('test.ppt', 'application/mspowerpoint'),
array('test.pptx', 'application/vnd.ms-powerpoint'),
array('test.xls', 'application/vnd.ms-excel'),
array('test.xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'),
array('test.ppt', 'application/vnd.ms-powerpoint'),
array('test.pptx', 'application/vnd.openxmlformats-officedocument.presentationml.presentation'),
array('bogus.docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'), array('bogus.docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'),
array('bogus.xlsx', 'application/vnd.ms-excel'),
array('bogus.pptx', 'application/vnd.ms-powerpoint'),
array('bogus.xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'),
array('bogus.pptx', 'application/vnd.openxmlformats-officedocument.presentationml.presentation'),
array('bogus2.docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'), array('bogus2.docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'),
array('bogus2.xlsx', 'application/vnd.ms-excel'),
array('bogus2.pptx', 'application/vnd.ms-powerpoint'),
array('bogus2.xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'),
array('bogus2.pptx', 'application/vnd.openxmlformats-officedocument.presentationml.presentation'),
)
);

// wrong mimetypes are gone
$this->assertNull($this->getMimeTypeIdFromDB('application/msexcel'));
$this->assertNull($this->getMimeTypeIdFromDB('application/mspowerpoint'));
}

/**
* Test that nothing happens and no error happens when all mimetypes are
* already correct and no old ones exist..
*/
public function testDoNothingWhenOnlyNewFiles() {
$this->addEntries(
array(
array('test.doc', 'application/msword'),
array('test.docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'),
array('test.xls', 'application/vnd.ms-excel'),
array('test.xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'),
array('test.ppt', 'application/vnd.ms-powerpoint'),
array('test.pptx', 'application/vnd.openxmlformats-officedocument.presentationml.presentation'),
)
);

$this->repair->run();

// force mimetype reload
DummyFileCache::clearCachedMimeTypes();
$this->storage->getCache()->loadMimeTypes();

$this->checkEntries(
array(
array('test.doc', 'application/msword'),
array('test.docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'),
array('test.xls', 'application/vnd.ms-excel'),
array('test.xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'),
array('test.ppt', 'application/vnd.ms-powerpoint'),
array('test.pptx', 'application/vnd.openxmlformats-officedocument.presentationml.presentation'),
) )
); );
} }

Loading…
Cancel
Save