Projet

Général

Profil

Paste
Télécharger (16,1 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / l10n_update / l10n_update.check.inc @ 64ad485a

1
<?php
2

    
3
/**
4
 * @file
5
 * Reusable API for l10n remote updates using $source objects
6
 *
7
 * These functions may not be safe for the installer as they use variables and report using watchdog
8
 */
9

    
10
/**
11
 * Threshold for timestamp comparison.
12
 *
13
 * Eliminates a difference between the download time
14
 * (Database: l10n_update_file.timestamp) and the actual .po file timestamp.
15
 */
16
define('L10N_UPDATE_TIMESTAMP_THRESHOLD', 2);
17

    
18
module_load_include('inc', 'l10n_update');
19

    
20
/**
21
 * Fetch update information for all projects / all languages.
22
 *
23
 * @param boolean $refresh
24
 *   TRUE = refresh the release data.
25
 *   We refresh anyway if the data is older than a day.
26
 *
27
 * @return array
28
 *   Available releases indexed by project and language.
29
 */
30
function l10n_update_available_releases($refresh = FALSE) {
31
  $frequency = variable_get('l10n_update_check_frequency', 0) * 24 * 3600;
32
  if (!$refresh && ($cache = cache_get('l10n_update_available_releases', 'cache_l10n_update')) && (!$frequency || $cache->created > REQUEST_TIME - $frequency)) {
33
    return $cache->data;
34
  }
35
  else {
36
    $projects = l10n_update_get_projects(TRUE);
37
    $languages = l10n_update_language_list();
38
    $local = variable_get('l10n_update_check_mode', L10N_UPDATE_CHECK_ALL) & L10N_UPDATE_CHECK_LOCAL;
39
    $remote = variable_get('l10n_update_check_mode', L10N_UPDATE_CHECK_ALL) & L10N_UPDATE_CHECK_REMOTE;
40
    $available = l10n_update_check_projects($projects, array_keys($languages), $local, $remote);
41
    cache_set('l10n_update_available_releases', $available, 'cache_l10n_update', $frequency ? REQUEST_TIME + $frequency : CACHE_PERMANENT);
42
    return $available;
43
  }
44
}
45

    
46
/**
47
 * Check latest release for project, language.
48
 *
49
 * @param $projects
50
 *   Projects to check (objects).
51
 * @param $languages
52
 *   Array of language codes to check, none to check all.
53
 * @param $check_local
54
 *   Check local translation file.
55
 * @param $check_remote
56
 *   Check remote translation file.
57
 *
58
 * @return array
59
 *   Available sources indexed by project, language.
60
 */
61
function l10n_update_check_projects($projects, $languages = NULL, $check_local = TRUE, $check_remote = TRUE) {
62
  $languages = $languages ? $languages : array_keys(l10n_update_language_list());
63
  $result = array();
64
  foreach ($projects as $name => $project) {
65
    foreach ($languages as $lang) {
66
      $source = l10n_update_source_build($project, $lang);
67
      if ($update = l10n_update_source_check($source, $check_local, $check_remote)) {
68
        $result[$name][$lang] = $update;
69
      }
70
    }
71
  }
72
  return $result;
73
}
74

    
75
/**
76
 * Compare available releases with history and get list of downloadable updates.
77
 *
78
 * @param $history
79
 *   Update history of projects.
80
 * @param $available
81
 *   Available project releases.
82
 * @return array
83
 *   Projects to be updated: 'not yet downloaded', 'newer timestamp available',
84
 *   'new version available'.
85
 *   Up to date projects are not included in the array.
86
 */
87
function l10n_update_build_updates($history, $available) {
88
  $updates = array();
89
  foreach ($available as $name => $project_updates) {
90
    foreach ($project_updates as $lang => $update) {
91
      if (!empty($update->timestamp)) {
92
        $current = !empty($history[$name][$lang]) ? $history[$name][$lang] : NULL;
93
        // Add when not current, timestamp newer or version difers (newer version)
94
        if (_l10n_update_source_compare($current, $update) == -1 || $current->version != $update->version) {
95
          $updates[$name][$lang] = $update;
96
        }
97
      }
98
    }
99
  }
100
  return $updates;
101
}
102

    
103
/**
104
 * Check updates for active projects and languages.
105
 *
106
 * @param $count
107
 *   Number of package translations to check.
108
 * @param $before
109
 *   Unix timestamp, check only updates that haven't been checked for this time.
110
 * @param $limit
111
 *   Maximum number of updates to do. We check $count translations
112
 *   but we stop after we do $limit updates.
113
 * @return array
114
 */
115
function l10n_update_check_translations($count, $before, $limit = 1) {
116
  $projects = l10n_update_get_projects();
117
  $updated = $checked = array();
118

    
119
  // Select active projects x languages ordered by last checked time
120
  $q = db_select('l10n_update_project', 'p');
121
  $q->leftJoin('l10n_update_file', 'f', 'p.name = f.project');
122
  $q->innerJoin('languages', 'l', 'l.language = f.language');
123
  $q->condition('p.status', 1);
124
  $q->condition('l.enabled', 1);
125
  // If the file is not there, or it is there, but we did not check since $before.
126
  $q->condition(db_or()->isNull('f.status')->condition(db_and()->condition('f.status', 1)->condition('f.last_checked', $before, '<')));
127
  $q->range(0, $count);
128
  $q->fields('p', array('name'));
129
  $q->fields('f');
130
  $q->addField('l', 'language', 'lang');
131
  $q->orderBy('last_checked');
132
  $result = $q->execute();
133

    
134
  if ($result) {
135
    $local = variable_get('l10n_update_check_mode', L10N_UPDATE_CHECK_ALL) & L10N_UPDATE_CHECK_LOCAL;
136
    $remote = variable_get('l10n_update_check_mode', L10N_UPDATE_CHECK_ALL) & L10N_UPDATE_CHECK_REMOTE;
137
    foreach ($result as $check) {
138
      if (count($updated) >= $limit) {
139
        break;
140
      }
141
      $checked[] = $check;
142
      if (!empty($projects[$check->name])) {
143
        $project = $projects[$check->name];
144
        $update = NULL;
145
        $source = l10n_update_source_build($project, $check->lang);
146
        $current = $check->filename ? $check : NULL;
147
        if ($available = l10n_update_source_check($source, $local, $remote)) {
148
          if (!$current || _l10n_update_source_compare($current, $available) == -1 || $current->version != $available->version) {
149
            $update = $available;
150
          }
151
        }
152
        if ($update) {
153
          // The update functions will update data and timestamps too
154
          l10n_update_source_update($update, variable_get('l10n_update_import_mode', LOCALE_IMPORT_KEEP));
155
          $updated[] = $update;
156
        }
157
        elseif ($current) {
158
          // No update available, just update timestamp for this row
159
          db_update('l10n_update_file')
160
            ->fields(array(
161
              'last_checked' => REQUEST_TIME,
162
            ))
163
            ->condition('project', $current->project)
164
            ->condition('language', $current->language)
165
            ->execute();
166
        }
167
        elseif ($source) {
168
          // Create a new record just for keeping last checked time
169
          $source->last_checked = REQUEST_TIME;
170
          drupal_write_record('l10n_update_file', $source);
171
        }
172
      }
173
    }
174
  }
175
  return array($checked, $updated);
176
}
177

    
178
/**
179
 * Build abstract translation source, to be mapped to a file or a download.
180
 *
181
 * @param $project
182
 *   Project object.
183
 * @param $langcode
184
 *   Language code.
185
 * @param $filename
186
 *   File name of translation file. May contains placeholders.
187
 * @return object
188
 *   Source object, which may have these properties:
189
 *   - 'project': Project name.
190
 *   - 'language': Language code.
191
 *   - 'type': Source type 'download' or 'localfile'.
192
 *   - 'uri': Local file path.
193
 *   - 'fileurl': Remote file URL for downloads.
194
 *   - 'filename': File name.
195
 *   - 'keep': TRUE to keep the downloaded file.
196
 *   - 'timestamp': Last update time of the file.
197
 */
198
function l10n_update_source_build($project, $langcode, $filename = L10N_UPDATE_DEFAULT_FILENAME) {
199
  $source = clone $project;
200
  $source->project = $project->name;
201
  $source->language = $langcode;
202
  $source->filename = l10n_update_build_string($source, $filename);
203
  return $source;
204
}
205

    
206
/**
207
 * Check local and remote sources for the file.
208
 *
209
 * @param $source
210
 *   Translation source object.
211
 *   @see l10n_update_source_build()
212
 * @param $check_local
213
 *   File object of local translation file.
214
 * @param $check_remote
215
 *   File object of remote translation file.
216
 * @return object
217
 *   File object of most recent translation; local or remote.
218
 */
219
function l10n_update_source_check($source, $check_local = TRUE, $check_remote = TRUE) {
220
  $local = $remote = NULL;
221
  if ($check_local) {
222
    $check = clone $source;
223
    if (l10n_update_source_check_file($check)) {
224
      $local = $check;
225
    }
226
  }
227
  if ($check_remote) {
228
    $check = clone $source;
229
    if (l10n_update_source_check_download($check)) {
230
      $remote = $check;
231
    }
232
  }
233

    
234
  // Get remote if newer than local only, they both can be empty
235
  return _l10n_update_source_compare($local, $remote) < 0 ? $remote : $local;
236
}
237

    
238
/**
239
 * Check remote file object.
240
 *
241
 * @param $source
242
 *   Remote translation file object. The object will be update
243
 *   with data of the remote file:
244
 *   - 'type': Fixed value 'download'.
245
 *   - 'fileurl': File name and path.
246
 *   - 'timestamp': Last updated time.
247
 *   @see l10n_update_source_build()
248
 * @return object
249
 *   An object containing the HTTP request headers, response code, headers,
250
 *   data, redirect status and updated timestamp.
251
 *   NULL if failure.
252
 */
253
function l10n_update_source_check_download($source) {
254
  $url = l10n_update_build_string($source, $source->l10n_path);
255
  $result = l10n_update_http_check($url);
256
  if ($result && !empty($result->updated)) {
257
    $source->type = 'download';
258
    // There may have been redirects so we store the resulting url
259
    $source->fileurl = isset($result->redirect_url) ? $result->redirect_url : $url;
260
    $source->timestamp = $result->updated;
261
    return $result;
262
  }
263
}
264

    
265
/**
266
 * Check whether we've got the file in the filesystem under 'translations'.
267
 *
268
 * It will search, similar to modules and themes:
269
 * - translations
270
 * - sites/all/translations
271
 * - sites/mysite/translations
272
 *
273
 * Using name as the key will return just the last one found.
274
 *
275
 * @param $source
276
 *   Translation file object. The object will be updated with data of local file.
277
 *   - 'type': Fixed value 'localfile'.
278
 *   - 'uri': File name and path.
279
 *   - 'timestamp': Last updated time.
280
 *   @see l10n_update_source_build()
281
 * @param $directory
282
 *   Files directory.
283
 * @return Object
284
 *  File object (filename, basename, name)
285
 *  NULL if failure.
286
 */
287
function l10n_update_source_check_file($source, $directory = 'translations') {
288
  $filename = '/' . preg_quote($source->filename) . '$/';
289

    
290
  // Using the 'name' key will return
291
  if ($files = drupal_system_listing($filename, $directory, 'name', 0)) {
292
    $file = current($files);
293
    $source->type = 'localfile';
294
    $source->uri = $file->uri;
295
    $source->timestamp = filemtime($file->uri);
296
    return $file;
297
  }
298
}
299

    
300
/**
301
 * Download and import or just import source, depending on type.
302
 *
303
 * @param $source
304
 *   Translation source object with information about the file location.
305
 *   Object will be updated with :
306
 *   - 'last_checked': Timestamp of current time;
307
 *   - 'import_date': Timestamp of current time;
308
 * @param $mode
309
 *   Download mode. How to treat exising and modified translations.
310
 * @return boolean
311
 *   TRUE on success, NULL on failure.
312
 */
313
function l10n_update_source_update($source, $mode) {
314
  if ($source->type == 'localfile' || l10n_update_source_download($source)) {
315
    if (l10n_update_source_import($source, $mode)) {
316
      l10n_update_source_history($source);
317
      return TRUE;
318
    }
319
  }
320
}
321

    
322
/**
323
 * Import source into locales table.
324
 *
325
 * @param $source
326
 *   Translation source object with information about the file location.
327
 *   Object will be updated with :
328
 *   - 'last_checked': Timestamp of current time;
329
 *   - 'import_date': Timestamp of current time;
330
 * @param $mode
331
 *   Download mode. How to treat exising and modified translations.
332
 * @return boolean
333
 *   Result array on success, FALSE on failure.
334
 */
335
function l10n_update_source_import($source, $mode) {
336
  if (!empty($source->uri) && $result = l10n_update_import_file($source->uri, $source->language, $mode)) {
337
    $source->last_checked = REQUEST_TIME;
338

    
339
    // We override the file timestamp here. The default file time stamp is the
340
    // release date from the l.d.o server. We change the timestamp to the
341
    // creation time on the webserver. On multi sites that share a common
342
    // sites/all/translations directory, the sharing sites use the local file
343
    // creation date as release date. Without this correction the local
344
    // file is always newer than the l.d.o. file, which results in unnecessary
345
    // translation import.
346
    $source->timestamp = time();
347

    
348
    return $result;
349
  }
350
}
351

    
352
/**
353
 * Download source file from remote server.
354
 *
355
 * If succesful this function returns the downloaded file in two ways:
356
 * - As a temporary $file object
357
 * - As a file path on the $source->uri property.
358
 *
359
 * @param $source
360
 *   Source object with all parameters
361
 *   - 'fileurl': url to download.
362
 *   - 'uri': alternate destination. If not present a temporary file
363
 *                 will be used and the path returned here.
364
 * @return object
365
 *   $file object if download successful.
366
 */
367
function l10n_update_source_download($source) {
368
  if (!empty($source->uri)) {
369
    $destination = $source->uri;
370
  }
371
  elseif ($directory = variable_get('l10n_update_download_store', '')) {
372
    $destination = $directory . '/' . $source->filename;
373
  }
374
  else {
375
    $destination = NULL;
376
  }
377
  if ($file = l10n_update_download_file($source->fileurl, $destination)) {
378
    $source->uri = $file;
379
    return $file;
380
  }
381
}
382

    
383
/**
384
 * Update the file history table and delete the file if temporary.
385
 *
386
 * @param $file
387
 *   Source object representing the file just imported or downloaded.
388
 */
389
function l10n_update_source_history($file) {
390
  // Update history table
391
  l10n_update_file_history($file);
392

    
393
  // If it's a downloaded file and not marked for keeping, delete the file.
394
  if ($file->type == 'download' && empty($file->keep)) {
395
    file_unmanaged_delete($file->uri);
396
    $file->uri = '';
397
  }
398
}
399

    
400
/**
401
 * Compare two update sources, looking for the newer one (bigger timestamp).
402
 *
403
 * This function can be used as a callback to compare two source objects.
404
 *
405
 * @param $current
406
 *   Source object of current project.
407
 * @param $update
408
 *   Source object of available update.
409
 * @return integer
410
 *   - '-1': $current < $update OR $current is missing
411
 *   - '0': $current == $update OR both $current and $updated are missing
412
 *   - '1': $current > $update OR $update is missing
413
 */
414
function _l10n_update_source_compare($current, $update) {
415
  if ($current && $update) {
416
    if (abs($current->timestamp - $update->timestamp) < L10N_UPDATE_TIMESTAMP_THRESHOLD) {
417
      return 0;
418
    }
419
    else {
420
      return $current->timestamp > $update->timestamp ? 1 : -1;
421
    }
422
  }
423
  elseif ($current && !$update) {
424
    return 1;
425
  }
426
  elseif (!$current && $update) {
427
    return -1;
428
  }
429
  else {
430
    return 0;
431
  }
432
}
433

    
434
/**
435
 * Prepare update list.
436
 *
437
 * @param $updates
438
 *   Array of update sources that may be indexed in multiple ways.
439
 * @param $projects
440
 *   Array of project names to be included, others will be filtered out.
441
 * @param $languages
442
 *   Array of language codes to be included, others will be filtered out.
443
 * @return array
444
 *   Plain array of filtered updates with directory applied.
445
 */
446
function _l10n_update_prepare_updates($updates, $projects = NULL, $languages = NULL) {
447
  $result = array();
448
  foreach ($updates as $key => $update) {
449
    if (is_array($update)) {
450
      // It is a sub array of updates yet, process and merge
451
      $result = array_merge($result, _l10n_update_prepare_updates($update, $projects, $languages));
452
    }
453
    elseif ((!$projects || in_array($update->project, $projects)) && (!$languages || in_array($update->language, $languages))) {
454
      $directory = variable_get('l10n_update_download_store', '');
455
      if ($directory && empty($update->uri)) {
456
        // If we have a destination folder set just if we have no uri
457
        if (empty($update->uri)) {
458
          $update->uri = $directory . '/' . $update->filename;
459
          $update->keep = TRUE;
460
        }
461
      }
462
      $result[] = $update;
463
    }
464
  }
465
  return $result;
466
}
467

    
468
/**
469
 * Language refresh. Runs a batch for loading the selected languages.
470
 *
471
 * To be used after adding a new language.
472
 *
473
 * @param $languages
474
 *   Array of language codes to check and download.
475
 */
476
function l10n_update_language_refresh($languages) {
477
  $projects = l10n_update_get_projects();
478
  if ($available = l10n_update_check_projects($projects, $languages)) {
479
    $history = l10n_update_get_history();
480
    if ($updates = l10n_update_build_updates($history, $available)) {
481
      module_load_include('batch.inc', 'l10n_update');
482
      // Filter out updates in other languages. If no languages, all of them will be updated
483
      $updates = _l10n_update_prepare_updates($updates);
484
      $batch = l10n_update_batch_multiple($updates, variable_get('l10n_update_import_mode', LOCALE_IMPORT_KEEP));
485
      batch_set($batch);
486
    }
487
  }
488
}