ExtractTask.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. <?php
  2. /**
  3. * Language string extractor
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2010, 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-2010, 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 {
  117. $defaultPath = APP;
  118. $message = __d('cake_console', "What is the path you would like to extract?\n[Q]uit [D]one");
  119. while (true) {
  120. $response = $this->in($message, null, $defaultPath);
  121. if (strtoupper($response) === 'Q') {
  122. $this->out(__d('cake_console', 'Extract Aborted'));
  123. $this->_stop();
  124. } elseif (strtoupper($response) === 'D') {
  125. $this->out();
  126. break;
  127. } elseif (is_dir($response)) {
  128. $this->_paths[] = $response;
  129. $defaultPath = 'D';
  130. } else {
  131. $this->err(__d('cake_console', 'The directory path you supplied was not found. Please try again.'));
  132. }
  133. $this->out();
  134. }
  135. }
  136. if (!empty($this->params['exclude-plugins']) && $this->_isExtractingApp()) {
  137. $this->_exclude = array_merge($this->_exclude, App::path('plugins'));
  138. }
  139. if (!empty($this->params['ignore-model-validation']) || !$this->_isExtractingApp()) {
  140. $this->_extractValidation = false;
  141. }
  142. if (!empty($this->params['validation-domain'])) {
  143. $this->_validationDomain = $this->params['validation-domain'];
  144. }
  145. if (isset($this->params['output'])) {
  146. $this->_output = $this->params['output'];
  147. } else {
  148. $message = __d('cake_console', "What is the path you would like to output?\n[Q]uit", $this->_paths[0] . DS . 'locale');
  149. while (true) {
  150. $response = $this->in($message, null, $this->_paths[0] . DS . 'locale');
  151. if (strtoupper($response) === 'Q') {
  152. $this->out(__d('cake_console', 'Extract Aborted'));
  153. $this->_stop();
  154. } elseif (is_dir($response)) {
  155. $this->_output = $response . DS;
  156. break;
  157. } else {
  158. $this->err(__d('cake_console', 'The directory path you supplied was not found. Please try again.'));
  159. }
  160. $this->out();
  161. }
  162. }
  163. if (isset($this->params['merge'])) {
  164. $this->_merge = !(strtolower($this->params['merge']) === 'no');
  165. } else {
  166. $this->out();
  167. $response = $this->in(__d('cake_console', 'Would you like to merge all domains strings into the default.pot file?'), array('y', 'n'), 'n');
  168. $this->_merge = strtolower($response) === 'y';
  169. }
  170. if (empty($this->_files)) {
  171. $this->_searchFiles();
  172. }
  173. $this->_extract();
  174. }
  175. /**
  176. * Extract text
  177. *
  178. * @return void
  179. * @access protected
  180. */
  181. protected function _extract() {
  182. $this->out();
  183. $this->out();
  184. $this->out(__d('cake_console', 'Extracting...'));
  185. $this->hr();
  186. $this->out(__d('cake_console', 'Paths:'));
  187. foreach ($this->_paths as $path) {
  188. $this->out(' ' . $path);
  189. }
  190. $this->out(__d('cake_console', 'Output Directory: ') . $this->_output);
  191. $this->hr();
  192. $this->_extractTokens();
  193. $this->_extractValidationMessages();
  194. $this->_buildFiles();
  195. $this->_writeFiles();
  196. $this->_paths = $this->_files = $this->_storage = array();
  197. $this->_strings = $this->_tokens = array();
  198. $this->_extractValidation = true;
  199. $this->out();
  200. $this->out(__d('cake_console', 'Done.'));
  201. }
  202. /**
  203. * Get & configure the option parser
  204. *
  205. * @return void
  206. */
  207. public function getOptionParser() {
  208. $parser = parent::getOptionParser();
  209. return $parser->description(__d('cake_console', 'CakePHP Language String Extraction:'))
  210. ->addOption('app', array('help' => __d('cake_console', 'Directory where your application is located.')))
  211. ->addOption('paths', array('help' => __d('cake_console', 'Comma separated list of paths.')))
  212. ->addOption('merge', array(
  213. 'help' => __d('cake_console', 'Merge all domain strings into the default.po file.'),
  214. 'choices' => array('yes', 'no')
  215. ))
  216. ->addOption('output', array('help' => __d('cake_console', 'Full path to output directory.')))
  217. ->addOption('files', array('help' => __d('cake_console', 'Comma separated list of files.')))
  218. ->addOption('exclude-plugins', array(
  219. 'boolean' => true,
  220. 'default' => true,
  221. 'help' => __d('cake_console', 'Ignores all files in plugins if this command is run inside from the same app directory')
  222. ))
  223. ->addOption('ignore-model-validation', array(
  224. 'boolean' => true,
  225. 'default' => false,
  226. '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')
  227. ))
  228. ->addOption('validation-domain', array(
  229. 'help' => __d('cake_console', 'If set to a value, the localization domain to be used for model validation messages')
  230. ))
  231. ->addOption('exclude', array(
  232. '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')
  233. ));
  234. }
  235. /**
  236. * Extract tokens out of all files to be processed
  237. *
  238. * @return void
  239. * @access protected
  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. * @access protected
  270. */
  271. protected function _parse($functionName, $map) {
  272. $count = 0;
  273. $tokenCount = count($this->_tokens);
  274. while (($tokenCount - $count) > 1) {
  275. list($countToken, $firstParenthesis) = array($this->_tokens[$count], $this->_tokens[$count + 1]);
  276. if (!is_array($countToken)) {
  277. $count++;
  278. continue;
  279. }
  280. list($type, $string, $line) = $countToken;
  281. if (($type == T_STRING) && ($string == $functionName) && ($firstParenthesis == '(')) {
  282. $position = $count;
  283. $depth = 0;
  284. while ($depth == 0) {
  285. if ($this->_tokens[$position] == '(') {
  286. $depth++;
  287. } elseif ($this->_tokens[$position] == ')') {
  288. $depth--;
  289. }
  290. $position++;
  291. }
  292. $mapCount = count($map);
  293. $strings = $this->_getStrings($position, $mapCount);
  294. if ($mapCount == count($strings)) {
  295. extract(array_combine($map, $strings));
  296. $domain = isset($domain) ? $domain : 'default';
  297. $string = isset($plural) ? $singular . "\0" . $plural : $singular;
  298. $this->_strings[$domain][$string][$this->_file][] = $line;
  299. } else {
  300. $this->_markerError($this->_file, $line, $functionName, $count);
  301. }
  302. }
  303. $count++;
  304. }
  305. }
  306. /**
  307. * Looks for models in the application and extracts the validation messages
  308. * to be added to the translation map
  309. *
  310. * @return void
  311. */
  312. protected function _extractValidationMessages() {
  313. if (!$this->_extractValidation) {
  314. return;
  315. }
  316. $models = App::objects('Model', null, false);
  317. App::uses('AppModel', 'Model');
  318. foreach ($models as $model) {
  319. App::uses($model, 'Model');
  320. $reflection = new ReflectionClass($model);
  321. $properties = $reflection->getDefaultProperties();
  322. $validate = $properties['validate'];
  323. if (empty($validate)) {
  324. continue;
  325. }
  326. $file = $reflection->getFileName();
  327. $domain = $this->_validationDomain;
  328. if (!empty($properties['validationDomain'])) {
  329. $domain = $properties['validationDomain'];
  330. }
  331. foreach ($validate as $field => $rules) {
  332. $this->_processValidationRules($field, $rules, $file, $domain);
  333. }
  334. }
  335. }
  336. /**
  337. * Process a validation rule for a field and looks for a message to be added
  338. * to the translation map
  339. *
  340. * @param string $field the name of the field that is being processed
  341. * @param array $rules the set of validation rules for the field
  342. * @param string $file the file name where this validation rule was found
  343. * @param string domain default domain to bind the validations to
  344. * @return void
  345. */
  346. protected function _processValidationRules($field, $rules, $file, $domain) {
  347. if (is_array($rules)) {
  348. $dims = Set::countDim($rules);
  349. if ($dims == 1 || ($dims == 2 && isset($rules['message']))) {
  350. $rules = array($rules);
  351. }
  352. foreach ($rules as $rule => $validateProp) {
  353. if (isset($validateProp['message'])) {
  354. $this->_strings[$domain][$validateProp['message']][$file][] = 'validation for field ' . $field;
  355. }
  356. }
  357. }
  358. }
  359. /**
  360. * Build the translate template file contents out of obtained strings
  361. *
  362. * @return void
  363. * @access protected
  364. */
  365. protected function _buildFiles() {
  366. foreach ($this->_strings as $domain => $strings) {
  367. foreach ($strings as $string => $files) {
  368. $occurrences = array();
  369. foreach ($files as $file => $lines) {
  370. $occurrences[] = $file . ':' . implode(';', $lines);
  371. }
  372. $occurrences = implode("\n#: ", $occurrences);
  373. $header = '#: ' . str_replace($this->_paths, '', $occurrences) . "\n";
  374. if (strpos($string, "\0") === false) {
  375. $sentence = "msgid \"{$string}\"\n";
  376. $sentence .= "msgstr \"\"\n\n";
  377. } else {
  378. list($singular, $plural) = explode("\0", $string);
  379. $sentence = "msgid \"{$singular}\"\n";
  380. $sentence .= "msgid_plural \"{$plural}\"\n";
  381. $sentence .= "msgstr[0] \"\"\n";
  382. $sentence .= "msgstr[1] \"\"\n\n";
  383. }
  384. $this->_store($domain, $header, $sentence);
  385. if ($domain != 'default' && $this->_merge) {
  386. $this->_store('default', $header, $sentence);
  387. }
  388. }
  389. }
  390. }
  391. /**
  392. * Prepare a file to be stored
  393. *
  394. * @return void
  395. * @access protected
  396. */
  397. protected function _store($domain, $header, $sentence) {
  398. if (!isset($this->_storage[$domain])) {
  399. $this->_storage[$domain] = array();
  400. }
  401. if (!isset($this->_storage[$domain][$sentence])) {
  402. $this->_storage[$domain][$sentence] = $header;
  403. } else {
  404. $this->_storage[$domain][$sentence] .= $header;
  405. }
  406. }
  407. /**
  408. * Write the files that need to be stored
  409. *
  410. * @return void
  411. * @access protected
  412. */
  413. protected function _writeFiles() {
  414. $overwriteAll = false;
  415. foreach ($this->_storage as $domain => $sentences) {
  416. $output = $this->_writeHeader();
  417. foreach ($sentences as $sentence => $header) {
  418. $output .= $header . $sentence;
  419. }
  420. $filename = $domain . '.pot';
  421. $File = new File($this->_output . $filename);
  422. $response = '';
  423. while ($overwriteAll === false && $File->exists() && strtoupper($response) !== 'Y') {
  424. $this->out();
  425. $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');
  426. if (strtoupper($response) === 'N') {
  427. $response = '';
  428. while ($response == '') {
  429. $response = $this->in(__d('cake_console', "What would you like to name this file?"), null, 'new_' . $filename);
  430. $File = new File($this->_output . $response);
  431. $filename = $response;
  432. }
  433. } elseif (strtoupper($response) === 'A') {
  434. $overwriteAll = true;
  435. }
  436. }
  437. $File->write($output);
  438. $File->close();
  439. }
  440. }
  441. /**
  442. * Build the translation template header
  443. *
  444. * @return string Translation template header
  445. * @access protected
  446. */
  447. protected function _writeHeader() {
  448. $output = "# LANGUAGE translation of CakePHP Application\n";
  449. $output .= "# Copyright YEAR NAME <EMAIL@ADDRESS>\n";
  450. $output .= "#\n";
  451. $output .= "#, fuzzy\n";
  452. $output .= "msgid \"\"\n";
  453. $output .= "msgstr \"\"\n";
  454. $output .= "\"Project-Id-Version: PROJECT VERSION\\n\"\n";
  455. $output .= "\"POT-Creation-Date: " . date("Y-m-d H:iO") . "\\n\"\n";
  456. $output .= "\"PO-Revision-Date: YYYY-mm-DD HH:MM+ZZZZ\\n\"\n";
  457. $output .= "\"Last-Translator: NAME <EMAIL@ADDRESS>\\n\"\n";
  458. $output .= "\"Language-Team: LANGUAGE <EMAIL@ADDRESS>\\n\"\n";
  459. $output .= "\"MIME-Version: 1.0\\n\"\n";
  460. $output .= "\"Content-Type: text/plain; charset=utf-8\\n\"\n";
  461. $output .= "\"Content-Transfer-Encoding: 8bit\\n\"\n";
  462. $output .= "\"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\\n\"\n\n";
  463. return $output;
  464. }
  465. /**
  466. * Get the strings from the position forward
  467. *
  468. * @param int $position Actual position on tokens array
  469. * @param int $target Number of strings to extract
  470. * @return array Strings extracted
  471. * @access protected
  472. */
  473. protected function _getStrings(&$position, $target) {
  474. $strings = array();
  475. while (count($strings) < $target && ($this->_tokens[$position] == ',' || $this->_tokens[$position][0] == T_CONSTANT_ENCAPSED_STRING)) {
  476. if ($this->_tokens[$position][0] == T_CONSTANT_ENCAPSED_STRING && $this->_tokens[$position+1] == '.') {
  477. $string = '';
  478. while ($this->_tokens[$position][0] == T_CONSTANT_ENCAPSED_STRING || $this->_tokens[$position] == '.') {
  479. if ($this->_tokens[$position][0] == T_CONSTANT_ENCAPSED_STRING) {
  480. $string .= $this->_formatString($this->_tokens[$position][1]);
  481. }
  482. $position++;
  483. }
  484. $strings[] = $string;
  485. } else if ($this->_tokens[$position][0] == T_CONSTANT_ENCAPSED_STRING) {
  486. $strings[] = $this->_formatString($this->_tokens[$position][1]);
  487. }
  488. $position++;
  489. }
  490. return $strings;
  491. }
  492. /**
  493. * Format a string to be added as a translatable string
  494. *
  495. * @param string $string String to format
  496. * @return string Formatted string
  497. * @access protected
  498. */
  499. protected function _formatString($string) {
  500. $quote = substr($string, 0, 1);
  501. $string = substr($string, 1, -1);
  502. if ($quote == '"') {
  503. $string = stripcslashes($string);
  504. } else {
  505. $string = strtr($string, array("\\'" => "'", "\\\\" => "\\"));
  506. }
  507. $string = str_replace("\r\n", "\n", $string);
  508. return addcslashes($string, "\0..\37\\\"");
  509. }
  510. /**
  511. * Indicate an invalid marker on a processed file
  512. *
  513. * @param string $file File where invalid marker resides
  514. * @param integer $line Line number
  515. * @param string $marker Marker found
  516. * @param integer $count Count
  517. * @return void
  518. * @access protected
  519. */
  520. protected function _markerError($file, $line, $marker, $count) {
  521. $this->out(__d('cake_console', "Invalid marker content in %s:%s\n* %s(", $file, $line, $marker), true);
  522. $count += 2;
  523. $tokenCount = count($this->_tokens);
  524. $parenthesis = 1;
  525. while ((($tokenCount - $count) > 0) && $parenthesis) {
  526. if (is_array($this->_tokens[$count])) {
  527. $this->out($this->_tokens[$count][1], false);
  528. } else {
  529. $this->out($this->_tokens[$count], false);
  530. if ($this->_tokens[$count] == '(') {
  531. $parenthesis++;
  532. }
  533. if ($this->_tokens[$count] == ')') {
  534. $parenthesis--;
  535. }
  536. }
  537. $count++;
  538. }
  539. $this->out("\n", true);
  540. }
  541. /**
  542. * Search files that may contain translatable strings
  543. *
  544. * @return void
  545. * @access protected
  546. */
  547. protected function _searchFiles() {
  548. $pattern = false;
  549. if (!empty($this->_exclude)) {
  550. $exclude = array();
  551. foreach ($this->_exclude as $e) {
  552. if ($e[0] !== DS) {
  553. $e = DS . $e;
  554. }
  555. $exclude[] = preg_quote($e, '/');
  556. }
  557. $pattern = '/' . implode('|', $exclude) . '/';
  558. }
  559. foreach ($this->_paths as $path) {
  560. $Folder = new Folder($path);
  561. $files = $Folder->findRecursive('.*\.(php|ctp|thtml|inc|tpl)', true);
  562. if (!empty($pattern)) {
  563. foreach ($files as $i => $file) {
  564. if (preg_match($pattern, $file)) {
  565. unset($files[$i]);
  566. }
  567. }
  568. $files = array_values($files);
  569. }
  570. $this->_files = array_merge($this->_files, $files);
  571. }
  572. }
  573. /**
  574. * Returns whether this execution is meant to extract string only from directories in folder represented by the
  575. * APP constant, i.e. this task is extracting strings from same application.
  576. *
  577. * @return boolean
  578. */
  579. protected function _isExtractingApp() {
  580. return $this->_paths === array(APP);
  581. }
  582. }