summaryrefslogtreecommitdiff
path: root/platform/www/bin/gittool.php
blob: 5ebd0c5849351c81cc67cbfc5b6e4004d168cb1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/usr/bin/env php
<?php

use splitbrain\phpcli\CLI;
use splitbrain\phpcli\Options;

if(!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__) . '/../') . '/');
define('NOSESSION', 1);
require_once(DOKU_INC . 'inc/init.php');

/**
 * Easily manage DokuWiki git repositories
 *
 * @author Andreas Gohr <andi@splitbrain.org>
 */
class GitToolCLI extends CLI {

    /**
     * Register options and arguments on the given $options object
     *
     * @param Options $options
     * @return void
     */
    protected function setup(Options $options) {
        $options->setHelp(
            "Manage git repositories for DokuWiki and its plugins and templates.\n\n" .
            "$> ./bin/gittool.php clone gallery template:ach\n" .
            "$> ./bin/gittool.php repos\n" .
            "$> ./bin/gittool.php origin -v"
        );

        $options->registerArgument(
            'command',
            'Command to execute. See below',
            true
        );

        $options->registerCommand(
            'clone',
            'Tries to install a known plugin or template (prefix with template:) via git. Uses the DokuWiki.org ' .
            'plugin repository to find the proper git repository. Multiple extensions can be given as parameters'
        );
        $options->registerArgument(
            'extension',
            'name of the extension to install, prefix with \'template:\' for templates',
            true,
            'clone'
        );

        $options->registerCommand(
            'install',
            'The same as clone, but when no git source repository can be found, the extension is installed via ' .
            'download'
        );
        $options->registerArgument(
            'extension',
            'name of the extension to install, prefix with \'template:\' for templates',
            true,
            'install'
        );

        $options->registerCommand(
            'repos',
            'Lists all git repositories found in this DokuWiki installation'
        );

        $options->registerCommand(
            '*',
            'Any unknown commands are assumed to be arguments to git and will be executed in all repositories ' .
            'found within this DokuWiki installation'
        );
    }

    /**
     * Your main program
     *
     * Arguments and options have been parsed when this is run
     *
     * @param Options $options
     * @return void
     */
    protected function main(Options $options) {
        $command = $options->getCmd();
        $args = $options->getArgs();
        if(!$command) $command = array_shift($args);

        switch($command) {
            case '':
                echo $options->help();
                break;
            case 'clone':
                $this->cmdClone($args);
                break;
            case 'install':
                $this->cmdInstall($args);
                break;
            case 'repo':
            case 'repos':
                $this->cmdRepos();
                break;
            default:
                $this->cmdGit($command, $args);
        }
    }

    /**
     * Tries to install the given extensions using git clone
     *
     * @param array $extensions
     */
    public function cmdClone($extensions) {
        $errors = array();
        $succeeded = array();

        foreach($extensions as $ext) {
            $repo = $this->getSourceRepo($ext);

            if(!$repo) {
                $this->error("could not find a repository for $ext");
                $errors[] = $ext;
            } else {
                if($this->cloneExtension($ext, $repo)) {
                    $succeeded[] = $ext;
                } else {
                    $errors[] = $ext;
                }
            }
        }

        echo "\n";
        if($succeeded) $this->success('successfully cloned the following extensions: ' . join(', ', $succeeded));
        if($errors) $this->error('failed to clone the following extensions: ' . join(', ', $errors));
    }

    /**
     * Tries to install the given extensions using git clone with fallback to install
     *
     * @param array $extensions
     */
    public function cmdInstall($extensions) {
        $errors = array();
        $succeeded = array();

        foreach($extensions as $ext) {
            $repo = $this->getSourceRepo($ext);

            if(!$repo) {
                $this->info("could not find a repository for $ext");
                if($this->downloadExtension($ext)) {
                    $succeeded[] = $ext;
                } else {
                    $errors[] = $ext;
                }
            } else {
                if($this->cloneExtension($ext, $repo)) {
                    $succeeded[] = $ext;
                } else {
                    $errors[] = $ext;
                }
            }
        }

        echo "\n";
        if($succeeded) $this->success('successfully installed the following extensions: ' . join(', ', $succeeded));
        if($errors) $this->error('failed to install the following extensions: ' . join(', ', $errors));
    }

    /**
     * Executes the given git command in every repository
     *
     * @param $cmd
     * @param $arg
     */
    public function cmdGit($cmd, $arg) {
        $repos = $this->findRepos();

        $shell = array_merge(array('git', $cmd), $arg);
        $shell = array_map('escapeshellarg', $shell);
        $shell = join(' ', $shell);

        foreach($repos as $repo) {
            if(!@chdir($repo)) {
                $this->error("Could not change into $repo");
                continue;
            }

            $this->info("executing $shell in $repo");
            $ret = 0;
            system($shell, $ret);

            if($ret == 0) {
                $this->success("git succeeded in $repo");
            } else {
                $this->error("git failed in $repo");
            }
        }
    }

    /**
     * Simply lists the repositories
     */
    public function cmdRepos() {
        $repos = $this->findRepos();
        foreach($repos as $repo) {
            echo "$repo\n";
        }
    }

    /**
     * Install extension from the given download URL
     *
     * @param string $ext
     * @return bool|null
     */
    private function downloadExtension($ext) {
        /** @var helper_plugin_extension_extension $plugin */
        $plugin = plugin_load('helper', 'extension_extension');
        if(!$ext) die("extension plugin not available, can't continue");

        $plugin->setExtension($ext);

        $url = $plugin->getDownloadURL();
        if(!$url) {
            $this->error("no download URL for $ext");
            return false;
        }

        $ok = false;
        try {
            $this->info("installing $ext via download from $url");
            $ok = $plugin->installFromURL($url);
        } catch(Exception $e) {
            $this->error($e->getMessage());
        }

        if($ok) {
            $this->success("installed $ext via download");
            return true;
        } else {
            $this->success("failed to install $ext via download");
            return false;
        }
    }

    /**
     * Clones the extension from the given repository
     *
     * @param string $ext
     * @param string $repo
     * @return bool
     */
    private function cloneExtension($ext, $repo) {
        if(substr($ext, 0, 9) == 'template:') {
            $target = fullpath(tpl_incdir() . '../' . substr($ext, 9));
        } else {
            $target = DOKU_PLUGIN . $ext;
        }

        $this->info("cloning $ext from $repo to $target");
        $ret = 0;
        system("git clone $repo $target", $ret);
        if($ret === 0) {
            $this->success("cloning of $ext succeeded");
            return true;
        } else {
            $this->error("cloning of $ext failed");
            return false;
        }
    }

    /**
     * Returns all git repositories in this DokuWiki install
     *
     * Looks in root, template and plugin directories only.
     *
     * @return array
     */
    private function findRepos() {
        $this->info('Looking for .git directories');
        $data = array_merge(
            glob(DOKU_INC . '.git', GLOB_ONLYDIR),
            glob(DOKU_PLUGIN . '*/.git', GLOB_ONLYDIR),
            glob(fullpath(tpl_incdir() . '../') . '/*/.git', GLOB_ONLYDIR)
        );

        if(!$data) {
            $this->error('Found no .git directories');
        } else {
            $this->success('Found ' . count($data) . ' .git directories');
        }
        $data = array_map('fullpath', array_map('dirname', $data));
        return $data;
    }

    /**
     * Returns the repository for the given extension
     *
     * @param $extension
     * @return false|string
     */
    private function getSourceRepo($extension) {
        /** @var helper_plugin_extension_extension $ext */
        $ext = plugin_load('helper', 'extension_extension');
        if(!$ext) die("extension plugin not available, can't continue");

        $ext->setExtension($extension);

        $repourl = $ext->getSourcerepoURL();
        if(!$repourl) return false;

        // match github repos
        if(preg_match('/github\.com\/([^\/]+)\/([^\/]+)/i', $repourl, $m)) {
            $user = $m[1];
            $repo = $m[2];
            return 'https://github.com/' . $user . '/' . $repo . '.git';
        }

        // match gitorious repos
        if(preg_match('/gitorious.org\/([^\/]+)\/([^\/]+)?/i', $repourl, $m)) {
            $user = $m[1];
            $repo = $m[2];
            if(!$repo) $repo = $user;

            return 'https://git.gitorious.org/' . $user . '/' . $repo . '.git';
        }

        // match bitbucket repos - most people seem to use mercurial there though
        if(preg_match('/bitbucket\.org\/([^\/]+)\/([^\/]+)/i', $repourl, $m)) {
            $user = $m[1];
            $repo = $m[2];
            return 'https://bitbucket.org/' . $user . '/' . $repo . '.git';
        }

        return false;
    }
}

// Main
$cli = new GitToolCLI();
$cli->run();