ExtractTask.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. <?php
  2. /**
  3. * Language string extractor
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @since CakePHP(tm) v 1.2.0.5012
  16. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  17. */
  18. App::uses('File', 'Utility');
  19. App::uses('Folder', 'Utility');
  20. /**
  21. * Language string extractor
  22. *
  23. * @package Cake.Console.Command.Task
  24. */
  25. class ExtractTask extends Shell {
  26. /**
  27. * Paths to use when looking for strings
  28. *
  29. * @var string
  30. */
  31. protected $_paths = array();
  32. /**
  33. * Files from where to extract
  34. *
  35. * @var array
  36. */
  37. protected $_files = array();
  38. /**
  39. * Merge all domains string into the default.pot file
  40. *
  41. * @var boolean
  42. */
  43. protected $_merge = false;
  44. /**
  45. * Current file being processed
  46. *
  47. * @var string
  48. */
  49. protected $_file = null;
  50. /**
  51. * Contains all content waiting to be write
  52. *
  53. * @var string
  54. */
  55. protected $_storage = array();
  56. /**
  57. * Extracted tokens
  58. *
  59. * @var array
  60. */
  61. protected $_tokens = array();
  62. /**
  63. * Extracted strings
  64. *
  65. * @var array
  66. */
  67. protected $_strings = array();
  68. /**
  69. * Destination path
  70. *
  71. * @var string
  72. */
  73. protected $_output = null;
  74. /**
  75. * An array of directories to exclude.
  76. *
  77. * @var array
  78. */
  79. protected $_exclude = array();
  80. /**
  81. * Holds whether this call should extract model validation messages
  82. *
  83. * @var boolean
  84. */
  85. protected $_extractValidation = true;
  86. /**
  87. * Holds the validation string domain to use for validation messages when extracting
  88. *
  89. * @var boolean
  90. */
  91. protected $_validationDomain = 'default';
  92. /**
  93. * Execution method always used for tasks
  94. *
  95. * @return void
  96. */
  97. public function execute() {
  98. if (!empty($this->params['exclude'])) {
  99. $this->_exclude = explode(',', $this->params['exclude']);
  100. }
  101. if (isset($this->params['files']) && !is_array($this->params['files'])) {
  102. $this->_files = explode(',', $this->params['files']);
  103. }
  104. if (isset($this->params['paths'])) {
  105. $this->_paths = explode(',', $this->params['paths']);
  106. } else if (isset($this->params['plugin'])) {
  107. $plugin = Inflector::camelize($this->params['plugin']);
  108. if (!CakePlugin::loaded($plugin)) {
  109. CakePlugin::load($plugin);
  110. }
  111. $this->_paths = array(CakePlugin::path($plugin));
  112. $this->params['plugin'] = $plugin;
  113. } else {
  114. $defaultPath = APP;
  115. $message = __d('cake_console', "What is the path you would like to extract?\n[Q]uit [D]one");
  116. while (true) {
  117. $response = $this->in($message, null, $defaultPath);
  118. if (strtoupper($response) === 'Q') {
  119. $this->out(__d('cake_console', 'Extract Aborted'));
  120. $this->_stop();
  121. } elseif (strtoupper($response) === 'D') {
  122. $this->out();
  123. break;
  124. } elseif (is_dir($response)) {
  125. $this->_paths[] = $response;
  126. $defaultPath = 'D';
  127. } else {
  128. $this->err(__d('cake_console', 'The directory path you supplied was not found. Please try again.'));
  129. }
  130. $this->out();
  131. }
  132. }
  133. if (!empty($this->params['exclude-plugins']) && $this->_isExtractingApp()) {
  134. $this->_exclude = array_merge($this->_exclude, App::path('plugins'));
  135. }
  136. if (!empty($this->params['ignore-model-validation']) || (!$this->_isExtractingApp() && empty($plugin))) {
  137. $this->_extractValidation = false;
  138. }
  139. if (!empty($this->params['validation-domain'])) {
  140. $this->_validationDomain = $this->params['validation-domain'];
  141. }
  142. if (isset($this->params['output'])) {
  143. $this->_output = $this->params['output'];
  144. } else if (isset($this->params['plugin'])) {
  145. $this->_output = $this->_paths[0] . DS . 'Locale';
  146. } else {
  147. $message = __d('cake_console', "What is the path you would like to output?\n[Q]uit", $this->_paths[0] . DS . 'Locale');
  148. while (true) {
  149. $response = $this->in($message, null, $this->_paths[0] . DS . 'Locale');
  150. if (strtoupper($response) === 'Q') {
  151. $this->out(__d('cake_console', 'Extract Aborted'));
  152. $this->_stop();
  153. } elseif (is_dir($response)) {
  154. $this->_output = $response . DS;
  155. break;
  156. } else {
  157. $this->err(__d('cake_console', 'The directory path you supplied was not found. Please try again.'));
  158. }
  159. $this->out();
  160. }
  161. }
  162. if (isset($this->params['merge'])) {
  163. $this->_merge = !(strtolower($this->params['merge']) === 'no');
  164. } else {
  165. $this->out();
  166. $response = $this->in(__d('cake_console', 'Would you like to merge all domains strings into the default.pot file?'), array('y', 'n'), 'n');
  167. $this->_merge = strtolower($response) === 'y';
  168. }
  169. if (empty($this->_files)) {
  170. $this->_searchFiles();
  171. }
  172. $this->_extract();
  173. }
  174. /**
  175. * Extract text
  176. *
  177. * @return void
  178. */
  179. protected function _extract() {
  180. $this->out();
  181. $this->out();
  182. $this->out(__d('cake_console', 'Extracting...'));
  183. $this->hr();
  184. $this->out(__d('cake_console', 'Paths:'));
  185. foreach ($this->_paths as $path) {
  186. $this->out(' ' . $path);
  187. }
  188. $this->out(__d('cake_console', 'Output Directory: ') . $this->_output);
  189. $this->hr();
  190. $this->_extractTokens();
  191. $this->_extractValidationMessages();
  192. $this->_buildFiles();
  193. $this->_writeFiles();
  194. $this->_paths = $this->_files = $this->_storage = array();
  195. $this->_strings = $this->_tokens = array();
  196. $this->_extractValidation = true;
  197. $this->out();
  198. $this->out(__d('cake_console', 'Done.'));
  199. }
  200. /**
  201. * Get & configure the option parser
  202. *
  203. * @return void
  204. */
  205. public function getOptionParser() {
  206. $parser = parent::getOptionParser();
  207. return $parser->description(__d('cake_console', 'CakePHP Language String Extraction:'))
  208. ->addOption('app', array('help' => __d('cake_console', 'Directory where your application is located.')))
  209. ->addOption('paths', array('help' => __d('cake_console', 'Comma separated list of paths.')))
  210. ->addOption('merge', array(
  211. 'help' => __d('cake_console', 'Merge all domain strings into the default.po file.'),
  212. 'choices' => array('yes', 'no')
  213. ))
  214. ->addOption('output', array('help' => __d('cake_console', 'Full path to output directory.')))
  215. ->addOption('files', array('help' => __d('cake_console', 'Comma separated list of files.')))
  216. ->addOption('exclude-plugins', array(
  217. 'boolean' => true,
  218. 'default' => true,
  219. 'help' => __d('cake_console', 'Ignores all files in plugins if this command is run inside from the same app directory.')
  220. ))
  221. ->addOption('plugin', array(
  222. 'help' => __d('cake_console', 'Extracts tokens only from the plugin specified and puts the result in the plugin\'s Locale directory.')
  223. ))
  224. ->addOption('ignore-model-validation', array(
  225. 'boolean' => true,
  226. 'default' => false,
  227. 'help' => __d('cake_console', 'Ignores validation messages in the $validate property. If this flag is not set and the command is run from the same app directory, all messages in model validation rules will be extracted as tokens.')
  228. ))
  229. ->addOption('validation-domain', array(
  230. 'help' => __d('cake_console', 'If set to a value, the localization domain to be used for model validation messages.')
  231. ))
  232. ->addOption('exclude', array(
  233. 'help' => __d('cake_console', 'Comma separated list of directories to exclude. Any path containing a path segment with the provided values will be skipped. E.g. test,vendors')
  234. ));
  235. }
  236. /**
  237. * Extract tokens out of all files to be processed
  238. *
  239. * @return void
  240. */
  241. protected function _extractTokens() {
  242. foreach ($this->_files as $file) {
  243. $this->_file = $file;
  244. $this->out(__d('cake_console', 'Processing %s...', $file));
  245. $code = file_get_contents($file);
  246. $allTokens = token_get_all($code);
  247. $this->_tokens = array();
  248. foreach ($allTokens as $token) {
  249. if (!is_array($token) || ($token[0] != T_WHITESPACE && $token[0] != T_INLINE_HTML)) {
  250. $this->_tokens[] = $token;
  251. }
  252. }
  253. unset($allTokens);
  254. $this->_parse('__', array('singular'));
  255. $this->_parse('__n', array('singular', 'plural'));
  256. $this->_parse('__d', array('domain', 'singular'));
  257. $this->_parse('__c', array('singular'));
  258. $this->_parse('__dc', array('domain', 'singular'));
  259. $this->_parse('__dn', array('domain', 'singular', 'plural'));
  260. $this->_parse('__dcn', array('domain', 'singular', 'plural'));
  261. }
  262. }
  263. /**
  264. * Parse tokens
  265. *
  266. * @param string $functionName Function name that indicates translatable string (e.g: '__')
  267. * @param array $map Array containing what variables it will find (e.g: domain, singular, plural)
  268. * @return void
  269. */
  270. protected function _parse($functionName, $map) {
  271. $count = 0;
  272. $tokenCount = count($this->_tokens);
  273. while (($tokenCount - $count) > 1) {
  274. list($countToken, $firstParenthesis) = array($this->_tokens[$count], $this->_tokens[$count + 1]);
  275. if (!is_array($countToken)) {
  276. $count++;
  277. continue;
  278. }
  279. list($type, $string, $line) = $countToken;
  280. if (($type == T_STRING) && ($string == $functionName) && ($firstParenthesis == '(')) {
  281. $position = $count;
  282. $depth = 0;
  283. while ($depth == 0) {
  284. if ($this->_tokens[$position] == '(') {
  285. $depth++;
  286. } elseif ($this->_tokens[$position] == ')') {
  287. $depth--;
  288. }
  289. $position++;
  290. }
  291. $mapCount = count($map);
  292. $strings = $this->_getStrings($position, $mapCount);
  293. if ($mapCount == count($strings)) {
  294. extract(array_combine($map, $strings));
  295. $domain = isset($domain) ? $domain : 'default';
  296. $string = isset($plural) ? $singular . "\0" . $plural : $singular;
  297. $this->_strings[$domain][$string][$this->_file][] = $line;
  298. } else {
  299. $this->_markerError($this->_file, $line, $functionName, $count);
  300. }
  301. }
  302. $count++;
  303. }
  304. }
  305. /**
  306. * Looks for models in the application and extracts the validation messages
  307. * to be added to the translation map
  308. *
  309. * @return void
  310. */
  311. protected function _extractValidationMessages() {
  312. if (!$this->_extractValidation) {
  313. return;
  314. }
  315. App::uses('AppModel', 'Model');
  316. $plugin = null;
  317. if (!empty($this->params['plugin'])) {
  318. App::uses($this->params['plugin'] . 'AppModel', $this->params['plugin'] . '.Model');
  319. $plugin = $this->params['plugin'] . '.';
  320. }
  321. $models = App::objects($plugin . 'Model', null, false);
  322. foreach ($models as $model) {
  323. App::uses($model, $plugin . 'Model');
  324. $reflection = new ReflectionClass($model);
  325. $properties = $reflection->getDefaultProperties();
  326. $validate = $properties['validate'];
  327. if (empty($validate)) {
  328. continue;
  329. }
  330. $file = $reflection->getFileName();
  331. $domain = $this->_validationDomain;
  332. if (!empty($properties['validationDomain'])) {
  333. $domain = $properties['validationDomain'];
  334. }
  335. foreach ($validate as $field => $rules) {
  336. $this->_processValidationRules($field, $rules, $file, $domain);
  337. }
  338. }
  339. }
  340. /**
  341. * Process a validation rule for a field and looks for a message to be added
  342. * to the translation map
  343. *
  344. * @param string $field the name of the field that is being processed
  345. * @param array $rules the set of validation rules for the field
  346. * @param string $file the file name where this validation rule was found
  347. * @param string $domain default domain to bind the validations to
  348. * @return void
  349. */
  350. protected function _processValidationRules($field, $rules, $file, $domain) {
  351. if (is_array($rules)) {
  352. $dims = Set::countDim($rules);
  353. if ($dims == 1 || ($dims == 2 && isset($rules['message']))) {
  354. $rules = array($rules);
  355. }
  356. foreach ($rules as $rule => $validateProp) {
  357. $message = null;
  358. if (isset($validateProp['message'])) {
  359. if (is_array($validateProp['message'])) {
  360. $message = $validateProp['message'][0];
  361. } else {
  362. $message = $validateProp['message'];
  363. }
  364. } elseif (is_string($rule)) {
  365. $message = $rule;
  366. }
  367. if ($message) {
  368. $this->_strings[$domain][$message][$file][] = 'validation for field ' . $field;
  369. }
  370. }
  371. }
  372. }
  373. /**
  374. * Build the translate template file contents out of obtained strings
  375. *
  376. * @return void
  377. */
  378. protected function _buildFiles() {
  379. foreach ($this->_strings as $domain => $strings) {
  380. foreach ($strings as $string => $files) {
  381. $occurrences = array();
  382. foreach ($files as $file => $lines) {
  383. $occurrences[] = $file . ':' . implode(';', $lines);
  384. }
  385. $occurrences = implode("\n#: ", $occurrences);
  386. $header = '#: ' . str_replace($this->_paths, '', $occurrences) . "\n";
  387. if (strpos($string, "\0") === false) {
  388. $sentence = "msgid \"{$string}\"\n";
  389. $sentence .= "msgstr \"\"\n\n";
  390. } else {
  391. list($singular, $plural) = explode("\0", $string);
  392. $sentence = "msgid \"{$singular}\"\n";
  393. $sentence .= "msgid_plural \"{$plural}\"\n";
  394. $sentence .= "msgstr[0] \"\"\n";
  395. $sentence .= "msgstr[1] \"\"\n\n";
  396. }
  397. $this->_store($domain, $header, $sentence);
  398. if ($domain != 'default' && $this->_merge) {
  399. $this->_store('default', $header, $sentence);
  400. }
  401. }
  402. }
  403. }
  404. /**
  405. * Prepare a file to be stored
  406. *
  407. * @param string $domain
  408. * @param string $header
  409. * @param string $sentence
  410. * @return void
  411. */
  412. protected function _store($domain, $header, $sentence) {
  413. if (!isset($this->_storage[$domain])) {
  414. $this->_storage[$domain] = array();
  415. }
  416. if (!isset($this->_storage[$domain][$sentence])) {
  417. $this->_storage[$domain][$sentence] = $header;
  418. } else {
  419. $this->_storage[$domain][$sentence] .= $header;
  420. }
  421. }
  422. /**
  423. * Write the files that need to be stored
  424. *
  425. * @return void
  426. */
  427. protected function _writeFiles() {
  428. $overwriteAll = false;
  429. foreach ($this->_storage as $domain => $sentences) {
  430. $output = $this->_writeHeader();
  431. foreach ($sentences as $sentence => $header) {
  432. $output .= $header . $sentence;
  433. }
  434. $filename = $domain . '.pot';
  435. $File = new File($this->_output . $filename);
  436. $response = '';
  437. while ($overwriteAll === false && $File->exists() && strtoupper($response) !== 'Y') {
  438. $this->out();
  439. $response = $this->in(__d('cake_console', 'Error: %s already exists in this location. Overwrite? [Y]es, [N]o, [A]ll', $filename), array('y', 'n', 'a'), 'y');
  440. if (strtoupper($response) === 'N') {
  441. $response = '';
  442. while ($response == '') {
  443. $response = $this->in(__d('cake_console', "What would you like to name this file?"), null, 'new_' . $filename);
  444. $File = new File($this->_output . $response);
  445. $filename = $response;
  446. }
  447. } elseif (strtoupper($response) === 'A') {
  448. $overwriteAll = true;
  449. }
  450. }
  451. $File->write($output);
  452. $File->close();
  453. }
  454. }
  455. /**
  456. * Build the translation template header
  457. *
  458. * @return string Translation template header
  459. */
  460. protected function _writeHeader() {
  461. $output = "# LANGUAGE translation of CakePHP Application\n";
  462. $output .= "# Copyright YEAR NAME <EMAIL@ADDRESS>\n";
  463. $output .= "#\n";
  464. $output .= "#, fuzzy\n";
  465. $output .= "msgid \"\"\n";
  466. $output .= "msgstr \"\"\n";
  467. $output .= "\"Project-Id-Version: PROJECT VERSION\\n\"\n";
  468. $output .= "\"POT-Creation-Date: " . date("Y-m-d H:iO") . "\\n\"\n";
  469. $output .= "\"PO-Revision-Date: YYYY-mm-DD HH:MM+ZZZZ\\n\"\n";
  470. $output .= "\"Last-Translator: NAME <EMAIL@ADDRESS>\\n\"\n";
  471. $output .= "\"Language-Team: LANGUAGE <EMAIL@ADDRESS>\\n\"\n";
  472. $output .= "\"MIME-Version: 1.0\\n\"\n";
  473. $output .= "\"Content-Type: text/plain; charset=utf-8\\n\"\n";
  474. $output .= "\"Content-Transfer-Encoding: 8bit\\n\"\n";
  475. $output .= "\"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\\n\"\n\n";
  476. return $output;
  477. }
  478. /**
  479. * Get the strings from the position forward
  480. *
  481. * @param integer $position Actual position on tokens array
  482. * @param integer $target Number of strings to extract
  483. * @return array Strings extracted
  484. */
  485. protected function _getStrings(&$position, $target) {
  486. $strings = array();
  487. while (count($strings) < $target && ($this->_tokens[$position] == ',' || $this->_tokens[$position][0] == T_CONSTANT_ENCAPSED_STRING)) {
  488. if ($this->_tokens[$position][0] == T_CONSTANT_ENCAPSED_STRING && $this->_tokens[$position+1] == '.') {
  489. $string = '';
  490. while ($this->_tokens[$position][0] == T_CONSTANT_ENCAPSED_STRING || $this->_tokens[$position] == '.') {
  491. if ($this->_tokens[$position][0] == T_CONSTANT_ENCAPSED_STRING) {
  492. $string .= $this->_formatString($this->_tokens[$position][1]);
  493. }
  494. $position++;
  495. }
  496. $strings[] = $string;
  497. } else if ($this->_tokens[$position][0] == T_CONSTANT_ENCAPSED_STRING) {
  498. $strings[] = $this->_formatString($this->_tokens[$position][1]);
  499. }
  500. $position++;
  501. }
  502. return $strings;
  503. }
  504. /**
  505. * Format a string to be added as a translatable string
  506. *
  507. * @param string $string String to format
  508. * @return string Formatted string
  509. */
  510. protected function _formatString($string) {
  511. $quote = substr($string, 0, 1);
  512. $string = substr($string, 1, -1);
  513. if ($quote == '"') {
  514. $string = stripcslashes($string);
  515. } else {
  516. $string = strtr($string, array("\\'" => "'", "\\\\" => "\\"));
  517. }
  518. $string = str_replace("\r\n", "\n", $string);
  519. return addcslashes($string, "\0..\37\\\"");
  520. }
  521. /**
  522. * Indicate an invalid marker on a processed file
  523. *
  524. * @param string $file File where invalid marker resides
  525. * @param integer $line Line number
  526. * @param string $marker Marker found
  527. * @param integer $count Count
  528. * @return void
  529. */
  530. protected function _markerError($file, $line, $marker, $count) {
  531. $this->out(__d('cake_console', "Invalid marker content in %s:%s\n* %s(", $file, $line, $marker), true);
  532. $count += 2;
  533. $tokenCount = count($this->_tokens);
  534. $parenthesis = 1;
  535. while ((($tokenCount - $count) > 0) && $parenthesis) {
  536. if (is_array($this->_tokens[$count])) {
  537. $this->out($this->_tokens[$count][1], false);
  538. } else {
  539. $this->out($this->_tokens[$count], false);
  540. if ($this->_tokens[$count] == '(') {
  541. $parenthesis++;
  542. }
  543. if ($this->_tokens[$count] == ')') {
  544. $parenthesis--;
  545. }
  546. }
  547. $count++;
  548. }
  549. $this->out("\n", true);
  550. }
  551. /**
  552. * Search files that may contain translatable strings
  553. *
  554. * @return void
  555. */
  556. protected function _searchFiles() {
  557. $pattern = false;
  558. if (!empty($this->_exclude)) {
  559. $exclude = array();
  560. foreach ($this->_exclude as $e) {
  561. if ($e[0] !== DS) {
  562. $e = DS . $e;
  563. }
  564. $exclude[] = preg_quote($e, '/');
  565. }
  566. $pattern = '/' . implode('|', $exclude) . '/';
  567. }
  568. foreach ($this->_paths as $path) {
  569. $Folder = new Folder($path);
  570. $files = $Folder->findRecursive('.*\.(php|ctp|thtml|inc|tpl)', true);
  571. if (!empty($pattern)) {
  572. foreach ($files as $i => $file) {
  573. if (preg_match($pattern, $file)) {
  574. unset($files[$i]);
  575. }
  576. }
  577. $files = array_values($files);
  578. }
  579. $this->_files = array_merge($this->_files, $files);
  580. }
  581. }
  582. /**
  583. * Returns whether this execution is meant to extract string only from directories in folder represented by the
  584. * APP constant, i.e. this task is extracting strings from same application.
  585. *
  586. * @return boolean
  587. */
  588. protected function _isExtractingApp() {
  589. return $this->_paths === array(APP);
  590. }
  591. }