You are here

recommender.install in Recommender API 7.5

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.install
View 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().
 *
 * Note: One big difference compared to previous release is to remove the {recommender_app} table and simply uses a varchar field 'recommender' to distinguish between different applications. Such a design violates database normal forms, and it might have negative effects on performance. But it does simplifies programing logic. Let's see if it needs change in the feature.
 */
function recommender_schema() {
  $schema = array(
    // 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,
        ),
        'recommender' => array(
          'description' => 'This field distinguishes different recommender applications.',
          'type' => 'varchar',
          'length' => 20,
          'not null' => TRUE,
        ),
        'source_id' => array(
          'description' => 'The first source entity_id. This is usually user id.',
          'type' => 'int',
          'size' => 'normal',
          'unsigned' => TRUE,
          'not null' => TRUE,
        ),
        'target_id' => 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',
      ),
      // note that this is not unique indexes.
      'indexes' => array(
        'recommender' => array(
          'recommender',
        ),
        'index_key' => array(
          'recommender',
          'source_id',
          'target_id',
        ),
      ),
    ),
    // 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,
        ),
        'recommender' => array(
          'description' => 'This field distinguishes different recommender applications.',
          'type' => 'varchar',
          'length' => 20,
          'not null' => TRUE,
        ),
        'source_id' => 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_id' => 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',
      ),
      // note that this is not unique indexes.
      'indexes' => array(
        'recommender' => array(
          'recommender',
        ),
        'index_key' => array(
          'recommender',
          'source_id',
          'target_id',
        ),
      ),
    ),
    // 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,
        ),
        'recommender' => array(
          'description' => 'This field distinguishes different recommender applications.',
          'type' => 'varchar',
          'length' => 20,
          'not null' => TRUE,
        ),
        'source_id' => 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_id' => 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',
      ),
      // note that this is not unique indexes.
      'indexes' => array(
        'recommender' => array(
          'recommender',
        ),
        'index_key' => array(
          'recommender',
          'source_id',
          'target_id',
        ),
      ),
    ),
    '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_id' => 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_id' => 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_id',
        'target_id',
      ),
    ),
  );
  return $schema;
}

/**
 * Implements hook_install().
 */
function recommender_install() {
  computing_create_record('recommender', 'PingMe', 'Ping recommender server');
}

/**
 * Implements hook_enable().
 */
function recommender_enable() {
  computing_create_record('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

Namesort descending Description
recommender_enable Implements hook_enable().
recommender_install Implements hook_install().
recommender_schema Implements hook_schema().