l10n_update.project.inc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. /**
  3. * @file
  4. * Library for querying Drupal projects
  5. *
  6. * Most code is taken from update module. We don't want to depend on it though as it may not be enabled.
  7. *
  8. * For each project, the information about where to fetch translations may be specified in the info files
  9. * as follows:
  10. *
  11. * - Localization server to be used for this project. Defaults to http://localize.drupal.org
  12. * l10n server = localize.drupal.org
  13. * (This should be enough if the server url, the one below, is defined somewhere else)
  14. *
  15. * - Metadata information for the localization server
  16. * l10n url = http://ftp.drupal.org/files/translations/l10n_server.xml
  17. * (We can fetch *all* the information we need from this single url)
  18. *
  19. * - Translation file URL template, will be used to build the file url to download
  20. * l10n path = http://ftp.drupal.org/files/translations/%core/%project/%project-%release.%language.po
  21. * (Alternatively you can use the %filename variable that will default to '%project-%release.%language.po')
  22. */
  23. /**
  24. * Rebuild project list
  25. *
  26. * @param $refresh
  27. * TRUE: Refresh project list.
  28. *
  29. * @return array
  30. * Array of project objects to be considered for translation update.
  31. */
  32. function l10n_update_build_projects($refresh = FALSE) {
  33. module_load_include('inc', 'l10n_update');
  34. // Get all stored projects, including disabled ones
  35. $current = l10n_update_get_projects($refresh, TRUE);
  36. // Now get the new project list, just enabled ones
  37. $projects = l10n_update_project_list();
  38. // Mark all previous projects as disabled and store new project data
  39. db_update('l10n_update_project')
  40. ->fields(array(
  41. 'status' => 0,
  42. ))
  43. ->execute();
  44. $default_server = l10n_update_default_server();
  45. foreach ($projects as $name => $data) {
  46. // For dev releases, remove the '-dev' part and trust the translation server
  47. // to fall back to the latest stable release for that branch.
  48. if (isset($data['info']['version']) && strpos($data['info']['version'], '-dev')) {
  49. if (preg_match("/^(\d+\.x-\d+\.).*$/", $data['info']['version'], $matches)) {
  50. // Example matches: 7.x-1.x-dev, 7.x-1.0-alpha1+5-dev => 7.x-1.x
  51. $data['info']['version'] = $matches[1] . 'x';
  52. }
  53. elseif (preg_match("/^(\d+\.).*$/", $data['info']['version'], $matches)) {
  54. // Example match: 7.33-dev => 7.x (Drupal core)
  55. $data['info']['version'] = $matches[1] . 'x';
  56. }
  57. }
  58. $data += array(
  59. 'version' => isset($data['info']['version']) ? $data['info']['version'] : '',
  60. 'core' => isset($data['info']['core']) ? $data['info']['core'] : DRUPAL_CORE_COMPATIBILITY,
  61. // The project can have its own l10n server, we use default if not
  62. 'l10n_server' => isset($data['info']['l10n server']) ? $data['info']['l10n server'] : NULL,
  63. // A project can provide the server url to fetch metadata, or the update url (path)
  64. 'l10n_url' => isset($data['info']['l10n url']) ? $data['info']['l10n url'] : NULL,
  65. 'l10n_path' => isset($data['info']['l10n path']) ? $data['info']['l10n path'] : NULL,
  66. 'status' => 1,
  67. );
  68. $project = (object) $data;
  69. // Unless the project provides a full l10n path (update url), we try to build one
  70. if (!isset($project->l10n_path)) {
  71. $server = NULL;
  72. if ($project->l10n_server || $project->l10n_url) {
  73. $server = l10n_update_server($project->l10n_server, $project->l10n_url);
  74. }
  75. else {
  76. // Use the default server
  77. $server = l10n_update_server($default_server['name'], $default_server['server_url']);
  78. }
  79. if ($server) {
  80. // Build the update path for this project, with project name and release replaced
  81. $project->l10n_path = l10n_update_build_string($project, $server['update_url']);
  82. }
  83. }
  84. // Create / update project record
  85. $update = empty($current[$name]) ? array() : array('name');
  86. // @todo Use db_merge() to avoid problems with saving existing projects.
  87. try {
  88. drupal_write_record('l10n_update_project', $project, $update);
  89. }
  90. catch (Exception $e) {
  91. watchdog('l10n_update', 'Could not insert a project into the l10n_update_project table, possibly because it already exists.', NULL, WATCHDOG_INFO);
  92. }
  93. $projects[$name] = $project;
  94. }
  95. return $projects;
  96. }
  97. /**
  98. * Get update module's project list
  99. *
  100. * @return array
  101. */
  102. function l10n_update_project_list() {
  103. $projects = array();
  104. $disabled = variable_get('l10n_update_check_disabled', 0);
  105. // Unlike update module, this one has no cache
  106. _l10n_update_project_info_list($projects, system_rebuild_module_data(), 'module', $disabled);
  107. _l10n_update_project_info_list($projects, system_rebuild_theme_data(), 'theme', $disabled);
  108. // Allow other modules to alter projects before fetching and comparing.
  109. drupal_alter('l10n_update_projects', $projects);
  110. return $projects;
  111. }
  112. /**
  113. * Refresh projects after enabling modules
  114. *
  115. * When new projects are installed, set a batch for locale import / update
  116. *
  117. * @param $modules
  118. * Array of module names.
  119. */
  120. function l10n_update_project_refresh($modules) {
  121. module_load_include('check.inc', 'l10n_update');
  122. $projects = array();
  123. // Get all current projects, including the recently installed.
  124. $current_projects = l10n_update_build_projects(TRUE);
  125. // Collect project data of newly installed projects.
  126. foreach ($modules as $name) {
  127. if (isset($current_projects[$name])) {
  128. $projects[$name] = $current_projects[$name];
  129. }
  130. }
  131. // If a translation is available and if update is required, lets go.
  132. if ($projects && $available = l10n_update_check_projects($projects)) {
  133. $history = l10n_update_get_history();
  134. if ($updates = l10n_update_build_updates($history, $available)) {
  135. module_load_include('batch.inc', 'l10n_update');
  136. // Filter out updates in other languages. If no languages, all of them will be updated
  137. $updates = _l10n_update_prepare_updates($updates);
  138. $batch = l10n_update_batch_multiple($updates, variable_get('l10n_update_import_mode', LOCALE_IMPORT_KEEP));
  139. batch_set($batch);
  140. }
  141. }
  142. }
  143. /**
  144. * Populate an array of project data.
  145. *
  146. * Based on _update_process_info_list()
  147. *
  148. * @param $projects
  149. * @param $list
  150. * @param $project_type
  151. * @param $disabled
  152. * TRUE to include disabled projects too
  153. */
  154. function _l10n_update_project_info_list(&$projects, $list, $project_type, $disabled = FALSE) {
  155. foreach ($list as $file) {
  156. if (!$disabled && empty($file->status)) {
  157. // Skip disabled modules or themes.
  158. continue;
  159. }
  160. // Skip if the .info file is broken.
  161. if (empty($file->info)) {
  162. continue;
  163. }
  164. // If the .info doesn't define the 'project', try to figure it out.
  165. if (!isset($file->info['project'])) {
  166. $file->info['project'] = l10n_update_get_project_name($file);
  167. }
  168. // If we still don't know the 'project', give up.
  169. if (empty($file->info['project'])) {
  170. continue;
  171. }
  172. // If we don't already know it, grab the change time on the .info file
  173. // itself. Note: we need to use the ctime, not the mtime (modification
  174. // time) since many (all?) tar implementations will go out of their way to
  175. // set the mtime on the files it creates to the timestamps recorded in the
  176. // tarball. We want to see the last time the file was changed on disk,
  177. // which is left alone by tar and correctly set to the time the .info file
  178. // was unpacked.
  179. if (!isset($file->info['_info_file_ctime'])) {
  180. $info_filename = dirname($file->uri) . '/' . $file->name . '.info';
  181. $file->info['_info_file_ctime'] = filectime($info_filename);
  182. }
  183. $project_name = $file->info['project'];
  184. if (!isset($projects[$project_name])) {
  185. // Only process this if we haven't done this project, since a single
  186. // project can have multiple modules or themes.
  187. $projects[$project_name] = array(
  188. 'name' => $project_name,
  189. 'info' => $file->info,
  190. 'datestamp' => isset($file->info['datestamp']) ? $file->info['datestamp'] : 0,
  191. 'includes' => array($file->name => isset($file->info['name']) ? $file->info['name'] : $file->name),
  192. 'project_type' => $project_name == 'drupal' ? 'core' : $project_type,
  193. );
  194. }
  195. else {
  196. $projects[$project_name]['includes'][$file->name] = $file->info['name'];
  197. $projects[$project_name]['info']['_info_file_ctime'] = max($projects[$project_name]['info']['_info_file_ctime'], $file->info['_info_file_ctime']);
  198. }
  199. }
  200. }
  201. /**
  202. * Given a $file object (as returned by system_rebuild_module_data()), figure
  203. * out what project it belongs to.
  204. *
  205. * Based on update_get_project_name().
  206. *
  207. * @param $file
  208. * @return string
  209. * @see system_get_files_database()
  210. */
  211. function l10n_update_get_project_name($file) {
  212. $project_name = '';
  213. if (isset($file->info['project'])) {
  214. $project_name = $file->info['project'];
  215. }
  216. elseif (isset($file->info['package']) && (strpos($file->info['package'], 'Core') === 0)) {
  217. $project_name = 'drupal';
  218. }
  219. return $project_name;
  220. }