split value to value1 and value2.

You need to update the DB schema and run /events/migrate02to021 to
migrate the data
pull/61/head
Christophe Vandeplas 2012-04-25 13:17:44 +02:00
parent aea079b8c4
commit 6e9f0f0d24
3 changed files with 94 additions and 9 deletions

View File

@ -134,9 +134,10 @@ class AppController extends Controller {
/**
* Updates the missing fields from v0.1 to v0.2 of CyDefSIG
* First you will need to manually update the database to the new schema.
* Then run this function by setting debug = 1 (or more) and call /events/migrate
* Log in as admin user and
* Then run this function by setting debug = 1 (or more) and call /events/migrate01to02
*/
function migrate() {
function migrate01to02() {
if (Configure::read('debug') == 0) throw new NotFoundException();
// generate uuids for events who have no uuid
$this->loadModel('Event');
@ -172,5 +173,41 @@ class AppController extends Controller {
}
/**
* Updates the missing fields from v0.2 to v0.2.1 of CyDefSIG
* First you will need to manually update the database to the new schema.
* Log in as admin user and
* Then run this function by setting debug = 1 (or more) and call /events/migrate02to021
*/
function migrate02to021() {
if (Configure::read('debug') == 0) throw new NotFoundException();
// search for composite value1 fields and explode it to value1 and value2
$this->loadModel('Attribute');
$params = array(
'conditions' => array(
'OR' => array(
'Attribute.type' => $this->Attribute->getCompositeTypes()
)
),
'recursive' => 0,
'fields' => array('Attribute.id', 'Attribute.value1'),
);
$attributes = $this->Attribute->find('all', $params);
echo '<p>Exploding composite fields in 2 columns: </p><ul>';
foreach ($attributes as $attribute) {
$pieces = explode('|', $attribute['Attribute']['value1']);
if (2 != sizeof($pieces)) continue; // do nothing if not 2 pieces
$this->Attribute->id = $attribute['Attribute']['id'];
echo '<li>'.$attribute['Attribute']['id'].' --> '.$attribute['Attribute']['value1'].' --> '.$pieces[0].' --> '.$pieces[1].'</li> ';
$this->Attribute->saveField('value1', $pieces[0]);
$this->Attribute->id = $attribute['Attribute']['id'];
$this->Attribute->saveField('value2', $pieces[1]);
}
echo "</ul> DONE</p>";
}
// FIXME change all Sanitize:html( to h( function. Shorter and same result.
}

View File

@ -3,7 +3,7 @@
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Apr 04, 2012 at 08:23 PM
-- Generation Time: Apr 25, 2012 at 01:16 PM
-- Server version: 5.5.9
-- PHP Version: 5.3.6
@ -24,7 +24,8 @@ CREATE TABLE `attributes` (
`event_id` int(11) NOT NULL,
`category` varchar(255) COLLATE utf8_bin NOT NULL,
`type` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`value` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`value1` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`value2` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`to_ids` tinyint(1) NOT NULL DEFAULT '1',
`uuid` varchar(40) COLLATE utf8_bin NOT NULL,
`revision` int(10) NOT NULL DEFAULT '0',

View File

@ -15,6 +15,11 @@ class Attribute extends AppModel {
*/
public $displayField = 'value';
public $virtualFields = array(
'value' => 'IF (Attribute.value2="", Attribute.value1, CONCAT(Attribute.value1, "|", Attribute.value2))'
);
var $order = array("Attribute.event_id" => "DESC", "Attribute.type" => "ASC");
/**
* Validation rules
@ -176,6 +181,19 @@ class Attribute extends AppModel {
if (empty($this->data['Attribute']['revision'])) $this->data['Attribute']['revision'] = 0;
$this->data['Attribute']['revision'] = 1 + $this->data['Attribute']['revision'] ;
// explode value of composite type in value1 and value2
// or copy value to value1 if not composite type
$composite_types = $this->getCompositeTypes();
if (in_array($this->data['Attribute']['type'], $composite_types)) {
// explode composite types in value1 and value2
$pieces = explode('|', $this->data['Attribute']['value']);
if (2 != sizeof($pieces)) throw new InternalErrorException('Composite type, but value not explodable');
$this->data['Attribute']['value1'] = $pieces[0];
$this->data['Attribute']['value2'] = $pieces[1];
} else {
$this->data['Attribute']['value1'] = $this->data['Attribute']['value'];
}
// always return true after a beforeSave()
return true;
}
@ -369,6 +387,17 @@ class Attribute extends AppModel {
}
function getCompositeTypes() {
// build the list of composite Attribute.type dynamically by checking if type contains a |
// default composite types
$composite_types = array('malware-sample');
// dynamically generated list
foreach ($this->validate['type']['rule'][1] as $type) {
$pieces = explode('|', $type);
if (2 == sizeof($pieces)) $composite_types[] = $type;
}
return $composite_types;
}
public function isOwnedByOrg($attributeid, $org) {
$this->id = $attributeid;
@ -391,18 +420,36 @@ class Attribute extends AppModel {
return null;
}
// prepare the conditions
$conditions = array(
'Attribute.event_id !=' => $attribute['event_id'],
// 'Attribute.type' => $attribute['type'], // LATER also filter on type
);
if (empty($attribute['value1'])) // prevent issues with empty fields
return null;
if (empty($attribute['value2'])) {
// no value2, only search for value 1
$conditions['OR'] = array(
'Attribute.value1' => $attribute['value1'],
'Attribute.value2' => $attribute['value1'],
);
} else {
// value2 also set, so search for both
$conditions['OR'] = array(
'Attribute.value1' => array($attribute['value1'],$attribute['value2']),
'Attribute.value2' => array($attribute['value1'],$attribute['value2']),
);
}
// do the search
$conditions = array(
'Attribute.value =' => $attribute['value'],
'Attribute.id !=' => $attribute['id'],
'Attribute.type =' => $attribute['type'], );
if (empty($fields)) {
$fields = array('Attribute.*');
}
$similar_events = $this->find('all',array('conditions' => $conditions,
'fields' => $fields,
'recursive' => 0,
'group' => array('Attribute.event_id'),
'order' => 'Attribute.event_id DESC', )
);
return $similar_events;