PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/cake/console/libs/tasks/extract.php

http://noserub.googlecode.com/
PHP | 494 lines | 323 code | 33 blank | 138 comment | 65 complexity | 14b9634acf66e77b10cd0d558ef0903f MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * Language string extractor
  4. *
  5. * Long description for file
  6. *
  7. * PHP versions 4 and 5
  8. *
  9. * CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
  10. * Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @filesource
  16. * @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  17. * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
  18. * @package cake
  19. * @subpackage cake.cake.console.libs
  20. * @since CakePHP(tm) v 1.2.0.5012
  21. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  22. */
  23. /**
  24. * Language string extractor
  25. *
  26. * @package cake
  27. * @subpackage cake.cake.console.libs.tasks
  28. */
  29. class ExtractTask extends Shell {
  30. /**
  31. * Paths to use when looking for strings
  32. *
  33. * @var string
  34. * @access private
  35. */
  36. var $__paths = array();
  37. /**
  38. * Files from where to extract
  39. *
  40. * @var array
  41. * @access private
  42. */
  43. var $__files = array();
  44. /**
  45. * Merge all domains string into the default.pot file
  46. *
  47. * @var boolean
  48. * @access private
  49. */
  50. var $__merge = false;
  51. /**
  52. * Current file being processed
  53. *
  54. * @var string
  55. * @access private
  56. */
  57. var $__file = null;
  58. /**
  59. * Contains all content waiting to be write
  60. *
  61. * @var string
  62. * @access private
  63. */
  64. var $__storage = array();
  65. /**
  66. * Extracted tokens
  67. *
  68. * @var array
  69. * @access private
  70. */
  71. var $__tokens = array();
  72. /**
  73. * Extracted strings
  74. *
  75. * @var array
  76. * @access private
  77. */
  78. var $__strings = array();
  79. /**
  80. * Destination path
  81. *
  82. * @var string
  83. * @access private
  84. */
  85. var $__output = null;
  86. /**
  87. * Execution method always used for tasks
  88. *
  89. * @return void
  90. * @access private
  91. */
  92. function execute() {
  93. if (isset($this->params['files']) && !is_array($this->params['files'])) {
  94. $this->__files = explode(',', $this->params['files']);
  95. }
  96. if (isset($this->params['paths'])) {
  97. $this->__paths = explode(',', $this->params['paths']);
  98. } else {
  99. $defaultPath = $this->params['working'];
  100. $message = sprintf(__("What is the full path you would like to extract?\nExample: %s\n[Q]uit [D]one", true), $this->params['root'] . DS . 'myapp');
  101. while (true) {
  102. $response = $this->in($message, null, $defaultPath);
  103. if (strtoupper($response) === 'Q') {
  104. $this->out(__('Extract Aborted', true));
  105. $this->_stop();
  106. } elseif (strtoupper($response) === 'D') {
  107. $this->out();
  108. break;
  109. } elseif (is_dir($response)) {
  110. $this->__paths[] = $response;
  111. $defaultPath = 'D';
  112. } else {
  113. $this->err(__('The directory path you supplied was not found. Please try again.', true));
  114. }
  115. $this->out();
  116. }
  117. }
  118. if (isset($this->params['output'])) {
  119. $this->__output = $this->params['output'];
  120. } else {
  121. $message = sprintf(__("What is the full path you would like to output?\nExample: %s\n[Q]uit", true), $this->__paths[0] . DS . 'locale');
  122. while (true) {
  123. $response = $this->in($message, null, $this->__paths[0] . DS . 'locale');
  124. if (strtoupper($response) === 'Q') {
  125. $this->out(__('Extract Aborted', true));
  126. $this->_stop();
  127. } elseif (is_dir($response)) {
  128. $this->__output = $response . DS;
  129. break;
  130. } else {
  131. $this->err(__('The directory path you supplied was not found. Please try again.', true));
  132. }
  133. $this->out();
  134. }
  135. }
  136. if (isset($this->params['merge'])) {
  137. $this->__merge = !(strtolower($this->params['merge']) === 'no');
  138. } else {
  139. $this->out();
  140. $response = $this->in(sprintf(__('Would you like to merge all domains strings into the default.pot file?', true)), array('y', 'n'), 'n');
  141. $this->__merge = strtolower($response) === 'y';
  142. }
  143. if (empty($this->__files)) {
  144. $this->__searchFiles();
  145. }
  146. $this->__extract();
  147. }
  148. /**
  149. * Extract text
  150. *
  151. * @return void
  152. * @access private
  153. */
  154. function __extract() {
  155. $this->out();
  156. $this->out();
  157. $this->out(__('Extracting...', true));
  158. $this->hr();
  159. $this->out(__('Paths:', true));
  160. foreach ($this->__paths as $path) {
  161. $this->out(' ' . $path);
  162. }
  163. $this->out(__('Output Directory: ', true) . $this->__output);
  164. $this->hr();
  165. $this->__extractTokens();
  166. }
  167. /**
  168. * Show help options
  169. *
  170. * @return void
  171. * @access public
  172. */
  173. function help() {
  174. $this->out(__('CakePHP Language String Extraction:', true));
  175. $this->hr();
  176. $this->out(__('The Extract script generates .pot file(s) with translations', true));
  177. $this->out(__('By default the .pot file(s) will be place in the locale directory of -app', true));
  178. $this->out(__('By default -app is ROOT/app', true));
  179. $this->hr();
  180. $this->out(__('Usage: cake i18n extract <command> <param1> <param2>...', true));
  181. $this->out();
  182. $this->out(__('Params:', true));
  183. $this->out(__(' -app [path...]: directory where your application is located', true));
  184. $this->out(__(' -root [path...]: path to install', true));
  185. $this->out(__(' -core [path...]: path to cake directory', true));
  186. $this->out(__(' -paths [comma separated list of paths, full path is needed]', true));
  187. $this->out(__(' -merge [yes|no]: Merge all domains strings into the default.pot file', true));
  188. $this->out(__(' -output [path...]: Full path to output directory', true));
  189. $this->out(__(' -files: [comma separated list of files, full path to file is needed]', true));
  190. $this->out();
  191. $this->out(__('Commands:', true));
  192. $this->out(__(' cake i18n extract help: Shows this help message.', true));
  193. $this->out();
  194. }
  195. /**
  196. * Extract tokens out of all files to be processed
  197. *
  198. * @return void
  199. * @access private
  200. */
  201. function __extractTokens() {
  202. foreach ($this->__files as $file) {
  203. $this->__file = $file;
  204. $this->out(sprintf(__('Processing %s...', true), $file));
  205. $code = file_get_contents($file);
  206. $allTokens = token_get_all($code);
  207. $this->__tokens = array();
  208. $lineNumber = 1;
  209. foreach ($allTokens as $token) {
  210. if ((!is_array($token)) || (($token[0] != T_WHITESPACE) && ($token[0] != T_INLINE_HTML))) {
  211. if (is_array($token)) {
  212. $token[] = $lineNumber;
  213. }
  214. $this->__tokens[] = $token;
  215. }
  216. if (is_array($token)) {
  217. $lineNumber += count(split("\n", $token[1])) - 1;
  218. } else {
  219. $lineNumber += count(split("\n", $token)) - 1;
  220. }
  221. }
  222. unset($allTokens);
  223. $this->__parse('__', array('singular'));
  224. $this->__parse('__n', array('singular', 'plural'));
  225. $this->__parse('__d', array('domain', 'singular'));
  226. $this->__parse('__c', array('singular'));
  227. $this->__parse('__dc', array('domain', 'singular'));
  228. $this->__parse('__dn', array('domain', 'singular', 'plural'));
  229. $this->__parse('__dcn', array('domain', 'singular', 'plural'));
  230. }
  231. $this->__buildFiles();
  232. $this->__writeFiles();
  233. $this->out();
  234. $this->out(__('Done.', true));
  235. }
  236. /**
  237. * Parse tokens
  238. *
  239. * @param string $functionName Function name that indicates translatable string (e.g: '__')
  240. * @param array $map Array containing what variables it will find (e.g: domain, singular, plural)
  241. * @return void
  242. * @access private
  243. */
  244. function __parse($functionName, $map) {
  245. $count = 0;
  246. $tokenCount = count($this->__tokens);
  247. while (($tokenCount - $count) > 7) {
  248. list($countToken, $firstParenthesis) = array($this->__tokens[$count], $this->__tokens[$count + 1]);
  249. if (!is_array($countToken)) {
  250. $count++;
  251. continue;
  252. }
  253. list($type, $string, $line) = $countToken;
  254. if (($type == T_STRING) && ($string == $functionName) && ($firstParenthesis == '(')) {
  255. $position = $count;
  256. $depth = 0;
  257. while ($depth == 0) {
  258. if ($this->__tokens[$position] == '(') {
  259. $depth++;
  260. } elseif ($this->__tokens[$position] == ')') {
  261. $depth--;
  262. }
  263. $position++;
  264. }
  265. $mapCount = count($map);
  266. $strings = array();
  267. while (count($strings) < $mapCount && ($this->__tokens[$position] == ',' || $this->__tokens[$position][0] == T_CONSTANT_ENCAPSED_STRING)) {
  268. if ($this->__tokens[$position][0] == T_CONSTANT_ENCAPSED_STRING) {
  269. $strings[] = $this->__tokens[$position][1];
  270. }
  271. $position++;
  272. }
  273. if ($mapCount == count($strings)) {
  274. extract(array_combine($map, $strings));
  275. if (!isset($domain)) {
  276. $domain = '\'default\'';
  277. }
  278. $string = $this->__formatString($singular);
  279. if (isset($plural)) {
  280. $string .= "\0" . $this->__formatString($plural);
  281. }
  282. $this->__strings[$this->__formatString($domain)][$string][$this->__file][] = $line;
  283. } else {
  284. $this->__markerError($this->__file, $line, $functionName, $count);
  285. }
  286. }
  287. $count++;
  288. }
  289. }
  290. /**
  291. * Build the translate template file contents out of obtained strings
  292. *
  293. * @return void
  294. * @access private
  295. */
  296. function __buildFiles() {
  297. foreach ($this->__strings as $domain => $strings) {
  298. foreach ($strings as $string => $files) {
  299. $occurances = array();
  300. foreach ($files as $file => $lines) {
  301. $occurances[] = $file . ':' . implode(';', $lines);
  302. }
  303. $occurances = implode("\n#: ", $occurances);
  304. $header = '#: ' . str_replace($this->__paths, '', $occurances) . "\n";
  305. if (strpos($string, "\0") === false) {
  306. $sentence = "msgid \"{$string}\"\n";
  307. $sentence .= "msgstr \"\"\n\n";
  308. } else {
  309. list($singular, $plural) = explode("\0", $string);
  310. $sentence = "msgid \"{$singular}\"\n";
  311. $sentence .= "msgid_plural \"{$plural}\"\n";
  312. $sentence .= "msgstr[0] \"\"\n";
  313. $sentence .= "msgstr[1] \"\"\n\n";
  314. }
  315. $this->__store($domain, $header, $sentence);
  316. if ($domain != 'default' && $this->__merge) {
  317. $this->__store('default', $header, $sentence);
  318. }
  319. }
  320. }
  321. }
  322. /**
  323. * Prepare a file to be stored
  324. *
  325. * @return void
  326. * @access private
  327. */
  328. function __store($domain, $header, $sentence) {
  329. if (!isset($this->__storage[$domain])) {
  330. $this->__storage[$domain] = array();
  331. }
  332. if (!isset($this->__storage[$domain][$sentence])) {
  333. $this->__storage[$domain][$sentence] = $header;
  334. } else {
  335. $this->__storage[$domain][$sentence] .= $header;
  336. }
  337. }
  338. /**
  339. * Write the files that need to be stored
  340. *
  341. * @return void
  342. * @access private
  343. */
  344. function __writeFiles() {
  345. foreach ($this->__storage as $domain => $sentences) {
  346. $output = $this->__writeHeader();
  347. foreach ($sentences as $sentence => $header) {
  348. $output .= $header . $sentence;
  349. }
  350. $filename = $domain . '.pot';
  351. $File = new File($this->__output . $filename);
  352. if ($File->exists()) {
  353. $response = '';
  354. while ($response == '') {
  355. $this->out();
  356. $response = $this->in(sprintf(__('Error: %s already exists in this location. Overwrite?', true), $filename), array('y', 'n'), 'y');
  357. if (strtoupper($response) === 'N') {
  358. $response = '';
  359. while ($response == '') {
  360. $response = $this->in(sprintf(__("What would you like to name this file?\nExample: %s", true), 'new_' . $filename), null, 'new_' . $filename);
  361. $File = new File($this->__output . $response);
  362. }
  363. }
  364. }
  365. }
  366. $File->write($output);
  367. $File->close();
  368. }
  369. }
  370. /**
  371. * Build the translation template header
  372. *
  373. * @return string Translation template header
  374. * @access private
  375. */
  376. function __writeHeader() {
  377. $output = "# LANGUAGE translation of CakePHP Application\n";
  378. $output .= "# Copyright YEAR NAME <EMAIL@ADDRESS>\n";
  379. $output .= "#\n";
  380. $output .= "#, fuzzy\n";
  381. $output .= "msgid \"\"\n";
  382. $output .= "msgstr \"\"\n";
  383. $output .= "\"Project-Id-Version: PROJECT VERSION\\n\"\n";
  384. $output .= "\"POT-Creation-Date: " . date("Y-m-d H:iO") . "\\n\"\n";
  385. $output .= "\"PO-Revision-Date: YYYY-mm-DD HH:MM+ZZZZ\\n\"\n";
  386. $output .= "\"Last-Translator: NAME <EMAIL@ADDRESS>\\n\"\n";
  387. $output .= "\"Language-Team: LANGUAGE <EMAIL@ADDRESS>\\n\"\n";
  388. $output .= "\"MIME-Version: 1.0\\n\"\n";
  389. $output .= "\"Content-Type: text/plain; charset=utf-8\\n\"\n";
  390. $output .= "\"Content-Transfer-Encoding: 8bit\\n\"\n";
  391. $output .= "\"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\\n\"\n\n";
  392. return $output;
  393. }
  394. /**
  395. * Format a string to be added as a translateable string
  396. *
  397. * @param string $string String to format
  398. * @return string Formatted string
  399. * @access private
  400. */
  401. function __formatString($string) {
  402. $quote = substr($string, 0, 1);
  403. $string = substr($string, 1, -1);
  404. if ($quote == '"') {
  405. $string = stripcslashes($string);
  406. } else {
  407. $string = strtr($string, array("\\'" => "'", "\\\\" => "\\"));
  408. }
  409. $string = str_replace("\r\n", "\n", $string);
  410. return addcslashes($string, "\0..\37\\\"");
  411. }
  412. /**
  413. * Indicate an invalid marker on a processed file
  414. *
  415. * @param string $file File where invalid marker resides
  416. * @param integer $line Line number
  417. * @param string $marker Marker found
  418. * @param integer $count Count
  419. * @return void
  420. * @access private
  421. */
  422. function __markerError($file, $line, $marker, $count) {
  423. $this->out(sprintf(__("Invalid marker content in %s:%s\n* %s(", true), $file, $line, $marker), true);
  424. $count += 2;
  425. $tokenCount = count($this->__tokens);
  426. $parenthesis = 1;
  427. while ((($tokenCount - $count) > 0) && $parenthesis) {
  428. if (is_array($this->__tokens[$count])) {
  429. $this->out($this->__tokens[$count][1], false);
  430. } else {
  431. $this->out($this->__tokens[$count], false);
  432. if ($this->__tokens[$count] == '(') {
  433. $parenthesis++;
  434. }
  435. if ($this->__tokens[$count] == ')') {
  436. $parenthesis--;
  437. }
  438. }
  439. $count++;
  440. }
  441. $this->out("\n", true);
  442. }
  443. /**
  444. * Search files that may contain translateable strings
  445. *
  446. * @return void
  447. * @access private
  448. */
  449. function __searchFiles() {
  450. foreach ($this->__paths as $path) {
  451. $Folder = new Folder($path);
  452. $files = $Folder->findRecursive('.*\.(php|ctp|thtml|inc|tpl)', true);
  453. $this->__files += $files;
  454. }
  455. }
  456. }
  457. ?>