l10n_update.parser.inc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * @file
  4. * Extends the update parser to work with releases
  5. *
  6. * The update parser uses version tag to index releases. We will use 'language' and 'tag'
  7. *
  8. * @todo Parse languages too
  9. *
  10. * @todo Update the server side and get rid of this
  11. */
  12. module_load_include('inc', 'l10n_update');
  13. /**
  14. * Get server information
  15. */
  16. function l10n_update_get_server($server) {
  17. // Fetch up to date information if available
  18. if (!empty($server['server_url']) && ($fetch = l10n_update_fetch_server($server['server_url']))) {
  19. $server = array_merge($server, $fetch);
  20. }
  21. // If we have an update url this is ok, otherwise we return none
  22. if (!empty($server['update_url'])) {
  23. return $server;
  24. }
  25. else {
  26. return FALSE;
  27. }
  28. }
  29. /**
  30. * Fetch remote server metadata from a server URL
  31. *
  32. * @param unknown_type $server_url
  33. * @return unknown_type
  34. */
  35. function l10n_update_fetch_server($url) {
  36. $xml = l10n_update_http_request($url);
  37. if (isset($xml->data)) {
  38. $data[] = $xml->data;
  39. $parser = new l10n_update_xml_parser;
  40. return $parser->parse($xml->data);
  41. }
  42. else {
  43. return FALSE;
  44. }
  45. }
  46. /**
  47. * Parser for server metadata
  48. */
  49. class l10n_update_xml_parser {
  50. var $current_language;
  51. var $current_server;
  52. var $current_languages;
  53. var $servers;
  54. /**
  55. * Parse an XML data file.
  56. *
  57. * It can contain information for one or more l10n_servers
  58. *
  59. * Example data, http://ftp.drupal.org/files/translations/l10n_server.xml
  60. */
  61. function parse($data) {
  62. $parser = xml_parser_create();
  63. xml_set_object($parser, $this);
  64. xml_set_element_handler($parser, 'start', 'end');
  65. xml_set_character_data_handler($parser, "data");
  66. xml_parse($parser, $data);
  67. xml_parser_free($parser);
  68. //return $this->servers;
  69. return $this->current_server;
  70. }
  71. function start($parser, $name, $attr) {
  72. $this->current_tag = $name;
  73. switch ($name) {
  74. case 'L10N_SERVER':
  75. unset($this->current_object);
  76. $this->current_server = array();
  77. $this->current_object = &$this->current_server;
  78. break;
  79. case 'LANGUAGES':
  80. unset($this->current_object);
  81. $this->current_languages = array();
  82. $this->current_object = &$this->current_languages;
  83. //$this->current_object = &$this->current_release;
  84. break;
  85. case 'LANGUAGE':
  86. unset($this->current_object);
  87. $this->current_language = array();
  88. $this->current_object = &$this->current_language;
  89. break;
  90. }
  91. }
  92. function end($parser, $name) {
  93. switch ($name) {
  94. case 'L10N_SERVER':
  95. unset($this->current_object);
  96. $this->servers[$this->current_server['name']] = $this->current_server;
  97. //$this->current_server = array();
  98. break;
  99. case 'LANGUAGE':
  100. unset($this->current_object);
  101. $this->current_languages[$this->current_language['code']] = $this->current_language;
  102. $this->current_language = array();
  103. break;
  104. case 'LANGUAGES':
  105. $this->current_server['languages'] = $this->current_languages;
  106. break;
  107. default:
  108. if (isset($this->current_object[strtolower($this->current_tag)])) {
  109. $this->current_object[strtolower($this->current_tag)] = trim($this->current_object[strtolower($this->current_tag)]);
  110. }
  111. $this->current_tag = '';
  112. }
  113. }
  114. function data($parser, $data) {
  115. if ($this->current_tag && !in_array($this->current_tag, array('L10N_SERVER', 'LANGUAGES', 'LANGUAGE'))) {
  116. $tag = strtolower($this->current_tag);
  117. if (isset($this->current_object[$tag])) {
  118. $this->current_object[$tag] .= $data;
  119. }
  120. else {
  121. $this->current_object[$tag] = $data;
  122. }
  123. }
  124. }
  125. }