| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
//////////////////////////////////////////////////////////////////////////////
|
| 5 |
// Core API hooks
|
| 6 |
|
| 7 |
/**
|
| 8 |
* Implementation of hook_enable().
|
| 9 |
*/
|
| 10 |
function bitcache_enable() {
|
| 11 |
drupal_set_message(t('Bitcache successfully installed. Please review the available <a href="@settings">configuration settings</a>.', array('@settings' => url('admin/settings/bitcache'))));
|
| 12 |
}
|
| 13 |
|
| 14 |
/**
|
| 15 |
* Implementation of hook_install().
|
| 16 |
*/
|
| 17 |
function bitcache_install() {
|
| 18 |
module_load_include('module', 'bitcache');
|
| 19 |
|
| 20 |
drupal_install_schema('bitcache');
|
| 21 |
db_query("INSERT INTO {bitcache_repositories} VALUES ('%s', 'bitcache', 'sql', 1, 1, 0, -10, '%s')", 'bitcache', serialize(array('title' => t('Default'), 'description' => '')));
|
| 22 |
}
|
| 23 |
|
| 24 |
/**
|
| 25 |
* Implementation of hook_uninstall().
|
| 26 |
*/
|
| 27 |
function bitcache_uninstall() {
|
| 28 |
db_query("DELETE FROM {bitcache_repositories} WHERE name = '%s'", 'bitcache');
|
| 29 |
drupal_uninstall_schema('bitcache');
|
| 30 |
|
| 31 |
db_query("DELETE FROM {variable} WHERE name LIKE '%s_%%'", 'bitcache');
|
| 32 |
cache_clear_all('variables', 'cache');
|
| 33 |
}
|
| 34 |
|
| 35 |
//////////////////////////////////////////////////////////////////////////////
|
| 36 |
// Schema API hooks
|
| 37 |
|
| 38 |
/**
|
| 39 |
* Implementation of hook_schema().
|
| 40 |
*/
|
| 41 |
function bitcache_schema() {
|
| 42 |
return array(
|
| 43 |
// Bitcache repositories
|
| 44 |
'bitcache_repositories' => array( // added in 6.x-1.0-beta2
|
| 45 |
'description' => t("Bitcache repositories"),
|
| 46 |
'fields' => array(
|
| 47 |
'name' => array(
|
| 48 |
'description' => t("Repository name."),
|
| 49 |
'type' => 'varchar',
|
| 50 |
'length' => 64,
|
| 51 |
'not null' => TRUE,
|
| 52 |
),
|
| 53 |
'module' => array(
|
| 54 |
'description' => t("Repository owner module."),
|
| 55 |
'type' => 'varchar',
|
| 56 |
'length' => 255,
|
| 57 |
'not null' => TRUE,
|
| 58 |
),
|
| 59 |
'adapter' => array(
|
| 60 |
'description' => t("Repository adapter ('file' or 'sql')."),
|
| 61 |
'type' => 'varchar',
|
| 62 |
'length' => 32,
|
| 63 |
'not null' => TRUE,
|
| 64 |
),
|
| 65 |
'enabled' => array(
|
| 66 |
'description' => t("Repository enabled? ('0' or '1')."),
|
| 67 |
'type' => 'int',
|
| 68 |
'size' => 'tiny',
|
| 69 |
'length' => 1,
|
| 70 |
'not null' => TRUE,
|
| 71 |
'default' => 0,
|
| 72 |
),
|
| 73 |
'mutable' => array(
|
| 74 |
'description' => t("Repository mutable? ('0' or '1')."),
|
| 75 |
'type' => 'int',
|
| 76 |
'size' => 'tiny',
|
| 77 |
'length' => 1,
|
| 78 |
'not null' => TRUE,
|
| 79 |
'default' => 0,
|
| 80 |
),
|
| 81 |
'indexed' => array(
|
| 82 |
'description' => t("Repository indexed? ('0' or '1')."),
|
| 83 |
'type' => 'int',
|
| 84 |
'size' => 'tiny',
|
| 85 |
'length' => 1,
|
| 86 |
'not null' => TRUE,
|
| 87 |
'default' => 0,
|
| 88 |
),
|
| 89 |
'weight' => array(
|
| 90 |
'description' => t("Repository weight."),
|
| 91 |
'type' => 'int',
|
| 92 |
'size' => 'tiny',
|
| 93 |
'length' => 4,
|
| 94 |
'not null' => TRUE,
|
| 95 |
'default' => 0,
|
| 96 |
),
|
| 97 |
'options' => array(
|
| 98 |
'description' => t("Repository options (serialized PHP)."),
|
| 99 |
'type' => 'text',
|
| 100 |
'size' => 'normal',
|
| 101 |
'not null' => TRUE,
|
| 102 |
),
|
| 103 |
),
|
| 104 |
'primary key' => array('name'),
|
| 105 |
),
|
| 106 |
|
| 107 |
// Bitcache data (default repository)
|
| 108 |
'bitcache_data' => array( // added in 6.x-1.0-beta2
|
| 109 |
'description' => t("Bitcache data"),
|
| 110 |
'fields' => array(
|
| 111 |
'id' => array(
|
| 112 |
'description' => t("Bitstream's digital fingerprint (SHA-1)."),
|
| 113 |
'type' => 'char',
|
| 114 |
'length' => 40,
|
| 115 |
'not null' => TRUE,
|
| 116 |
),
|
| 117 |
'data' => array(
|
| 118 |
'description' => t("Bitstream's contents."),
|
| 119 |
'type' => 'blob',
|
| 120 |
'size' => 'big',
|
| 121 |
'not null' => TRUE,
|
| 122 |
),
|
| 123 |
),
|
| 124 |
'primary key' => array('id'),
|
| 125 |
),
|
| 126 |
|
| 127 |
// Bitcache index
|
| 128 |
'bitcache_index' => array( // added in 6.x-1.0-beta3
|
| 129 |
'description' => t("Bitcache index"),
|
| 130 |
'fields' => array(
|
| 131 |
'id' => array(
|
| 132 |
'description' => t("Bitstream's digital fingerprint (SHA-1)."),
|
| 133 |
'type' => 'char',
|
| 134 |
'length' => 40,
|
| 135 |
'not null' => TRUE,
|
| 136 |
),
|
| 137 |
'size' => array(
|
| 138 |
'description' => t("Bitstream's size in bytes."),
|
| 139 |
'type' => 'int',
|
| 140 |
'unsigned' => TRUE,
|
| 141 |
),
|
| 142 |
'in_bitcache' => array(
|
| 143 |
'description' => t(""),
|
| 144 |
'type' => 'int',
|
| 145 |
'size' => 'tiny',
|
| 146 |
'length' => 1,
|
| 147 |
'not null' => TRUE,
|
| 148 |
'default' => 0,
|
| 149 |
),
|
| 150 |
),
|
| 151 |
'primary key' => array('id'),
|
| 152 |
),
|
| 153 |
);
|
| 154 |
}
|
| 155 |
|
| 156 |
/**
|
| 157 |
* Implementation of hook_schema_alter().
|
| 158 |
*/
|
| 159 |
function bitcache_schema_alter($schema) {
|
| 160 |
// This is not executed on installation/uninstallation, but only when the
|
| 161 |
// schema is loaded at runtime; it's needed in order for repositories
|
| 162 |
// created by third-party modules to have a schema without them having to
|
| 163 |
// duplicate the definition of the {bitcache_data} table, above.
|
| 164 |
if (function_exists('bitcache_get_repository_tables')) {
|
| 165 |
foreach (bitcache_get_repository_tables(TRUE) as $table) {
|
| 166 |
if ($table != BITCACHE_TABLE_DEFAULT) {
|
| 167 |
$schema[$table] = $schema[BITCACHE_TABLE_DEFAULT];
|
| 168 |
}
|
| 169 |
}
|
| 170 |
}
|
| 171 |
}
|
| 172 |
|
| 173 |
//////////////////////////////////////////////////////////////////////////////
|
| 174 |
// Schema API updates
|
| 175 |
|
| 176 |
/**
|
| 177 |
* Creates the {bitcache_data} table.
|
| 178 |
*
|
| 179 |
* @since 6.x-1.0-beta2
|
| 180 |
*/
|
| 181 |
function bitcache_update_6002() {
|
| 182 |
$updates = array();
|
| 183 |
db_create_table($updates, 'bitcache_data',
|
| 184 |
array(
|
| 185 |
'fields' => array(
|
| 186 |
'id' => array(
|
| 187 |
'type' => 'char',
|
| 188 |
'length' => 40,
|
| 189 |
'not null' => TRUE,
|
| 190 |
),
|
| 191 |
'data' => array(
|
| 192 |
'type' => 'blob',
|
| 193 |
'size' => 'big',
|
| 194 |
'not null' => TRUE,
|
| 195 |
),
|
| 196 |
),
|
| 197 |
'primary key' => array('id'),
|
| 198 |
)
|
| 199 |
);
|
| 200 |
return $updates;
|
| 201 |
}
|
| 202 |
|
| 203 |
/**
|
| 204 |
* Creates the {bitcache_repositories} table.
|
| 205 |
*
|
| 206 |
* @since 6.x-1.0-beta2
|
| 207 |
*/
|
| 208 |
function bitcache_update_6003() {
|
| 209 |
$updates = array();
|
| 210 |
db_create_table($updates, 'bitcache_repositories',
|
| 211 |
array(
|
| 212 |
'fields' => array(
|
| 213 |
'name' => array(
|
| 214 |
'type' => 'varchar',
|
| 215 |
'length' => 64,
|
| 216 |
'not null' => TRUE,
|
| 217 |
),
|
| 218 |
'module' => array(
|
| 219 |
'type' => 'varchar',
|
| 220 |
'length' => 255,
|
| 221 |
'not null' => TRUE,
|
| 222 |
),
|
| 223 |
'adapter' => array(
|
| 224 |
'type' => 'varchar',
|
| 225 |
'length' => 32,
|
| 226 |
'not null' => TRUE,
|
| 227 |
),
|
| 228 |
'enabled' => array(
|
| 229 |
'type' => 'int',
|
| 230 |
'size' => 'tiny',
|
| 231 |
'length' => 1,
|
| 232 |
'not null' => TRUE,
|
| 233 |
'default' => 0,
|
| 234 |
),
|
| 235 |
'mutable' => array(
|
| 236 |
'type' => 'int',
|
| 237 |
'size' => 'tiny',
|
| 238 |
'length' => 1,
|
| 239 |
'not null' => TRUE,
|
| 240 |
'default' => 0,
|
| 241 |
),
|
| 242 |
'indexed' => array(
|
| 243 |
'type' => 'int',
|
| 244 |
'size' => 'tiny',
|
| 245 |
'length' => 1,
|
| 246 |
'not null' => TRUE,
|
| 247 |
'default' => 0,
|
| 248 |
),
|
| 249 |
'weight' => array(
|
| 250 |
'type' => 'int',
|
| 251 |
'size' => 'tiny',
|
| 252 |
'length' => 4,
|
| 253 |
'not null' => TRUE,
|
| 254 |
'default' => 0,
|
| 255 |
),
|
| 256 |
'options' => array(
|
| 257 |
'type' => 'text',
|
| 258 |
'size' => 'normal',
|
| 259 |
'not null' => TRUE,
|
| 260 |
),
|
| 261 |
),
|
| 262 |
'primary key' => array('name'),
|
| 263 |
)
|
| 264 |
);
|
| 265 |
return $updates;
|
| 266 |
}
|
| 267 |
|
| 268 |
/**
|
| 269 |
* Migrates repository definitions from the {variable} table to the
|
| 270 |
* {bitcache_repositories} table.
|
| 271 |
*
|
| 272 |
* @since 6.x-1.0-beta2
|
| 273 |
*/
|
| 274 |
function bitcache_update_6004() {
|
| 275 |
$updates = array();
|
| 276 |
|
| 277 |
module_load_include('module', 'bitcache');
|
| 278 |
$updates[] = bitcache_update_sql("INSERT INTO {bitcache_repositories} VALUES ('bitcache', 'bitcache', 'sql', 1, 1, 0, -10, '%s')", serialize(array('title' => t('Default'), 'description' => '')));
|
| 279 |
$updates[] = bitcache_update_sql("INSERT INTO {bitcache_repositories} VALUES ('default', '', 'file', 1, 1, 0, -1, '%s')", serialize(array('title' => t('Default (file system)'), 'description' => '', 'location' => BITCACHE_ROOT)));
|
| 280 |
|
| 281 |
$result = db_query("SELECT name, value FROM {variable} WHERE name LIKE 'bitcache_repository[%]' ORDER BY name");
|
| 282 |
while ($variable = db_fetch_object($result)) {
|
| 283 |
if (preg_match('/^bitcache_repository\[([^\]]+)\]$/', $variable->name, $matches)) {
|
| 284 |
$options = unserialize($variable->value);
|
| 285 |
$options = array('title' => @$options['title'], 'description' => @$options['description'], 'location' => @$options['location']); // any other options will be lost
|
| 286 |
$updates[] = $update = bitcache_update_sql("INSERT INTO {bitcache_repositories} VALUES ('" . db_escape_string($matches[1]) . "', '', 'file', 1, 1, 0, 0, '%s')", serialize($options));
|
| 287 |
if (!empty($update['success'])) {
|
| 288 |
$updates[] = bitcache_update_sql("DELETE FROM {variable} WHERE name = '" . db_escape_string($variable->name) . "'");
|
| 289 |
}
|
| 290 |
}
|
| 291 |
}
|
| 292 |
|
| 293 |
return $updates;
|
| 294 |
}
|
| 295 |
|
| 296 |
/**
|
| 297 |
* Creates the {bitcache_index} table.
|
| 298 |
*
|
| 299 |
* @since 6.x-1.0-beta3
|
| 300 |
*/
|
| 301 |
function bitcache_update_6005() {
|
| 302 |
$updates = array();
|
| 303 |
db_create_table($updates, 'bitcache_index',
|
| 304 |
array(
|
| 305 |
'fields' => array(
|
| 306 |
'id' => array(
|
| 307 |
'type' => 'char',
|
| 308 |
'length' => 40,
|
| 309 |
'not null' => TRUE,
|
| 310 |
),
|
| 311 |
'size' => array(
|
| 312 |
'type' => 'int',
|
| 313 |
'unsigned' => TRUE,
|
| 314 |
),
|
| 315 |
'in_bitcache' => array(
|
| 316 |
'type' => 'int',
|
| 317 |
'size' => 'tiny',
|
| 318 |
'length' => 1,
|
| 319 |
'not null' => TRUE,
|
| 320 |
'default' => 0,
|
| 321 |
),
|
| 322 |
),
|
| 323 |
'primary key' => array('id'),
|
| 324 |
)
|
| 325 |
);
|
| 326 |
return $updates;
|
| 327 |
}
|
| 328 |
|
| 329 |
/**
|
| 330 |
* Populates the {bitcache_index} table.
|
| 331 |
*
|
| 332 |
* @since 6.x-1.0-beta3
|
| 333 |
*/
|
| 334 |
function bitcache_update_6006() {
|
| 335 |
$updates = array();
|
| 336 |
$updates[] = bitcache_update_sql("UPDATE {bitcache_repositories} SET indexed = 1 WHERE name = 'bitcache'");
|
| 337 |
$result = db_query("SELECT id, LENGTH(data) AS size FROM {bitcache_data} ORDER BY id");
|
| 338 |
while ($row = db_fetch_object($result)) {
|
| 339 |
@db_query("INSERT INTO {bitcache_index} (id, size, in_bitcache) VALUES ('%s', %d, 1)", $row->id, $row->size);
|
| 340 |
}
|
| 341 |
return $updates;
|
| 342 |
}
|
| 343 |
|
| 344 |
/**
|
| 345 |
* Rewrites the existing file system paths to use tokens.
|
| 346 |
*
|
| 347 |
* @since 6.x-1.0-beta3
|
| 348 |
*/
|
| 349 |
function bitcache_update_6007() {
|
| 350 |
$updates = array();
|
| 351 |
$result = db_query("SELECT name, options FROM {bitcache_repositories} WHERE adapter = 'file' ORDER BY enabled, name");
|
| 352 |
while ($row = db_fetch_object($result)) {
|
| 353 |
$row->options = unserialize($row->options);
|
| 354 |
$row->options['location'] = preg_replace('!^' . preg_quote(file_directory_path()) . '$!', '[file-directory-path]', $row->options['location']);
|
| 355 |
$row->options['location'] = preg_replace('!^' . preg_quote(file_directory_path()) . '/!', '[file-directory-path]/', $row->options['location']);
|
| 356 |
$row->options = serialize($row->options);
|
| 357 |
$updates[] = bitcache_update_sql("UPDATE {bitcache_repositories} SET options = '%s' WHERE name = '%s'", $row->options, $row->name);
|
| 358 |
}
|
| 359 |
return $updates;
|
| 360 |
}
|
| 361 |
|
| 362 |
//////////////////////////////////////////////////////////////////////////////
|
| 363 |
// Database API helpers
|
| 364 |
|
| 365 |
function bitcache_update_sql($sql) {
|
| 366 |
$arguments = array_slice(func_get_args(), 1);
|
| 367 |
return array('success' => (db_query($sql, $arguments) !== FALSE), 'query' => check_plain($sql));
|
| 368 |
}
|