PoDatabaseWriter.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. /**
  3. * @file
  4. * Definition of PoDatabaseWriter.
  5. */
  6. /**
  7. * Gettext PO writer working with the locale module database.
  8. */
  9. class PoDatabaseWriter implements PoWriterInterface {
  10. /**
  11. * An associative array indicating what data should be overwritten, if any.
  12. *
  13. * Elements of the array:
  14. * - override_options
  15. * - not_customized: boolean indicating that not customized strings should
  16. * be overwritten.
  17. * - customized: boolean indicating that customized strings should be
  18. * overwritten.
  19. * - customized: the strings being imported should be saved as customized.
  20. * One of L10N_UPDATE_CUSTOMIZED or L10N_UPDATE_NOT_CUSTOMIZED.
  21. *
  22. * @var array
  23. */
  24. private $_options;
  25. /**
  26. * Language code of the language being written to the database.
  27. *
  28. * @var string
  29. */
  30. private $_langcode;
  31. /**
  32. * Header of the po file written to the database.
  33. *
  34. * @var PoHeader
  35. */
  36. private $_header;
  37. /**
  38. * Associative array summarizing the number of changes done.
  39. *
  40. * Keys for the array:
  41. * - additions: number of source strings newly added
  42. * - updates: number of translations updated
  43. * - deletes: number of translations deleted
  44. * - skips: number of strings skipped due to disallowed HTML
  45. *
  46. * @var array
  47. */
  48. private $_report;
  49. /**
  50. * Database storage to store the strings in.
  51. *
  52. * @var StringDatabaseStorage
  53. */
  54. protected $storage;
  55. /**
  56. * Constructor, initialize reporting array.
  57. */
  58. function __construct() {
  59. $this->setReport();
  60. $this->storage = new StringDatabaseStorage();
  61. }
  62. /**
  63. * Implements PoMetadataInterface::getLangcode().
  64. */
  65. public function getLangcode() {
  66. return $this->_langcode;
  67. }
  68. /**
  69. * Implements PoMetadataInterface::setLangcode().
  70. */
  71. public function setLangcode($langcode) {
  72. $this->_langcode = $langcode;
  73. }
  74. /**
  75. * Get the report of the write operations.
  76. */
  77. public function getReport() {
  78. return $this->_report;
  79. }
  80. /**
  81. * Set the report array of write operations.
  82. *
  83. * @param array $report
  84. * Associative array with result information.
  85. */
  86. function setReport($report = array()) {
  87. $report += array(
  88. 'additions' => 0,
  89. 'updates' => 0,
  90. 'deletes' => 0,
  91. 'skips' => 0,
  92. 'strings' => array(),
  93. );
  94. $this->_report = $report;
  95. }
  96. /**
  97. * Get the options used by the writer.
  98. */
  99. function getOptions() {
  100. return $this->_options;
  101. }
  102. /**
  103. * Set the options for the current writer.
  104. */
  105. function setOptions(array $options) {
  106. if (!isset($options['overwrite_options'])) {
  107. $options['overwrite_options'] = array();
  108. }
  109. $options['overwrite_options'] += array(
  110. 'not_customized' => FALSE,
  111. 'customized' => FALSE,
  112. );
  113. $options += array(
  114. 'customized' => L10N_UPDATE_NOT_CUSTOMIZED,
  115. );
  116. $this->_options = $options;
  117. }
  118. /**
  119. * Implements PoMetadataInterface::getHeader().
  120. */
  121. function getHeader() {
  122. return $this->_header;
  123. }
  124. /**
  125. * Implements PoMetadataInterface::setHeader().
  126. *
  127. * Sets the header and configure Drupal accordingly.
  128. *
  129. * Before being able to process the given header we need to know in what
  130. * context this database write is done. For this the options must be set.
  131. *
  132. * A langcode is required to set the current header's PluralForm.
  133. *
  134. * @param PoHeader $header
  135. * Header metadata.
  136. *
  137. * @throws Exception
  138. */
  139. function setHeader(PoHeader $header) {
  140. $this->_header = $header;
  141. $languages = language_list();
  142. // Check for options.
  143. $options = $this->getOptions();
  144. if (empty($options)) {
  145. throw new \Exception('Options should be set before assigning a PoHeader.');
  146. }
  147. $overwrite_options = $options['overwrite_options'];
  148. // Check for langcode.
  149. $langcode = $this->_langcode;
  150. if (empty($langcode)) {
  151. throw new \Exception('Langcode should be set before assigning a PoHeader.');
  152. }
  153. // Check is language is already created.
  154. if (!isset($languages[$langcode])) {
  155. throw new \Exception('Language should be known before using it.');
  156. }
  157. if (array_sum($overwrite_options) || empty($languages[$langcode]->plurals)) {
  158. // Get and store the plural formula if available.
  159. $plural = $header->getPluralForms();
  160. if (isset($plural) && $p = $header->parsePluralForms($plural)) {
  161. list($nplurals, $formula) = $p;
  162. db_update('languages')
  163. ->fields(array(
  164. 'plurals' => $nplurals,
  165. 'formula' => $formula,
  166. ))
  167. ->condition('language', $langcode)
  168. ->execute();
  169. }
  170. }
  171. }
  172. /**
  173. * Implements PoWriterInterface::writeItem().
  174. */
  175. function writeItem(PoItem $item) {
  176. if ($item->isPlural()) {
  177. $item->setSource(join(L10N_UPDATE_PLURAL_DELIMITER, $item->getSource()));
  178. $item->setTranslation(join(L10N_UPDATE_PLURAL_DELIMITER, $item->getTranslation()));
  179. }
  180. $this->importString($item);
  181. }
  182. /**
  183. * Implements PoWriterInterface::writeItems().
  184. */
  185. public function writeItems(PoReaderInterface $reader, $count = -1) {
  186. $forever = $count == -1;
  187. while (($count-- > 0 || $forever) && ($item = $reader->readItem())) {
  188. $this->writeItem($item);
  189. }
  190. }
  191. /**
  192. * Imports one string into the database.
  193. *
  194. * @param PoItem $item
  195. * The item being imported.
  196. *
  197. * @return int
  198. * The string ID of the existing string modified or the new string added.
  199. */
  200. private function importString(PoItem $item) {
  201. // Initialize overwrite options if not set.
  202. $this->_options['overwrite_options'] += array(
  203. 'not_customized' => FALSE,
  204. 'customized' => FALSE,
  205. );
  206. $overwrite_options = $this->_options['overwrite_options'];
  207. $customized = $this->_options['customized'];
  208. $context = $item->getContext();
  209. $source = $item->getSource();
  210. $translation = $item->getTranslation();
  211. // Look up the source string and any existing translation.
  212. $strings = $this->storage->getTranslations(array(
  213. 'language' => $this->_langcode,
  214. 'source' => $source,
  215. 'context' => $context
  216. ));
  217. $string = reset($strings);
  218. if (!empty($translation)) {
  219. // Skip this string unless it passes a check for dangerous code.
  220. if (!locale_string_is_safe($translation)) {
  221. watchdog('l10n_update', 'Import of string "%string" was skipped because of disallowed or malformed HTML.', array('%string' => $translation), WATCHDOG_ERROR);
  222. $this->_report['skips']++;
  223. return 0;
  224. }
  225. elseif ($string) {
  226. $string->setString($translation);
  227. if ($string->isNew()) {
  228. // No translation in this language.
  229. $string->setValues(array(
  230. 'language' => $this->_langcode,
  231. 'customized' => $customized
  232. ));
  233. $string->save();
  234. $this->_report['additions']++;
  235. }
  236. elseif ($overwrite_options[$string->customized ? 'customized' : 'not_customized']) {
  237. // Translation exists, only overwrite if instructed.
  238. $string->customized = $customized;
  239. $string->save();
  240. $this->_report['updates']++;
  241. }
  242. $this->_report['strings'][] = $string->getId();
  243. return $string->lid;
  244. }
  245. else {
  246. // No such source string in the database yet.
  247. $string = $this->storage->createString(array('source' => $source, 'context' => $context))
  248. ->save();
  249. $target = $this->storage->createTranslation(array(
  250. 'lid' => $string->getId(),
  251. 'language' => $this->_langcode,
  252. 'translation' => $translation,
  253. 'customized' => $customized,
  254. ))->save();
  255. $this->_report['additions']++;
  256. $this->_report['strings'][] = $string->getId();
  257. return $string->lid;
  258. }
  259. }
  260. elseif ($string && !$string->isNew() && $overwrite_options[$string->customized ? 'customized' : 'not_customized']) {
  261. // Empty translation, remove existing if instructed.
  262. $string->delete();
  263. $this->_report['deletes']++;
  264. $this->_report['strings'][] = $string->lid;
  265. return $string->lid;
  266. }
  267. }
  268. }