[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/audio/ -> audio.install (source)

   1  <?php
   2  // $Id: audio.install,v 1.24 2008/12/21 23:21:18 drewish Exp $
   3  
   4  /**
   5   * Implementation of hook_install().
   6   */
   7  function audio_install() {
   8    drupal_install_schema('audio');
   9    _audio_add_default_perms();
  10  }
  11  
  12  /**
  13   * Implementation of hook_uninstall().
  14   */
  15  function audio_uninstall() {
  16    drupal_uninstall_schema('audio');
  17    variable_del('audio_allowed_extensions');
  18    variable_del('audio_block_random_n');
  19    variable_del('audio_default_downloadable');
  20    variable_del('audio_default_title_format');
  21    variable_del('audio_player_mp3');
  22    variable_del('audio_player_wav');
  23    variable_del('audio_show_form_title');
  24    variable_del('audio_tag_settings');
  25    variable_del('audio_teaser_format');
  26  }
  27  
  28  function audio_schema() {
  29    $schema['audio'] = array(
  30      'description' => t('Main audio table.'),
  31      'fields' => array(
  32        'vid' => array(
  33          'type' => 'int',
  34          'unsigned' => TRUE,
  35          'not null' => TRUE,
  36          'default' => 0,
  37        ),
  38        'nid' => array(
  39          'type' => 'int',
  40          'unsigned' => TRUE,
  41          'not null' => TRUE,
  42          'default' => 0,
  43        ),
  44        'fid' => array(
  45          'type' => 'int',
  46          'unsigned' => TRUE,
  47          'not null' => TRUE,
  48          'description' => t('Primary Key: The {files}.fid.'),
  49        ),
  50        'title_format' => array(
  51          'type' => 'varchar',
  52          'length' => 128,
  53          'default' => '',
  54        ),
  55        'play_count' => array(
  56          'type' => 'int',
  57          'size' => 'medium',
  58          'not null' => TRUE,
  59          'default' => 0,
  60        ),
  61        'download_count' => array(
  62          'type' => 'int',
  63          'size' => 'medium',
  64          'not null' => TRUE,
  65          'default' => 0,
  66        ),
  67        'downloadable' => array(
  68          'type' => 'int',
  69          'size' => 'tiny',
  70          'not null' => TRUE,
  71          'default' => 1,
  72        ),
  73        'format' => array(
  74          'type' => 'varchar',
  75          'length' => 10,
  76          'not null' => TRUE,
  77          'default' => '',
  78        ),
  79        'sample_rate' => array(
  80          'type' => 'int',
  81          'size' => 'medium',
  82          'not null' => TRUE,
  83          'default' => 0,
  84        ),
  85        'channel_mode' => array(
  86          'type' => 'varchar',
  87          'length' => 10,
  88          'not null' => TRUE,
  89          'default' => '',
  90        ),
  91        'bitrate' => array(
  92          'type' => 'float',
  93          'size' => 'medium',
  94          'not null' => TRUE,
  95          'default' => 0,
  96        ),
  97        'bitrate_mode' => array(
  98          'type' => 'varchar',
  99          'length' => 4,
 100          'not null' => TRUE,
 101          'default' => '',
 102        ),
 103        'playtime' => array(
 104          'type' => 'varchar',
 105          'length' => 10,
 106          'not null' => TRUE,
 107          'default' => '',
 108        ),
 109      ),
 110      'primary key' => array('vid'),
 111      'indexes' => array(
 112        'audio_fid' => array('fid'),
 113      ),
 114    );
 115    $schema['audio_metadata'] = array(
 116      'description' => t('Extended data about audio files.'),
 117      'fields' => array(
 118        'vid' => array(
 119          'type' => 'int',
 120          'unsigned' => TRUE,
 121          'not null' => TRUE,
 122          'default' => 0,
 123        ),
 124        'tag' => array(
 125          'type' => 'varchar',
 126          'length' => 45,
 127          'not null' => TRUE,
 128          'default' => '',
 129        ),
 130        'value' => array(
 131          'type' => 'varchar',
 132          'length' => 255,
 133          'not null' => TRUE,
 134          'default' => '',
 135        ),
 136        'clean' => array(
 137          'type' => 'varchar',
 138          'length' => 255,
 139          'not null' => TRUE,
 140          'default' => '',
 141        ),
 142      ),
 143      'primary key' => array('vid', 'tag', 'value'),
 144      'indexes' => array(
 145        'audio_metadata_tags' => array('clean'),
 146      ),
 147    );
 148    return $schema;
 149  }
 150  
 151  /**
 152   * Add permission to download and view audio to the anonymous and authenticated
 153   * roles by default.
 154   */
 155  function _audio_add_default_perms() {
 156    $newperms = array('download audio', 'play audio', 'view download stats');
 157    $rids = array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID);
 158    foreach ($rids as $rid) {
 159      $result = db_result(db_query("SELECT perm FROM {permission} WHERE rid = %d", $rid));
 160      if ($result) {
 161        $perms = explode(', ', $result);
 162        foreach ($newperms as $newperm) {
 163          if (!in_array($newperm, $perms)) {
 164            $perms[] = $newperm;
 165          }
 166        }
 167        db_query("UPDATE {permission} SET perm = '%s' WHERE rid = %d", implode(', ', $perms), $rid);
 168      }
 169      else {
 170        db_query("INSERT INTO {permission} (rid, perm) VALUES (%d, '%s')", $rid, implode(', ', $newperms));
 171      }
 172    }
 173  }
 174  
 175  /**
 176   * Implementation of hook_update_last_removed().
 177   */
 178  function audio_update_last_removed() {
 179    return 111;
 180  }
 181  
 182  /**
 183   * Drop the audio_file.origname field.
 184   *
 185   * This is the first update for the 5.2 branch, using the update naming
 186   * convention described in: http://drupal.org/node/114774#update-n
 187   */
 188  function audio_update_5200() {
 189    $ret = array();
 190    $ret[] = update_sql("UPDATE {audio_file} SET filename = origname");
 191    db_drop_field($ret, 'audio_file', 'origname');
 192    return $ret;
 193  }
 194  
 195  /**
 196   * Use the token module for title and teasers.
 197   */
 198  function audio_update_5201() {
 199    $ret = array();
 200  
 201    // Build an array of conversions. First the hard coded values...
 202    $tokens = array(
 203      '!filelength' => '[audio-file-length]',
 204      '!fileformat' => '[audio-file-format]',
 205      '!player' => '[audio-player]',
 206      '!filename' => '[audio-filename]',
 207    );
 208    // ...then the tags.
 209    foreach (audio_get_tags_allowed() as $tag) {
 210      $tokens['!'. $tag] = '[audio-tag-'. strtr($tag, '_', '-') .']';
 211    }
 212  
 213    // Gather a list of all the different title formats and then replace them
 214    // with the new token based equivalents.
 215    $result = db_query('SELECT DISTINCT title_format FROM {audio}');
 216    while ($o = db_fetch_object($result)) {
 217      $new_value = strtr($o->title_format, $tokens);
 218      db_query("UPDATE {audio} SET title_format = '%s' WHERE title_format = '%s'", $new_value, $o->title_format);
 219    }
 220  
 221    // Update the default title and teaser formats.
 222    foreach (array('audio_default_title_format', 'audio_teaser_format') as $variable) {
 223      if ($old_value = variable_get($variable, FALSE)) {
 224        $new_value = strtr($old_value, $tokens);
 225        variable_set($variable, $new_value);
 226      }
 227    }
 228  
 229    // Let them know if they don't have token installed.
 230    if (!module_exists('token')) {
 231      drupal_set_message(t('The audio module requires that the <a href="http://drupal.org/project/token">token module</a> be installed. You should install it as soon as possible.'));
 232    }
 233  
 234    return $ret;
 235  }
 236  
 237  /**
 238   * Move the fields from {audio_file} to {audio}. Rename the file fields
 239   * in preparation for having remote_* fields.
 240   */
 241  function audio_update_5202() {
 242    $ret = array();
 243    db_change_field($ret, 'audio', 'fileformat', 'file_format',
 244      array(
 245        'type' => 'varchar',
 246        'length' => 10,
 247        'not null' => TRUE,
 248        'default' => '',
 249      )
 250    );
 251    db_add_field($ret, 'audio', 'file_mime',
 252      array(
 253        'type' => 'varchar',
 254        'length' => 255,
 255        'not null' => TRUE,
 256        'default' => '',
 257      )
 258    );
 259    db_add_field($ret, 'audio', 'file_name',
 260      array(
 261        'type' => 'varchar',
 262        'length' => 255,
 263        'not null' => TRUE,
 264        'default' => '',
 265      )
 266    );
 267    db_add_field($ret, 'audio', 'file_path',
 268      array(
 269        'type' => 'varchar',
 270        'length' => 255,
 271        'not null' => TRUE,
 272        'default' => '',
 273      )
 274    );
 275    db_add_field($ret, 'audio', 'file_size',
 276      array(
 277        'type' => 'int',
 278        'size' => 'medium',
 279        'not null' => TRUE,
 280        'default' => 0,
 281      )
 282    );
 283    db_add_field($ret, 'audio', 'remote_size',
 284      array(
 285        'type' => 'varchar',
 286        'length' => 255,
 287        'not null' => TRUE,
 288        'default' => '',
 289      )
 290    );
 291    db_add_field($ret, 'audio', 'remote_size',
 292      array(
 293        'type' => 'int',
 294        'size' => 'medium',
 295        'not null' => TRUE,
 296        'default' => 0,
 297      )
 298    );
 299    $ret[] = update_sql("UPDATE {audio} a INNER JOIN {audio_file} af ON a.vid = af.vid SET a.file_name = af.filename, a.file_path = af.filepath, a.file_mime = af.filemime, a.file_size = af.filesize");
 300    db_drop_table($ret, 'audio_file');
 301  
 302    // Build an array of renamed tokens.
 303    $tokens = array(
 304      '[audio-file-format]' => '[audio-format]', // Work around name conflicts.
 305      '[audio-file-length]' => '[audio-length]',
 306      '[audio-filename]' => '[audio-file-name]',
 307      '[audio-filepath]' => '[audio-file-path]',
 308      '[audio-filemime]' => '[audio-file-mime]',
 309      '[audio-filesize]' => '[audio-file-size]',
 310    );
 311  
 312    // Gather a list of all the different title formats and then replace them
 313    // with the new token based equivalents.
 314    $result = db_query('SELECT DISTINCT title_format FROM {audio}');
 315    while ($o = db_fetch_object($result)) {
 316      $new_value = strtr($o->title_format, $tokens);
 317      if ($new_value != $o->title_format) {
 318        db_query("UPDATE {audio} SET title_format = '%s' WHERE title_format = '%s'", $new_value, $o->title_format);
 319      }
 320    }
 321  
 322    // Update the default title and teaser formats.
 323    foreach (array('audio_default_title_format', 'audio_teaser_format') as $variable) {
 324      if ($old_value = variable_get($variable, FALSE)) {
 325        variable_set($variable, strtr($old_value, $tokens));
 326      }
 327    }
 328  
 329    return $ret;
 330  }
 331  
 332  
 333  /**
 334   * Now that we've got raw tokens we need to update the existing audio nodes to
 335   * update the title_format and replace the existing audio-tag-* tokens with the
 336   * new audio-tag-*-raw tokens.
 337   */
 338  function audio_update_5203() {
 339    $ret = array();
 340  
 341    // Build an array of tokens to rename.
 342    $tokens = array();
 343    foreach (audio_get_tags_allowed() as $tag) {
 344      $name = strtr($tag, '_', '-');
 345      $tokens["[audio-tag-$name]"] =  "[audio-tag-$name-raw]";
 346    }
 347  
 348    // Gather a list of all the different title formats and then replace them
 349    // with the new token based equivalents.
 350    $result = db_query('SELECT DISTINCT title_format FROM {audio}');
 351    while ($o = db_fetch_object($result)) {
 352      $new_value = strtr($o->title_format, $tokens);
 353      if ($new_value != $o->title_format) {
 354        db_query("UPDATE {audio} SET title_format = '%s' WHERE title_format = '%s'", $new_value, $o->title_format);
 355        $ret[] = array('success' => TRUE, 'query' => check_plain("Updated audio node titles from '$o->title_format' to '$new_value'."));
 356      }
 357    }
 358  
 359    // Update the default title format.
 360    if ($old_value = variable_get('audio_default_title_format', FALSE)) {
 361      $new_value = strtr($old_value, $tokens);
 362      if ($old_value != $new_value) {
 363        variable_set('audio_default_title_format', $new_value);
 364        $ret[] = array('success' => TRUE, 'query' => check_plain("Updated the default audio node title from '$old_value' to '$new_value'."));
 365      }
 366    }
 367  
 368    return $ret;
 369  }
 370  
 371  /**
 372   * Use unsigned normal int for vid, nid and file_size.
 373   */
 374  function audio_update_6000() {
 375    $ret = array();
 376    db_drop_primary_key($ret, 'audio');
 377    db_change_field($ret, 'audio', 'vid', 'vid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0), array('primary key' => array('vid')));
 378    db_change_field($ret, 'audio', 'nid', 'nid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
 379    db_change_field($ret, 'audio', 'file_size', 'file_size', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
 380    return $ret;
 381  }
 382  
 383  /**
 384   * Ensure previous update was applied; use unsigned normal int for audio_metadata vid.
 385   */
 386  function audio_update_6001() {
 387    $ret = array();
 388    db_drop_primary_key($ret, 'audio');
 389    db_change_field($ret, 'audio', 'vid', 'vid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0), array('primary key' => array('vid')));
 390    db_drop_primary_key($ret, 'audio_metadata');
 391    db_change_field($ret, 'audio_metadata', 'vid', 'vid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
 392    db_add_primary_key($ret, 'audio_metadata', array('vid', 'tag', 'value'));
 393    return $ret;
 394  }
 395  
 396  /**
 397   * Move the audio files from the {audio} table to the {files} table and then
 398   * remove the columns.
 399   *
 400   * @return unknown
 401   */
 402  function audio_update_6002() {
 403    $ret = array();
 404  
 405    // Create the fid field so we've got a place to store the file id's as we
 406    // migrate them.
 407    $fid_field = array(
 408      'type' => 'int',
 409      'unsigned' => TRUE,
 410      'not null' => TRUE,
 411      'description' => t('Primary Key: The {files}.fid.'),
 412    );
 413    db_add_field($ret, 'audio', 'fid', $fid_field);
 414    db_add_index($ret, 'audio', 'audio_fid', array('fid'));
 415  
 416    // Load all the distinct filepaths, note that this may update multiple audio
 417    // rows at once.
 418    $result = db_query("SELECT DISTINCT file_path FROM {audio} WHERE fid IS NULL OR fid = 0");
 419    while ($file = db_fetch_object($result)) {
 420      // Then move the data into the files table.
 421      db_query("INSERT INTO {files} (uid, filename, filepath, filemime, filesize, status, timestamp) SELECT n.uid, '%s', ai.file_path, ai.file_mime, ai.file_size, 1, n.created AS timestamp FROM {node} n INNER JOIN {audio} ai ON n.vid = ai.vid WHERE ai.file_path = '%s' LIMIT 1", array(basename($file->file_path), $file->file_path));
 422      db_query("UPDATE {audio} SET fid = %d WHERE file_path = '%s'", db_last_insert_id('files', 'fid'), $file->file_path);
 423    }
 424  
 425    // Drop the old fields.
 426    db_drop_field($ret, 'audio', 'file_mime');
 427    db_drop_field($ret, 'audio', 'file_name');
 428    db_drop_field($ret, 'audio', 'file_path');
 429    db_drop_field($ret, 'audio', 'file_size');
 430  
 431    // Rename the file_format field to format.
 432    $format_field = array(
 433      'type' => 'varchar',
 434      'length' => 10,
 435      'not null' => TRUE,
 436      'default' => '',
 437    );
 438    db_change_field($ret, 'audio', 'file_format', 'format', $format_field);
 439  
 440    return $ret;
 441  }


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7