Projet

Général

Profil

Paste
Télécharger (9,68 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / l10n_update / l10n_update.project.inc @ 503b3f7b

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
 * @param $refresh
29
 *   TRUE: Refresh project list.
30
 *
31
 * @return array
32
 *   Array of project objects to be considered for translation update.
33
 */
34
function l10n_update_build_projects($refresh = FALSE) {
35
  module_load_include('inc', 'l10n_update');
36
  // Get all stored projects, including disabled ones
37
  $current = l10n_update_get_projects($refresh, TRUE);
38
  // Now get the new project list, just enabled ones
39
  $projects = l10n_update_project_list();
40

    
41
  // Mark all previous projects as disabled and store new project data
42
  db_update('l10n_update_project')
43
    ->fields(array(
44
      'status' => 0,
45
    ))
46
    ->execute();
47

    
48
  $default_server = l10n_update_default_server();
49

    
50
  if (module_exists('update')) {
51
    $projects_info = update_get_available(TRUE);
52
  }
53
  foreach ($projects as $name => $data) {
54

    
55
    // Force update fetch of project data in cases where Drupal's performance
56
    // optimized approach is missing out on some projects.
57
    // @see http://drupal.org/node/1671570#comment-6216090
58
    if (module_exists('update') && !isset($projects_info[$name])) {
59
      module_load_include('fetch.inc', 'update');
60
      _update_process_fetch_task($data);
61
      $available = _update_get_cached_available_releases();
62
      if (!empty($available[$name])) {
63
        $projects_info[$name] = $available[$name];
64
      }
65
    }
66

    
67
    if (isset($projects_info[$name]['releases']) && $projects_info[$name]['project_status'] != 'not-fetched') {
68
      // Find out if a dev version is installed.
69
      if (preg_match("/^[0-9]+\.x-([0-9]+)\..*-dev$/", $data['info']['version'], $matches)) {
70
        // Find a suitable release to use as alternative translation.
71
        foreach ($projects_info[$name]['releases'] as $project_release) {
72
          // The first release with the same major release number which is not
73
          // a dev release is the one. Releases are sorted the most recent first.
74
          if ($project_release['version_major'] == $matches[1] &&
75
              (!isset($project_release['version_extra']) || $project_release['version_extra'] != 'dev')) {
76
            $release = $project_release;
77
            break;
78
          }
79
        }
80
      }
81
      if (!empty($release['version'])) {
82
        $data['info']['version'] = $release['version'];
83
      }
84

    
85
      unset($release);
86
    }
87

    
88
    $data += array(
89
      'version' => isset($data['info']['version']) ? $data['info']['version'] : '',
90
      'core' => isset($data['info']['core']) ? $data['info']['core'] : DRUPAL_CORE_COMPATIBILITY,
91
      // The project can have its own l10n server, we use default if not
92
      'l10n_server' => isset($data['info']['l10n server']) ? $data['info']['l10n server'] : NULL,
93
      // A project can provide the server url to fetch metadata, or the update url (path)
94
      'l10n_url' => isset($data['info']['l10n url']) ? $data['info']['l10n url'] : NULL,
95
      'l10n_path' => isset($data['info']['l10n path']) ? $data['info']['l10n path'] : NULL,
96
      'status' => 1,
97
    );
98
    $project = (object) $data;
99
    // Unless the project provides a full l10n path (update url), we try to build one
100
    if (!isset($project->l10n_path)) {
101
      $server = NULL;
102
      if ($project->l10n_server || $project->l10n_url) {
103
        $server = l10n_update_server($project->l10n_server, $project->l10n_url);
104
      }
105
      else {
106
        // Use the default server
107
        $server = l10n_update_server($default_server['name'], $default_server['server_url']);
108
      }
109
      if ($server) {
110
        // Build the update path for this project, with project name and release replaced
111
        $project->l10n_path = l10n_update_build_string($project, $server['update_url']);
112
      }
113
    }
114
    // Create / update project record
115
    $update = empty($current[$name]) ? array() : array('name');
116
    // @todo Use db_merge() to avoid problems with saving existing projects.
117
    try {
118
      drupal_write_record('l10n_update_project', $project, $update);
119
    }
120
    catch (Exception $e) {
121
      watchdog('l10n_update', 'Could not insert a project into the l10n_update_project table, possibly because it already exists.', NULL, WATCHDOG_INFO);
122
    }
123
    $projects[$name] = $project;
124
  }
125
  return $projects;
126
}
127

    
128
/**
129
 * Get update module's project list
130
 *
131
 * @return array
132
 */
133
function l10n_update_project_list() {
134
  $projects = array();
135
  $disabled = variable_get('l10n_update_check_disabled', 0);
136
  // Unlike update module, this one has no cache
137
  _l10n_update_project_info_list($projects, system_rebuild_module_data(), 'module', $disabled);
138
  _l10n_update_project_info_list($projects, system_rebuild_theme_data(), 'theme', $disabled);
139
  // Allow other modules to alter projects before fetching and comparing.
140
  drupal_alter('l10n_update_projects', $projects);
141
  return $projects;
142
}
143

    
144
/**
145
 * Refresh projects after enabling modules
146
 *
147
 * When new projects are installed, set a batch for locale import / update
148
 *
149
 * @param $modules
150
 *   Array of module names.
151
 */
152
function l10n_update_project_refresh($modules) {
153
  module_load_include('check.inc', 'l10n_update');
154
  $projects = array();
155

    
156
  // Get all current projects, including the recently installed.
157
  $current_projects = l10n_update_build_projects(TRUE);
158
  // Collect project data of newly installed projects.
159
  foreach ($modules as $name) {
160
    if (isset($current_projects[$name])) {
161
      $projects[$name] = $current_projects[$name];
162
    }
163
  }
164

    
165
  // If a translation is available and if update is required, lets go.
166
  if ($projects && $available = l10n_update_check_projects($projects)) {
167
    $history = l10n_update_get_history();
168
    if ($updates = l10n_update_build_updates($history, $available)) {
169
      module_load_include('batch.inc', 'l10n_update');
170
      // Filter out updates in other languages. If no languages, all of them will be updated
171
      $updates = _l10n_update_prepare_updates($updates);
172
      $batch = l10n_update_batch_multiple($updates, variable_get('l10n_update_import_mode', LOCALE_IMPORT_KEEP));
173
      batch_set($batch);
174
    }
175
  }
176
}
177

    
178
/**
179
 * Populate an array of project data.
180
 *
181
 * Based on _update_process_info_list()
182
 *
183
 * @param $projects
184
 * @param $list
185
 * @param $project_type
186
 * @param $disabled
187
 *   TRUE to include disabled projects too
188
 */
189
function _l10n_update_project_info_list(&$projects, $list, $project_type, $disabled = FALSE) {
190
  foreach ($list as $file) {
191
    if (!$disabled && empty($file->status)) {
192
      // Skip disabled modules or themes.
193
      continue;
194
    }
195

    
196
    // Skip if the .info file is broken.
197
    if (empty($file->info)) {
198
      continue;
199
    }
200

    
201
    // If the .info doesn't define the 'project', try to figure it out.
202
    if (!isset($file->info['project'])) {
203
      $file->info['project'] = l10n_update_get_project_name($file);
204
    }
205

    
206
    // If we still don't know the 'project', give up.
207
    if (empty($file->info['project'])) {
208
      continue;
209
    }
210

    
211
    // If we don't already know it, grab the change time on the .info file
212
    // itself. Note: we need to use the ctime, not the mtime (modification
213
    // time) since many (all?) tar implementations will go out of their way to
214
    // set the mtime on the files it creates to the timestamps recorded in the
215
    // tarball. We want to see the last time the file was changed on disk,
216
    // which is left alone by tar and correctly set to the time the .info file
217
    // was unpacked.
218
    if (!isset($file->info['_info_file_ctime'])) {
219
      $info_filename = dirname($file->uri) . '/' . $file->name . '.info';
220
      $file->info['_info_file_ctime'] = filectime($info_filename);
221
    }
222

    
223
    $project_name = $file->info['project'];
224
    if (!isset($projects[$project_name])) {
225
      // Only process this if we haven't done this project, since a single
226
      // project can have multiple modules or themes.
227
      $projects[$project_name] = array(
228
        'name' => $project_name,
229
        'info' => $file->info,
230
        'datestamp' => isset($file->info['datestamp']) ? $file->info['datestamp'] : 0,
231
        'includes' => array($file->name => $file->info['name']),
232
        'project_type' => $project_name == 'drupal' ? 'core' : $project_type,
233
      );
234
    }
235
    else {
236
      $projects[$project_name]['includes'][$file->name] = $file->info['name'];
237
      $projects[$project_name]['info']['_info_file_ctime'] = max($projects[$project_name]['info']['_info_file_ctime'], $file->info['_info_file_ctime']);
238
    }
239
  }
240
}
241

    
242
/**
243
 * Given a $file object (as returned by system_rebuild_module_data()), figure
244
 * out what project it belongs to.
245
 *
246
 * Based on update_get_project_name().
247
 *
248
 * @param $file
249
 * @return string
250
 * @see system_get_files_database()
251
 */
252
function l10n_update_get_project_name($file) {
253
  $project_name = '';
254
  if (isset($file->info['project'])) {
255
    $project_name = $file->info['project'];
256
  }
257
  elseif (isset($file->info['package']) && (strpos($file->info['package'], 'Core') === 0)) {
258
    $project_name = 'drupal';
259
  }
260
  return $project_name;
261
}