Projet

Général

Profil

Paste
Télécharger (3,46 ko) Statistiques
| Branche: | Révision:

root / htmltest / sites / all / modules / imce / imce.install @ a5572547

1
<?php
2

    
3
/**
4
 * @file
5
 * Installs, updates, and uninstalls IMCE.
6
 */
7

    
8
/**
9
 * Implements hook_install().
10
 */
11
function imce_install() {
12
  module_load_include('inc', 'imce', 'inc/imce.core.profiles');
13
  imce_install_profiles();
14
}
15

    
16
/**
17
 * Implements hook_uninstall().
18
 */
19
function imce_uninstall() {
20
  db_delete('file_usage')->condition('module', 'imce')->execute();
21
  variable_del('imce_profiles');
22
  variable_del('imce_roles_profiles');
23
  variable_del('imce_settings_textarea');
24
  variable_del('imce_settings_absurls');
25
  variable_del('imce_settings_replace');
26
  variable_del('imce_settings_thumb_method');
27
  variable_del('imce_settings_disable_private');
28
  variable_del('imce_custom_content');
29
  variable_del('imce_custom_process');
30
  variable_del('imce_custom_init');
31
  variable_del('imce_custom_scan');
32
  variable_del('imce_custom_response');
33
}
34

    
35
/**
36
 * Updates from 6.x to 7.x.
37
 */
38
function imce_update_7000() {
39
  // Update role-profile assignments
40
  $roles_profiles = variable_get('imce_roles_profiles', array());
41
  if (!empty($roles_profiles)) {
42
    $scheme = variable_get('file_default_scheme', 'public');
43
    foreach ($roles_profiles as $rid => &$role) {
44
      $role[$scheme . '_pid'] = $role['pid'];
45
      unset($role['pid']);
46
    }
47
    variable_set('imce_roles_profiles', $roles_profiles);
48
  }
49
  // Update textarea ids
50
  $ids = str_replace(' ', '', variable_get('imce_settings_textarea', ''));
51
  if ($ids != '') {
52
    $ids = explode(',', $ids);
53
    foreach ($ids as &$id) {
54
      $id .= '*';
55
    }
56
    variable_set('imce_settings_textarea', implode(', ', $ids));
57
  }
58
}
59

    
60
/**
61
 * Migrates imce files from {files} to {file_managed}.
62
 * Removes {imce_files} in favor of {file_usage}.
63
 */
64
function imce_update_7001(&$sandbox) {
65
  if (!db_table_exists('imce_files') || !db_table_exists('files')) {
66
    return;
67
  }
68
  // Initiate progress
69
  if (!isset($sandbox['progress'])) {
70
    $sandbox['progress'] = 0;
71
    $sandbox['last_fid_processed'] = 0;
72
    $sandbox['max'] = db_query("SELECT COUNT(*) FROM {imce_files} i INNER JOIN {files} f ON i.fid = f.fid")->fetchField();
73
  }
74
  // Prepare variables
75
  $limit = 250;
76
  $basedir = variable_get('file_directory_path', conf_path() . '/files') . '/';
77
  $baselen = strlen($basedir);
78
  $scheme = file_default_scheme() . '://';
79
  $result = db_query_range('SELECT f.* FROM {imce_files} i INNER JOIN {files} f ON i.fid = f.fid WHERE i.fid > :fid ORDER BY i.fid', 0, $limit, array(':fid' => $sandbox['last_fid_processed']))->fetchAll();
80
  // Migrate imce files from {files} to {file_managed}
81
  foreach ($result as $file) {
82
    $relpath = substr($file->filepath, 0, $baselen) == $basedir ? substr($file->filepath, $baselen) : $file->filepath;
83
    $file->uri = file_stream_wrapper_uri_normalize($scheme . $relpath);
84
    unset($file->filepath);
85
    if (!db_query("SELECT 1 FROM {file_managed} WHERE fid = :fid", array(':fid' => $file->fid))->fetchField()) {
86
      // Check duplicate uri
87
      if ($fid = db_query("SELECT fid FROM {file_managed} WHERE uri = :uri", array(':uri' => $file->uri))->fetchField()) {
88
        $file->fid = $fid;
89
      }
90
      else {
91
        drupal_write_record('file_managed', $file);
92
      }
93
    }
94
    file_usage_add($file, 'imce', 'file', $file->fid);
95
    $sandbox['progress']++;
96
    $sandbox['last_fid_processed'] = $file->fid;
97
  }
98
  // Drop {imce_files} if the progress is complete.
99
  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
100
  if ($sandbox['#finished'] >= 1) {
101
    db_drop_table('imce_files');
102
    return t('Migrated IMCE files.');
103
  }
104
}