Projet

Général

Profil

Paste
Télécharger (4 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / imce / imce.install @ 05237dd8

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_settings_admin_theme');
29
  variable_del('imce_custom_content');
30
  variable_del('imce_custom_process');
31
  variable_del('imce_custom_init');
32
  variable_del('imce_custom_scan');
33
  variable_del('imce_custom_response');
34
}
35

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

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

    
107
/**
108
 * Fixes misconfigurations where anonymous user is given User-1 profile
109
 */
110
function imce_update_7002() {
111
  $roles = variable_get('imce_roles_profiles', array());
112
  $rid = DRUPAL_ANONYMOUS_RID;
113
  if (!empty($roles[$rid])) {
114
    $update = FALSE;
115
    foreach ($roles[$rid] as $key => $value) {
116
      if ($value == 1 && substr($key, -4) == '_pid') {
117
        $roles[$rid][$key] = '0';
118
        $update = TRUE;
119
      }
120
    }
121
    if ($update) {
122
      variable_set('imce_roles_profiles', $roles);
123
    }
124
  }
125
}