[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

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

   1  <?php
   2  // $Id: image.install,v 1.33.2.3 2010/08/25 13:35:11 joachim Exp $
   3  
   4  /**
   5   * Implementation of hook_schema().
   6   */
   7  function image_schema() {
   8    $schema['image'] = array(
   9      'description' => 'Stores image files information.',
  10      'fields' => array(
  11        'nid' => array(
  12          'description' => 'Primary Key: The {node}.nid of the image node.',
  13          'type' => 'int',
  14          'unsigned' => TRUE,
  15          'not null' => TRUE,
  16          'default' => 0,
  17        ),
  18        'fid' => array(
  19          'description' => 'Index: The {files}.fid of the image file.',
  20          'type' => 'int',
  21          'unsigned' => TRUE,
  22          'not null' => TRUE,
  23          'default' => 0,
  24        ),
  25        'image_size' => array(
  26          'description' => 'Primary Key: The {files}.filename of the image file. For image module this indicates the file size.',
  27          'type' => 'varchar',
  28          'length' => 32,
  29          'not null' => TRUE,
  30          'default' => '',
  31        ),
  32      ),
  33      'primary key' => array('nid', 'image_size'),
  34      'indexes' => array(
  35        'fid' => array('fid'),
  36      ),
  37    );
  38    return $schema;
  39  }
  40  
  41  /**
  42   * Implementation of hook_install().
  43   */
  44  function image_install() {
  45    drupal_install_schema('image');
  46    // Set reasonable default node options (not promoted to front page).
  47    variable_set('node_options_image', array('status'));
  48  }
  49  
  50  /**
  51   * Implementation of hook_uninstall().
  52   */
  53  function image_uninstall() {
  54    drupal_uninstall_schema('image');
  55  
  56    variable_del('image_max_upload_size');
  57    variable_del('image_updated');
  58    variable_del('image_default_path');
  59    variable_del('image_sizes');
  60    variable_del('image_block_0_number_images');
  61    variable_del('image_block_1_number_images');
  62  }
  63  
  64  /**
  65   * Implementation of hook_requirements().
  66   */
  67  function image_requirements($phase) {
  68    $requirements = array();
  69  
  70    if ($phase == 'runtime') {
  71      // Make sure we've got a working toolkit
  72      if ($toolkit = image_get_toolkit()) {
  73        $requirements['image_toolkit'] = array(
  74          'value' => t('The %toolkit_name toolkit is installed.', array('%toolkit_name' => $toolkit)),
  75          'severity' => REQUIREMENT_OK,
  76        );
  77      }
  78      else {
  79        $requirements['image_toolkit'] = array(
  80          'value' => t('Not installed.'),
  81          'severity' => REQUIREMENT_ERROR,
  82          'description' => t('No image toolkit is currently enabled. Without one the image module will not be able to resize your images. You can select one from the <a href="@link">image toolkit settings page</a>.', array('@link' => url('admin/settings/image-toolkit'))),
  83        );
  84      }
  85      $requirements['image_toolkit']['title'] = t('Image toolkit');
  86  
  87  
  88      // File paths
  89      $image_path = file_create_path(file_directory_path() . '/' . variable_get('image_default_path', 'images'));
  90      $temp_path = $image_path . '/temp';
  91      if (!file_check_directory($image_path, FILE_CREATE_DIRECTORY)) {
  92        $requirements['image_dirs'] = array(
  93          'value' => t('Missing directory.'),
  94          'severity' => REQUIREMENT_ERROR,
  95          'description' => t("The image module's image directory %image_dir is missing.", array('%image_dir' => $image_path)),
  96        );
  97      }
  98      else if (!file_check_directory($temp_path, FILE_CREATE_DIRECTORY)) {
  99        $requirements['image_dirs'] = array(
 100          'value' => t('Missing temp directory.'),
 101          'severity' => REQUIREMENT_ERROR,
 102          'description' => t("The image module's temp directory %temp_dir is missing.", array('%temp_dir' => $temp_path)),
 103        );
 104      }
 105      else {
 106        $requirements['image_dirs'] = array(
 107          'value' => t('Exists (%path).', array('%path' => $image_path)),
 108          'severity' => REQUIREMENT_OK,
 109        );
 110      }
 111      $requirements['image_dirs']['title'] = t('Image module directories');
 112    }
 113  
 114    return $requirements;
 115  }
 116  
 117  /**
 118   * Upgrade to the new image_sizes variable format.
 119   */
 120  function image_update_2() {
 121    $sizes = variable_get('image_sizes', FALSE);
 122    if ($sizes) {
 123      $new_sizes = array(IMAGE_ORIGINAL => array('width' => '', 'height' => '', 'label' => t('Original')));
 124      foreach ($sizes as $size) {
 125        $key = drupal_strtolower($size['label']);
 126        $size['label'] = drupal_ucfirst($size['label']);
 127        $new_sizes[$key] = $size;
 128      }
 129      variable_set('image_sizes', $new_sizes);
 130    }
 131    return array();
 132  }
 133  
 134  /**
 135   * Add the link field to each size.
 136   */
 137  function image_update_3() {
 138    $sizes = variable_get('image_sizes', FALSE);
 139    if ($sizes) {
 140      $new_sizes = array();
 141      foreach ($sizes as $key => $size) {
 142        $size['link'] = 1;
 143        $new_sizes[$key] = $size;
 144      }
 145      variable_set('image_sizes', $new_sizes);
 146    }
 147    return array();
 148  }
 149  
 150  /**
 151   * Clean up all the records that aren't in the files directory.
 152   */
 153  function image_update_4() {
 154    $ret = array();
 155  
 156    // Locate image files that aren't stored in the files directory.
 157    $files_path = rtrim(file_directory_path(), '\\/');
 158    $result = db_query("SELECT f.nid, f.fid, f.filename, f.filepath FROM {files} f INNER JOIN {node} n ON f.nid = n.nid WHERE n.type = 'image' AND f.filename = '_original' AND NOT f.filepath LIKE '%s/%%'", $files_path);
 159    while ($file = db_fetch_object($result)) {
 160      $file->filepath = file_create_path($file->filepath);
 161      if (file_exists($file->filepath)) {
 162        // File exists, make sure there's not a duplicate record.
 163        if (db_result(db_query("SELECT COUNT(*) FROM {files} f INNER JOIN {node} n ON f.nid = n.nid WHERE n.type = 'image' AND filepath = '%s' AND fid <> %d", $file->filepath, $file->fid))) {
 164          $ret[] = update_sql("DELETE FROM {files} WHERE fid = " . (int) $file->fid);
 165          $ret[] = update_sql("DELETE FROM {file_revisions} WHERE fid = " . (int) $file->fid);
 166        }
 167        else {
 168          $ret[] = update_sql("UPDATE {files} SET filepath = '" . db_escape_string($file->filepath) . "' WHERE fid = " . (int) $file->fid);
 169        }
 170      }
 171      else {
 172        $ret[] = update_sql("DELETE FROM {files} WHERE fid = " . (int) $file->fid);
 173        $ret[] = update_sql("DELETE FROM {file_revisions} WHERE fid = " . (int) $file->fid);
 174      }
 175    }
 176  
 177    // Check for and remove {files} with duplicate filenames.
 178    $result = db_query("SELECT f1.fid, f1.nid, f1.filepath FROM {files} f1 INNER JOIN {node} n ON f1.nid = n.nid  WHERE n.type = 'image' AND EXISTS ( SELECT * FROM {files} f2 WHERE f2.filepath = f1.filepath AND f1.fid <> f2.fid AND f1.fid < f2.fid )");
 179    while ($file = db_fetch_object($result)) {
 180      $ret[] = update_sql("DELETE FROM {files} WHERE fid = " . (int) $file->fid);
 181    }
 182  
 183    // Delete rows from {file_revisions} that don't have matching {files}.
 184    $ret[] = update_sql("DELETE FROM {file_revisions} WHERE NOT EXISTS (SELECT * FROM {files} WHERE {files}.fid = {file_revisions}.fid)");
 185  
 186    return $ret;
 187  }
 188  
 189  /**
 190   * Make sure that everyone's size settings are in the right format.
 191   */
 192  function image_update_5() {
 193    $ret = array();
 194  
 195    if ($old_sizes = variable_get('image_sizes', FALSE)) {
 196      // Make sure all the required sizes are represented.
 197      if (!isset($old_sizes[IMAGE_ORIGINAL])) {
 198        drupal_set_message(t("The original image size was missing so no changes were made. See this <a href='@link'>image module issue</a> for more information. Include the following:<br /><pre>@old_sizes\n</pre>", array('@link' => 'http://drupal.org/node/158334', '@old_sizes' => print_r($old_sizes, 1))), 'error');
 199        return array();
 200      }
 201      // These sizes may already exist under incorrect keys. We'll put a default
 202      // copy in that will either be overwritten by the existing version, or used
 203      // if there isn't an existing version.
 204      if (!isset($old_sizes[IMAGE_PREVIEW])) {
 205        $old_sizes[IMAGE_PREVIEW] = array('width' => 640, 'height' => 640, 'label' => t('Preview'), 'link' => IMAGE_LINK_SHOWN);
 206      }
 207      if (!isset($old_sizes[IMAGE_THUMBNAIL])) {
 208        $old_sizes[IMAGE_THUMBNAIL] = array('width' => 100, 'height' => 100, 'label' => t('Thumbnail'), 'link' => IMAGE_LINK_SHOWN);
 209      }
 210  
 211      $new_sizes = array();
 212      foreach ($old_sizes as $old_key => $size) {
 213        // Keys should be lowercase and less than 32 chars long.
 214        $new_key = drupal_strtolower(drupal_substr($old_key, 0, 32));
 215        // Update the files.
 216        if ($new_key != $old_key) {
 217          $ret[] = update_sql("UPDATE {files} f INNER JOIN {node} n ON f.nid = n.nid SET f.filename = '" . db_escape_string($new_key) . "' WHERE n.type = 'image' AND filename = '" . db_escape_string($old_key) . "'");
 218        }
 219        $new_sizes[$new_key] = $size;
 220      }
 221      // Save the sizes.
 222      variable_set('image_sizes', $new_sizes);
 223    }
 224  
 225    return $ret;
 226  }
 227  
 228  /**
 229   * Move image files into their own table.
 230   *
 231   * First update for the 5.2 branch, using the update naming convention layed
 232   * out in: http://drupal.org/node/114774#update-n
 233   */
 234  function image_update_5200() {
 235    $ret = array();
 236  
 237    // Versions prior to 5 had an {image} table which is no longer used on 5.1
 238    // but not removed.
 239    if (db_table_exists('image')) {
 240      db_rename_table($ret, 'image', 'image_old');
 241      // Notify the user that their table has been moved.
 242      drupal_set_message('Your existing {image} table has been renamed {image_old}.');
 243    }
 244  
 245    switch ($GLOBALS['db_type']) {
 246      case 'mysql':
 247      case 'mysqli':
 248        $ret[] = update_sql("CREATE TABLE {image} (
 249            `nid` INTEGER UNSIGNED NOT NULL,
 250            `fid` INTEGER UNSIGNED NOT NULL,
 251            `image_size` VARCHAR(32) NOT NULL,
 252            PRIMARY KEY (`nid`, `image_size`),
 253            INDEX image_fid(`fid`)
 254          ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
 255        break;
 256  
 257      case 'pgsql':
 258        $ret[] = update_sql("CREATE TABLE {image} (
 259            nid int_unsigned NOT NULL,
 260            fid int_unsigned NOT NULL,
 261            image_size VARCHAR(32) NOT NULL,
 262            PRIMARY KEY (nid, image_size)
 263          );");
 264        $ret[] = update_sql("CREATE INDEX {image_fid} on {image}(fid);");
 265        break;
 266    }
 267    // Copy image files records into the new table.
 268    drupal_load('module', 'image');
 269    $args = array_map('db_escape_string', array_keys(image_get_sizes()));
 270    $cond = " IN ('" . implode("', '", $args) . "')";
 271  
 272    // Upgrade from 5.x-1.x to 5.x-2.x.
 273    // IGNORE prevents duplicate insertions from bad data:
 274    // @see http://drupal.org/node/207557
 275    if (db_table_exists('file_revisions')) {
 276      $ret[] = update_sql("INSERT IGNORE INTO {image} SELECT DISTINCT f.nid, f.fid, f.filename FROM {files} f INNER JOIN {node} n ON f.nid = n.nid WHERE n.type = 'image' AND f.filename" . $cond);
 277      $ret[] = update_sql("DELETE FROM {file_revisions} WHERE EXISTS (SELECT * FROM {image} WHERE {image}.fid = {file_revisions}.fid)");
 278    }
 279    // Upgrade from 5.x to 6.x-1.x.
 280    else {
 281      $ret[] = update_sql("INSERT IGNORE INTO {image} SELECT DISTINCT u.nid, f.fid, f.filename FROM {upload} u INNER JOIN {files} f ON u.fid = f.fid INNER JOIN {node} n ON u.nid = n.nid WHERE n.type = 'image' AND f.filename" . $cond);
 282      $ret[] = update_sql("DELETE FROM {upload} WHERE EXISTS (SELECT * FROM {image} WHERE {image}.fid = {upload}.fid)");
 283    }
 284  
 285    return $ret;
 286  }
 287  
 288  /**
 289   * Updating image size settings to use scaling as the default operation.
 290   */
 291  function image_update_5201() {
 292    drupal_load('module', 'image');
 293    $sizes = image_get_sizes();
 294    foreach ($sizes as $key => $size) {
 295      $sizes[$key]['operation'] = 'scale';
 296    }
 297    variable_set('image_sizes', $sizes);
 298  
 299    return array();
 300  }
 301  
 302  /**
 303   * Add primary key and index on 'fid' to {image} table.
 304   */
 305  function image_update_6100() {
 306    $ret = array();
 307    db_change_field($ret, 'image', 'nid', 'nid', array(
 308      'type' => 'int',
 309      'unsigned' => TRUE,
 310      'not null' => TRUE,
 311      'default' => 0,
 312    ));
 313    db_change_field($ret, 'image', 'fid', 'fid', array(
 314      'type' => 'int',
 315      'unsigned' => TRUE,
 316      'not null' => TRUE,
 317      'default' => 0,
 318    ));
 319  
 320    /**
 321     * We remove and re-add primary keys and indexes because the column types
 322     * are changed.
 323     * Sometimes however these don't exist in the first place (@see
 324     * <http://drupal.org/node/562810>; the @ takes care of suppressing the
 325     * error message this causes.
 326     */
 327    @db_drop_primary_key($ret, 'image');
 328    db_add_primary_key($ret, 'image', array('nid', 'image_size'));
 329    @db_drop_index($ret, 'image', 'image_fid');
 330    db_add_index($ret, 'image', 'fid', array('fid'));
 331  
 332    // Notify the user that they may get harmless fail messages here.
 333    $ret[] = array('success' => TRUE, 'query' => t('If you see a message of the form "Failed: ALTER TABLE {image} DROP PRIMARY KEY" or "DROP INDEX image_fid" here it is harmless.'));
 334    return $ret;
 335  }
 336  
 337  /**
 338   * Ensure that 'image_default_path' variable contains no trailing slash.
 339   */
 340  function image_update_6101() {
 341    $ret = array();
 342    $path = variable_get('image_default_path', NULL);
 343    if (!empty($path)) {
 344      variable_set('image_default_path', rtrim($path, '/'));
 345    }
 346    return $ret;
 347  }
 348  
 349  /**
 350   * Set reasonable default node options (not promoted to front page).
 351   */
 352  function image_update_6102() {
 353    $ret = array();
 354    variable_set('node_options_image', variable_get('node_options_image', array('status')));
 355    return $ret;
 356  }
 357  
 358  /**
 359   * Rename permission "edit images" to "edit any images".
 360   */
 361  function image_update_6103() {
 362    $ret = array();
 363    $result = db_query("SELECT rid, perm FROM {permission} ORDER BY rid");
 364    while ($role = db_fetch_object($result)) {
 365      $renamed_permission = strtr($role->perm, array(
 366        'edit images' => 'edit any images, delete any images',
 367        'edit own images' => 'edit own images, delete own images',
 368      ));
 369      if ($renamed_permission != $role->perm) {
 370        $args = array($renamed_permission, $role->rid);
 371        $query = "UPDATE {permission} SET perm = '%s' WHERE rid = %d";
 372        _db_query_callback($args, TRUE);
 373        $query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query);
 374        $ret[] = update_sql($query);
 375      }
 376    }
 377    return $ret;
 378  }
 379  
 380  /**
 381   * Fix default value for {image}.image_size, introduced in image_update_5200().
 382   */
 383  function image_update_6104() {
 384    $ret = array();
 385    $query = "ALTER TABLE {image} ALTER COLUMN image_size SET DEFAULT ''";
 386    $ret[] = update_sql($query);
 387    return $ret;
 388  }
 389  


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7