/blog/core/model/modx/modtranslate095.class.php

https://bitbucket.org/orchdork10159/dnsman.ly · PHP · 168 lines · 99 code · 9 blank · 60 comment · 16 complexity · 6c34f5ca53144260daa8d0f594f9edf7 MD5 · raw file

  1. <?php
  2. /**
  3. * MODX Revolution
  4. *
  5. * Copyright 2006-2013 by MODX, LLC.
  6. * All rights reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it under
  9. * the terms of the GNU General Public License as published by the Free Software
  10. * Foundation; either version 2 of the License, or (at your option) any later
  11. * version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  15. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU General Public License along with
  19. * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  20. * Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. * @package modx
  23. */
  24. /**
  25. * Utility class for assisting in migrating content from older MODX releases.
  26. *
  27. * @package modx
  28. */
  29. class modTranslate095 {
  30. /**
  31. * A reference to the modX instance
  32. * @var modX $modx
  33. */
  34. public $modx= null;
  35. /**
  36. * The parsing engine for interpreting Evolution-style tags
  37. * @var modParser095
  38. */
  39. public $parser= null;
  40. /**
  41. * Initializes the class and sets up a translation map
  42. * @param modX $modx A reference to the modX instance
  43. */
  44. function __construct(modX &$modx) {
  45. $this->modx = &$modx;
  46. $this->preTranslationSearch= array('[*','[~','[+','[!');
  47. $this->preTranslationReplace= array('#trans->[*','#trans->[~','#trans->[+','#trans->[!');
  48. $this->tagTranslation= array (
  49. '[[++' => array ('[(', ')]', '++'),
  50. '[[$' => array ('{{', '}}', '$'),
  51. '[[*' => array ('#trans->[*', '*]', '*'),
  52. '[[~' => array ('#trans->[~', '~]', '~'),
  53. '[[+' => array ('#trans->[+', '+]', '+'),
  54. '[[!' => array ('#trans->[!', '!]', '!'),
  55. );
  56. }
  57. /**
  58. * Gets the parsing engine
  59. *
  60. * @return modParser095
  61. */
  62. public function getParser() {
  63. if (!is_object($this->parser) || !($this->parser instanceof modParser095)) {
  64. $this->parser= & $this->modx->getService('parser095', 'modParser095');
  65. }
  66. return $this->parser;
  67. }
  68. /**
  69. * Translate the site into Revolution-style tags
  70. *
  71. * @param boolean $save Whether or not to actually save the content changed
  72. * @param null $classes An array of classes and fields to translate
  73. * @param array $files An array of files to attempt to translate
  74. * @param boolean|string $toFile If true, will write the file to the specified log
  75. * @return void
  76. */
  77. public function translateSite($save= false, $classes= null, $files= array (), $toFile= false) {
  78. $parser = $this->getParser();
  79. $parser->tagTranslation = $this->tagTranslation;
  80. if ($classes === null) {
  81. $classes= array (
  82. 'modResource' => array ('content', 'pagetitle', 'longtitle', 'description', 'menutitle', 'introtext'),
  83. 'modTemplate' => array ('content'),
  84. 'modChunk' => array ('snippet'),
  85. 'modSnippet' => array ('snippet'),
  86. 'modPlugin' => array ('plugincode'),
  87. 'modTemplateVar' => array ('default_text'),
  88. 'modTemplateVarResource' => array ('value'),
  89. 'modSystemSetting' => array ('value')
  90. );
  91. }
  92. ob_start();
  93. echo "Processing classes: " . print_r($classes, true) . "\n\n\n";
  94. foreach ($classes as $className => $fields) {
  95. $resources= $this->modx->getCollection($className);
  96. if ($resources) {
  97. foreach ($resources as $resource) {
  98. foreach ($fields as $field) {
  99. $content= $resource->get($field);
  100. if ($content) {
  101. echo "[BEGIN TRANSLATING FIELD] {$field}\n";
  102. $content = str_replace($this->preTranslationSearch, $this->preTranslationReplace, $content);
  103. while ($parser->translate($content, array(), true)) {
  104. $resource->set($field, $content);
  105. }
  106. echo "[END TRANSLATING FIELD] {$field}\n\n";
  107. }
  108. }
  109. if ($save) {
  110. $resource->save();
  111. }
  112. }
  113. }
  114. }
  115. if (!empty ($files)) {
  116. echo $this->translateFiles($save, $files);
  117. }
  118. $log= ob_get_contents();
  119. ob_end_clean();
  120. if ($toFile) {
  121. $cacheManager= $this->modx->getCacheManager();
  122. $cacheManager->writeFile($toFile, $log);
  123. } else {
  124. echo $log;
  125. }
  126. }
  127. /**
  128. * Translate specific files
  129. *
  130. * @param boolean $save If true, will save the translation
  131. * @param array $files An array of files to translate
  132. * @return string
  133. */
  134. public function translateFiles($save= false, $files= array()) {
  135. $output= '';
  136. if (is_array($files) && !empty ($files)) {
  137. $parser= $this->getParser();
  138. $parser->tagTranslation=$this->tagTranslation;
  139. $cacheManager= $this->modx->getCacheManager();
  140. $output .= "Processing files: " . print_r($files, true) . "\n\n\n";
  141. ob_start();
  142. foreach ($files as $file) {
  143. if (file_exists($file)) {
  144. echo "[BEGIN TRANSLATING FILE] {$file}\n";
  145. $content= file_get_contents($file);
  146. if ($content !== false) {
  147. $content=str_replace($this->preTranslationSearch,$this->preTranslationReplace,$content);
  148. while ($parser->translate($content, array(), true)) {}
  149. if ($save) $cacheManager->writeFile($file, $content);
  150. }
  151. echo "[END TRANSLATING FILE] {$file}\n";
  152. }
  153. }
  154. $output .= ob_get_contents();
  155. ob_end_clean();
  156. }
  157. return $output;
  158. }
  159. }