l10n_update.project.inc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. * @return array
  27. */
  28. function l10n_update_build_projects() {
  29. module_load_include('inc', 'l10n_update');
  30. // Get all stored projects, including disabled ones
  31. $current = l10n_update_get_projects(NULL, TRUE);
  32. // Now get the new project list, just enabled ones
  33. $projects = l10n_update_project_list();
  34. // Mark all previous projects as disabled and store new project data
  35. db_update('l10n_update_project')
  36. ->fields(array(
  37. 'status' => 0,
  38. ))
  39. ->execute();
  40. $default_server = l10n_update_default_server();
  41. if (module_exists('update')) {
  42. $projects_info = update_get_available(TRUE);
  43. }
  44. foreach ($projects as $name => $data) {
  45. if (isset($projects_info[$name]['releases']) && $projects_info[$name]['project_status'] != 'not-fetched') {
  46. // Find out if a dev version is installed.
  47. if (preg_match("/^[0-9]+\.x-([0-9]+)\..*-dev$/", $data['info']['version'], $matches)) {
  48. // Find a suitable release to use as alternative translation.
  49. foreach ($projects_info[$name]['releases'] as $project_release) {
  50. // The first release with the same major release number which is not
  51. // a dev release is the one. Releases are sorted the most recent first.
  52. if ($project_release['version_major'] == $matches[1] &&
  53. (!isset($project_release['version_extra']) || $project_release['version_extra'] != 'dev')) {
  54. $release = $project_release;
  55. break;
  56. }
  57. }
  58. }
  59. elseif ($name == "drupal" || preg_match("/HEAD/", $data['info']['version'], $matches)) {
  60. // Pick latest available release.
  61. $release = array_shift($projects_info[$name]['releases']);
  62. }
  63. if (!empty($release['version'])) {
  64. $data['info']['version'] = $release['version'];
  65. }
  66. unset($release);
  67. }
  68. $data += array(
  69. 'version' => isset($data['info']['version']) ? $data['info']['version'] : '',
  70. 'core' => isset($data['info']['core']) ? $data['info']['core'] : DRUPAL_CORE_COMPATIBILITY,
  71. // The project can have its own l10n server, we use default if not
  72. 'l10n_server' => isset($data['info']['l10n server']) ? $data['info']['l10n server'] : NULL,
  73. // A project can provide the server url to fetch metadata, or the update url (path)
  74. 'l10n_url' => isset($data['info']['l10n url']) ? $data['info']['l10n url'] : NULL,
  75. 'l10n_path' => isset($data['info']['l10n path']) ? $data['info']['l10n path'] : NULL,
  76. 'status' => 1,
  77. );
  78. $project = (object) $data;
  79. // Unless the project provides a full l10n path (update url), we try to build one
  80. if (!isset($project->l10n_path)) {
  81. $server = NULL;
  82. if ($project->l10n_server || $project->l10n_url) {
  83. $server = l10n_update_server($project->l10n_server, $project->l10n_url);
  84. }
  85. else {
  86. // Use the default server
  87. $server = l10n_update_server($default_server['name'], $default_server['server_url']);
  88. }
  89. if ($server) {
  90. // Build the update path for this project, with project name and release replaced
  91. $project->l10n_path = l10n_update_build_string($project, $server['update_url']);
  92. }
  93. }
  94. // Create / update project record
  95. $update = empty($current[$name]) ? array() : array('name');
  96. drupal_write_record('l10n_update_project', $project, $update);
  97. $projects[$name] = $project;
  98. }
  99. return $projects;
  100. }
  101. /**
  102. * Get update module's project list
  103. *
  104. * @return array
  105. */
  106. function l10n_update_project_list() {
  107. $projects = array();
  108. $disabled = variable_get('l10n_update_check_disabled', 0);
  109. // Unlike update module, this one has no cache
  110. _l10n_update_project_info_list($projects, system_rebuild_module_data(), 'module', $disabled);
  111. _l10n_update_project_info_list($projects, system_rebuild_theme_data(), 'theme', $disabled);
  112. // Allow other modules to alter projects before fetching and comparing.
  113. drupal_alter('l10n_update_projects', $projects);
  114. return $projects;
  115. }
  116. /**
  117. * Refresh projects after enabling modules
  118. *
  119. * When new projects are installed, set a batch for locale import / update
  120. *
  121. * @param $modules
  122. * Array of module names.
  123. */
  124. function l10n_update_project_refresh($modules) {
  125. module_load_include('check.inc', 'l10n_update');
  126. $projects = array();
  127. // Get all current projects, including the recently installed.
  128. $current_projects = l10n_update_build_projects();
  129. // Collect project data of newly installed projects.
  130. foreach ($modules as $name) {
  131. if (isset($current_projects[$name])) {
  132. $projects[$name] = $current_projects[$name];
  133. }
  134. }
  135. // If a translation is available and if update is required, lets go.
  136. if ($projects && $available = l10n_update_check_projects($projects)) {
  137. $history = l10n_update_get_history();
  138. if ($updates = l10n_update_build_updates($history, $available)) {
  139. module_load_include('batch.inc', 'l10n_update');
  140. // Filter out updates in other languages. If no languages, all of them will be updated
  141. $updates = _l10n_update_prepare_updates($updates);
  142. $batch = l10n_update_batch_multiple($updates, variable_get('l10n_update_import_mode', LOCALE_IMPORT_KEEP));
  143. batch_set($batch);
  144. }
  145. }
  146. }
  147. /**
  148. * Populate an array of project data.
  149. *
  150. * Based on _update_process_info_list()
  151. *
  152. * @param $projects
  153. * @param $list
  154. * @param $project_type
  155. * @param $disabled
  156. * TRUE to include disabled projects too
  157. */
  158. function _l10n_update_project_info_list(&$projects, $list, $project_type, $disabled = FALSE) {
  159. foreach ($list as $file) {
  160. if (!$disabled && empty($file->status)) {
  161. // Skip disabled modules or themes.
  162. continue;
  163. }
  164. // Skip if the .info file is broken.
  165. if (empty($file->info)) {
  166. continue;
  167. }
  168. // If the .info doesn't define the 'project', try to figure it out.
  169. if (!isset($file->info['project'])) {
  170. $file->info['project'] = l10n_update_get_project_name($file);
  171. }
  172. // If we still don't know the 'project', give up.
  173. if (empty($file->info['project'])) {
  174. continue;
  175. }
  176. // If we don't already know it, grab the change time on the .info file
  177. // itself. Note: we need to use the ctime, not the mtime (modification
  178. // time) since many (all?) tar implementations will go out of their way to
  179. // set the mtime on the files it creates to the timestamps recorded in the
  180. // tarball. We want to see the last time the file was changed on disk,
  181. // which is left alone by tar and correctly set to the time the .info file
  182. // was unpacked.
  183. if (!isset($file->info['_info_file_ctime'])) {
  184. $info_filename = dirname($file->uri) . '/' . $file->name . '.info';
  185. $file->info['_info_file_ctime'] = filectime($info_filename);
  186. }
  187. $project_name = $file->info['project'];
  188. if (!isset($projects[$project_name])) {
  189. // Only process this if we haven't done this project, since a single
  190. // project can have multiple modules or themes.
  191. $projects[$project_name] = array(
  192. 'name' => $project_name,
  193. 'info' => $file->info,
  194. 'datestamp' => isset($file->info['datestamp']) ? $file->info['datestamp'] : 0,
  195. 'includes' => array($file->name => $file->info['name']),
  196. 'project_type' => $project_name == 'drupal' ? 'core' : $project_type,
  197. );
  198. }
  199. else {
  200. $projects[$project_name]['includes'][$file->name] = $file->info['name'];
  201. $projects[$project_name]['info']['_info_file_ctime'] = max($projects[$project_name]['info']['_info_file_ctime'], $file->info['_info_file_ctime']);
  202. }
  203. }
  204. }
  205. /**
  206. * Given a $file object (as returned by system_rebuild_module_data()), figure
  207. * out what project it belongs to.
  208. *
  209. * Based on update_get_project_name().
  210. *
  211. * @param $file
  212. * @return string
  213. * @see system_get_files_database()
  214. */
  215. function l10n_update_get_project_name($file) {
  216. $project_name = '';
  217. if (isset($file->info['project'])) {
  218. $project_name = $file->info['project'];
  219. }
  220. elseif (isset($file->info['package']) && (strpos($file->info['package'], 'Core') === 0)) {
  221. $project_name = 'drupal';
  222. }
  223. return $project_name;
  224. }