1313
1414namespace phpbb \titania \console \command \extension ;
1515
16- use Chumper \Zipper \Zipper ;
17- use phpbb \db \driver \driver_interface as db ;
1816use phpbb \user ;
1917use phpbb \language \language as phpbb_language ;
2018use Symfony \Component \Console \Input \InputArgument ;
2119use Symfony \Component \Console \Input \InputInterface ;
2220use Symfony \Component \Console \Input \InputOption ;
2321use Symfony \Component \Console \Output \OutputInterface ;
24- use Symfony \ Component \ Filesystem \ Filesystem ;
22+ use ZipArchive ;
2523
2624/**
2725 * Class language
2826 *
29- * A script that will package British English from the latest phpBB version
27+ * A script that will package British English from the latest phpBB version
3028 * battye was here in 2019
3129 *
3230 * @package phpbb\titania\console\command\extension
@@ -37,18 +35,12 @@ class language extends \phpbb\console\command\command
3735 // 1) Name only (referencing Titania): php bin/phpbbcli.php titania:extension:language phpBB-3.2.7.zip --name
3836 // 2) Full path: php bin/phpbbcli.php titania:extension:language /var/www/phpBB/ext/phpbb/titania/includes/phpbb_packages/phpBB-3.2.7.zip
3937
40- const COMMAND_NAME = 'titania:extension:language ' ;
41- const COMMAND_TMP_DIRECTORY = 'ext/phpbb/titania/files/contrib_temp/tmp ' ;
42- const COMMAND_LANGUAGE_DIRECTORY = 'ext/phpbb/titania/includes/language_packages ' ;
38+ private const COMMAND_NAME = 'titania:extension:language ' ;
39+ private const COMMAND_LANGUAGE_DIRECTORY = 'ext/phpbb/titania/includes/language_packages ' ;
4340
4441 /** @var phpbb_language */
4542 protected $ language ;
4643
47- /** @var db */
48- protected $ db ;
49-
50- /** @var $root_path */
51- protected $ root_path ;
5244
5345 /** @var OutputInterface */
5446 protected $ output ;
@@ -57,7 +49,7 @@ class language extends \phpbb\console\command\command
5749 protected $ input ;
5850
5951 /** @var string */
60- protected $ tmp_folder ;
52+ protected $ root_path ;
6153
6254 /** @var string */
6355 protected $ language_folder ;
@@ -67,7 +59,6 @@ class language extends \phpbb\console\command\command
6759 *
6860 * @param user $user
6961 * @param phpbb_language $language
70- * @param db $db
7162 * @param string $root_path
7263 * @param string $php_ext
7364 */
@@ -81,9 +72,6 @@ public function __construct(user $user, phpbb_language $language, $root_path, $p
8172 // Set up the injected properties
8273 $ this ->language = $ language ;
8374 $ this ->root_path = $ root_path ;
84-
85- // Temporary folder
86- $ this ->tmp_folder = $ this ->root_path . self ::COMMAND_TMP_DIRECTORY ;
8775 $ this ->language_folder = $ this ->root_path . self ::COMMAND_LANGUAGE_DIRECTORY ;
8876
8977 $ language_files = ['console ' ];
@@ -106,9 +94,10 @@ protected function configure()
10694
10795 /**
10896 * Execute the script
109- * @param InputInterface $input
97+ *
98+ * @param InputInterface $input
11099 * @param OutputInterface $output
111- * @return int|null| void
100+ * @return void
112101 * @throws \Exception
113102 */
114103 protected function execute (InputInterface $ input , OutputInterface $ output )
@@ -139,9 +128,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
139128
140129 /**
141130 * Get the version number from the phpBB package
142- * @param $zip_path
143- * @return array
144- * @throws \Exception
131+ *
132+ * @param string $zip_path The path to the zip file.
133+ * @return array An array containing the major, minor, and patch versions.
134+ * @throws \Exception If the zip file name is not in the expected format.
145135 */
146136 private function extract_version_number ($ zip_path )
147137 {
@@ -165,9 +155,10 @@ private function extract_version_number($zip_path)
165155
166156 /**
167157 * Package British English
168- * @param string $zip_path
158+ *
159+ * @param string $zip_path The path to the zip file.
169160 * @return void
170- * @throws \Exception
161+ * @throws \Exception If the language pack already exists, the phpBB package is not found, or a zip error occurs.
171162 */
172163 private function package ($ zip_path )
173164 {
@@ -181,46 +172,78 @@ private function package($zip_path)
181172 throw new \Exception ($ this ->language ->lang ('CLI_EXTENSION_LANGUAGE_PACK_EXISTS ' , $ save_name ));
182173 }
183174
184- else if (!file_exists ($ zip_path ))
175+ if (!file_exists ($ zip_path ))
185176 {
186177 // The phpBB package could not be found - quit.
187178 throw new \Exception ($ this ->language ->lang ('CLI_EXTENSION_LANGUAGE_FILE_NOT_FOUND ' , $ zip_path ));
188179 }
189180
181+ // These are the language directories (and license) we want to extract from phpBB in order to form British English
182+ $ language_directories = [
183+ 'docs/LICENSE.txt ' ,
184+ 'ext/phpbb/viglink/language/en ' ,
185+ 'language/en ' ,
186+ 'styles/prosilver/theme/en ' ,
187+ ];
188+
189+ // Create our Zip instance
190+ $ zip = new ZipArchive ();
191+
192+ if ($ zip ->open ($ save_name , ZipArchive::CREATE ) !== true )
193+ {
194+ throw new \Exception ($ this ->language ->lang ('CLI_EXTENSION_LANGUAGE_ZIP_ERROR ' , self ::COMMAND_LANGUAGE_DIRECTORY ));
195+ }
196+
197+ $ phpbb_zip = new ZipArchive ();
198+ if ($ phpbb_zip ->open ($ zip_path ) === true )
199+ {
200+ foreach ($ language_directories as $ dir )
201+ {
202+ $ this ->add_to_zip ($ phpbb_zip , $ zip , $ save_version , 'phpBB3/ ' . $ dir );
203+ }
204+ $ phpbb_zip ->close ();
205+ }
190206 else
191207 {
192- // These are the language directories (and license) we want to extract from phpBB in order to form British English
193- $ language_directories = [
194- 'docs/LICENSE.txt ' ,
195- 'ext/phpbb/viglink/language/en ' ,
196- 'language/en ' ,
197- 'styles/prosilver/theme/en ' ,
198- ];
199-
200- // Create our Zipper instance
201- $ zip = new Zipper ();
202-
203- // Extract the relevant folders from the main phpBB3 directory into an appropriate temporary directory
204- $ zip ->make ($ zip_path )
205- ->folder ('phpBB3 ' )
206- ->extractTo ($ this ->tmp_folder , $ language_directories , Zipper::WHITELIST );
207-
208- // Move the license file
209- $ system = new Filesystem ();
210- $ system ->copy ($ this ->tmp_folder . '/docs/LICENSE.txt ' , $ this ->tmp_folder . '/language/en/LICENSE ' );
211- $ system ->remove ($ this ->tmp_folder . '/docs ' );
212-
213- // Create the new zip file with our British English language pack
214- $ zip ->make ($ save_name )
215- ->folder ($ save_version )
216- ->add ($ this ->tmp_folder )
217- ->close ();
218-
219- // This deletes the entire temporary folder
220- $ system ->remove ($ this ->tmp_folder );
221-
222- // Script has completed
223- $ this ->output ->writeln ($ this ->language ->lang ('CLI_EXTENSION_LANGUAGE_PACK_GENERATED ' , $ save_name ));
208+ throw new \Exception ($ this ->language ->lang ('CLI_EXTENSION_LANGUAGE_FILE_NOT_FOUND ' , $ zip_path ));
209+ }
210+
211+ $ zip ->renameName ($ save_version . '/docs/LICENSE.txt ' , $ save_version . '/language/en/LICENSE ' );
212+ $ zip ->close ();
213+
214+ $ this ->output ->writeln ($ this ->language ->lang ('CLI_EXTENSION_LANGUAGE_PACK_GENERATED ' , $ save_name ));
215+ }
216+
217+ /**
218+ * Add a directory structure and file contents from a source zip archive to a destination zip archive.
219+ *
220+ * @param ZipArchive $source_zip The source zip archive.
221+ * @param ZipArchive $dest_zip The destination zip archive.
222+ * @param string $save_version The version string to prepend to file paths in the destination archive.
223+ * @param string $source_path The path to the directory within the source archive.
224+ *
225+ * @return void
226+ */
227+ private function add_to_zip (ZipArchive $ source_zip , ZipArchive $ dest_zip , $ save_version , $ source_path )
228+ {
229+ for ($ i = 0 ; $ i < $ source_zip ->numFiles ; $ i ++)
230+ {
231+ $ stat = $ source_zip ->statIndex ($ i );
232+ if (str_starts_with ($ stat ['name ' ], $ source_path ))
233+ {
234+ if ($ stat ['size ' ] <= 0 && strpos ($ stat ['name ' ], '/ ' , strlen ($ source_path )))
235+ {
236+ $ dest_zip ->addEmptyDir ($ save_version . '/ ' . substr ($ stat ['name ' ], strlen ('phpBB3/ ' )));
237+ continue ;
238+ }
239+
240+ $ file_contents = $ source_zip ->getFromName ($ stat ['name ' ]);
241+ if ($ file_contents !== false )
242+ {
243+ $ local_path = $ save_version . '/ ' . substr ($ stat ['name ' ], strlen ('phpBB3/ ' ));
244+ $ dest_zip ->addFromString ($ local_path , $ file_contents );
245+ }
246+ }
224247 }
225248 }
226249}
0 commit comments