modtranslate095.class.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /*
  3. * This file is part of MODX Revolution.
  4. *
  5. * Copyright (c) MODX, LLC. All Rights Reserved.
  6. *
  7. * For complete copyright and license information, see the COPYRIGHT and LICENSE
  8. * files found in the top-level directory of this distribution.
  9. */
  10. /**
  11. * Utility class for assisting in migrating content from older MODX releases.
  12. *
  13. * @package modx
  14. * @deprecated
  15. */
  16. class modTranslate095 {
  17. /**
  18. * A reference to the modX instance
  19. * @var modX $modx
  20. */
  21. public $modx= null;
  22. /**
  23. * The parsing engine for interpreting Evolution-style tags
  24. * @var modParser095
  25. */
  26. public $parser= null;
  27. /**
  28. * Initializes the class and sets up a translation map
  29. * @param modX $modx A reference to the modX instance
  30. */
  31. function __construct(modX &$modx) {
  32. $this->modx = &$modx;
  33. $this->preTranslationSearch= array('[*','[~','[+','[!');
  34. $this->preTranslationReplace= array('#trans->[*','#trans->[~','#trans->[+','#trans->[!');
  35. $this->tagTranslation= array (
  36. '[[++' => array ('[(', ')]', '++'),
  37. '[[$' => array ('{{', '}}', '$'),
  38. '[[*' => array ('#trans->[*', '*]', '*'),
  39. '[[~' => array ('#trans->[~', '~]', '~'),
  40. '[[+' => array ('#trans->[+', '+]', '+'),
  41. '[[!' => array ('#trans->[!', '!]', '!'),
  42. );
  43. }
  44. /**
  45. * Gets the parsing engine
  46. *
  47. * @return modParser095
  48. */
  49. public function getParser() {
  50. if (!is_object($this->parser) || !($this->parser instanceof modParser095)) {
  51. $this->parser= & $this->modx->getService('parser095', 'modParser095');
  52. }
  53. return $this->parser;
  54. }
  55. /**
  56. * Translate the site into Revolution-style tags
  57. *
  58. * @param boolean $save Whether or not to actually save the content changed
  59. * @param null $classes An array of classes and fields to translate
  60. * @param array $files An array of files to attempt to translate
  61. * @param boolean|string $toFile If true, will write the file to the specified log
  62. * @return void
  63. */
  64. public function translateSite($save= false, $classes= null, $files= array (), $toFile= false) {
  65. $parser = $this->getParser();
  66. $parser->tagTranslation = $this->tagTranslation;
  67. if ($classes === null) {
  68. $classes= array (
  69. 'modResource' => array ('content', 'pagetitle', 'longtitle', 'description', 'menutitle', 'introtext'),
  70. 'modTemplate' => array ('content'),
  71. 'modChunk' => array ('snippet'),
  72. 'modSnippet' => array ('snippet'),
  73. 'modPlugin' => array ('plugincode'),
  74. 'modTemplateVar' => array ('default_text'),
  75. 'modTemplateVarResource' => array ('value'),
  76. 'modSystemSetting' => array ('value')
  77. );
  78. }
  79. ob_start();
  80. echo "Processing classes: " . print_r($classes, true) . "\n\n\n";
  81. foreach ($classes as $className => $fields) {
  82. $resources= $this->modx->getCollection($className);
  83. if ($resources) {
  84. foreach ($resources as $resource) {
  85. foreach ($fields as $field) {
  86. $content= $resource->get($field);
  87. if ($content) {
  88. echo "[BEGIN TRANSLATING FIELD] {$field}\n";
  89. $content = str_replace($this->preTranslationSearch, $this->preTranslationReplace, $content);
  90. while ($parser->translate($content, array(), true)) {
  91. $resource->set($field, $content);
  92. }
  93. echo "[END TRANSLATING FIELD] {$field}\n\n";
  94. }
  95. }
  96. if ($save) {
  97. $resource->save();
  98. }
  99. }
  100. }
  101. }
  102. if (!empty ($files)) {
  103. echo $this->translateFiles($save, $files);
  104. }
  105. $log= ob_get_contents();
  106. ob_end_clean();
  107. if ($toFile) {
  108. $cacheManager= $this->modx->getCacheManager();
  109. $cacheManager->writeFile($toFile, $log);
  110. } else {
  111. echo $log;
  112. }
  113. }
  114. /**
  115. * Translate specific files
  116. *
  117. * @param boolean $save If true, will save the translation
  118. * @param array $files An array of files to translate
  119. * @return string
  120. */
  121. public function translateFiles($save= false, $files= array()) {
  122. $output= '';
  123. if (is_array($files) && !empty ($files)) {
  124. $parser= $this->getParser();
  125. $parser->tagTranslation=$this->tagTranslation;
  126. $cacheManager= $this->modx->getCacheManager();
  127. $output .= "Processing files: " . print_r($files, true) . "\n\n\n";
  128. ob_start();
  129. foreach ($files as $file) {
  130. if (file_exists($file)) {
  131. echo "[BEGIN TRANSLATING FILE] {$file}\n";
  132. $content= file_get_contents($file);
  133. if ($content !== false) {
  134. $content=str_replace($this->preTranslationSearch,$this->preTranslationReplace,$content);
  135. while ($parser->translate($content, array(), true)) {}
  136. if ($save) $cacheManager->writeFile($file, $content);
  137. }
  138. echo "[END TRANSLATING FILE] {$file}\n";
  139. }
  140. }
  141. $output .= ob_get_contents();
  142. ob_end_clean();
  143. }
  144. return $output;
  145. }
  146. }