PHRAS-2161_coll-id-in-log-docs_MASTER

wip
This commit is contained in:
Jean-Yves Gaulier
2018-07-18 20:33:28 +02:00
parent b8bebbce11
commit b65500bb40
7 changed files with 227 additions and 158 deletions

View File

@@ -193,8 +193,7 @@ class FixAutoincrements extends Command
$databox,
'coll',
[
'collusr' => 'coll_id',
'log_colls' => 'coll_id',
'collusr' => 'coll_id',
]
);
}

View File

@@ -67,14 +67,15 @@ class Session_Logger
public function log(record_adapter $record, $action, $final, $comment)
{
$sql = 'INSERT INTO log_docs
(id, log_id, date, record_id, action, final, comment)
VALUES (null, :log_id, NOW(), :record_id, :action, :final, :comm)';
(id, log_id, date, record_id, coll_id, action, final, comment)
VALUES (null, :log_id, NOW(), :record_id, :coll_id, :action, :final, :comm)';
$stmt = $this->databox->get_connection()->prepare($sql);
$params = [
':log_id' => $this->get_id(),
':record_id' => $record->getRecordId(),
':coll_id' => $record->getCollectionId(),
':action' => $action,
':final' => $final,
':comm' => $comment,
@@ -139,17 +140,6 @@ class Session_Logger
$log_id = $conn->lastInsertId();
$stmt->closeCursor();
$sql = "INSERT INTO log_colls (id, log_id, coll_id) VALUES (null, :log_id, :coll_id)";
$stmt = $conn->prepare($sql);
foreach ($colls as $collId) {
$stmt->execute([
':log_id' => $log_id,
':coll_id' => $collId
]);
}
$stmt->closeCursor();
unset($stmt, $conn);
return new Session_Logger($databox, $log_id);

View File

@@ -1202,7 +1202,7 @@ class databox extends base implements ThumbnailedElement
public function clear_logs()
{
foreach (['log', 'log_colls', 'log_docs', 'log_search', 'log_view', 'log_thumb'] as $table) {
foreach (['log', 'log_docs', 'log_search', 'log_view', 'log_thumb'] as $table) {
$this->get_connection()->delete($table, []);
}

View File

@@ -13,11 +13,11 @@ use Alchemy\Phrasea\Application;
class patch_410alpha12a implements patchInterface
{
/** @var string */
private $release = '4.1.0-alpha.12';
/** @var string */
private $release = '4.1.0-alpha.12';
// source : https://www.npmjs.com/package/mime-db
private $mimes = '{
// source : https://www.npmjs.com/package/mime-db
private $mimes = '{
"application/1d-interleaved-parityfec": {
"source": "iana"
},
@@ -7703,84 +7703,84 @@ class patch_410alpha12a implements patchInterface
}';
/** @var array */
private $concern = [base::DATA_BOX];
/** @var array */
private $concern = [base::DATA_BOX];
/**
* {@inheritdoc}
*/
public function get_release()
{
return $this->release;
}
/**
* {@inheritdoc}
*/
public function getDoctrineMigrations()
{
return [];
}
/**
* {@inheritdoc}
*/
public function require_all_upgrades()
{
return false;
}
/**
* {@inheritdoc}
*/
public function concern()
{
return $this->concern;
}
/**
* {@inheritdoc}
*/
public function apply(base $databox, Application $app)
{
$sql = "DROP TABLE IF EXISTS `ext2mime`";
$databox->get_connection()->executeQuery($sql);
$sql = "CREATE TEMPORARY TABLE `ext2mime` (\n"
. " `ext` char(32) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL DEFAULT '',\n"
. " `mime` varchar(256) CHARACTER SET latin1 COLLATE latin1_bin DEFAULT NULL,\n"
. " PRIMARY KEY (`ext`)\n"
. ") ENGINE=InnoDB DEFAULT CHARSET=latin1";
$databox->get_connection()->executeQuery($sql);
// some mimes give the same ext (conflict), we use the first one.
$sql = "INSERT IGNORE INTO `ext2mime` (`ext`, `mime`) VALUES (:ext, :mime)";
$stmt = $databox->get_connection()->prepare($sql);
$mimes = json_decode($this->mimes, true); // as assoc
foreach($mimes as $mime=>$m) {
if(array_key_exists('extensions', $m) && is_array($m['extensions'])) {
foreach($m['extensions'] as $ext) {
$stmt->execute([':ext'=>$ext, ':mime'=>$mime]);
}
}
/**
* {@inheritdoc}
*/
public function get_release()
{
return $this->release;
}
$stmt->closeCursor();
// patch the tables only for truncated values
/**
* {@inheritdoc}
*/
public function getDoctrineMigrations()
{
return [];
}
// record.mime was char(40) ; subdef 64
$sql = "UPDATE `record` AS `r` INNER JOIN `ext2mime` AS `e` ON `e`.`ext` = RIGHT(`r`.`originalname`, LOCATE('.', REVERSE(`r`.`originalname`))-1)\n"
. "SET `r`.`mime`=`e`.`mime` WHERE LENGTH(`r`.`mime`)>39";
$databox->get_connection()->executeQuery($sql);
/**
* {@inheritdoc}
*/
public function require_all_upgrades()
{
return false;
}
// subdef.mime was char(64)
$sql = "UPDATE `subdef` AS `s` INNER JOIN `ext2mime` AS `e` ON `e`.`ext` = RIGHT(`s`.`name`, LOCATE('.', REVERSE(`s`.`name`))-1)\n"
. "SET `s`.`mime`=`e`.`mime` WHERE LENGTH(`s`.`mime`)>63";
$databox->get_connection()->executeQuery($sql);
/**
* {@inheritdoc}
*/
public function concern()
{
return $this->concern;
}
/**
* {@inheritdoc}
*/
public function apply(base $databox, Application $app)
{
$sql = "DROP TABLE IF EXISTS `ext2mime`";
$databox->get_connection()->executeQuery($sql);
$sql = "CREATE TEMPORARY TABLE `ext2mime` (\n"
. " `ext` char(32) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL DEFAULT '',\n"
. " `mime` varchar(256) CHARACTER SET latin1 COLLATE latin1_bin DEFAULT NULL,\n"
. " PRIMARY KEY (`ext`)\n"
. ") ENGINE=InnoDB DEFAULT CHARSET=latin1";
$databox->get_connection()->executeQuery($sql);
// some mimes give the same ext (conflict), we use the first one.
$sql = "INSERT IGNORE INTO `ext2mime` (`ext`, `mime`) VALUES (:ext, :mime)";
$stmt = $databox->get_connection()->prepare($sql);
$mimes = json_decode($this->mimes, true); // as assoc
foreach($mimes as $mime=>$m) {
if(array_key_exists('extensions', $m) && is_array($m['extensions'])) {
foreach($m['extensions'] as $ext) {
$stmt->execute([':ext'=>$ext, ':mime'=>$mime]);
}
}
}
$stmt->closeCursor();
// patch the tables only for truncated values
// record.mime was char(40) ; subdef 64
$sql = "UPDATE `record` AS `r` INNER JOIN `ext2mime` AS `e` ON `e`.`ext` = RIGHT(`r`.`originalname`, LOCATE('.', REVERSE(`r`.`originalname`))-1)\n"
. "SET `r`.`mime`=`e`.`mime` WHERE LENGTH(`r`.`mime`)>39";
$databox->get_connection()->executeQuery($sql);
// subdef.mime was char(64)
$sql = "UPDATE `subdef` AS `s` INNER JOIN `ext2mime` AS `e` ON `e`.`ext` = RIGHT(`s`.`name`, LOCATE('.', REVERSE(`s`.`name`))-1)\n"
. "SET `s`.`mime`=`e`.`mime` WHERE LENGTH(`s`.`mime`)>63";
$databox->get_connection()->executeQuery($sql);
$sql = "DROP TABLE IF EXISTS `ext2mime`";
$databox->get_connection()->executeQuery($sql);
}
$sql = "DROP TABLE IF EXISTS `ext2mime`";
$databox->get_connection()->executeQuery($sql);
}
}

View File

@@ -0,0 +1,124 @@
<?php
/**
* This file is part of Phraseanet
*
* (c) 2005-2016 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use Alchemy\Phrasea\Application;
class patch_410alpha13a implements patchInterface
{
/** @var string */
private $release = '4.1.0-alpha.13';
/** @var array */
private $concern = [base::DATA_BOX];
/**
* {@inheritdoc}
*/
public function get_release()
{
return $this->release;
}
/**
* {@inheritdoc}
*/
public function getDoctrineMigrations()
{
return [];
}
/**
* {@inheritdoc}
*/
public function require_all_upgrades()
{
return false;
}
/**
* {@inheritdoc}
*/
public function concern()
{
return $this->concern;
}
/**
* {@inheritdoc}
*/
public function apply(base $databox, Application $app)
{
$sql = "DROP TABLE IF EXISTS `log_colls`";
$databox->get_connection()->prepare($sql)->execute();
$sql = "ALTER TABLE `log_docs` ADD `coll_id` INT(11) UNSIGNED NULL DEFAULT NULL, ADD INDEX(coll_id)";
try {
$databox->get_connection()->prepare($sql)->execute();
}
catch(\Exception $e) {
// no-op
}
$sql = "CREATE TEMPORARY TABLE `tmp_colls` (\n"
. " `id` int(11) unsigned NOT NULL,\n"
. " `coll_id` int(11) unsigned NOT NULL,\n"
. " PRIMARY KEY (`id`)\n"
. ")";
$databox->get_connection()->prepare($sql)->execute();
$tsql = [
[
'sql' => "TRUNCATE tmp_colls",
'stmt' => null,
],
[
'sql' => "INSERT INTO tmp_colls\n"
. " SELECT id, COALESCE(SUBSTRING_INDEX(GROUP_CONCAT(final ORDER BY r2_id DESC), ',', 1), 0) AS coll_id FROM\n"
. " (\n"
. " SELECT r1.record_id, r1.id, r2.id AS r2_id, r2.final FROM\n"
. " (select id, record_id FROM log_docs WHERE ISNULL(coll_id) LIMIT 1000000) AS r1\n"
. " LEFT JOIN log_docs AS r2\n"
. " ON r2.record_id=r1.record_id AND r2.action IN('add', 'collection') AND r2.id<=r1.id\n"
. " )\n"
. " AS t GROUP BY id",
'stmt' => null,
],
[
'sql' => "UPDATE tmp_colls INNER JOIN log_docs USING(id) SET log_docs.coll_id=tmp_colls.coll_id",
'stmt' => null,
]
];
foreach($tsql as $k => $v) {
$tsql[$k]['stmt'] = $databox->get_connection()->prepare($v['sql']);
}
$nchanged = 0;
do {
foreach($tsql as $k => $v) {
printf("%s\n\n", $v['sql']);
/** @var \Doctrine\DBAL\Driver\Statement $stmt */
$stmt = $v['stmt'];
$stmt->execute();
$nchanged = $stmt->rowCount();
$stmt->closeCursor();
}
}
while($nchanged != 0);
$sql = "DROP TABLE `tmp_colls`";
$databox->get_connection()->prepare($sql)->execute();
return true;
}
}

View File

@@ -1208,13 +1208,14 @@ class record_adapter implements RecordInterface, cache_cacheableInterface
try {
$log_id = $app['phraseanet.logger']($collection->get_databox())->get_id();
$sql = 'INSERT INTO log_docs (id, log_id, date, record_id, action, final, comment)'
. ' VALUES (null, :log_id, now(), :record_id, "add", :coll_id,"")';
$sql = 'INSERT INTO log_docs (id, log_id, date, record_id, coll_id, action, final, comment)'
. ' VALUES (null, :log_id, now(), :record_id, :coll_id, "add", :final, "")';
$stmt = $connection->prepare($sql);
$stmt->execute([
':log_id' => $log_id,
':record_id' => $story_id,
':coll_id' => $collection->get_coll_id(),
':final' => $collection->get_coll_id(),
]);
$stmt->closeCursor();
} catch (\Exception $e) {
@@ -1261,14 +1262,15 @@ class record_adapter implements RecordInterface, cache_cacheableInterface
try {
$log_id = $app['phraseanet.logger']($databox)->get_id();
$sql = "INSERT INTO log_docs (id, log_id, date, record_id, action, final, comment)"
. " VALUES (null, :log_id, now(), :record_id, 'add', :coll_id, '')";
$sql = "INSERT INTO log_docs (id, log_id, date, record_id, coll_id, action, final, comment)"
. " VALUES (null, :log_id, now(), :record_id, :coll_id, 'add', :final, '')";
$stmt = $databox->get_connection()->prepare($sql);
$stmt->execute([
':log_id' => $log_id,
':record_id' => $record_id,
':coll_id' => $file->getCollection()->get_coll_id(),
':final' => $file->getCollection()->get_coll_id(),
]);
$stmt->closeCursor();
} catch (\Exception $e) {

View File

@@ -2385,67 +2385,6 @@
<engine>InnoDB</engine>
</table>
<table name="log_colls">
<fields>
<field>
<name>id</name>
<type>int(11) unsigned</type>
<null></null>
<extra>auto_increment</extra>
<default></default>
<comment></comment>
</field>
<field>
<name>log_id</name>
<type>int(11) unsigned</type>
<null></null>
<extra></extra>
<default></default>
<comment></comment>
</field>
<field>
<name>coll_id</name>
<type>int(11) unsigned</type>
<null></null>
<extra></extra>
<default></default>
<comment></comment>
</field>
</fields>
<indexes>
<index>
<name>PRIMARY</name>
<type>PRIMARY</type>
<fields>
<field>id</field>
</fields>
</index>
<index>
<name>couple</name>
<type>UNIQUE</type>
<fields>
<field>log_id</field>
<field>coll_id</field>
</fields>
</index>
<index>
<name>log_id</name>
<type>INDEX</type>
<fields>
<field>log_id</field>
</fields>
</index>
<index>
<name>coll_id</name>
<type>INDEX</type>
<fields>
<field>coll_id</field>
</fields>
</index>
</indexes>
<engine>InnoDB</engine>
</table>
<table name="log_search">
<fields>
<field>
@@ -2570,6 +2509,14 @@
<default></default>
<comment></comment>
</field>
<field>
<name>coll_id</name>
<type>int(11) unsigned</type>
<null></null>
<extra></extra>
<default>null</default>
<comment></comment>
</field>
<field>
<name>action</name>
<type>enum('push','add','validate','edit','collection','status','print','substit','publish','download','mail','ftp','delete')</type>
@@ -2618,6 +2565,13 @@
<field>record_id</field>
</fields>
</index>
<index>
<name>coll_id</name>
<type>INDEX</type>
<fields>
<field>coll_id</field>
</fields>
</index>
<index>
<name>action</name>
<type>INDEX</type>