PoDatabaseWriter.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. public 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. public 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. public function getOptions() {
  100. return $this->_options;
  101. }
  102. /**
  103. * Set the options for the current writer.
  104. */
  105. public 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. public 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. * Exception is thrown when required properties are not set.
  139. */
  140. public function setHeader(PoHeader $header) {
  141. $this->_header = $header;
  142. $languages = language_list();
  143. // Check for options.
  144. $options = $this->getOptions();
  145. if (empty($options)) {
  146. throw new \Exception('Options should be set before assigning a PoHeader.');
  147. }
  148. $overwrite_options = $options['overwrite_options'];
  149. // Check for langcode.
  150. $langcode = $this->_langcode;
  151. if (empty($langcode)) {
  152. throw new \Exception('Langcode should be set before assigning a PoHeader.');
  153. }
  154. // Check is language is already created.
  155. if (!isset($languages[$langcode])) {
  156. throw new \Exception('Language should be known before using it.');
  157. }
  158. if (array_sum($overwrite_options) || empty($languages[$langcode]->plurals)) {
  159. // Get and store the plural formula if available.
  160. $plural = $header->getPluralForms();
  161. if (isset($plural) && $p = $header->parsePluralForms($plural)) {
  162. list($nplurals, $formula) = $p;
  163. db_update('languages')
  164. ->fields(array(
  165. 'plurals' => $nplurals,
  166. 'formula' => $formula,
  167. ))
  168. ->condition('language', $langcode)
  169. ->execute();
  170. }
  171. }
  172. }
  173. /**
  174. * Implements PoWriterInterface::writeItem().
  175. */
  176. public function writeItem(PoItem $item) {
  177. if ($item->isPlural()) {
  178. $sources = $item->getSource();
  179. $translations = $item->getTranslation();
  180. // Build additional source strings for plurals.
  181. $entries = array_keys($translations);
  182. for ($i = 3; $i <= count($entries); $i++) {
  183. $sources[] = $sources[1];
  184. }
  185. $translations = array_map('_locale_import_append_plural', $translations, $entries);
  186. $sources = array_map('_locale_import_append_plural', $sources, $entries);
  187. $plid = 0;
  188. foreach ($entries as $index) {
  189. $item->setSource($sources[$index]);
  190. $item->setTranslation($translations[$index]);
  191. $plid = $this->importString($item, $plid, $index);
  192. }
  193. }
  194. else {
  195. $this->importString($item);
  196. }
  197. }
  198. /**
  199. * Implements PoWriterInterface::writeItems().
  200. */
  201. public function writeItems(PoReaderInterface $reader, $count = -1) {
  202. $forever = $count == -1;
  203. while (($count-- > 0 || $forever) && ($item = $reader->readItem())) {
  204. $this->writeItem($item);
  205. }
  206. }
  207. /**
  208. * Imports one string into the database.
  209. *
  210. * @param PoItem $item
  211. * The item being imported.
  212. * @param integer $plid
  213. * The parent string identifier for plural strings.
  214. * @param integer $plural
  215. * The plural index number.
  216. *
  217. * @return int
  218. * The string ID of the existing string modified or the new string added.
  219. */
  220. private function importString(PoItem $item, $plid = 0, $plural = 0) {
  221. // Initialize overwrite options if not set.
  222. $this->_options['overwrite_options'] += array(
  223. 'not_customized' => FALSE,
  224. 'customized' => FALSE,
  225. );
  226. $overwrite_options = $this->_options['overwrite_options'];
  227. $customized = $this->_options['customized'];
  228. $context = $item->getContext();
  229. $source = $item->getSource();
  230. $translation = $item->getTranslation();
  231. $textgroup = $item->getTextgroup();
  232. // Look up the source string and any existing translation.
  233. $strings = $this->storage->getTranslations(array(
  234. 'language' => $this->_langcode,
  235. 'source' => $source,
  236. 'context' => $context,
  237. 'textgroup' => $textgroup,
  238. ));
  239. $string = reset($strings);
  240. if (!empty($translation)) {
  241. // Skip this string unless it passes a check for dangerous code.
  242. if (!locale_string_is_safe($translation)) {
  243. watchdog('l10n_update', 'Import of string "%string" was skipped because of disallowed or malformed HTML.', array('%string' => $translation), WATCHDOG_ERROR);
  244. $this->_report['skips']++;
  245. return 0;
  246. }
  247. elseif ($string) {
  248. $string->setString($translation);
  249. if ($string->isNew()) {
  250. // No translation in this language.
  251. $string->setValues(array(
  252. 'plid' => $plid,
  253. 'plural' => $plural,
  254. 'language' => $this->_langcode,
  255. 'customized' => $customized,
  256. ));
  257. $string->save();
  258. $this->_report['additions']++;
  259. }
  260. elseif ($overwrite_options[$string->customized ? 'customized' : 'not_customized']) {
  261. // Translation exists, only overwrite if instructed.
  262. $string->customized = $customized;
  263. $string->save();
  264. $this->_report['updates']++;
  265. }
  266. $this->_report['strings'][] = $string->getId();
  267. return $string->lid;
  268. }
  269. else {
  270. // No such source string in the database yet.
  271. $string = $this->storage->createString(array('source' => $source, 'context' => $context, 'textgroup' => $textgroup))
  272. ->save();
  273. $this->storage->createTranslation(array(
  274. 'lid' => $string->getId(),
  275. 'plid' => $plid,
  276. 'plural' => $plural,
  277. 'language' => $this->_langcode,
  278. 'translation' => $translation,
  279. 'customized' => $customized,
  280. ))->save();
  281. $this->_report['additions']++;
  282. $this->_report['strings'][] = $string->getId();
  283. return $string->lid;
  284. }
  285. }
  286. elseif ($string && !$string->isNew() && $overwrite_options[$string->customized ? 'customized' : 'not_customized']) {
  287. // Empty translation, remove existing if instructed.
  288. $string->delete();
  289. $this->_report['deletes']++;
  290. $this->_report['strings'][] = $string->lid;
  291. return $string->lid;
  292. }
  293. }
  294. }