xref: /plugin/dev/www/PluginWizard.php (revision c2c589bc25fa0a4dd3283b1b704fd012223d7011)
189e2f9d1SAndreas Gohr<?php
289e2f9d1SAndreas Gohr
389e2f9d1SAndreas Gohrnamespace dokuwiki\plugin\dev\www;
489e2f9d1SAndreas Gohr
589e2f9d1SAndreas Gohruse dokuwiki\plugin\dev\Skeletor;
689e2f9d1SAndreas Gohruse splitbrain\PHPArchive\ArchiveIllegalCompressionException;
789e2f9d1SAndreas Gohruse splitbrain\PHPArchive\ArchiveIOException;
889e2f9d1SAndreas Gohruse splitbrain\PHPArchive\Zip;
989e2f9d1SAndreas Gohr
1089e2f9d1SAndreas Gohrclass PluginWizard
1189e2f9d1SAndreas Gohr{
1289e2f9d1SAndreas Gohr    /**
1389e2f9d1SAndreas Gohr     * @throws ArchiveIllegalCompressionException
1489e2f9d1SAndreas Gohr     * @throws ArchiveIOException
1589e2f9d1SAndreas Gohr     */
1689e2f9d1SAndreas Gohr    public function handle()
1789e2f9d1SAndreas Gohr    {
1889e2f9d1SAndreas Gohr        if (!isset($_POST['base'])) return null;
1989e2f9d1SAndreas Gohr
20cde324c2SAndreas Gohr        $base = preg_replace('/[^a-z0-9]/i', '', $_POST['base']);
21cde324c2SAndreas Gohr
2289e2f9d1SAndreas Gohr        $skeletor = new Skeletor(
2389e2f9d1SAndreas Gohr            Skeletor::TYPE_PLUGIN,
24cde324c2SAndreas Gohr            $base,
2589e2f9d1SAndreas Gohr            $_POST['desc'] ?: '',
2689e2f9d1SAndreas Gohr            $_POST['author'] ?: '',
2789e2f9d1SAndreas Gohr            $_POST['mail'] ?: '',
2889e2f9d1SAndreas Gohr            '',
2989e2f9d1SAndreas Gohr            $_POST['url'] ?: ''
3089e2f9d1SAndreas Gohr        );
3189e2f9d1SAndreas Gohr        $skeletor->addBasics();
3289e2f9d1SAndreas Gohr
3389e2f9d1SAndreas Gohr        if (!empty($_POST['use_lang'])) $skeletor->addLang();
3489e2f9d1SAndreas Gohr        if (!empty($_POST['use_conf'])) $skeletor->addConf();
3589e2f9d1SAndreas Gohr        if (!empty($_POST['use_test'])) $skeletor->addTest();
3689e2f9d1SAndreas Gohr
3789e2f9d1SAndreas Gohr        foreach ($_POST['components'] as $id) {
38*c2c589bcSAndreas Gohr            [$type, , , $component] = array_pad(explode('_', $id, 4), 4, '');
3989e2f9d1SAndreas Gohr            if (isset($_POST['options'][$id])) {
4089e2f9d1SAndreas Gohr                $options = array_filter(array_map('trim', explode(',', $_POST['options'][$id])));
4189e2f9d1SAndreas Gohr            } else {
4289e2f9d1SAndreas Gohr                $options = [];
4389e2f9d1SAndreas Gohr            }
4489e2f9d1SAndreas Gohr
4589e2f9d1SAndreas Gohr            $skeletor->addComponent($type, $component, $options);
4689e2f9d1SAndreas Gohr        }
4789e2f9d1SAndreas Gohr
4889e2f9d1SAndreas Gohr        $zip = new Zip();
4989e2f9d1SAndreas Gohr        $zip->setCompression(9);
5089e2f9d1SAndreas Gohr        $zip->create();
5189e2f9d1SAndreas Gohr        foreach ($skeletor->getFiles() as $file => $content) {
52cde324c2SAndreas Gohr            $zip->addData($base . '/' . $file, $content);
5389e2f9d1SAndreas Gohr        }
5489e2f9d1SAndreas Gohr
5589e2f9d1SAndreas Gohr        return $zip->getArchive();
5689e2f9d1SAndreas Gohr    }
5789e2f9d1SAndreas Gohr
5889e2f9d1SAndreas Gohr
5989e2f9d1SAndreas Gohr    /**
6089e2f9d1SAndreas Gohr     * Get options for all available plugin types
6189e2f9d1SAndreas Gohr     */
6289e2f9d1SAndreas Gohr    public function getPluginTypes()
6389e2f9d1SAndreas Gohr    {
6489e2f9d1SAndreas Gohr        return Skeletor::PLUGIN_TYPES;
6589e2f9d1SAndreas Gohr    }
6689e2f9d1SAndreas Gohr
6789e2f9d1SAndreas Gohr    public function getEvents()
6889e2f9d1SAndreas Gohr    {
6989e2f9d1SAndreas Gohr        return array_map('trim', file(__DIR__ . '/../events.txt', FILE_IGNORE_NEW_LINES));
7089e2f9d1SAndreas Gohr    }
7189e2f9d1SAndreas Gohr}
72