ExtractTask.php 18 KB

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