l10n_update.project.inc 9.7 KB

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