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