]> git.mxchange.org Git - friendica.git/blob - src/Console/Extract.php
Convert class calls into DI calls
[friendica.git] / src / Console / Extract.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Console;
23
24 /**
25  * Extracts translation strings from the Friendica project's files to be exported
26  * to Transifex for translation.
27  *
28  * Outputs a PHP file with language strings used by Friendica
29  */
30 class Extract extends \Asika\SimpleConsole\Console
31 {
32         protected $helpOptions = ['h', 'help', '?'];
33
34         protected function getHelp()
35         {
36                 $help = <<<HELP
37 console extract - Generate translation string file for the Friendica project (deprecated)
38 Usage
39         bin/console extract [-h|--help|-?] [-v]
40
41 Description
42         This script was used to generate the translation string file to be exported to Transifex,
43         please use bin/run_xgettext.sh instead
44
45 Options
46     -h|--help|-? Show help information
47     -v           Show more debug information.
48 HELP;
49                 return $help;
50         }
51
52         protected function doExecute()
53         {
54                 if ($this->getOption('v')) {
55                         $this->out('Class: ' . __CLASS__);
56                         $this->out('Arguments: ' . var_export($this->args, true));
57                         $this->out('Options: ' . var_export($this->options, true));
58                 }
59
60                 if (count($this->args) > 0) {
61                         throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
62                 }
63
64                 $s = '<?php' . PHP_EOL;
65                 $s .= '
66                 function string_plural_select($n){
67                         return ($n != 1);
68                 }
69
70                 ';
71
72                 $arr = [];
73
74                 $files = array_merge(
75                         ['index.php', 'boot.php'],
76                         glob('mod/*'),
77                         glob('include/*'),
78                         glob('addon/*/*'),
79                         $this->globRecursive('src')
80                 );
81
82                 foreach ($files as $file) {
83                         $str = file_get_contents($file);
84
85                         $pat = '|->t\(([^\)]*+)[\)]|';
86                         $patt = '|->tt\(([^\)]*+)[\)]|';
87
88                         $matches = [];
89                         $matchestt = [];
90
91                         preg_match_all($pat, $str, $matches);
92                         preg_match_all($patt, $str, $matchestt);
93
94                         if (count($matches) || count($matchestt)) {
95                                 $s .= '// ' . $file . PHP_EOL;
96                         }
97
98                         if (!empty($matches[1])) {
99                                 foreach ($matches[1] as $long_match) {
100                                         $match_arr = preg_split('/(?<=[\'"])\s*,/', $long_match);
101                                         $match = $match_arr[0];
102                                         if (!in_array($match, $arr)) {
103                                                 if (substr($match, 0, 1) == '$') {
104                                                         continue;
105                                                 }
106
107                                                 $arr[] = $match;
108
109                                                 $s .= '$a->strings[' . $match . '] = ' . $match . ';' . "\n";
110                                         }
111                                 }
112                         }
113                         if (!empty($matchestt[1])) {
114                                 foreach ($matchestt[1] as $match) {
115                                         $matchtkns = preg_split("|[ \t\r\n]*,[ \t\r\n]*|", $match);
116                                         if (count($matchtkns) == 3 && !in_array($matchtkns[0], $arr)) {
117                                                 if (substr($matchtkns[1], 0, 1) == '$') {
118                                                         continue;
119                                                 }
120
121                                                 $arr[] = $matchtkns[0];
122
123                                                 $s .= '$a->strings[' . $matchtkns[0] . "] = array(\n";
124                                                 $s .= "\t0 => " . $matchtkns[0] . ",\n";
125                                                 $s .= "\t1 => " . $matchtkns[1] . ",\n";
126                                                 $s .= ");\n";
127                                         }
128                                 }
129                         }
130                 }
131
132                 $s .= '// Timezones' . PHP_EOL;
133
134                 $zones = timezone_identifiers_list();
135                 foreach ($zones as $zone) {
136                         $s .= '$a->strings[\'' . $zone . '\'] = \'' . $zone . '\';' . "\n";
137                 }
138
139                 $this->out($s);
140
141                 return 0;
142         }
143
144         private function globRecursive($path) {
145                 $dir_iterator = new \RecursiveDirectoryIterator($path);
146                 $iterator = new \RecursiveIteratorIterator($dir_iterator, \RecursiveIteratorIterator::SELF_FIRST);
147
148                 $return = [];
149                 foreach ($iterator as $file) {
150                         if ($file->getBasename() != '.' && $file->getBasename() != '..') {
151                                 $return[] = $file->getPathname();
152                         }
153                 }
154
155                 return $return;
156         }
157 }