]> git.mxchange.org Git - friendica.git/blob - src/Console/Extract.php
All references to boot.php are now removed
[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 use \RecursiveDirectoryIterator;
25 use \RecursiveIteratorIterator;
26
27 /**
28  * Extracts translation strings from the Friendica project's files to be exported
29  * to Transifex for translation.
30  *
31  * Outputs a PHP file with language strings used by Friendica
32  */
33 class Extract extends \Asika\SimpleConsole\Console
34 {
35         protected $helpOptions = ['h', 'help', '?'];
36
37         protected function getHelp()
38         {
39                 $help = <<<HELP
40 console extract - Generate translation string file for the Friendica project (deprecated)
41 Usage
42         bin/console extract [-h|--help|-?] [-v]
43
44 Description
45         This script was used to generate the translation string file to be exported to Transifex,
46         please use bin/run_xgettext.sh instead
47
48 Options
49     -h|--help|-? Show help information
50     -v           Show more debug information.
51 HELP;
52                 return $help;
53         }
54
55         protected function doExecute(): int
56         {
57                 if ($this->getOption('v')) {
58                         $this->out('Class: ' . __CLASS__);
59                         $this->out('Arguments: ' . var_export($this->args, true));
60                         $this->out('Options: ' . var_export($this->options, true));
61                 }
62
63                 if (count($this->args) > 0) {
64                         throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
65                 }
66
67                 $s = '<?php' . PHP_EOL;
68                 $s .= '
69                 function string_plural_select($n){
70                         return ($n != 1);
71                 }
72
73                 ';
74
75                 $arr = [];
76
77                 $files = array_merge(
78                         ['index.php'],
79                         glob('mod/*'),
80                         glob('include/*'),
81                         glob('addon/*/*'),
82                         $this->globRecursive('src')
83                 );
84
85                 foreach ($files as $file) {
86                         $str = file_get_contents($file);
87
88                         $pat = '|->t\(([^\)]*+)[\)]|';
89                         $patt = '|->tt\(([^\)]*+)[\)]|';
90
91                         $matches = [];
92                         $matchestt = [];
93
94                         preg_match_all($pat, $str, $matches);
95                         preg_match_all($patt, $str, $matchestt);
96
97                         if (count($matches) || count($matchestt)) {
98                                 $s .= '// ' . $file . PHP_EOL;
99                         }
100
101                         if (!empty($matches[1])) {
102                                 foreach ($matches[1] as $long_match) {
103                                         $match_arr = preg_split('/(?<=[\'"])\s*,/', $long_match);
104                                         $match = $match_arr[0];
105                                         if (!in_array($match, $arr)) {
106                                                 if (substr($match, 0, 1) == '$') {
107                                                         continue;
108                                                 }
109
110                                                 $arr[] = $match;
111
112                                                 $s .= '$a->strings[' . $match . '] = ' . $match . ';' . "\n";
113                                         }
114                                 }
115                         }
116                         if (!empty($matchestt[1])) {
117                                 foreach ($matchestt[1] as $match) {
118                                         $matchtkns = preg_split("|[ \t\r\n]*,[ \t\r\n]*|", $match);
119                                         if (count($matchtkns) == 3 && !in_array($matchtkns[0], $arr)) {
120                                                 if (substr($matchtkns[1], 0, 1) == '$') {
121                                                         continue;
122                                                 }
123
124                                                 $arr[] = $matchtkns[0];
125
126                                                 $s .= '$a->strings[' . $matchtkns[0] . "] = [\n";
127                                                 $s .= "\t0 => " . $matchtkns[0] . ",\n";
128                                                 $s .= "\t1 => " . $matchtkns[1] . ",\n";
129                                                 $s .= "];\n";
130                                         }
131                                 }
132                         }
133                 }
134
135                 $s .= '// Timezones' . PHP_EOL;
136
137                 $zones = timezone_identifiers_list();
138                 foreach ($zones as $zone) {
139                         $s .= '$a->strings[\'' . $zone . '\'] = \'' . $zone . '\';' . "\n";
140                 }
141
142                 $this->out($s);
143
144                 return 0;
145         }
146
147         /**
148          * Returns an array with found files and directories including their paths.
149          *
150          * @param string $path Base path to scan
151          *
152          * @return array A flat array with found files and directories
153          */
154         private function globRecursive(string $path): array
155         {
156                 $dir_iterator = new RecursiveDirectoryIterator($path);
157                 $iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
158
159                 $return = [];
160                 foreach ($iterator as $file) {
161                         if ($file->getBasename() != '.' && $file->getBasename() != '..') {
162                                 $return[] = $file->getPathname();
163                         }
164                 }
165
166                 return $return;
167         }
168 }