PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/sapphire/core/i18nTextCollector.php

https://github.com/benbruscella/vpcounselling.com
PHP | 419 lines | 282 code | 42 blank | 95 comment | 31 complexity | 3cd71df69c74118aa41d15d15b08c532 MD5 | raw file
  1. <?php
  2. /**
  3. * SilverStripe-variant of the "gettext" tool:
  4. * Parses the string content of all PHP-files and SilverStripe templates
  5. * for ocurrences of the _t() translation method. Also uses the {@link i18nEntityProvider}
  6. * interface to get dynamically defined entities by executing the
  7. * {@link provideI18nEntities()} method on all implementors of this interface.
  8. *
  9. * Collects all found entities (and their natural language text for the default locale)
  10. * into language-files for each module in an array notation. Creates or overwrites these files,
  11. * e.g. sapphire/lang/en_US.php.
  12. *
  13. * The collector needs to be run whenever you make new translatable
  14. * entities available. Please don't alter the arrays in language tables manually.
  15. *
  16. * Usage through URL: http://localhost/dev/tasks/i18nTextCollectorTast
  17. * Usage through URL (module-specific): http://localhost/dev/tasks/i18nTextCollectorTask/?module=mymodule
  18. * Usage on CLI: sake dev/tasks/i18nTextCollectorTask
  19. * Usage on CLI (module-specific): sake dev/tasks/i18nTextCollectorTask module=mymodule
  20. *
  21. * Requires PHP 5.1+ due to class_implements() limitations
  22. *
  23. * @author Bernat Foj Capell <bernat@silverstripe.com>
  24. * @author Ingo Schommer <FIRSTNAME@silverstripe.com>
  25. * @package sapphire
  26. * @subpackage i18n
  27. * @uses i18nEntityProvider
  28. * @uses i18n
  29. */
  30. class i18nTextCollector extends Object {
  31. protected $defaultLocale;
  32. /**
  33. * @var string $basePath The directory base on which the collector should act.
  34. * Usually the webroot set through {@link Director::baseFolder()}.
  35. * @todo Fully support changing of basePath through {@link SSViewer} and {@link ManifestBuilder}
  36. */
  37. public $basePath;
  38. /**
  39. * @var string $basePath The directory base on which the collector should create new lang folders and files.
  40. * Usually the webroot set through {@link Director::baseFolder()}.
  41. * Can be overwritten for testing or export purposes.
  42. * @todo Fully support changing of basePath through {@link SSViewer} and {@link ManifestBuilder}
  43. */
  44. public $baseSavePath;
  45. /**
  46. * @param $locale
  47. */
  48. function __construct($locale = null) {
  49. $this->defaultLocale = ($locale) ? $locale : i18n::default_locale();
  50. $this->basePath = Director::baseFolder();
  51. $this->baseSavePath = Director::baseFolder();
  52. parent::__construct();
  53. }
  54. /**
  55. * This is the main method to build the master string tables with the original strings.
  56. * It will search for existent modules that use the i18n feature, parse the _t() calls
  57. * and write the resultant files in the lang folder of each module.
  58. *
  59. * @uses DataObject->collectI18nStatics()
  60. *
  61. * @param array $restrictToModules
  62. */
  63. public function run($restrictToModules = null) {
  64. //Debug::message("Collecting text...", false);
  65. $modules = array();
  66. // A master string tables array (one mst per module)
  67. $entitiesByModule = array();
  68. //Search for and process existent modules, or use the passed one instead
  69. if($restrictToModules && count($restrictToModules)) {
  70. foreach($restrictToModules as $restrictToModule) {
  71. $modules[] = basename($restrictToModule);
  72. }
  73. } else {
  74. $modules = scandir($this->basePath);
  75. }
  76. foreach($modules as $module) {
  77. // Only search for calls in folder with a _config.php file (which means they are modules)
  78. $isValidModuleFolder = (
  79. is_dir("$this->basePath/$module")
  80. && is_file("$this->basePath/$module/_config.php")
  81. && substr($module,0,1) != '.'
  82. );
  83. if(!$isValidModuleFolder) continue;
  84. // we store the master string tables
  85. $processedEntities = $this->processModule($module);
  86. if(isset($entitiesByModule[$module])) {
  87. $entitiesByModule[$module] = array_merge_recursive($entitiesByModule[$module], $processedEntities);
  88. } else {
  89. $entitiesByModule[$module] = $processedEntities;
  90. }
  91. // extract all entities for "foreign" modules (fourth argument)
  92. foreach($entitiesByModule[$module] as $fullName => $spec) {
  93. if(isset($spec[3]) && $spec[3] && $spec[3] != $module) {
  94. $othermodule = $spec[3];
  95. if(!isset($entitiesByModule[$othermodule])) $entitiesByModule[$othermodule] = array();
  96. unset($spec[3]);
  97. $entitiesByModule[$othermodule][$fullName] = $spec;
  98. unset($entitiesByModule[$module][$fullName]);
  99. }
  100. }
  101. }
  102. // Write the generated master string tables
  103. $this->writeMasterStringFile($entitiesByModule);
  104. //Debug::message("Done!", false);
  105. }
  106. /**
  107. * Build the module's master string table
  108. *
  109. * @param string $module Module's name
  110. */
  111. protected function processModule($module) {
  112. $entitiesArr = array();
  113. //Debug::message("Processing Module '{$module}'", false);
  114. // Search for calls in code files if these exists
  115. if(is_dir("$this->basePath/$module/code")) {
  116. $fileList = $this->getFilesRecursive("$this->basePath/$module/code");
  117. } else if($module == 'sapphire') {
  118. // sapphire doesn't have the usual module structure, so we'll scan all subfolders
  119. $fileList = $this->getFilesRecursive("$this->basePath/$module");
  120. }
  121. foreach($fileList as $filePath) {
  122. // exclude ss-templates, they're scanned separately
  123. if(substr($filePath,-3) == 'php') {
  124. $content = file_get_contents($filePath);
  125. $entitiesArr = array_merge($entitiesArr,(array)$this->collectFromCode($content, $module));
  126. $entitiesArr = array_merge($entitiesArr, (array)$this->collectFromEntityProviders($filePath, $module));
  127. }
  128. }
  129. // Search for calls in template files if these exists
  130. if(is_dir("$this->basePath/$module/templates")) {
  131. $fileList = $this->getFilesRecursive("$this->basePath/$module/templates");
  132. foreach($fileList as $index => $filePath) {
  133. $content = file_get_contents($filePath);
  134. // templates use their filename as a namespace
  135. $namespace = basename($filePath);
  136. $entitiesArr = array_merge($entitiesArr, (array)$this->collectFromTemplate($content, $module, $namespace));
  137. }
  138. }
  139. // sort for easier lookup and comparison with translated files
  140. ksort($entitiesArr);
  141. return $entitiesArr;
  142. }
  143. public function collectFromCode($content, $module) {
  144. $entitiesArr = array();
  145. $regexRule = '_t[[:space:]]*\(' .
  146. '[[:space:]]*("[^"]*"|\\\'[^\']*\\\')[[:space:]]*,' . // namespace.entity
  147. '[[:space:]]*(("([^"]|\\\")*"|\'([^\']|\\\\\')*\')' . // value
  148. '([[:space:]]*\\.[[:space:]]*("([^"]|\\\")*"|\'([^\']|\\\\\')*\'))*)' . // concatenations
  149. '([[:space:]]*,[[:space:]]*[^,)]*)?([[:space:]]*,' . // priority (optional)
  150. '[[:space:]]*("([^"]|\\\")*"|\'([^\']|\\\\\')*\'))?[[:space:]]*' . // comment (optional)
  151. '\)';
  152. while (ereg($regexRule, $content, $regs)) {
  153. $entitiesArr = array_merge($entitiesArr, (array)$this->entitySpecFromRegexMatches($regs));
  154. // remove parsed content to continue while() loop
  155. $content = str_replace($regs[0],"",$content);
  156. }
  157. ksort($entitiesArr);
  158. return $entitiesArr;
  159. }
  160. public function collectFromTemplate($content, $module, $fileName) {
  161. $entitiesArr = array();
  162. // Search for included templates
  163. preg_match_all('/<' . '% include +([A-Za-z0-9_]+) +%' . '>/', $content, $regs, PREG_SET_ORDER);
  164. foreach($regs as $reg) {
  165. $includeName = $reg[1];
  166. $includeFileName = "{$includeName}.ss";
  167. $filePath = SSViewer::getTemplateFileByType($includeName, 'Includes');
  168. if(!$filePath) $filePath = SSViewer::getTemplateFileByType($includeName, 'main');
  169. if($filePath) {
  170. $includeContent = file_get_contents($filePath);
  171. $entitiesArr = array_merge($entitiesArr,(array)$this->collectFromTemplate($includeContent, $module, $includeFileName));
  172. }
  173. // @todo Will get massively confused if you include the includer -> infinite loop
  174. }
  175. // @todo respect template tags (< % _t() % > instead of _t())
  176. $regexRule = '_t[[:space:]]*\(' .
  177. '[[:space:]]*("[^"]*"|\\\'[^\']*\\\')[[:space:]]*,' . // namespace.entity
  178. '[[:space:]]*(("([^"]|\\\")*"|\'([^\']|\\\\\')*\')' . // value
  179. '([[:space:]]*\\.[[:space:]]*("([^"]|\\\")*"|\'([^\']|\\\\\')*\'))*)' . // concatenations
  180. '([[:space:]]*,[[:space:]]*[^,)]*)?([[:space:]]*,' . // priority (optional)
  181. '[[:space:]]*("([^"]|\\\")*"|\'([^\']|\\\\\')*\'))?[[:space:]]*' . // comment (optional)
  182. '\)';
  183. while(ereg($regexRule,$content,$regs)) {
  184. $entitiesArr = array_merge($entitiesArr,(array)$this->entitySpecFromRegexMatches($regs, $fileName));
  185. // remove parsed content to continue while() loop
  186. $content = str_replace($regs[0],"",$content);
  187. }
  188. ksort($entitiesArr);
  189. return $entitiesArr;
  190. }
  191. /**
  192. * @uses i18nEntityProvider
  193. */
  194. function collectFromEntityProviders($filePath) {
  195. $entitiesArr = array();
  196. $classes = ClassInfo::classes_for_file($filePath);
  197. if($classes) foreach($classes as $class) {
  198. // Not all classes can be instanciated without mandatory arguments,
  199. // so entity collection doesn't work for all SilverStripe classes currently
  200. // Requires PHP 5.1+
  201. if(class_exists($class) && in_array('i18nEntityProvider', class_implements($class))) {
  202. $reflectionClass = new ReflectionClass($class);
  203. if($reflectionClass->isAbstract()) continue;
  204. $obj = singleton($class);
  205. $entitiesArr = array_merge($entitiesArr,(array)$obj->provideI18nEntities());
  206. }
  207. }
  208. ksort($entitiesArr);
  209. return $entitiesArr;
  210. }
  211. /**
  212. * @todo Fix regexes so the deletion of quotes, commas and newlines from wrong matches isn't necessary
  213. */
  214. protected function entitySpecFromRegexMatches($regs, $_namespace = null) {
  215. // remove wrapping quotes
  216. $fullName = substr($regs[1],1,-1);
  217. // split fullname into entity parts
  218. $entityParts = explode('.', $fullName);
  219. if(count($entityParts) > 1) {
  220. // templates don't have a custom namespace
  221. $entity = array_pop($entityParts);
  222. // namespace might contain dots, so we explode
  223. $namespace = implode('.',$entityParts);
  224. } else {
  225. $entity = array_pop($entityParts);
  226. $namespace = $_namespace;
  227. }
  228. // If a dollar sign is used in the entity name,
  229. // we can't resolve without running the method,
  230. // and skip the processing. This is mostly used for
  231. // dynamically translating static properties, e.g. looping
  232. // through $db, which are detected by {@link collectFromEntityProviders}.
  233. if(strpos('$', $entity) !== FALSE) return false;
  234. // remove wrapping quotes
  235. $value = ($regs[2]) ? substr($regs[2],1,-1) : null;
  236. $value = ereg_replace("([^\\])['\"][[:space:]]*\.[[:space:]]*['\"]",'\\1',$value);
  237. // only escape quotes when wrapped in double quotes, to make them safe for insertion
  238. // into single-quoted PHP code. If they're wrapped in single quotes, the string should
  239. // be properly escaped already
  240. if(substr($regs[2],0,1) == '"') {
  241. // Double quotes don't need escaping
  242. $value = str_replace('\\"','"', $value);
  243. // But single quotes do
  244. $value = str_replace("'","\\'", $value);
  245. }
  246. // remove starting comma and any newlines
  247. $eol = PHP_EOL;
  248. $prio = ($regs[10]) ? trim(preg_replace("/$eol/", '', substr($regs[10],1))) : null;
  249. // remove wrapping quotes
  250. $comment = ($regs[12]) ? substr($regs[12],1,-1) : null;
  251. return array(
  252. "{$namespace}.{$entity}" => array(
  253. $value,
  254. $prio,
  255. $comment
  256. )
  257. );
  258. }
  259. /**
  260. * Input for langArrayCodeForEntitySpec() should be suitable for insertion
  261. * into single-quoted strings, so needs to be escaped already.
  262. *
  263. * @param string $entity The entity name, e.g. CMSMain.BUTTONSAVE
  264. */
  265. public function langArrayCodeForEntitySpec($entityFullName, $entitySpec) {
  266. $php = '';
  267. $eol = PHP_EOL;
  268. $entityParts = explode('.', $entityFullName);
  269. if(count($entityParts) > 1) {
  270. // templates don't have a custom namespace
  271. $entity = array_pop($entityParts);
  272. // namespace might contain dots, so we implode back
  273. $namespace = implode('.',$entityParts);
  274. } else {
  275. user_error("i18nTextCollector::langArrayCodeForEntitySpec(): Wrong entity format for $entityFullName with values" . var_export($entitySpec, true), E_USER_WARNING);
  276. return false;
  277. }
  278. $value = $entitySpec[0];
  279. $prio = (isset($entitySpec[1])) ? addcslashes($entitySpec[1],'\'') : null;
  280. $comment = (isset($entitySpec[2])) ? addcslashes($entitySpec[2],'\'') : null;
  281. $php .= '$lang[\'' . $this->defaultLocale . '\'][\'' . $namespace . '\'][\'' . $entity . '\'] = ';
  282. if ($prio) {
  283. $php .= "array($eol\t'" . $value . "',$eol\t" . $prio;
  284. if ($comment) {
  285. $php .= ",$eol\t'" . $comment . '\'';
  286. }
  287. $php .= "$eol);";
  288. } else {
  289. $php .= '\'' . $value . '\';';
  290. }
  291. $php .= "$eol";
  292. return $php;
  293. }
  294. /**
  295. * Write the master string table of every processed module
  296. */
  297. protected function writeMasterStringFile($entitiesByModule) {
  298. // Write each module language file
  299. if($entitiesByModule) foreach($entitiesByModule as $module => $entities) {
  300. $php = '';
  301. $eol = PHP_EOL;
  302. // Create folder for lang files
  303. $langFolder = $this->baseSavePath . '/' . $module . '/lang';
  304. if(!file_exists($langFolder)) {
  305. Filesystem::makeFolder($langFolder, Filesystem::$folder_create_mask);
  306. touch($langFolder . '/_manifest_exclude');
  307. }
  308. // Open the English file and write the Master String Table
  309. $langFile = $langFolder . '/' . $this->defaultLocale . '.php';
  310. if($fh = fopen($langFile, "w")) {
  311. if($entities) foreach($entities as $fullName => $spec) {
  312. $php .= $this->langArrayCodeForEntitySpec($fullName, $spec);
  313. }
  314. // test for valid PHP syntax by eval'ing it
  315. try{
  316. eval($php);
  317. } catch(Exception $e) {
  318. user_error('i18nTextCollector->writeMasterStringFile(): Invalid PHP language file. Error: ' . $e->toString(), E_USER_ERROR);
  319. }
  320. fwrite($fh, "<"."?php{$eol}{$eol}global \$lang;{$eol}{$eol}" . $php . "{$eol}?".">");
  321. fclose($fh);
  322. //Debug::message("Created file: $langFolder/" . $this->defaultLocale . ".php", false);
  323. } else {
  324. user_error("Cannot write language file! Please check permissions of $langFolder/" . $this->defaultLocale . ".php", E_USER_ERROR);
  325. }
  326. }
  327. }
  328. /**
  329. * Helper function that searches for potential files to be parsed
  330. *
  331. * @param string $folder base directory to scan (will scan recursively)
  332. * @param array $fileList Array where potential files will be added to
  333. */
  334. protected function getFilesRecursive($folder, &$fileList = null) {
  335. if(!$fileList) $fileList = array();
  336. $items = scandir($folder);
  337. $isValidFolder = (
  338. !in_array('_manifest_exclude', $items)
  339. && !preg_match('/\/tests$/', $folder)
  340. );
  341. if($items && $isValidFolder) foreach($items as $item) {
  342. if(substr($item,0,1) == '.') continue;
  343. if(substr($item,-4) == '.php') $fileList[substr($item,0,-4)] = "$folder/$item";
  344. else if(substr($item,-3) == '.ss') $fileList[$item] = "$folder/$item";
  345. else if(is_dir("$folder/$item")) $this->getFilesRecursive("$folder/$item", $fileList);
  346. }
  347. return $fileList;
  348. }
  349. public function getDefaultLocale() {
  350. return $this->defaultLocale;
  351. }
  352. public function setDefaultLocale($locale) {
  353. $this->defaultLocale = $locale;
  354. }
  355. }
  356. ?>