Projet

Général

Profil

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

root / drupal7 / sites / all / modules / l10n_update / l10n_update.project.inc @ a2baadd1

1
<?php
2

    
3
/**
4
 * @file
5
 * Library for querying Drupal projects
6
 *
7
 * Most code is taken from update module. We don't want to depend on it though as it may not be enabled.
8
 *
9
 * For each project, the information about where to fetch translations may be specified in the info files
10
 * as follows:
11
 *
12
 * - Localization server to be used for this project. Defaults to http://localize.drupal.org
13
 *   l10n server = localize.drupal.org
14
 *   (This should be enough if the server url, the one below, is defined somewhere else)
15
 *
16
 * - Metadata information for the localization server
17
 *   l10n url = http://ftp.drupal.org/files/translations/l10n_server.xml
18
 *   (We can fetch *all* the information we need from this single url)
19
 *
20
 * - Translation file URL template, will be used to build the file url to download
21
 *   l10n path = http://ftp.drupal.org/files/translations/%core/%project/%project-%release.%language.po
22
 *   (Alternatively you can use the %filename variable that will default to '%project-%release.%language.po')
23
 */
24

    
25
/**
26
 * Rebuild project list
27
 *
28
 * @return array
29
 */
30
function l10n_update_build_projects() {
31
  module_load_include('inc', 'l10n_update');
32
  // Get all stored projects, including disabled ones
33
  $current = l10n_update_get_projects(NULL, TRUE);
34
  // Now get the new project list, just enabled ones
35
  $projects = l10n_update_project_list();
36

    
37
  // Mark all previous projects as disabled and store new project data
38
  db_update('l10n_update_project')
39
    ->fields(array(
40
      'status' => 0,
41
    ))
42
    ->execute();
43

    
44
  $default_server = l10n_update_default_server();
45

    
46
  if (module_exists('update')) {
47
    $projects_info = update_get_available(TRUE);
48
  }
49
  foreach ($projects as $name => $data) {
50
    if (isset($projects_info[$name]['releases']) && $projects_info[$name]['project_status'] != 'not-fetched') {
51
      // Find out if a dev version is installed.
52
      if (preg_match("/^[0-9]+\.x-([0-9]+)\..*-dev$/", $data['info']['version'], $matches)) {
53
        // Find a suitable release to use as alternative translation.
54
        foreach ($projects_info[$name]['releases'] as $project_release) {
55
          // The first release with the same major release number which is not 
56
          // a dev release is the one. Releases are sorted the most recent first.
57
          if ($project_release['version_major'] == $matches[1] &&
58
              (!isset($project_release['version_extra']) || $project_release['version_extra'] != 'dev')) {
59
            $release = $project_release;
60
            break;
61
          }
62
        }
63
      }
64
      elseif ($name == "drupal" || preg_match("/HEAD/", $data['info']['version'], $matches)) {
65
        // Pick latest available release.
66
        $release = array_shift($projects_info[$name]['releases']);
67
      }
68

    
69
      if (!empty($release['version'])) {
70
        $data['info']['version'] = $release['version'];
71
      }
72

    
73
      unset($release);
74
    }
75

    
76
    $data += array(
77
      'version' => isset($data['info']['version']) ? $data['info']['version'] : '',
78
      'core' => isset($data['info']['core']) ? $data['info']['core'] : DRUPAL_CORE_COMPATIBILITY,
79
      // The project can have its own l10n server, we use default if not
80
      'l10n_server' => isset($data['info']['l10n server']) ? $data['info']['l10n server'] : NULL,
81
      // A project can provide the server url to fetch metadata, or the update url (path)
82
      'l10n_url' => isset($data['info']['l10n url']) ? $data['info']['l10n url'] : NULL,
83
      'l10n_path' => isset($data['info']['l10n path']) ? $data['info']['l10n path'] : NULL,
84
      'status' => 1,
85
    );
86
    $project = (object) $data;
87
    // Unless the project provides a full l10n path (update url), we try to build one
88
    if (!isset($project->l10n_path)) {
89
      $server = NULL;
90
      if ($project->l10n_server || $project->l10n_url) {
91
        $server = l10n_update_server($project->l10n_server, $project->l10n_url);
92
      }
93
      else {
94
        // Use the default server
95
        $server = l10n_update_server($default_server['name'], $default_server['server_url']);
96
      }
97
      if ($server) {
98
        // Build the update path for this project, with project name and release replaced
99
        $project->l10n_path = l10n_update_build_string($project, $server['update_url']);
100
      }
101
    }
102
    // Create / update project record
103
    $update = empty($current[$name]) ? array() : array('name');
104
    drupal_write_record('l10n_update_project', $project, $update);
105
    $projects[$name] = $project;
106
  }
107
  return $projects;
108
}
109

    
110
/**
111
 * Get update module's project list
112
 *
113
 * @return array
114
 */
115
function l10n_update_project_list() {
116
  $projects = array();
117
  $disabled = variable_get('l10n_update_check_disabled', 0);
118
  // Unlike update module, this one has no cache
119
  _l10n_update_project_info_list($projects, system_rebuild_module_data(), 'module', $disabled);
120
  _l10n_update_project_info_list($projects, system_rebuild_theme_data(), 'theme', $disabled);
121
  // Allow other modules to alter projects before fetching and comparing.
122
  drupal_alter('l10n_update_projects', $projects);
123
  return $projects;
124
}
125

    
126
/**
127
 * Refresh projects after enabling modules
128
 *
129
 * When new projects are installed, set a batch for locale import / update
130
 *
131
 * @param $modules
132
 *   Array of module names.
133
 */
134
function l10n_update_project_refresh($modules) {
135
  module_load_include('check.inc', 'l10n_update');
136
  $projects = array();
137

    
138
  // Get all current projects, including the recently installed.
139
  $current_projects = l10n_update_build_projects();
140
  // Collect project data of newly installed projects.
141
  foreach ($modules as $name) {
142
    if (isset($current_projects[$name])) {
143
      $projects[$name] = $current_projects[$name];
144
    }
145
  }
146

    
147
  // If a translation is available and if update is required, lets go.
148
  if ($projects && $available = l10n_update_check_projects($projects)) {
149
    $history = l10n_update_get_history();
150
    if ($updates = l10n_update_build_updates($history, $available)) {
151
      module_load_include('batch.inc', 'l10n_update');
152
      // Filter out updates in other languages. If no languages, all of them will be updated
153
      $updates = _l10n_update_prepare_updates($updates);
154
      $batch = l10n_update_batch_multiple($updates, variable_get('l10n_update_import_mode', LOCALE_IMPORT_KEEP));
155
      batch_set($batch);
156
    }
157
  }
158
}
159

    
160
/**
161
 * Populate an array of project data.
162
 *
163
 * Based on _update_process_info_list()
164
 *
165
 * @param $projects
166
 * @param $list
167
 * @param $project_type
168
 * @param $disabled
169
 *   TRUE to include disabled projects too
170
 */
171
function _l10n_update_project_info_list(&$projects, $list, $project_type, $disabled = FALSE) {
172
  foreach ($list as $file) {
173
    if (!$disabled && empty($file->status)) {
174
      // Skip disabled modules or themes.
175
      continue;
176
    }
177

    
178
    // Skip if the .info file is broken.
179
    if (empty($file->info)) {
180
      continue;
181
    }
182

    
183
    // If the .info doesn't define the 'project', try to figure it out.
184
    if (!isset($file->info['project'])) {
185
      $file->info['project'] = l10n_update_get_project_name($file);
186
    }
187

    
188
    // If we still don't know the 'project', give up.
189
    if (empty($file->info['project'])) {
190
      continue;
191
    }
192

    
193
    // If we don't already know it, grab the change time on the .info file
194
    // itself. Note: we need to use the ctime, not the mtime (modification
195
    // time) since many (all?) tar implementations will go out of their way to
196
    // set the mtime on the files it creates to the timestamps recorded in the
197
    // tarball. We want to see the last time the file was changed on disk,
198
    // which is left alone by tar and correctly set to the time the .info file
199
    // was unpacked.
200
    if (!isset($file->info['_info_file_ctime'])) {
201
      $info_filename = dirname($file->uri) . '/' . $file->name . '.info';
202
      $file->info['_info_file_ctime'] = filectime($info_filename);
203
    }
204

    
205
    $project_name = $file->info['project'];
206
    if (!isset($projects[$project_name])) {
207
      // Only process this if we haven't done this project, since a single
208
      // project can have multiple modules or themes.
209
      $projects[$project_name] = array(
210
        'name' => $project_name,
211
        'info' => $file->info,
212
        'datestamp' => isset($file->info['datestamp']) ? $file->info['datestamp'] : 0,
213
        'includes' => array($file->name => $file->info['name']),
214
        'project_type' => $project_name == 'drupal' ? 'core' : $project_type,
215
      );
216
    }
217
    else {
218
      $projects[$project_name]['includes'][$file->name] = $file->info['name'];
219
      $projects[$project_name]['info']['_info_file_ctime'] = max($projects[$project_name]['info']['_info_file_ctime'], $file->info['_info_file_ctime']);
220
    }
221
  }
222
}
223

    
224
/**
225
 * Given a $file object (as returned by system_rebuild_module_data()), figure
226
 * out what project it belongs to.
227
 *
228
 * Based on update_get_project_name().
229
 *
230
 * @param $file
231
 * @return string
232
 * @see system_get_files_database()
233
 */
234
function l10n_update_get_project_name($file) {
235
  $project_name = '';
236
  if (isset($file->info['project'])) {
237
    $project_name = $file->info['project'];
238
  }
239
  elseif (isset($file->info['package']) && (strpos($file->info['package'], 'Core') === 0)) {
240
    $project_name = 'drupal';
241
  }
242
  return $project_name;
243
}