recommender.install in Recommender API 7.4
Same filename and directory in other branches
Installation file for the Recommender API module. Note: Not compatible with 6.x releases. Please uninstall 6.x releases before installing the D7 release.
File
recommender.installView source
<?php
/**
* @file
* Installation file for the Recommender API module.
* Note: Not compatible with 6.x releases. Please uninstall 6.x releases before installing the D7 release.
*/
/**
* Implements hook_schema().
*/
function recommender_schema() {
$schema = array(
// table to save recommender_info()
'recommender_app' => array(
'description' => 'Applications that use recommender API and their default parameters.',
'fields' => array(
'id' => array(
'description' => 'Unique id',
'type' => 'serial',
'size' => 'normal',
'unsigned' => TRUE,
'not null' => TRUE,
),
'name' => array(
'description' => 'The application that uses the recommender API',
'type' => 'varchar',
'not null' => TRUE,
'length' => 60,
),
'title' => array(
'description' => 'The human readable title of the application that uses the recommender API',
'type' => 'varchar',
'not null' => TRUE,
'length' => 255,
'default' => '',
),
'cron' => array(
'description' => 'Seconds to wait before the next cron.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => FALSE,
),
'execution_id' => array(
'description' => 'The id of async_command to be executed.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => FALSE,
),
'params' => array(
'description' => 'The default parameters for the recommender API for this app, using JSON',
'type' => 'text',
'not null' => FALSE,
),
'data' => array(
'description' => 'Storage of recommender app serialization data.',
'type' => 'blob',
'not null' => FALSE,
),
),
'primary key' => array(
'id',
),
'unique keys' => array(
'app_name' => array(
'name',
),
),
'foreign keys' => array(
'execution_id' => array(
'table' => 'async_command',
'columns' => array(
'execution_id' => 'id',
),
),
),
),
// table to save preference scores
'recommender_preference' => array(
'description' => 'This is the main table to save preference data. The structure is the same to prediction/similarity table.',
'fields' => array(
// this id might be redundant.
'id' => array(
'description' => 'Unique index for each preference data',
'type' => 'serial',
'size' => 'normal',
'unsigned' => TRUE,
'not null' => TRUE,
),
'app_id' => array(
'description' => 'This field distinguishes different applications.',
'type' => 'int',
'size' => 'normal',
'unsigned' => TRUE,
'not null' => TRUE,
),
'source_eid' => array(
'description' => 'The first source entity_id. This is usually user id.',
'type' => 'int',
'size' => 'normal',
'unsigned' => TRUE,
'not null' => TRUE,
),
'target_eid' => array(
'description' => 'The target entity_id. This is usually item id.',
'type' => 'int',
'size' => 'normal',
'unsigned' => TRUE,
'not null' => TRUE,
),
'score' => array(
'type' => 'float',
'size' => 'normal',
'not null' => FALSE,
'description' => 'The preference score. Higher score means source prefers target more.',
),
'updated' => array(
'description' => 'The Unix timestamp this preference is last changed',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
),
'primary key' => array(
'id',
),
'foreign keys' => array(
'app_id' => array(
'table' => 'recommender_app',
'columns' => array(
'app_id' => 'id',
),
),
),
// note that this is not unique indexes.
'indexes' => array(
'index_key' => array(
'app_id',
'source_eid',
'target_eid',
),
),
),
// table to save similarity scores
'recommender_similarity' => array(
'description' => 'This is the main table to save similarity data. The structure is the same to prediction table, but stores different data',
'fields' => array(
// this id might be redundant.
'id' => array(
'description' => 'Unique index for each similarity pair',
'type' => 'serial',
'size' => 'normal',
'unsigned' => TRUE,
'not null' => TRUE,
),
'app_id' => array(
'description' => 'This field distinguishes different applications.',
'type' => 'int',
'size' => 'normal',
'unsigned' => TRUE,
'not null' => TRUE,
),
'source_eid' => array(
'description' => 'The first source entity_id. The type is the same to the target entity',
'type' => 'int',
'size' => 'normal',
'unsigned' => TRUE,
'not null' => TRUE,
),
'target_eid' => array(
'description' => 'The target entity_id. The type is the same to the source entity',
'type' => 'int',
'size' => 'normal',
'unsigned' => TRUE,
'not null' => TRUE,
),
'score' => array(
'type' => 'float',
'size' => 'normal',
'not null' => FALSE,
'description' => 'Similarity score. The bigger, the more similar',
),
'updated' => array(
'description' => 'The Unix timestamp this similarity is last changed',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
),
'primary key' => array(
'id',
),
'foreign keys' => array(
'app_id' => array(
'table' => 'recommender_app',
'columns' => array(
'app_id' => 'id',
),
),
),
// note that this is not unique indexes.
'indexes' => array(
'index_key' => array(
'app_id',
'source_eid',
'target_eid',
),
),
),
// table to save predictions
'recommender_prediction' => array(
'description' => 'This is the main table to save prediction data. The structure is the same to similarity table, but here source and target are different type of entities',
'fields' => array(
// this id might be redundant.
'id' => array(
'description' => 'Unique index for each prediction link',
'type' => 'serial',
'size' => 'normal',
'unsigned' => TRUE,
'not null' => TRUE,
),
'app_id' => array(
'description' => 'This field distinguishes different recommender applications.',
'type' => 'int',
'size' => 'normal',
'unsigned' => TRUE,
'not null' => TRUE,
),
'source_eid' => array(
'description' => 'The entity_id for which the prediction is generated. This is usually user id',
'type' => 'int',
'size' => 'normal',
'unsigned' => TRUE,
'not null' => TRUE,
),
'target_eid' => array(
'description' => 'The entity_id of the target prediction. This is usually item id.',
'type' => 'int',
'size' => 'normal',
'unsigned' => TRUE,
'not null' => TRUE,
),
'score' => array(
'type' => 'float',
'size' => 'normal',
'not null' => FALSE,
'description' => 'The prediction score. Higher score means source prefers target more.',
),
'updated' => array(
'description' => 'The Unix timestamp this prediction is last changed',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
),
'primary key' => array(
'id',
),
'foreign keys' => array(
'app_id' => array(
'table' => 'recommender_app',
'columns' => array(
'app_id' => 'id',
),
),
),
// note that this is not unique indexes.
'indexes' => array(
'index_key' => array(
'app_id',
'source_eid',
'target_eid',
),
),
),
'recommender_preference_staging' => array(
'description' => 'This table stages user-item preference data if it is from a SQL query. Should get purged before app running',
'fields' => array(
'source_eid' => array(
'description' => 'The entity_id of the owner of the preference. This is usually user id',
'type' => 'int',
'size' => 'normal',
'unsigned' => TRUE,
'not null' => TRUE,
),
'target_eid' => array(
'description' => 'The entity_id of the target of the preference. This is usually item id.',
'type' => 'int',
'size' => 'normal',
'unsigned' => TRUE,
'not null' => TRUE,
),
'score' => array(
'type' => 'float',
'size' => 'normal',
'not null' => FALSE,
'description' => 'The preference score. Higher score means source prefers target more.',
),
'updated' => array(
'description' => 'The Unix timestamp this preference is last changed',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
),
'primary key' => array(
'source_eid',
'target_eid',
),
'indexes' => array(
//'score' => array('score'),
'updated' => array(
'updated',
),
),
),
);
return $schema;
}
/**
* Implements hook_install().
*/
function recommender_install() {
async_command_create_command('recommender', 'PingMe', 'Ping recommender server');
}
/**
* Implements hook_enable().
*/
function recommender_enable() {
async_command_create_command('recommender', 'PingMe', 'Ping recommender server');
}
/**
* Implements hook_update_N().
* Make {recommender_app} 'name' field longer.
*/
function recommender_update_7001() {
db_change_field('recommender_app', 'name', 'name', array(
'description' => 'The application that uses the recommender API',
'type' => 'varchar',
'not null' => TRUE,
'length' => 60,
));
}
/**
* Add cron settings to {recommender_app}
*/
function recommender_update_7002() {
$schema = recommender_schema();
db_add_field('recommender_app', 'cron', $schema['recommender_app']['fields']['cron']);
db_add_field('recommender_app', 'execution_id', $schema['recommender_app']['fields']['execution_id']);
}
/**
* Add table {recommender_preference}
*/
function recommender_update_7003() {
$schema = recommender_schema();
db_create_table("recommender_preference", $schema['recommender_preference']);
}
/**
* Use JSON to store recommender params
*/
function recommender_update_7004() {
$results = db_query('SELECT id, params FROM {recommender_app}');
foreach ($results as $row) {
$json_params = json_encode(unserialize($row->params));
db_query('UPDATE {recommender_app} SET params=:params WHERE id=:id', array(
':params' => $json_params,
':id' => $row->id,
));
}
}
/**
* Replace sql as table.
*/
function recommender_update_7005() {
$results = db_query('SELECT id, params FROM {recommender_app}');
foreach ($results as $row) {
$params = json_decode($row->params, TRUE);
if (isset($params['sql'])) {
$params['table'] = $params['sql'];
unset($params['sql']);
db_query('UPDATE {recommender_app} SET params=:params WHERE id=:id', array(
':params' => json_encode($params),
':id' => $row->id,
));
}
}
}
Functions
Name![]() |
Description |
---|---|
recommender_enable | Implements hook_enable(). |
recommender_install | Implements hook_install(). |
recommender_schema | Implements hook_schema(). |
recommender_update_7001 | Implements hook_update_N(). Make {recommender_app} 'name' field longer. |
recommender_update_7002 | Add cron settings to {recommender_app} |
recommender_update_7003 | Add table {recommender_preference} |
recommender_update_7004 | Use JSON to store recommender params |
recommender_update_7005 | Replace sql as table. |