xref: /dokuwiki/bin/gittool.php (revision 8c7c53b0321a3cd3116b8d3b2ad27863a38dece7)
1cbfa4829SPhy#!/usr/bin/env php
2c39ae2c9SAndreas Gohr<?php
3cbeaa4a0SAndreas Gohr
4cbeaa4a0SAndreas Gohruse splitbrain\phpcli\CLI;
5cbeaa4a0SAndreas Gohruse splitbrain\phpcli\Options;
6cbeaa4a0SAndreas Gohr
7b1f206e1SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC', realpath(__DIR__ . '/../') . '/');
8c39ae2c9SAndreas Gohrdefine('NOSESSION', 1);
9c39ae2c9SAndreas Gohrrequire_once(DOKU_INC . 'inc/init.php');
10c39ae2c9SAndreas Gohr
11c39ae2c9SAndreas Gohr/**
12c39ae2c9SAndreas Gohr * Easily manage DokuWiki git repositories
13c39ae2c9SAndreas Gohr *
14c39ae2c9SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
15c39ae2c9SAndreas Gohr */
16*8c7c53b0SAndreas Gohrclass GitToolCLI extends CLI
17*8c7c53b0SAndreas Gohr{
18c39ae2c9SAndreas Gohr
199fb66494SAndreas Gohr    /**
209fb66494SAndreas Gohr     * Register options and arguments on the given $options object
219fb66494SAndreas Gohr     *
22cbeaa4a0SAndreas Gohr     * @param Options $options
239fb66494SAndreas Gohr     * @return void
249fb66494SAndreas Gohr     */
25cbeaa4a0SAndreas Gohr    protected function setup(Options $options) {
269fb66494SAndreas Gohr        $options->setHelp(
279fb66494SAndreas Gohr            "Manage git repositories for DokuWiki and its plugins and templates.\n\n" .
289fb66494SAndreas Gohr            "$> ./bin/gittool.php clone gallery template:ach\n" .
299fb66494SAndreas Gohr            "$> ./bin/gittool.php repos\n" .
30ae1ce4a6SAndreas Gohr            "$> ./bin/gittool.php origin -v"
319fb66494SAndreas Gohr        );
32c39ae2c9SAndreas Gohr
339fb66494SAndreas Gohr        $options->registerArgument(
349fb66494SAndreas Gohr            'command',
359fb66494SAndreas Gohr            'Command to execute. See below',
369fb66494SAndreas Gohr            true
379fb66494SAndreas Gohr        );
38c39ae2c9SAndreas Gohr
399fb66494SAndreas Gohr        $options->registerCommand(
409fb66494SAndreas Gohr            'clone',
419fb66494SAndreas Gohr            'Tries to install a known plugin or template (prefix with template:) via git. Uses the DokuWiki.org ' .
429fb66494SAndreas Gohr            'plugin repository to find the proper git repository. Multiple extensions can be given as parameters'
439fb66494SAndreas Gohr        );
449fb66494SAndreas Gohr        $options->registerArgument(
459fb66494SAndreas Gohr            'extension',
469fb66494SAndreas Gohr            'name of the extension to install, prefix with \'template:\' for templates',
479fb66494SAndreas Gohr            true,
489fb66494SAndreas Gohr            'clone'
499fb66494SAndreas Gohr        );
50c39ae2c9SAndreas Gohr
519fb66494SAndreas Gohr        $options->registerCommand(
529fb66494SAndreas Gohr            'install',
539fb66494SAndreas Gohr            'The same as clone, but when no git source repository can be found, the extension is installed via ' .
549fb66494SAndreas Gohr            'download'
559fb66494SAndreas Gohr        );
569fb66494SAndreas Gohr        $options->registerArgument(
579fb66494SAndreas Gohr            'extension',
589fb66494SAndreas Gohr            'name of the extension to install, prefix with \'template:\' for templates',
599fb66494SAndreas Gohr            true,
609fb66494SAndreas Gohr            'install'
619fb66494SAndreas Gohr        );
62c39ae2c9SAndreas Gohr
639fb66494SAndreas Gohr        $options->registerCommand(
649fb66494SAndreas Gohr            'repos',
659fb66494SAndreas Gohr            'Lists all git repositories found in this DokuWiki installation'
669fb66494SAndreas Gohr        );
67c39ae2c9SAndreas Gohr
689fb66494SAndreas Gohr        $options->registerCommand(
699fb66494SAndreas Gohr            '*',
709fb66494SAndreas Gohr            'Any unknown commands are assumed to be arguments to git and will be executed in all repositories ' .
719fb66494SAndreas Gohr            'found within this DokuWiki installation'
729fb66494SAndreas Gohr        );
73c39ae2c9SAndreas Gohr    }
74c39ae2c9SAndreas Gohr
75c39ae2c9SAndreas Gohr    /**
769fb66494SAndreas Gohr     * Your main program
779fb66494SAndreas Gohr     *
789fb66494SAndreas Gohr     * Arguments and options have been parsed when this is run
799fb66494SAndreas Gohr     *
80cbeaa4a0SAndreas Gohr     * @param Options $options
819fb66494SAndreas Gohr     * @return void
829fb66494SAndreas Gohr     */
83cbeaa4a0SAndreas Gohr    protected function main(Options $options) {
849fb66494SAndreas Gohr        $command = $options->getCmd();
85cbeaa4a0SAndreas Gohr        $args = $options->getArgs();
86cbeaa4a0SAndreas Gohr        if(!$command) $command = array_shift($args);
879fb66494SAndreas Gohr
889fb66494SAndreas Gohr        switch($command) {
899fb66494SAndreas Gohr            case '':
901c36b3d8SAndreas Gohr                echo $options->help();
919fb66494SAndreas Gohr                break;
929fb66494SAndreas Gohr            case 'clone':
932b2d0ba9SAndreas Gohr                $this->cmdClone($args);
949fb66494SAndreas Gohr                break;
959fb66494SAndreas Gohr            case 'install':
962b2d0ba9SAndreas Gohr                $this->cmdInstall($args);
979fb66494SAndreas Gohr                break;
989fb66494SAndreas Gohr            case 'repo':
999fb66494SAndreas Gohr            case 'repos':
1002b2d0ba9SAndreas Gohr                $this->cmdRepos();
1019fb66494SAndreas Gohr                break;
1029fb66494SAndreas Gohr            default:
1032b2d0ba9SAndreas Gohr                $this->cmdGit($command, $args);
1049fb66494SAndreas Gohr        }
1059fb66494SAndreas Gohr    }
1069fb66494SAndreas Gohr
1079fb66494SAndreas Gohr    /**
108c39ae2c9SAndreas Gohr     * Tries to install the given extensions using git clone
109c39ae2c9SAndreas Gohr     *
11042ea7f44SGerrit Uitslag     * @param array $extensions
111c39ae2c9SAndreas Gohr     */
1122b2d0ba9SAndreas Gohr    public function cmdClone($extensions) {
113b1f206e1SAndreas Gohr        $errors = [];
114b1f206e1SAndreas Gohr        $succeeded = [];
115c39ae2c9SAndreas Gohr
116c39ae2c9SAndreas Gohr        foreach($extensions as $ext) {
117c39ae2c9SAndreas Gohr            $repo = $this->getSourceRepo($ext);
118c39ae2c9SAndreas Gohr
119c39ae2c9SAndreas Gohr            if (!$repo) {
1209fb66494SAndreas Gohr                $this->error("could not find a repository for $ext");
121c39ae2c9SAndreas Gohr                $errors[] = $ext;
122b1f206e1SAndreas Gohr            } elseif ($this->cloneExtension($ext, $repo)) {
123c39ae2c9SAndreas Gohr                $succeeded[] = $ext;
124c39ae2c9SAndreas Gohr            } else {
125c39ae2c9SAndreas Gohr                $errors[] = $ext;
126c39ae2c9SAndreas Gohr            }
127c39ae2c9SAndreas Gohr        }
128c39ae2c9SAndreas Gohr
129c39ae2c9SAndreas Gohr        echo "\n";
130b1f206e1SAndreas Gohr        if($succeeded) $this->success('successfully cloned the following extensions: ' . implode(', ', $succeeded));
131b1f206e1SAndreas Gohr        if($errors) $this->error('failed to clone the following extensions: ' . implode(', ', $errors));
132c39ae2c9SAndreas Gohr    }
133c39ae2c9SAndreas Gohr
134c39ae2c9SAndreas Gohr    /**
135c39ae2c9SAndreas Gohr     * Tries to install the given extensions using git clone with fallback to install
136c39ae2c9SAndreas Gohr     *
13742ea7f44SGerrit Uitslag     * @param array $extensions
138c39ae2c9SAndreas Gohr     */
1392b2d0ba9SAndreas Gohr    public function cmdInstall($extensions) {
140b1f206e1SAndreas Gohr        $errors = [];
141b1f206e1SAndreas Gohr        $succeeded = [];
142c39ae2c9SAndreas Gohr
143c39ae2c9SAndreas Gohr        foreach($extensions as $ext) {
144c39ae2c9SAndreas Gohr            $repo = $this->getSourceRepo($ext);
145c39ae2c9SAndreas Gohr
146c39ae2c9SAndreas Gohr            if (!$repo) {
1479fb66494SAndreas Gohr                $this->info("could not find a repository for $ext");
148c39ae2c9SAndreas Gohr                if($this->downloadExtension($ext)) {
149c39ae2c9SAndreas Gohr                    $succeeded[] = $ext;
150c39ae2c9SAndreas Gohr                } else {
151c39ae2c9SAndreas Gohr                    $errors[] = $ext;
152c39ae2c9SAndreas Gohr                }
153b1f206e1SAndreas Gohr            } elseif ($this->cloneExtension($ext, $repo)) {
154c39ae2c9SAndreas Gohr                $succeeded[] = $ext;
155c39ae2c9SAndreas Gohr            } else {
156c39ae2c9SAndreas Gohr                $errors[] = $ext;
157c39ae2c9SAndreas Gohr            }
158c39ae2c9SAndreas Gohr        }
159c39ae2c9SAndreas Gohr
160c39ae2c9SAndreas Gohr        echo "\n";
161b1f206e1SAndreas Gohr        if($succeeded) $this->success('successfully installed the following extensions: ' . implode(', ', $succeeded));
162b1f206e1SAndreas Gohr        if($errors) $this->error('failed to install the following extensions: ' . implode(', ', $errors));
163c39ae2c9SAndreas Gohr    }
164c39ae2c9SAndreas Gohr
165c39ae2c9SAndreas Gohr    /**
166c39ae2c9SAndreas Gohr     * Executes the given git command in every repository
167c39ae2c9SAndreas Gohr     *
168c39ae2c9SAndreas Gohr     * @param $cmd
169c39ae2c9SAndreas Gohr     * @param $arg
170c39ae2c9SAndreas Gohr     */
1712b2d0ba9SAndreas Gohr    public function cmdGit($cmd, $arg) {
172c39ae2c9SAndreas Gohr        $repos = $this->findRepos();
173c39ae2c9SAndreas Gohr
174b1f206e1SAndreas Gohr        $shell = array_merge(['git', $cmd], $arg);
175c39ae2c9SAndreas Gohr        $shell = array_map('escapeshellarg', $shell);
176b1f206e1SAndreas Gohr        $shell = implode(' ', $shell);
177c39ae2c9SAndreas Gohr
178c39ae2c9SAndreas Gohr        foreach($repos as $repo) {
179c39ae2c9SAndreas Gohr            if(!@chdir($repo)) {
1809fb66494SAndreas Gohr                $this->error("Could not change into $repo");
181c39ae2c9SAndreas Gohr                continue;
182c39ae2c9SAndreas Gohr            }
183c39ae2c9SAndreas Gohr
1849fb66494SAndreas Gohr            $this->info("executing $shell in $repo");
185c39ae2c9SAndreas Gohr            $ret = 0;
186c39ae2c9SAndreas Gohr            system($shell, $ret);
187c39ae2c9SAndreas Gohr
188c39ae2c9SAndreas Gohr            if($ret == 0) {
1899fb66494SAndreas Gohr                $this->success("git succeeded in $repo");
190c39ae2c9SAndreas Gohr            } else {
1919fb66494SAndreas Gohr                $this->error("git failed in $repo");
192c39ae2c9SAndreas Gohr            }
193c39ae2c9SAndreas Gohr        }
194c39ae2c9SAndreas Gohr    }
195c39ae2c9SAndreas Gohr
196c39ae2c9SAndreas Gohr    /**
197c39ae2c9SAndreas Gohr     * Simply lists the repositories
198c39ae2c9SAndreas Gohr     */
1992b2d0ba9SAndreas Gohr    public function cmdRepos() {
200c39ae2c9SAndreas Gohr        $repos = $this->findRepos();
201c39ae2c9SAndreas Gohr        foreach($repos as $repo) {
202c39ae2c9SAndreas Gohr            echo "$repo\n";
203c39ae2c9SAndreas Gohr        }
204c39ae2c9SAndreas Gohr    }
205c39ae2c9SAndreas Gohr
206c39ae2c9SAndreas Gohr    /**
207c39ae2c9SAndreas Gohr     * Install extension from the given download URL
208c39ae2c9SAndreas Gohr     *
209c39ae2c9SAndreas Gohr     * @param string $ext
2107e8500eeSGerrit Uitslag     * @return bool|null
211c39ae2c9SAndreas Gohr     */
212c39ae2c9SAndreas Gohr    private function downloadExtension($ext) {
213c39ae2c9SAndreas Gohr        /** @var helper_plugin_extension_extension $plugin */
214c39ae2c9SAndreas Gohr        $plugin = plugin_load('helper', 'extension_extension');
215c39ae2c9SAndreas Gohr        if(!$ext) die("extension plugin not available, can't continue");
2167e8500eeSGerrit Uitslag
217c39ae2c9SAndreas Gohr        $plugin->setExtension($ext);
218c39ae2c9SAndreas Gohr
219c39ae2c9SAndreas Gohr        $url = $plugin->getDownloadURL();
220c39ae2c9SAndreas Gohr        if(!$url) {
2219fb66494SAndreas Gohr            $this->error("no download URL for $ext");
222c39ae2c9SAndreas Gohr            return false;
223c39ae2c9SAndreas Gohr        }
224c39ae2c9SAndreas Gohr
225c39ae2c9SAndreas Gohr        $ok = false;
226c39ae2c9SAndreas Gohr        try {
2279fb66494SAndreas Gohr            $this->info("installing $ext via download from $url");
228c39ae2c9SAndreas Gohr            $ok = $plugin->installFromURL($url);
229c39ae2c9SAndreas Gohr        } catch(Exception $e) {
2309fb66494SAndreas Gohr            $this->error($e->getMessage());
231c39ae2c9SAndreas Gohr        }
232c39ae2c9SAndreas Gohr
233c39ae2c9SAndreas Gohr        if($ok) {
2349fb66494SAndreas Gohr            $this->success("installed $ext via download");
235c39ae2c9SAndreas Gohr            return true;
236c39ae2c9SAndreas Gohr        } else {
2379fb66494SAndreas Gohr            $this->success("failed to install $ext via download");
238c39ae2c9SAndreas Gohr            return false;
239c39ae2c9SAndreas Gohr        }
240c39ae2c9SAndreas Gohr    }
241c39ae2c9SAndreas Gohr
242c39ae2c9SAndreas Gohr    /**
243c39ae2c9SAndreas Gohr     * Clones the extension from the given repository
244c39ae2c9SAndreas Gohr     *
245c39ae2c9SAndreas Gohr     * @param string $ext
246c39ae2c9SAndreas Gohr     * @param string $repo
247c39ae2c9SAndreas Gohr     * @return bool
248c39ae2c9SAndreas Gohr     */
249c39ae2c9SAndreas Gohr    private function cloneExtension($ext, $repo) {
250c39ae2c9SAndreas Gohr        if(substr($ext, 0, 9) == 'template:') {
251c39ae2c9SAndreas Gohr            $target = fullpath(tpl_incdir() . '../' . substr($ext, 9));
252c39ae2c9SAndreas Gohr        } else {
253c39ae2c9SAndreas Gohr            $target = DOKU_PLUGIN . $ext;
254c39ae2c9SAndreas Gohr        }
255c39ae2c9SAndreas Gohr
2569fb66494SAndreas Gohr        $this->info("cloning $ext from $repo to $target");
257c39ae2c9SAndreas Gohr        $ret = 0;
258c39ae2c9SAndreas Gohr        system("git clone $repo $target", $ret);
259c39ae2c9SAndreas Gohr        if($ret === 0) {
2609fb66494SAndreas Gohr            $this->success("cloning of $ext succeeded");
261c39ae2c9SAndreas Gohr            return true;
262c39ae2c9SAndreas Gohr        } else {
2639fb66494SAndreas Gohr            $this->error("cloning of $ext failed");
264c39ae2c9SAndreas Gohr            return false;
265c39ae2c9SAndreas Gohr        }
266c39ae2c9SAndreas Gohr    }
267c39ae2c9SAndreas Gohr
268c39ae2c9SAndreas Gohr    /**
269c39ae2c9SAndreas Gohr     * Returns all git repositories in this DokuWiki install
270c39ae2c9SAndreas Gohr     *
271c39ae2c9SAndreas Gohr     * Looks in root, template and plugin directories only.
272c39ae2c9SAndreas Gohr     *
273c39ae2c9SAndreas Gohr     * @return array
274c39ae2c9SAndreas Gohr     */
275c39ae2c9SAndreas Gohr    private function findRepos() {
2769fb66494SAndreas Gohr        $this->info('Looking for .git directories');
277c39ae2c9SAndreas Gohr        $data = array_merge(
278c39ae2c9SAndreas Gohr            glob(DOKU_INC . '.git', GLOB_ONLYDIR),
279c39ae2c9SAndreas Gohr            glob(DOKU_PLUGIN . '*/.git', GLOB_ONLYDIR),
280c39ae2c9SAndreas Gohr            glob(fullpath(tpl_incdir() . '../') . '/*/.git', GLOB_ONLYDIR)
281c39ae2c9SAndreas Gohr        );
282c39ae2c9SAndreas Gohr
283c39ae2c9SAndreas Gohr        if(!$data) {
2849fb66494SAndreas Gohr            $this->error('Found no .git directories');
285c39ae2c9SAndreas Gohr        } else {
2869fb66494SAndreas Gohr            $this->success('Found ' . count($data) . ' .git directories');
287c39ae2c9SAndreas Gohr        }
288b0c7084fSAndreas Gohr        $data = array_map('fullpath', array_map('dirname', $data));
289c39ae2c9SAndreas Gohr        return $data;
290c39ae2c9SAndreas Gohr    }
291c39ae2c9SAndreas Gohr
292c39ae2c9SAndreas Gohr    /**
293c39ae2c9SAndreas Gohr     * Returns the repository for the given extension
294c39ae2c9SAndreas Gohr     *
295c39ae2c9SAndreas Gohr     * @param $extension
29642ea7f44SGerrit Uitslag     * @return false|string
297c39ae2c9SAndreas Gohr     */
298c39ae2c9SAndreas Gohr    private function getSourceRepo($extension) {
299c39ae2c9SAndreas Gohr        /** @var helper_plugin_extension_extension $ext */
300c39ae2c9SAndreas Gohr        $ext = plugin_load('helper', 'extension_extension');
301c39ae2c9SAndreas Gohr        if(!$ext) die("extension plugin not available, can't continue");
3027e8500eeSGerrit Uitslag
303c39ae2c9SAndreas Gohr        $ext->setExtension($extension);
304c39ae2c9SAndreas Gohr
30568cf024bSAndreas Gohr        $repourl = $ext->getSourcerepoURL();
30668cf024bSAndreas Gohr        if(!$repourl) return false;
30768cf024bSAndreas Gohr
308c39ae2c9SAndreas Gohr        // match github repos
30968cf024bSAndreas Gohr        if(preg_match('/github\.com\/([^\/]+)\/([^\/]+)/i', $repourl, $m)) {
310c39ae2c9SAndreas Gohr            $user = $m[1];
311c39ae2c9SAndreas Gohr            $repo = $m[2];
312c39ae2c9SAndreas Gohr            return 'https://github.com/' . $user . '/' . $repo . '.git';
313c39ae2c9SAndreas Gohr        }
314c39ae2c9SAndreas Gohr
315c39ae2c9SAndreas Gohr        // match gitorious repos
31668cf024bSAndreas Gohr        if(preg_match('/gitorious.org\/([^\/]+)\/([^\/]+)?/i', $repourl, $m)) {
317c39ae2c9SAndreas Gohr            $user = $m[1];
318c39ae2c9SAndreas Gohr            $repo = $m[2];
319c39ae2c9SAndreas Gohr            if(!$repo) $repo = $user;
320c39ae2c9SAndreas Gohr
321c39ae2c9SAndreas Gohr            return 'https://git.gitorious.org/' . $user . '/' . $repo . '.git';
322c39ae2c9SAndreas Gohr        }
323c39ae2c9SAndreas Gohr
324c39ae2c9SAndreas Gohr        // match bitbucket repos - most people seem to use mercurial there though
32568cf024bSAndreas Gohr        if(preg_match('/bitbucket\.org\/([^\/]+)\/([^\/]+)/i', $repourl, $m)) {
326c39ae2c9SAndreas Gohr            $user = $m[1];
327c39ae2c9SAndreas Gohr            $repo = $m[2];
328c39ae2c9SAndreas Gohr            return 'https://bitbucket.org/' . $user . '/' . $repo . '.git';
329c39ae2c9SAndreas Gohr        }
330c39ae2c9SAndreas Gohr
331c39ae2c9SAndreas Gohr        return false;
332c39ae2c9SAndreas Gohr    }
333c39ae2c9SAndreas Gohr}
334c39ae2c9SAndreas Gohr
335b0b7909bSAndreas Gohr// Main
336b0b7909bSAndreas Gohr$cli = new GitToolCLI();
337b0b7909bSAndreas Gohr$cli->run();
338