Implemented new method of storing filename and mimetype of file objects

This commit is contained in:
Aleksander Machniak 2013-04-19 14:20:55 +02:00
parent 08332b5238
commit e03156fc1f
14 changed files with 65 additions and 74 deletions

View file

@ -21,7 +21,9 @@ CREATE TABLE `kolab_cache` (
`dtend` DATETIME, `dtend` DATETIME,
`tags` VARCHAR(255) NOT NULL, `tags` VARCHAR(255) NOT NULL,
`words` TEXT NOT NULL, `words` TEXT NOT NULL,
PRIMARY KEY(`resource`,`type`,`msguid`) `filename` varchar(255) DEFAULT NULL,
PRIMARY KEY(`resource`,`type`,`msguid`),
INDEX `resource_filename` (`resource`, `filename`)
) /*!40000 ENGINE=INNODB */ /*!40101 CHARACTER SET utf8 COLLATE utf8_general_ci */; ) /*!40000 ENGINE=INNODB */ /*!40101 CHARACTER SET utf8 COLLATE utf8_general_ci */;
INSERT INTO `system` (`name`, `value`) VALUES ('libkolab-version', '2013011000'); INSERT INTO `system` (`name`, `value`) VALUES ('libkolab-version', '2013041900');

View file

@ -0,0 +1,3 @@
DELETE FROM `kolab_cache` WHERE `type` = 'file';
ALTER TABLE `kolab_cache` ADD `filename` varchar(255) DEFAULT NULL;
ALTER TABLE `kolab_cache` ADD INDEX `resource_filename` (`resource`, `filename`);

View file

@ -438,6 +438,18 @@ abstract class kolab_format
$object['x-custom'][] = array($cp->identifier, $cp->value); $object['x-custom'][] = array($cp->identifier, $cp->value);
} }
// merge with additional data, e.g. attachments from the message
if ($data) {
foreach ($data as $idx => $value) {
if (is_array($value)) {
$object[$idx] = array_merge((array)$object[$idx], $value);
}
else {
$object[$idx] = $value;
}
}
}
return $object; return $object;
} }

View file

@ -98,16 +98,12 @@ class kolab_format_configuration extends kolab_format
if (!empty($this->data)) if (!empty($this->data))
return $this->data; return $this->data;
$this->init(); // read common object props into local data object
$object = parent::to_array($data);
$type_map = array_flip($this->type_map); $type_map = array_flip($this->type_map);
// read object properties $object['type'] = $type_map[$this->obj->type()];
$object = array(
'uid' => $this->obj->uid(),
'created' => self::php_datetime($this->obj->created()),
'changed' => self::php_datetime($this->obj->lastModified()),
'type' => $type_map[$this->obj->type()],
);
// read type-specific properties // read type-specific properties
switch ($object['type']) { switch ($object['type']) {

View file

@ -266,7 +266,7 @@ class kolab_format_contact extends kolab_format
return $this->data; return $this->data;
// read common object props into local data object // read common object props into local data object
$object = parent::to_array(); $object = parent::to_array($data);
$object['name'] = $this->obj->name(); $object['name'] = $this->obj->name();

View file

@ -88,7 +88,7 @@ class kolab_format_distributionlist extends kolab_format
return $this->data; return $this->data;
// read common object props into local data object // read common object props into local data object
$object = parent::to_array(); $object = parent::to_array($data);
// add object properties // add object properties
$object += array( $object += array(

View file

@ -147,7 +147,7 @@ class kolab_format_event extends kolab_format_xcal
return $this->data; return $this->data;
// read common xcal props // read common xcal props
$object = parent::to_array(); $object = parent::to_array($data);
// read object properties // read object properties
$object += array( $object += array(
@ -177,13 +177,13 @@ class kolab_format_event extends kolab_format_xcal
// skip cid: attachments which are mime message parts handled by kolab_storage_folder // skip cid: attachments which are mime message parts handled by kolab_storage_folder
if (substr($attach->uri(), 0, 4) != 'cid:' && $attach->label()) { if (substr($attach->uri(), 0, 4) != 'cid:' && $attach->label()) {
$name = $attach->label(); $name = $attach->label();
$data = $attach->data(); $content = $attach->data();
$object['_attachments'][$name] = array( $object['_attachments'][$name] = array(
'name' => $name, 'name' => $name,
'mimetype' => $attach->mimetype(), 'mimetype' => $attach->mimetype(),
'size' => strlen($data), 'size' => strlen($content),
'content' => $data, 'content' => $content,
); );
} }
else if (substr($attach->uri(), 0, 4) == 'http') { else if (substr($attach->uri(), 0, 4) == 'http') {
@ -207,18 +207,6 @@ class kolab_format_event extends kolab_format_xcal
$object['thisandfuture'] = $this->obj->thisAndFuture(); $object['thisandfuture'] = $this->obj->thisAndFuture();
} }
// merge with additional data, e.g. attachments from the message
if ($data) {
foreach ($data as $idx => $value) {
if (is_array($value)) {
$object[$idx] = array_merge((array)$object[$idx], $value);
}
else {
$object[$idx] = $value;
}
}
}
return $this->data = $object; return $this->data = $object;
} }

View file

@ -98,11 +98,12 @@ class kolab_format_file extends kolab_format
public function to_array($data = array()) public function to_array($data = array())
{ {
// return cached result // return cached result
if (!empty($this->data)) if (!empty($this->data)) {
return $this->data; return $this->data;
}
// read common object props into local data object // read common object props into local data object
$object = parent::to_array(); $object = parent::to_array($data);
$sensitivity_map = array_flip($this->sensitivity_map); $sensitivity_map = array_flip($this->sensitivity_map);
@ -113,18 +114,6 @@ class kolab_format_file extends kolab_format
'notes' => $this->obj->note(), 'notes' => $this->obj->note(),
); );
// merge with additional data, e.g. attachments from the message
if ($data) {
foreach ($data as $idx => $value) {
if (is_array($value)) {
$object[$idx] = array_merge((array)$object[$idx], $value);
}
else {
$object[$idx] = $value;
}
}
}
return $this->data = $object; return $this->data = $object;
} }
@ -141,22 +130,14 @@ class kolab_format_file extends kolab_format
$tags[] = rcube_utils::normalize_string($cat); $tags[] = rcube_utils::normalize_string($cat);
} }
return $tags; // Add file mimetype to tags
} if (!empty($this->data['_attachments'])) {
$attachment = array_shift($this->data['_attachments']);
/** if ($attachment['mimetype']) {
* Callback for kolab_storage_cache to get words to index for fulltext search $tags[] = $attachment['mimetype'];
* }
* @return array List of words to save in cache
*/
public function get_words()
{
// Store filename in 'words' for fast access to file by name
if (empty($this->data['_attachments'])) {
return array();
} }
$attachment = array_shift($this->data['_attachments']); return $tags;
return array($attachment['name']);
} }
} }

View file

@ -71,7 +71,7 @@ class kolab_format_journal extends kolab_format
return $this->data; return $this->data;
// read common object props into local data object // read common object props into local data object
$object = parent::to_array(); $object = parent::to_array($data);
// TODO: read object properties // TODO: read object properties

View file

@ -71,7 +71,7 @@ class kolab_format_note extends kolab_format
return $this->data; return $this->data;
// read common object props into local data object // read common object props into local data object
$object = parent::to_array(); $object = parent::to_array($data);
// TODO: read object properties // TODO: read object properties

View file

@ -80,7 +80,7 @@ class kolab_format_task extends kolab_format_xcal
return $this->data; return $this->data;
// read common xcal props // read common xcal props
$object = parent::to_array(); $object = parent::to_array($data);
$object['complete'] = intval($this->obj->percentComplete()); $object['complete'] = intval($this->obj->percentComplete());

View file

@ -95,7 +95,7 @@ abstract class kolab_format_xcal extends kolab_format
public function to_array($data = array()) public function to_array($data = array())
{ {
// read common object props // read common object props
$object = parent::to_array(); $object = parent::to_array($data);
$status_map = array_flip($this->status_map); $status_map = array_flip($this->status_map);
$sensitivity_map = array_flip($this->sensitivity_map); $sensitivity_map = array_flip($this->sensitivity_map);

View file

@ -238,8 +238,8 @@ class kolab_storage_cache
$result = $this->db->query( $result = $this->db->query(
"INSERT INTO kolab_cache ". "INSERT INTO kolab_cache ".
" (resource, type, msguid, uid, created, changed, data, xml, dtstart, dtend, tags, words)". " (resource, type, msguid, uid, created, changed, data, xml, dtstart, dtend, tags, words, filename)".
" VALUES (?, ?, ?, ?, " . $this->db->now() . ", ?, ?, ?, ?, ?, ?, ?)", " VALUES (?, ?, ?, ?, " . $this->db->now() . ", ?, ?, ?, ?, ?, ?, ?, ?)",
$this->resource_uri, $this->resource_uri,
$objtype, $objtype,
$msguid, $msguid,
@ -250,7 +250,8 @@ class kolab_storage_cache
$sql_data['dtstart'], $sql_data['dtstart'],
$sql_data['dtend'], $sql_data['dtend'],
$sql_data['tags'], $sql_data['tags'],
$sql_data['words'] $sql_data['words'],
$sql_data['filename']
); );
if (!$this->db->affected_rows($result)) { if (!$this->db->affected_rows($result)) {
@ -515,9 +516,9 @@ class kolab_storage_cache
*/ */
private function _serialize($object) private function _serialize($object)
{ {
$bincols = array_flip($this->binary_cols); $bincols = array_flip($this->binary_cols);
$sql_data = array('changed' => null, 'dtstart' => null, 'dtend' => null, 'xml' => '', 'tags' => '', 'words' => ''); $sql_data = array('changed' => null, 'dtstart' => null, 'dtend' => null, 'xml' => '', 'tags' => '', 'words' => '');
$objtype = $object['_type'] ? $object['_type'] : $this->folder->type; $objtype = $object['_type'] ? $object['_type'] : $this->folder->type;
// set type specific values // set type specific values
if ($objtype == 'event') { if ($objtype == 'event') {
@ -537,14 +538,20 @@ class kolab_storage_cache
if ($object['due']) if ($object['due'])
$sql_data['dtend'] = date('Y-m-d H:i:s', is_object($object['due']) ? $object['due']->format('U') : $object['due']); $sql_data['dtend'] = date('Y-m-d H:i:s', is_object($object['due']) ? $object['due']->format('U') : $object['due']);
} }
else if ($objtype == 'file') {
if (!empty($object['_attachments'])) {
reset($object['_attachments']);
$sql_data['filename'] = $object['_attachments'][key($object['_attachments'])]['name'];
}
}
if ($object['changed']) { if ($object['changed']) {
$sql_data['changed'] = date('Y-m-d H:i:s', is_object($object['changed']) ? $object['changed']->format('U') : $object['changed']); $sql_data['changed'] = date('Y-m-d H:i:s', is_object($object['changed']) ? $object['changed']->format('U') : $object['changed']);
} }
if ($object['_formatobj']) { if ($object['_formatobj']) {
$sql_data['xml'] = preg_replace('!(</?[a-z0-9:-]+>)[\n\r\t\s]+!ms', '$1', (string)$object['_formatobj']->write(3.0)); $sql_data['xml'] = preg_replace('!(</?[a-z0-9:-]+>)[\n\r\t\s]+!ms', '$1', (string)$object['_formatobj']->write(3.0));
$sql_data['tags'] = ' ' . join(' ', $object['_formatobj']->get_tags()) . ' '; // pad with spaces for strict/prefix search $sql_data['tags'] = ' ' . join(' ', $object['_formatobj']->get_tags()) . ' '; // pad with spaces for strict/prefix search
$sql_data['words'] = ' ' . join(' ', $object['_formatobj']->get_words()) . ' '; $sql_data['words'] = ' ' . join(' ', $object['_formatobj']->get_words()) . ' ';
} }
@ -625,6 +632,7 @@ class kolab_storage_cache
$this->db->quote($sql_data['dtend']), $this->db->quote($sql_data['dtend']),
$this->db->quote($sql_data['tags']), $this->db->quote($sql_data['tags']),
$this->db->quote($sql_data['words']), $this->db->quote($sql_data['words']),
$this->db->quote($sql_data['filename']),
); );
$line = '(' . join(',', $values) . ')'; $line = '(' . join(',', $values) . ')';
} }
@ -632,7 +640,7 @@ class kolab_storage_cache
if ($buffer && (!$msguid || (strlen($buffer) + strlen($line) > $this->max_sql_packet))) { if ($buffer && (!$msguid || (strlen($buffer) + strlen($line) > $this->max_sql_packet))) {
$result = $this->db->query( $result = $this->db->query(
"INSERT INTO kolab_cache ". "INSERT INTO kolab_cache ".
" (resource, type, msguid, uid, created, changed, data, xml, dtstart, dtend, tags, words)". " (resource, type, msguid, uid, created, changed, data, xml, dtstart, dtend, tags, words, filename)".
" VALUES $buffer" " VALUES $buffer"
); );
if (!$this->db->affected_rows($result)) { if (!$this->db->affected_rows($result)) {
@ -728,7 +736,7 @@ class kolab_storage_cache
if (!isset($this->uid2msg[$uid])) { if (!isset($this->uid2msg[$uid])) {
// use IMAP SEARCH to get the right message // use IMAP SEARCH to get the right message
$index = $this->imap->search_once($this->folder->name, ($deleted ? '' : 'UNDELETED ') . $index = $this->imap->search_once($this->folder->name, ($deleted ? '' : 'UNDELETED ') .
'HEADER SUBJECT ' . rcube_imap_generic::escape($uid)); 'HEADER SUBJECT ' . rcube_imap_generic::escape($uid));
$results = $index->get(); $results = $index->get();
$this->uid2msg[$uid] = $results[0]; $this->uid2msg[$uid] = $results[0];
} }

View file

@ -19,10 +19,10 @@
<email>machniak@kolabsys.com</email> <email>machniak@kolabsys.com</email>
<active>yes</active> <active>yes</active>
</developer> </developer>
<date>2012-11-21</date> <date>2013-04-19</date>
<version> <version>
<release>0.9-beta</release> <release>0.9</release>
<api>0.9-beta</api> <api>0.9</api>
</version> </version>
<stability> <stability>
<release>stable</release> <release>stable</release>
@ -83,6 +83,7 @@
<file name="config.inc.php.dist" role="data"></file> <file name="config.inc.php.dist" role="data"></file>
<file name="LICENSE" role="data"></file> <file name="LICENSE" role="data"></file>
<file name="README" role="data"></file> <file name="README" role="data"></file>
<file name="UPGRADING" role="data"></file>
</dir> </dir>
<!-- / --> <!-- / -->
</contents> </contents>