tracker.install 4.23 KB
Newer Older
Dries's avatar
Dries committed
1 2
<?php

3 4 5 6 7
/**
 * @file
 * Install, update, and uninstall functions for tracker.module.
 */

Dries's avatar
Dries committed
8
/**
9
 * Implements hook_uninstall().
Dries's avatar
Dries committed
10 11
 */
function tracker_uninstall() {
12
  Drupal::state()->delete('tracker.index_nid');
Dries's avatar
Dries committed
13 14 15
}

/**
16
 * Implements hook_enable().
Dries's avatar
Dries committed
17 18 19 20
 */
function tracker_enable() {
  $max_nid = db_query('SELECT MAX(nid) FROM {node}')->fetchField();
  if ($max_nid != 0) {
21
    Drupal::state()->set('tracker.index_nid', $max_nid);
Dries's avatar
Dries committed
22 23 24 25 26 27 28
    // To avoid timing out while attempting to do a complete indexing, we
    // simply call our cron job to remove stale records and begin the process.
    tracker_cron();
  }
}

/**
29
 * Implements hook_schema().
Dries's avatar
Dries committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
 */
function tracker_schema() {
  $schema['tracker_node'] = array(
    'description' => 'Tracks when nodes were last changed or commented on.',
    'fields' => array(
      'nid' => array(
        'description' => 'The {node}.nid this record tracks.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'published' => array(
        'description' => 'Boolean indicating whether the node is published.',
        'type' => 'int',
        'not null' => FALSE,
        'default' => 0,
        'size' => 'tiny',
      ),
      'changed' => array(
        'description' => 'The Unix timestamp when the node was most recently saved or commented on.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'indexes' => array(
      'tracker' => array('published', 'changed'),
    ),
    'primary key' => array('nid'),
    'foreign keys' => array(
62 63 64 65
      'tracked_node' => array(
        'table' => 'node',
        'columns' => array('nid' => 'nid'),
      ),
Dries's avatar
Dries committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    ),
  );

  $schema['tracker_user'] = array(
    'description' => 'Tracks when nodes were last changed or commented on, for each user that authored the node or one of its comments.',
    'fields' => array(
      'nid' => array(
        'description' => 'The {node}.nid this record tracks.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'uid' => array(
        'description' => 'The {users}.uid of the node author or commenter.',
        'type' => 'int',
82
        'unsigned' => TRUE,
Dries's avatar
Dries committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
        'not null' => TRUE,
        'default' => 0,
      ),
      'published' => array(
        'description' => 'Boolean indicating whether the node is published.',
        'type' => 'int',
        'not null' => FALSE,
        'default' => 0,
        'size' => 'tiny',
      ),
      'changed' => array(
        'description' => 'The Unix timestamp when the node was most recently saved or commented on.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'indexes' => array(
      'tracker' => array('uid', 'published', 'changed'),
    ),
    'primary key' => array('nid', 'uid'),
    'foreign keys' => array(
106 107 108 109 110 111 112 113
      'tracked_node' => array(
        'table' => 'node',
        'columns' => array('nid' => 'nid'),
      ),
      'tracked_user' => array(
        'table' => 'users',
        'columns' => array('uid' => 'uid'),
      ),
Dries's avatar
Dries committed
114 115 116 117 118
    ),
  );

  return $schema;
}
119 120

/**
121
 * @addtogroup updates-7.x-to-8.x
122 123 124 125 126 127 128 129 130 131 132 133
 * @{
 */

/**
 * Moves tracker settings from variable to config.
 */
function tracker_update_8000() {
  update_variables_to_config('tracker.settings', array(
    'tracker_batch_size' => 'cron_index_limit',
  ));
}

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
/**
 * Make *id fields unsigned.
 */
function tracker_update_8001() {
  db_drop_primary_key('tracker_user');
  db_drop_index('tracker_user', 'tracker');
  db_change_field('tracker_user', 'uid', 'uid',
    array(
      'description' => 'The {users}.uid of the node author or commenter.',
      'type' => 'int',
      'unsigned' => TRUE,
      'not null' => TRUE,
      'default' => 0,
    ),
    array('primary key' => array('nid', 'uid'), 'indexes' => array(
      'tracker' => array('uid', 'published', 'changed'),
    ))
  );
}

154
/**
155 156 157 158 159 160 161 162 163 164
 * Convert tracker_index_nid variable to state system.
 *
 * @ingroup state_upgrade
 */
function tracker_update_8002() {
  update_variables_to_state(array('tracker_index_nid' => 'tracker.index_nid'));
}

/**
 * @} End of "defgroup updates-7.x-to-8.x".
165 166
 * The next series of updates should start at 9000.
 */