ExtractTask.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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. if (!$reflection->isSubClassOf('Model')) {
  326. continue;
  327. }
  328. $properties = $reflection->getDefaultProperties();
  329. $validate = $properties['validate'];
  330. if (empty($validate)) {
  331. continue;
  332. }
  333. $file = $reflection->getFileName();
  334. $domain = $this->_validationDomain;
  335. if (!empty($properties['validationDomain'])) {
  336. $domain = $properties['validationDomain'];
  337. }
  338. foreach ($validate as $field => $rules) {
  339. $this->_processValidationRules($field, $rules, $file, $domain);
  340. }
  341. }
  342. }
  343. /**
  344. * Process a validation rule for a field and looks for a message to be added
  345. * to the translation map
  346. *
  347. * @param string $field the name of the field that is being processed
  348. * @param array $rules the set of validation rules for the field
  349. * @param string $file the file name where this validation rule was found
  350. * @param string $domain default domain to bind the validations to
  351. * @return void
  352. */
  353. protected function _processValidationRules($field, $rules, $file, $domain) {
  354. if (is_array($rules)) {
  355. $dims = Set::countDim($rules);
  356. if ($dims == 1 || ($dims == 2 && isset($rules['message']))) {
  357. $rules = array($rules);
  358. }
  359. foreach ($rules as $rule => $validateProp) {
  360. $message = null;
  361. if (isset($validateProp['message'])) {
  362. if (is_array($validateProp['message'])) {
  363. $message = $validateProp['message'][0];
  364. } else {
  365. $message = $validateProp['message'];
  366. }
  367. } elseif (is_string($rule)) {
  368. $message = $rule;
  369. }
  370. if ($message) {
  371. $this->_strings[$domain][$message][$file][] = 'validation for field ' . $field;
  372. }
  373. }
  374. }
  375. }
  376. /**
  377. * Build the translate template file contents out of obtained strings
  378. *
  379. * @return void
  380. */
  381. protected function _buildFiles() {
  382. foreach ($this->_strings as $domain => $strings) {
  383. foreach ($strings as $string => $files) {
  384. $occurrences = array();
  385. foreach ($files as $file => $lines) {
  386. $occurrences[] = $file . ':' . implode(';', $lines);
  387. }
  388. $occurrences = implode("\n#: ", $occurrences);
  389. $header = '#: ' . str_replace($this->_paths, '', $occurrences) . "\n";
  390. if (strpos($string, "\0") === false) {
  391. $sentence = "msgid \"{$string}\"\n";
  392. $sentence .= "msgstr \"\"\n\n";
  393. } else {
  394. list($singular, $plural) = explode("\0", $string);
  395. $sentence = "msgid \"{$singular}\"\n";
  396. $sentence .= "msgid_plural \"{$plural}\"\n";
  397. $sentence .= "msgstr[0] \"\"\n";
  398. $sentence .= "msgstr[1] \"\"\n\n";
  399. }
  400. $this->_store($domain, $header, $sentence);
  401. if ($domain != 'default' && $this->_merge) {
  402. $this->_store('default', $header, $sentence);
  403. }
  404. }
  405. }
  406. }
  407. /**
  408. * Prepare a file to be stored
  409. *
  410. * @param string $domain
  411. * @param string $header
  412. * @param string $sentence
  413. * @return void
  414. */
  415. protected function _store($domain, $header, $sentence) {
  416. if (!isset($this->_storage[$domain])) {
  417. $this->_storage[$domain] = array();
  418. }
  419. if (!isset($this->_storage[$domain][$sentence])) {
  420. $this->_storage[$domain][$sentence] = $header;
  421. } else {
  422. $this->_storage[$domain][$sentence] .= $header;
  423. }
  424. }
  425. /**
  426. * Write the files that need to be stored
  427. *
  428. * @return void
  429. */
  430. protected function _writeFiles() {
  431. $overwriteAll = false;
  432. foreach ($this->_storage as $domain => $sentences) {
  433. $output = $this->_writeHeader();
  434. foreach ($sentences as $sentence => $header) {
  435. $output .= $header . $sentence;
  436. }
  437. $filename = $domain . '.pot';
  438. $File = new File($this->_output . $filename);
  439. $response = '';
  440. while ($overwriteAll === false && $File->exists() && strtoupper($response) !== 'Y') {
  441. $this->out();
  442. $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');
  443. if (strtoupper($response) === 'N') {
  444. $response = '';
  445. while ($response == '') {
  446. $response = $this->in(__d('cake_console', "What would you like to name this file?"), null, 'new_' . $filename);
  447. $File = new File($this->_output . $response);
  448. $filename = $response;
  449. }
  450. } elseif (strtoupper($response) === 'A') {
  451. $overwriteAll = true;
  452. }
  453. }
  454. $File->write($output);
  455. $File->close();
  456. }
  457. }
  458. /**
  459. * Build the translation template header
  460. *
  461. * @return string Translation template header
  462. */
  463. protected function _writeHeader() {
  464. $output = "# LANGUAGE translation of CakePHP Application\n";
  465. $output .= "# Copyright YEAR NAME <EMAIL@ADDRESS>\n";
  466. $output .= "#\n";
  467. $output .= "#, fuzzy\n";
  468. $output .= "msgid \"\"\n";
  469. $output .= "msgstr \"\"\n";
  470. $output .= "\"Project-Id-Version: PROJECT VERSION\\n\"\n";
  471. $output .= "\"POT-Creation-Date: " . date("Y-m-d H:iO") . "\\n\"\n";
  472. $output .= "\"PO-Revision-Date: YYYY-mm-DD HH:MM+ZZZZ\\n\"\n";
  473. $output .= "\"Last-Translator: NAME <EMAIL@ADDRESS>\\n\"\n";
  474. $output .= "\"Language-Team: LANGUAGE <EMAIL@ADDRESS>\\n\"\n";
  475. $output .= "\"MIME-Version: 1.0\\n\"\n";
  476. $output .= "\"Content-Type: text/plain; charset=utf-8\\n\"\n";
  477. $output .= "\"Content-Transfer-Encoding: 8bit\\n\"\n";
  478. $output .= "\"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\\n\"\n\n";
  479. return $output;
  480. }
  481. /**
  482. * Get the strings from the position forward
  483. *
  484. * @param integer $position Actual position on tokens array
  485. * @param integer $target Number of strings to extract
  486. * @return array Strings extracted
  487. */
  488. protected function _getStrings(&$position, $target) {
  489. $strings = array();
  490. while (count($strings) < $target && ($this->_tokens[$position] == ',' || $this->_tokens[$position][0] == T_CONSTANT_ENCAPSED_STRING)) {
  491. if ($this->_tokens[$position][0] == T_CONSTANT_ENCAPSED_STRING && $this->_tokens[$position+1] == '.') {
  492. $string = '';
  493. while ($this->_tokens[$position][0] == T_CONSTANT_ENCAPSED_STRING || $this->_tokens[$position] == '.') {
  494. if ($this->_tokens[$position][0] == T_CONSTANT_ENCAPSED_STRING) {
  495. $string .= $this->_formatString($this->_tokens[$position][1]);
  496. }
  497. $position++;
  498. }
  499. $strings[] = $string;
  500. } else if ($this->_tokens[$position][0] == T_CONSTANT_ENCAPSED_STRING) {
  501. $strings[] = $this->_formatString($this->_tokens[$position][1]);
  502. }
  503. $position++;
  504. }
  505. return $strings;
  506. }
  507. /**
  508. * Format a string to be added as a translatable string
  509. *
  510. * @param string $string String to format
  511. * @return string Formatted string
  512. */
  513. protected function _formatString($string) {
  514. $quote = substr($string, 0, 1);
  515. $string = substr($string, 1, -1);
  516. if ($quote == '"') {
  517. $string = stripcslashes($string);
  518. } else {
  519. $string = strtr($string, array("\\'" => "'", "\\\\" => "\\"));
  520. }
  521. $string = str_replace("\r\n", "\n", $string);
  522. return addcslashes($string, "\0..\37\\\"");
  523. }
  524. /**
  525. * Indicate an invalid marker on a processed file
  526. *
  527. * @param string $file File where invalid marker resides
  528. * @param integer $line Line number
  529. * @param string $marker Marker found
  530. * @param integer $count Count
  531. * @return void
  532. */
  533. protected function _markerError($file, $line, $marker, $count) {
  534. $this->out(__d('cake_console', "Invalid marker content in %s:%s\n* %s(", $file, $line, $marker), true);
  535. $count += 2;
  536. $tokenCount = count($this->_tokens);
  537. $parenthesis = 1;
  538. while ((($tokenCount - $count) > 0) && $parenthesis) {
  539. if (is_array($this->_tokens[$count])) {
  540. $this->out($this->_tokens[$count][1], false);
  541. } else {
  542. $this->out($this->_tokens[$count], false);
  543. if ($this->_tokens[$count] == '(') {
  544. $parenthesis++;
  545. }
  546. if ($this->_tokens[$count] == ')') {
  547. $parenthesis--;
  548. }
  549. }
  550. $count++;
  551. }
  552. $this->out("\n", true);
  553. }
  554. /**
  555. * Search files that may contain translatable strings
  556. *
  557. * @return void
  558. */
  559. protected function _searchFiles() {
  560. $pattern = false;
  561. if (!empty($this->_exclude)) {
  562. $exclude = array();
  563. foreach ($this->_exclude as $e) {
  564. if (DS !== '\\' && $e[0] !== DS) {
  565. $e = DS . $e;
  566. }
  567. $exclude[] = preg_quote($e, '/');
  568. }
  569. $pattern = '/' . implode('|', $exclude) . '/';
  570. }
  571. foreach ($this->_paths as $path) {
  572. $Folder = new Folder($path);
  573. $files = $Folder->findRecursive('.*\.(php|ctp|thtml|inc|tpl)', true);
  574. if (!empty($pattern)) {
  575. foreach ($files as $i => $file) {
  576. if (preg_match($pattern, $file)) {
  577. unset($files[$i]);
  578. }
  579. }
  580. $files = array_values($files);
  581. }
  582. $this->_files = array_merge($this->_files, $files);
  583. }
  584. }
  585. /**
  586. * Returns whether this execution is meant to extract string only from directories in folder represented by the
  587. * APP constant, i.e. this task is extracting strings from same application.
  588. *
  589. * @return boolean
  590. */
  591. protected function _isExtractingApp() {
  592. return $this->_paths === array(APP);
  593. }
  594. }