]> git.mxchange.org Git - friendica.git/blob - src/Console/Extract.php
Legacy "include" fragments have been 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('addon/*/*'),
81                         $this->globRecursive('src')
82                 );
83
84                 foreach ($files as $file) {
85                         $str = file_get_contents($file);
86
87                         $pat = '|->t\(([^\)]*+)[\)]|';
88                         $patt = '|->tt\(([^\)]*+)[\)]|';
89
90                         $matches = [];
91                         $matchestt = [];
92
93                         preg_match_all($pat, $str, $matches);
94                         preg_match_all($patt, $str, $matchestt);
95
96                         if (count($matches) || count($matchestt)) {
97                                 $s .= '// ' . $file . PHP_EOL;
98                         }
99
100                         if (!empty($matches[1])) {
101                                 foreach ($matches[1] as $long_match) {
102                                         $match_arr = preg_split('/(?<=[\'"])\s*,/', $long_match);
103                                         $match = $match_arr[0];
104                                         if (!in_array($match, $arr)) {
105                                                 if (substr($match, 0, 1) == '$') {
106                                                         continue;
107                                                 }
108
109                                                 $arr[] = $match;
110
111                                                 $s .= '$a->strings[' . $match . '] = ' . $match . ';' . "\n";
112                                         }
113                                 }
114                         }
115                         if (!empty($matchestt[1])) {
116                                 foreach ($matchestt[1] as $match) {
117                                         $matchtkns = preg_split("|[ \t\r\n]*,[ \t\r\n]*|", $match);
118                                         if (count($matchtkns) == 3 && !in_array($matchtkns[0], $arr)) {
119                                                 if (substr($matchtkns[1], 0, 1) == '$') {
120                                                         continue;
121                                                 }
122
123                                                 $arr[] = $matchtkns[0];
124
125                                                 $s .= '$a->strings[' . $matchtkns[0] . "] = [\n";
126                                                 $s .= "\t0 => " . $matchtkns[0] . ",\n";
127                                                 $s .= "\t1 => " . $matchtkns[1] . ",\n";
128                                                 $s .= "];\n";
129                                         }
130                                 }
131                         }
132                 }
133
134                 $s .= '// Timezones' . PHP_EOL;
135
136                 $zones = timezone_identifiers_list();
137                 foreach ($zones as $zone) {
138                         $s .= '$a->strings[\'' . $zone . '\'] = \'' . $zone . '\';' . "\n";
139                 }
140
141                 $this->out($s);
142
143                 return 0;
144         }
145
146         /**
147          * Returns an array with found files and directories including their paths.
148          *
149          * @param string $path Base path to scan
150          *
151          * @return array A flat array with found files and directories
152          */
153         private function globRecursive(string $path): array
154         {
155                 $dir_iterator = new RecursiveDirectoryIterator($path);
156                 $iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
157
158                 $return = [];
159                 foreach ($iterator as $file) {
160                         if ($file->getBasename() != '.' && $file->getBasename() != '..') {
161                                 $return[] = $file->getPathname();
162                         }
163                 }
164
165                 return $return;
166         }
167 }