Projet

Général

Profil

Paste
Télécharger (14,8 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / image / image.install @ 01f36513

1
<?php
2

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

    
8
/**
9
 * Implements hook_install().
10
 */
11
function image_install() {
12
  // Create the styles directory and ensure it's writable.
13
  $directory = file_default_scheme() . '://styles';
14
  file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
15
}
16

    
17
/**
18
 * Implements hook_uninstall().
19
 */
20
function image_uninstall() {
21
  // Remove the styles directory and generated images.
22
  file_unmanaged_delete_recursive(file_default_scheme() . '://styles');
23
}
24

    
25
/**
26
 * Implements hook_schema().
27
 */
28
function image_schema() {
29
  $schema = array();
30

    
31
  $schema['cache_image'] = drupal_get_schema_unprocessed('system', 'cache');
32
  $schema['cache_image']['description'] = 'Cache table used to store information about image manipulations that are in-progress.';
33

    
34
  $schema['image_styles'] = array(
35
    'description' => 'Stores configuration options for image styles.',
36
    'fields' => array(
37
      'isid' => array(
38
        'description' => 'The primary identifier for an image style.',
39
        'type' => 'serial',
40
        'unsigned' => TRUE,
41
        'not null' => TRUE,
42
      ),
43
      'name' => array(
44
        'description' => 'The style machine name.',
45
        'type' => 'varchar',
46
        'length' => 255,
47
        'not null' => TRUE,
48
      ),
49
      'label' => array(
50
        'description' => 'The style administrative name.',
51
        'type' => 'varchar',
52
        'length' => 255,
53
        'not null' => TRUE,
54
        'default' => '',
55
      ),
56
    ),
57
    'primary key' => array('isid'),
58
    'unique keys' => array(
59
      'name' => array('name'),
60
    ),
61
  );
62

    
63
  $schema['image_effects'] = array(
64
    'description' => 'Stores configuration options for image effects.',
65
    'fields' => array(
66
      'ieid' => array(
67
        'description' => 'The primary identifier for an image effect.',
68
        'type' => 'serial',
69
        'unsigned' => TRUE,
70
        'not null' => TRUE,
71
      ),
72
      'isid' => array(
73
        'description' => 'The {image_styles}.isid for an image style.',
74
        'type' => 'int',
75
        'unsigned' => TRUE,
76
        'not null' => TRUE,
77
        'default' => 0,
78
      ),
79
      'weight' => array(
80
        'description' => 'The weight of the effect in the style.',
81
        'type' => 'int',
82
        'unsigned' => FALSE,
83
        'not null' => TRUE,
84
        'default' => 0,
85
      ),
86
      'name' => array(
87
        'description' => 'The unique name of the effect to be executed.',
88
        'type' => 'varchar',
89
        'length' => 255,
90
        'not null' => TRUE,
91
      ),
92
      'data' => array(
93
        'description' => 'The configuration data for the effect.',
94
        'type' => 'blob',
95
        'not null' => TRUE,
96
        'size' => 'big',
97
        'serialize' => TRUE,
98
      ),
99
    ),
100
    'primary key' => array('ieid'),
101
    'indexes' => array(
102
      'isid' => array('isid'),
103
      'weight' => array('weight'),
104
    ),
105
    'foreign keys' => array(
106
      'image_style' => array(
107
        'table' => 'image_styles',
108
        'columns' => array('isid' => 'isid'),
109
      ),
110
    ),
111
  );
112

    
113
  return $schema;
114
}
115

    
116
/**
117
 * Implements hook_field_schema().
118
 */
119
function image_field_schema($field) {
120
  return array(
121
    'columns' => array(
122
      'fid' => array(
123
        'description' => 'The {file_managed}.fid being referenced in this field.',
124
        'type' => 'int',
125
        'not null' => FALSE,
126
        'unsigned' => TRUE,
127
      ),
128
      'alt' => array(
129
        'description' => "Alternative image text, for the image's 'alt' attribute.",
130
        'type' => 'varchar',
131
        'length' => 512,
132
        'not null' => FALSE,
133
      ),
134
      'title' => array(
135
        'description' => "Image title text, for the image's 'title' attribute.",
136
        'type' => 'varchar',
137
        'length' => 1024,
138
        'not null' => FALSE,
139
      ),
140
      'width' => array(
141
        'description' => 'The width of the image in pixels.',
142
        'type' => 'int',
143
        'unsigned' => TRUE,
144
      ),
145
      'height' => array(
146
        'description' => 'The height of the image in pixels.',
147
        'type' => 'int',
148
        'unsigned' => TRUE,
149
      ),
150
    ),
151
    'indexes' => array(
152
      'fid' => array('fid'),
153
    ),
154
    'foreign keys' => array(
155
      'fid' => array(
156
        'table' => 'file_managed',
157
        'columns' => array('fid' => 'fid'),
158
      ),
159
    ),
160
  );
161
}
162

    
163
/**
164
 * Implements hook_update_dependencies().
165
 */
166
function image_update_dependencies() {
167
  $dependencies['image'][7002] = array(
168
    // Image update 7002 uses field API functions, so must run after
169
    // Field API has been enabled.
170
    'system' => 7020,
171
  );
172
  return $dependencies;
173
}
174

    
175
/**
176
 * Install the schema for users upgrading from the contributed module.
177
 */
178
function image_update_7000() {
179
  if (!db_table_exists('image_styles')) {
180
    $schema = array();
181

    
182
    $schema['cache_image'] = system_schema_cache_7054();
183
    $schema['cache_image']['description'] = 'Cache table used to store information about image manipulations that are in-progress.';
184

    
185
    $schema['image_styles'] = array(
186
      'description' => 'Stores configuration options for image styles.',
187
      'fields' => array(
188
        'isid' => array(
189
          'description' => 'The primary identifier for an image style.',
190
          'type' => 'serial',
191
          'unsigned' => TRUE,
192
          'not null' => TRUE,
193
        ),
194
        'name' => array(
195
          'description' => 'The style name.',
196
          'type' => 'varchar',
197
          'length' => 255,
198
          'not null' => TRUE,
199
        ),
200
      ),
201
      'primary key' => array('isid'),
202
      'unique keys' => array(
203
        'name' => array('name'),
204
      ),
205
    );
206

    
207
    $schema['image_effects'] = array(
208
      'description' => 'Stores configuration options for image effects.',
209
      'fields' => array(
210
        'ieid' => array(
211
          'description' => 'The primary identifier for an image effect.',
212
          'type' => 'serial',
213
          'unsigned' => TRUE,
214
          'not null' => TRUE,
215
        ),
216
        'isid' => array(
217
          'description' => 'The {image_styles}.isid for an image style.',
218
          'type' => 'int',
219
          'unsigned' => TRUE,
220
          'not null' => TRUE,
221
          'default' => 0,
222
        ),
223
        'weight' => array(
224
          'description' => 'The weight of the effect in the style.',
225
          'type' => 'int',
226
          'unsigned' => FALSE,
227
          'not null' => TRUE,
228
          'default' => 0,
229
        ),
230
        'name' => array(
231
          'description' => 'The unique name of the effect to be executed.',
232
          'type' => 'varchar',
233
          'length' => 255,
234
          'not null' => TRUE,
235
        ),
236
        'data' => array(
237
          'description' => 'The configuration data for the effect.',
238
          'type' => 'blob',
239
          'not null' => TRUE,
240
          'size' => 'big',
241
          'serialize' => TRUE,
242
        ),
243
      ),
244
      'primary key' => array('ieid'),
245
      'indexes' => array(
246
        'isid' => array('isid'),
247
        'weight' => array('weight'),
248
      ),
249
      'foreign keys' => array(
250
        'image_style' => array(
251
          'table' => 'image_styles',
252
          'columns' => array('isid' => 'isid'),
253
        ),
254
      ),
255
    );
256

    
257
    db_create_table('cache_image', $schema['cache_image']);
258
    db_create_table('image_styles', $schema['image_styles']);
259
    db_create_table('image_effects', $schema['image_effects']);
260
  }
261
}
262

    
263
/**
264
 * @addtogroup updates-7.x-extra
265
 * @{
266
 */
267

    
268
/**
269
 * Rename possibly misnamed {image_effect} table to {image_effects}.
270
 */
271
function image_update_7001() {
272
  // Due to a bug in earlier versions of image_update_7000() it is possible
273
  // to end up with an {image_effect} table where there should be an
274
  // {image_effects} table.
275
  if (!db_table_exists('image_effects') && db_table_exists('image_effect')) {
276
    db_rename_table('image_effect', 'image_effects');
277
  }
278
}
279

    
280
/**
281
 * Add width and height columns to a specific table.
282
 *
283
 * @param $table
284
 *   The name of the database table to be updated.
285
 * @param $columns
286
 *   Keyed array of columns this table is supposed to have.
287
 */
288
function _image_update_7002_add_columns($table, $field_name) {
289
  $spec = array(
290
    'type' => 'int',
291
    'unsigned' => TRUE,
292
  );
293

    
294
  $spec['description'] = 'The width of the image in pixels.';
295
  db_add_field($table, $field_name . '_width', $spec);
296

    
297
  $spec['description'] = 'The height of the image in pixels.';
298
  db_add_field($table, $field_name . '_height', $spec);
299
}
300

    
301
/**
302
 * Populate image dimensions in a specific table.
303
 *
304
 * @param $table
305
 *   The name of the database table to be updated.
306
 * @param $columns
307
 *   Keyed array of columns this table is supposed to have.
308
 * @param $last_fid
309
 *   The fid of the last image to have been processed.
310
 *
311
 * @return
312
 *   The number of images that were processed.
313
 */
314
function _image_update_7002_populate_dimensions($table, $field_name, &$last_fid) {
315
  // Define how many images to process per pass.
316
  $images_per_pass = 100;
317

    
318
  // Query the database for fid / URI pairs.
319
  $query = db_select($table, NULL, array('fetch' => PDO::FETCH_ASSOC));
320
  $query->join('file_managed', NULL, $table . '.' . $field_name . '_fid = file_managed.fid');
321

    
322
  if ($last_fid) {
323
    $query->condition('file_managed.fid', $last_fid, '>');
324
  }
325

    
326
  $result = $query->fields('file_managed', array('fid', 'uri'))
327
    ->orderBy('file_managed.fid')
328
    ->range(0, $images_per_pass)
329
    ->execute();
330

    
331
  $count = 0;
332
  foreach ($result as $file) {
333
    $count++;
334
    $info = image_get_info($file['uri']);
335

    
336
    if (is_array($info)) {
337
      db_update($table)
338
        ->fields(array(
339
          $field_name . '_width' => $info['width'],
340
          $field_name . '_height' => $info['height'],
341
        ))
342
        ->condition($field_name . '_fid', $file['fid'])
343
        ->execute();
344
    }
345
  }
346

    
347
  // If less than the requested number of rows were returned then this table
348
  // has been fully processed.
349
  $last_fid = ($count < $images_per_pass) ? NULL : $file['fid'];
350
  return $count;
351
}
352

    
353
/**
354
 * Add width and height columns to image field schema and populate.
355
 */
356
function image_update_7002(array &$sandbox) {
357
  if (empty($sandbox)) {
358
    // Setup the sandbox.
359
    $sandbox = array(
360
      'tables' => array(),
361
      'total' => 0,
362
      'processed' => 0,
363
      'last_fid' => NULL,
364
    );
365

    
366
    $fields = _update_7000_field_read_fields(array(
367
      'module' => 'image',
368
      'storage_type' => 'field_sql_storage',
369
      'deleted' => 0,
370
    ));
371

    
372
    foreach ($fields as $field) {
373
      $tables = array(
374
        _field_sql_storage_tablename($field),
375
        _field_sql_storage_revision_tablename($field),
376
      );
377
      foreach ($tables as $table) {
378
        // Add the width and height columns to the table.
379
        _image_update_7002_add_columns($table, $field['field_name']);
380

    
381
        // How many rows need dimensions populated?
382
        $count = db_select($table)->countQuery()->execute()->fetchField();
383

    
384
        if (!$count) {
385
          continue;
386
        }
387

    
388
        $sandbox['total'] += $count;
389
        $sandbox['tables'][$table] = $field['field_name'];
390
      }
391
    }
392

    
393
    // If no tables need rows populated with dimensions then we are done.
394
    if (empty($sandbox['tables'])) {
395
      $sandbox = array();
396
      return;
397
    }
398
  }
399

    
400
  // Process the table at the top of the list.
401
  $keys = array_keys($sandbox['tables']);
402
  $table = reset($keys);
403
  $sandbox['processed'] += _image_update_7002_populate_dimensions($table, $sandbox['tables'][$table], $sandbox['last_fid']);
404

    
405
  // Has the table been fully processed?
406
  if (!$sandbox['last_fid']) {
407
    unset($sandbox['tables'][$table]);
408
  }
409

    
410
  $sandbox['#finished'] = count($sandbox['tables']) ? ($sandbox['processed'] / $sandbox['total']) : 1;
411
}
412

    
413
/**
414
 * Remove the variables that set alt and title length since they were not
415
 * used for database column size and could cause PDO exceptions.
416
 */
417
function image_update_7003() {
418
  variable_del('image_alt_length');
419
  variable_del('image_title_length');
420
}
421

    
422
/**
423
 * Use a large setting (512 and 1024 characters) for the length of the image alt
424
 * and title fields.
425
 */
426
function image_update_7004() {
427
  $alt_spec = array(
428
    'type' => 'varchar',
429
    'length' => 512,
430
    'not null' => FALSE,
431
  );
432

    
433
  $title_spec = array(
434
    'type' => 'varchar',
435
    'length' => 1024,
436
    'not null' => FALSE,
437
  );
438

    
439
  $fields = _update_7000_field_read_fields(array(
440
    'module' => 'image',
441
    'storage_type' => 'field_sql_storage',
442
  ));
443

    
444
  foreach ($fields as $field_name => $field) {
445
    $tables = array(
446
      _field_sql_storage_tablename($field),
447
      _field_sql_storage_revision_tablename($field),
448
    );
449
    $alt_column = $field['field_name'] . '_alt';
450
    $title_column = $field['field_name'] . '_title';
451
    foreach ($tables as $table) {
452
      db_change_field($table, $alt_column, $alt_column, $alt_spec);
453
      db_change_field($table, $title_column, $title_column, $title_spec);
454
    }
455
  }
456
}
457

    
458
/**
459
 * Add a column to the 'image_style' table to store administrative labels.
460
 */
461
function image_update_7005() {
462
  $field = array(
463
    'type' => 'varchar',
464
    'length' => 255,
465
    'not null' => TRUE,
466
    'default' => '',
467
    'description' => 'The style administrative name.',
468
  );
469
  db_add_field('image_styles', 'label', $field);
470

    
471
  // Do a direct query here, rather than calling image_styles(),
472
  // in case Image module is disabled.
473
  $styles = db_query('SELECT name FROM {image_styles}')->fetchCol();
474
  foreach ($styles as $style) {
475
    db_update('image_styles')
476
      ->fields(array('label' => $style))
477
      ->condition('name', $style)
478
      ->execute();
479
  }
480
}
481

    
482
/**
483
 * @} End of "addtogroup updates-7.x-extra".
484
 */
485

    
486
/**
487
 * Implements hook_requirements() to check the PHP GD Library.
488
 *
489
 * @param $phase
490
 */
491
function image_requirements($phase) {
492
  $requirements = array();
493

    
494
  if ($phase == 'runtime') {
495
    // Check for the PHP GD library.
496
    if (function_exists('imagegd2')) {
497
      $info = gd_info();
498
      $requirements['image_gd'] = array(
499
        'value' => $info['GD Version'],
500
      );
501

    
502
      // Check for filter and rotate support.
503
      if (function_exists('imagefilter') && function_exists('imagerotate')) {
504
        $requirements['image_gd']['severity'] = REQUIREMENT_OK;
505
      }
506
      else {
507
        $requirements['image_gd']['severity'] = REQUIREMENT_WARNING;
508
        $requirements['image_gd']['description'] = t('The GD Library for PHP is enabled, but was compiled without support for functions used by the rotate and desaturate effects. It was probably compiled using the official GD libraries from http://www.libgd.org instead of the GD library bundled with PHP. You should recompile PHP --with-gd using the bundled GD library. See <a href="http://www.php.net/manual/book.image.php">the PHP manual</a>.');
509
      }
510
    }
511
    else {
512
      $requirements['image_gd'] = array(
513
        'value' => t('Not installed'),
514
        'severity' => REQUIREMENT_ERROR,
515
        'description' => t('The GD library for PHP is missing or outdated. Check the <a href="@url">PHP image documentation</a> for information on how to correct this.', array('@url' => 'http://www.php.net/manual/book.image.php')),
516
      );
517
    }
518
    $requirements['image_gd']['title'] = t('GD library rotate and desaturate effects');
519
  }
520

    
521
  return $requirements;
522
}