Project

General

Profile

Paste
Download (8.83 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / l10n_update / l10n_update.project.inc @ 620f9137

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
  foreach ($projects as $name => $data) {
51
    // For dev releases, remove the '-dev' part and trust the translation server
52
    // to fall back to the latest stable release for that branch.
53
    if (isset($data['info']['version']) && strpos($data['info']['version'], '-dev')) {
54
      if (preg_match("/^(\d+\.x-\d+\.).*$/", $data['info']['version'], $matches)) {
55
        // Example matches: 7.x-1.x-dev, 7.x-1.0-alpha1+5-dev => 7.x-1.x
56
        $data['info']['version'] = $matches[1] . 'x';
57
      }
58
      elseif (preg_match("/^(\d+\.).*$/", $data['info']['version'], $matches)) {
59
        // Example match: 7.33-dev => 7.x (Drupal core)
60
        $data['info']['version'] = $matches[1] . 'x';
61
      }
62
    }
63

    
64
    $data += array(
65
      'version' => isset($data['info']['version']) ? $data['info']['version'] : '',
66
      'core' => isset($data['info']['core']) ? $data['info']['core'] : DRUPAL_CORE_COMPATIBILITY,
67
      // The project can have its own l10n server, we use default if not
68
      'l10n_server' => isset($data['info']['l10n server']) ? $data['info']['l10n server'] : NULL,
69
      // A project can provide the server url to fetch metadata, or the update url (path)
70
      'l10n_url' => isset($data['info']['l10n url']) ? $data['info']['l10n url'] : NULL,
71
      'l10n_path' => isset($data['info']['l10n path']) ? $data['info']['l10n path'] : NULL,
72
      'status' => 1,
73
    );
74
    $project = (object) $data;
75
    // Unless the project provides a full l10n path (update url), we try to build one
76
    if (!isset($project->l10n_path)) {
77
      $server = NULL;
78
      if ($project->l10n_server || $project->l10n_url) {
79
        $server = l10n_update_server($project->l10n_server, $project->l10n_url);
80
      }
81
      else {
82
        // Use the default server
83
        $server = l10n_update_server($default_server['name'], $default_server['server_url']);
84
      }
85
      if ($server) {
86
        // Build the update path for this project, with project name and release replaced
87
        $project->l10n_path = l10n_update_build_string($project, $server['update_url']);
88
      }
89
    }
90
    // Create / update project record
91
    $update = empty($current[$name]) ? array() : array('name');
92
    // @todo Use db_merge() to avoid problems with saving existing projects.
93
    try {
94
      drupal_write_record('l10n_update_project', $project, $update);
95
    }
96
    catch (Exception $e) {
97
      watchdog('l10n_update', 'Could not insert a project into the l10n_update_project table, possibly because it already exists.', NULL, WATCHDOG_INFO);
98
    }
99
    $projects[$name] = $project;
100
  }
101
  return $projects;
102
}
103

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

    
120
/**
121
 * Refresh projects after enabling modules
122
 *
123
 * When new projects are installed, set a batch for locale import / update
124
 *
125
 * @param $modules
126
 *   Array of module names.
127
 */
128
function l10n_update_project_refresh($modules) {
129
  module_load_include('check.inc', 'l10n_update');
130
  $projects = array();
131

    
132
  // Get all current projects, including the recently installed.
133
  $current_projects = l10n_update_build_projects(TRUE);
134
  // Collect project data of newly installed projects.
135
  foreach ($modules as $name) {
136
    if (isset($current_projects[$name])) {
137
      $projects[$name] = $current_projects[$name];
138
    }
139
  }
140

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

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

    
172
    // Skip if the .info file is broken.
173
    if (empty($file->info)) {
174
      continue;
175
    }
176

    
177
    // If the .info doesn't define the 'project', try to figure it out.
178
    if (!isset($file->info['project'])) {
179
      $file->info['project'] = l10n_update_get_project_name($file);
180
    }
181

    
182
    // If we still don't know the 'project', give up.
183
    if (empty($file->info['project'])) {
184
      continue;
185
    }
186

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

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

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