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