1c39ae2c9SAndreas Gohr#!/usr/bin/php 2c39ae2c9SAndreas Gohr<?php 3c39ae2c9SAndreas Gohr 4c39ae2c9SAndreas Gohrif('cli' != php_sapi_name()) die(); 5c39ae2c9SAndreas Gohrini_set('memory_limit', '128M'); 6c39ae2c9SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__) . '/../') . '/'); 7c39ae2c9SAndreas Gohrdefine('NOSESSION', 1); 8c39ae2c9SAndreas Gohrrequire_once(DOKU_INC . 'inc/init.php'); 9c39ae2c9SAndreas Gohr 10c39ae2c9SAndreas Gohr 11c39ae2c9SAndreas Gohr/** 12c39ae2c9SAndreas Gohr * Easily manage DokuWiki git repositories 13c39ae2c9SAndreas Gohr * 14c39ae2c9SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 15c39ae2c9SAndreas Gohr */ 169fb66494SAndreas Gohrclass GitToolCLI extends DokuCLI { 17c39ae2c9SAndreas Gohr 189fb66494SAndreas Gohr /** 199fb66494SAndreas Gohr * Register options and arguments on the given $options object 209fb66494SAndreas Gohr * 219fb66494SAndreas Gohr * @param DokuCLI_Options $options 229fb66494SAndreas Gohr * @return void 239fb66494SAndreas Gohr */ 249fb66494SAndreas Gohr protected function setup(DokuCLI_Options $options) { 259fb66494SAndreas Gohr $options->setHelp( 269fb66494SAndreas Gohr "Manage git repositories for DokuWiki and its plugins and templates.\n\n" . 279fb66494SAndreas Gohr "$> ./bin/gittool.php clone gallery template:ach\n" . 289fb66494SAndreas Gohr "$> ./bin/gittool.php repos\n" . 29*ae1ce4a6SAndreas Gohr "$> ./bin/gittool.php origin -v" 309fb66494SAndreas Gohr ); 31c39ae2c9SAndreas Gohr 329fb66494SAndreas Gohr $options->registerArgument( 339fb66494SAndreas Gohr 'command', 349fb66494SAndreas Gohr 'Command to execute. See below', 359fb66494SAndreas Gohr true 369fb66494SAndreas Gohr ); 37c39ae2c9SAndreas Gohr 389fb66494SAndreas Gohr $options->registerCommand( 399fb66494SAndreas Gohr 'clone', 409fb66494SAndreas Gohr 'Tries to install a known plugin or template (prefix with template:) via git. Uses the DokuWiki.org ' . 419fb66494SAndreas Gohr 'plugin repository to find the proper git repository. Multiple extensions can be given as parameters' 429fb66494SAndreas Gohr ); 439fb66494SAndreas Gohr $options->registerArgument( 449fb66494SAndreas Gohr 'extension', 459fb66494SAndreas Gohr 'name of the extension to install, prefix with \'template:\' for templates', 469fb66494SAndreas Gohr true, 479fb66494SAndreas Gohr 'clone' 489fb66494SAndreas Gohr ); 49c39ae2c9SAndreas Gohr 509fb66494SAndreas Gohr $options->registerCommand( 519fb66494SAndreas Gohr 'install', 529fb66494SAndreas Gohr 'The same as clone, but when no git source repository can be found, the extension is installed via ' . 539fb66494SAndreas Gohr 'download' 549fb66494SAndreas Gohr ); 559fb66494SAndreas Gohr $options->registerArgument( 569fb66494SAndreas Gohr 'extension', 579fb66494SAndreas Gohr 'name of the extension to install, prefix with \'template:\' for templates', 589fb66494SAndreas Gohr true, 599fb66494SAndreas Gohr 'install' 609fb66494SAndreas Gohr ); 61c39ae2c9SAndreas Gohr 629fb66494SAndreas Gohr $options->registerCommand( 639fb66494SAndreas Gohr 'repos', 649fb66494SAndreas Gohr 'Lists all git repositories found in this DokuWiki installation' 659fb66494SAndreas Gohr ); 66c39ae2c9SAndreas Gohr 679fb66494SAndreas Gohr $options->registerCommand( 689fb66494SAndreas Gohr '*', 699fb66494SAndreas Gohr 'Any unknown commands are assumed to be arguments to git and will be executed in all repositories ' . 709fb66494SAndreas Gohr 'found within this DokuWiki installation' 719fb66494SAndreas Gohr ); 72c39ae2c9SAndreas Gohr } 73c39ae2c9SAndreas Gohr 74c39ae2c9SAndreas Gohr /** 759fb66494SAndreas Gohr * Your main program 769fb66494SAndreas Gohr * 779fb66494SAndreas Gohr * Arguments and options have been parsed when this is run 789fb66494SAndreas Gohr * 799fb66494SAndreas Gohr * @param DokuCLI_Options $options 809fb66494SAndreas Gohr * @return void 819fb66494SAndreas Gohr */ 829fb66494SAndreas Gohr protected function main(DokuCLI_Options $options) { 839fb66494SAndreas Gohr $command = $options->getCmd(); 849fb66494SAndreas Gohr if(!$command) $command = array_shift($options->args); 859fb66494SAndreas Gohr 869fb66494SAndreas Gohr switch($command) { 879fb66494SAndreas Gohr case '': 889fb66494SAndreas Gohr echo $options->help('foo'); 899fb66494SAndreas Gohr break; 909fb66494SAndreas Gohr case 'clone': 919fb66494SAndreas Gohr $this->cmd_clone($options->args); 929fb66494SAndreas Gohr break; 939fb66494SAndreas Gohr case 'install': 949fb66494SAndreas Gohr $this->cmd_install($options->args); 959fb66494SAndreas Gohr break; 969fb66494SAndreas Gohr case 'repo': 979fb66494SAndreas Gohr case 'repos': 989fb66494SAndreas Gohr $this->cmd_repos(); 999fb66494SAndreas Gohr break; 1009fb66494SAndreas Gohr default: 1019fb66494SAndreas Gohr $this->cmd_git($command, $options->args); 1029fb66494SAndreas Gohr } 1039fb66494SAndreas Gohr } 1049fb66494SAndreas Gohr 1059fb66494SAndreas Gohr /** 106c39ae2c9SAndreas Gohr * Tries to install the given extensions using git clone 107c39ae2c9SAndreas Gohr * 108c39ae2c9SAndreas Gohr * @param $extensions 109c39ae2c9SAndreas Gohr */ 110c39ae2c9SAndreas Gohr public function cmd_clone($extensions) { 111c39ae2c9SAndreas Gohr $errors = array(); 112c39ae2c9SAndreas Gohr $succeeded = array(); 113c39ae2c9SAndreas Gohr 114c39ae2c9SAndreas Gohr foreach($extensions as $ext) { 115c39ae2c9SAndreas Gohr $repo = $this->getSourceRepo($ext); 116c39ae2c9SAndreas Gohr 117c39ae2c9SAndreas Gohr if(!$repo) { 1189fb66494SAndreas Gohr $this->error("could not find a repository for $ext"); 119c39ae2c9SAndreas Gohr $errors[] = $ext; 120c39ae2c9SAndreas Gohr } else { 121c39ae2c9SAndreas Gohr if($this->cloneExtension($ext, $repo)) { 122c39ae2c9SAndreas Gohr $succeeded[] = $ext; 123c39ae2c9SAndreas Gohr } else { 124c39ae2c9SAndreas Gohr $errors[] = $ext; 125c39ae2c9SAndreas Gohr } 126c39ae2c9SAndreas Gohr } 127c39ae2c9SAndreas Gohr } 128c39ae2c9SAndreas Gohr 129c39ae2c9SAndreas Gohr echo "\n"; 1309fb66494SAndreas Gohr if($succeeded) $this->success('successfully cloned the following extensions: ' . join(', ', $succeeded)); 1319fb66494SAndreas Gohr if($errors) $this->error('failed to clone the following extensions: ' . join(', ', $errors)); 132c39ae2c9SAndreas Gohr } 133c39ae2c9SAndreas Gohr 134c39ae2c9SAndreas Gohr /** 135c39ae2c9SAndreas Gohr * Tries to install the given extensions using git clone with fallback to install 136c39ae2c9SAndreas Gohr * 137c39ae2c9SAndreas Gohr * @param $extensions 138c39ae2c9SAndreas Gohr */ 139c39ae2c9SAndreas Gohr public function cmd_install($extensions) { 140c39ae2c9SAndreas Gohr $errors = array(); 141c39ae2c9SAndreas Gohr $succeeded = array(); 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 } 153c39ae2c9SAndreas Gohr } else { 154c39ae2c9SAndreas Gohr if($this->cloneExtension($ext, $repo)) { 155c39ae2c9SAndreas Gohr $succeeded[] = $ext; 156c39ae2c9SAndreas Gohr } else { 157c39ae2c9SAndreas Gohr $errors[] = $ext; 158c39ae2c9SAndreas Gohr } 159c39ae2c9SAndreas Gohr } 160c39ae2c9SAndreas Gohr } 161c39ae2c9SAndreas Gohr 162c39ae2c9SAndreas Gohr echo "\n"; 1639fb66494SAndreas Gohr if($succeeded) $this->success('successfully installed the following extensions: ' . join(', ', $succeeded)); 1649fb66494SAndreas Gohr if($errors) $this->error('failed to install the following extensions: ' . join(', ', $errors)); 165c39ae2c9SAndreas Gohr } 166c39ae2c9SAndreas Gohr 167c39ae2c9SAndreas Gohr /** 168c39ae2c9SAndreas Gohr * Executes the given git command in every repository 169c39ae2c9SAndreas Gohr * 170c39ae2c9SAndreas Gohr * @param $cmd 171c39ae2c9SAndreas Gohr * @param $arg 172c39ae2c9SAndreas Gohr */ 173c39ae2c9SAndreas Gohr public function cmd_git($cmd, $arg) { 174c39ae2c9SAndreas Gohr $repos = $this->findRepos(); 175c39ae2c9SAndreas Gohr 176c39ae2c9SAndreas Gohr $shell = array_merge(array('git', $cmd), $arg); 177c39ae2c9SAndreas Gohr $shell = array_map('escapeshellarg', $shell); 178c39ae2c9SAndreas Gohr $shell = join(' ', $shell); 179c39ae2c9SAndreas Gohr 180c39ae2c9SAndreas Gohr foreach($repos as $repo) { 181c39ae2c9SAndreas Gohr if(!@chdir($repo)) { 1829fb66494SAndreas Gohr $this->error("Could not change into $repo"); 183c39ae2c9SAndreas Gohr continue; 184c39ae2c9SAndreas Gohr } 185c39ae2c9SAndreas Gohr 186c39ae2c9SAndreas Gohr echo "\n"; 1879fb66494SAndreas Gohr $this->info("executing $shell in $repo"); 188c39ae2c9SAndreas Gohr $ret = 0; 189c39ae2c9SAndreas Gohr system($shell, $ret); 190c39ae2c9SAndreas Gohr 191c39ae2c9SAndreas Gohr if($ret == 0) { 1929fb66494SAndreas Gohr $this->success("git succeeded in $repo"); 193c39ae2c9SAndreas Gohr } else { 1949fb66494SAndreas Gohr $this->error("git failed in $repo"); 195c39ae2c9SAndreas Gohr } 196c39ae2c9SAndreas Gohr } 197c39ae2c9SAndreas Gohr } 198c39ae2c9SAndreas Gohr 199c39ae2c9SAndreas Gohr /** 200c39ae2c9SAndreas Gohr * Simply lists the repositories 201c39ae2c9SAndreas Gohr */ 202c39ae2c9SAndreas Gohr public function cmd_repos() { 203c39ae2c9SAndreas Gohr $repos = $this->findRepos(); 204c39ae2c9SAndreas Gohr foreach($repos as $repo) { 205c39ae2c9SAndreas Gohr echo "$repo\n"; 206c39ae2c9SAndreas Gohr } 207c39ae2c9SAndreas Gohr } 208c39ae2c9SAndreas Gohr 209c39ae2c9SAndreas Gohr /** 210c39ae2c9SAndreas Gohr * Install extension from the given download URL 211c39ae2c9SAndreas Gohr * 212c39ae2c9SAndreas Gohr * @param string $ext 213c39ae2c9SAndreas Gohr * @return bool 214c39ae2c9SAndreas Gohr */ 215c39ae2c9SAndreas Gohr private function downloadExtension($ext) { 216c39ae2c9SAndreas Gohr /** @var helper_plugin_extension_extension $plugin */ 217c39ae2c9SAndreas Gohr $plugin = plugin_load('helper', 'extension_extension'); 218c39ae2c9SAndreas Gohr if(!$ext) die("extension plugin not available, can't continue"); 219c39ae2c9SAndreas Gohr $plugin->setExtension($ext); 220c39ae2c9SAndreas Gohr 221c39ae2c9SAndreas Gohr $url = $plugin->getDownloadURL(); 222c39ae2c9SAndreas Gohr if(!$url) { 2239fb66494SAndreas Gohr $this->error("no download URL for $ext"); 224c39ae2c9SAndreas Gohr return false; 225c39ae2c9SAndreas Gohr } 226c39ae2c9SAndreas Gohr 227c39ae2c9SAndreas Gohr $ok = false; 228c39ae2c9SAndreas Gohr try { 2299fb66494SAndreas Gohr $this->info("installing $ext via download from $url"); 230c39ae2c9SAndreas Gohr $ok = $plugin->installFromURL($url); 231c39ae2c9SAndreas Gohr } catch(Exception $e) { 2329fb66494SAndreas Gohr $this->error($e->getMessage()); 233c39ae2c9SAndreas Gohr } 234c39ae2c9SAndreas Gohr 235c39ae2c9SAndreas Gohr if($ok) { 2369fb66494SAndreas Gohr $this->success("installed $ext via download"); 237c39ae2c9SAndreas Gohr return true; 238c39ae2c9SAndreas Gohr } else { 2399fb66494SAndreas Gohr $this->success("failed to install $ext via download"); 240c39ae2c9SAndreas Gohr return false; 241c39ae2c9SAndreas Gohr } 242c39ae2c9SAndreas Gohr } 243c39ae2c9SAndreas Gohr 244c39ae2c9SAndreas Gohr /** 245c39ae2c9SAndreas Gohr * Clones the extension from the given repository 246c39ae2c9SAndreas Gohr * 247c39ae2c9SAndreas Gohr * @param string $ext 248c39ae2c9SAndreas Gohr * @param string $repo 249c39ae2c9SAndreas Gohr * @return bool 250c39ae2c9SAndreas Gohr */ 251c39ae2c9SAndreas Gohr private function cloneExtension($ext, $repo) { 252c39ae2c9SAndreas Gohr if(substr($ext, 0, 9) == 'template:') { 253c39ae2c9SAndreas Gohr $target = fullpath(tpl_incdir() . '../' . substr($ext, 9)); 254c39ae2c9SAndreas Gohr } else { 255c39ae2c9SAndreas Gohr $target = DOKU_PLUGIN . $ext; 256c39ae2c9SAndreas Gohr } 257c39ae2c9SAndreas Gohr 2589fb66494SAndreas Gohr $this->info("cloning $ext from $repo to $target"); 259c39ae2c9SAndreas Gohr $ret = 0; 260c39ae2c9SAndreas Gohr system("git clone $repo $target", $ret); 261c39ae2c9SAndreas Gohr if($ret === 0) { 2629fb66494SAndreas Gohr $this->success("cloning of $ext succeeded"); 263c39ae2c9SAndreas Gohr return true; 264c39ae2c9SAndreas Gohr } else { 2659fb66494SAndreas Gohr $this->error("cloning of $ext failed"); 266c39ae2c9SAndreas Gohr return false; 267c39ae2c9SAndreas Gohr } 268c39ae2c9SAndreas Gohr } 269c39ae2c9SAndreas Gohr 270c39ae2c9SAndreas Gohr /** 271c39ae2c9SAndreas Gohr * Returns all git repositories in this DokuWiki install 272c39ae2c9SAndreas Gohr * 273c39ae2c9SAndreas Gohr * Looks in root, template and plugin directories only. 274c39ae2c9SAndreas Gohr * 275c39ae2c9SAndreas Gohr * @return array 276c39ae2c9SAndreas Gohr */ 277c39ae2c9SAndreas Gohr private function findRepos() { 2789fb66494SAndreas Gohr $this->info('Looking for .git directories'); 279c39ae2c9SAndreas Gohr $data = array_merge( 280c39ae2c9SAndreas Gohr glob(DOKU_INC . '.git', GLOB_ONLYDIR), 281c39ae2c9SAndreas Gohr glob(DOKU_PLUGIN . '*/.git', GLOB_ONLYDIR), 282c39ae2c9SAndreas Gohr glob(fullpath(tpl_incdir() . '../') . '/*/.git', GLOB_ONLYDIR) 283c39ae2c9SAndreas Gohr ); 284c39ae2c9SAndreas Gohr 285c39ae2c9SAndreas Gohr if(!$data) { 2869fb66494SAndreas Gohr $this->error('Found no .git directories'); 287c39ae2c9SAndreas Gohr } else { 2889fb66494SAndreas Gohr $this->success('Found ' . count($data) . ' .git directories'); 289c39ae2c9SAndreas Gohr } 290b0c7084fSAndreas Gohr $data = array_map('fullpath', array_map('dirname', $data)); 291c39ae2c9SAndreas Gohr return $data; 292c39ae2c9SAndreas Gohr } 293c39ae2c9SAndreas Gohr 294c39ae2c9SAndreas Gohr /** 295c39ae2c9SAndreas Gohr * Returns the repository for the given extension 296c39ae2c9SAndreas Gohr * 297c39ae2c9SAndreas Gohr * @param $extension 298c39ae2c9SAndreas Gohr * @return bool|string 299c39ae2c9SAndreas Gohr */ 300c39ae2c9SAndreas Gohr private function getSourceRepo($extension) { 301c39ae2c9SAndreas Gohr /** @var helper_plugin_extension_extension $ext */ 302c39ae2c9SAndreas Gohr $ext = plugin_load('helper', 'extension_extension'); 303c39ae2c9SAndreas Gohr if(!$ext) die("extension plugin not available, can't continue"); 304c39ae2c9SAndreas Gohr $ext->setExtension($extension); 305c39ae2c9SAndreas Gohr 30668cf024bSAndreas Gohr $repourl = $ext->getSourcerepoURL(); 30768cf024bSAndreas Gohr if(!$repourl) return false; 30868cf024bSAndreas Gohr 309c39ae2c9SAndreas Gohr // match github repos 31068cf024bSAndreas Gohr if(preg_match('/github\.com\/([^\/]+)\/([^\/]+)/i', $repourl, $m)) { 311c39ae2c9SAndreas Gohr $user = $m[1]; 312c39ae2c9SAndreas Gohr $repo = $m[2]; 313c39ae2c9SAndreas Gohr return 'https://github.com/' . $user . '/' . $repo . '.git'; 314c39ae2c9SAndreas Gohr } 315c39ae2c9SAndreas Gohr 316c39ae2c9SAndreas Gohr // match gitorious repos 31768cf024bSAndreas Gohr if(preg_match('/gitorious.org\/([^\/]+)\/([^\/]+)?/i', $repourl, $m)) { 318c39ae2c9SAndreas Gohr $user = $m[1]; 319c39ae2c9SAndreas Gohr $repo = $m[2]; 320c39ae2c9SAndreas Gohr if(!$repo) $repo = $user; 321c39ae2c9SAndreas Gohr 322c39ae2c9SAndreas Gohr return 'https://git.gitorious.org/' . $user . '/' . $repo . '.git'; 323c39ae2c9SAndreas Gohr } 324c39ae2c9SAndreas Gohr 325c39ae2c9SAndreas Gohr // match bitbucket repos - most people seem to use mercurial there though 32668cf024bSAndreas Gohr if(preg_match('/bitbucket\.org\/([^\/]+)\/([^\/]+)/i', $repourl, $m)) { 327c39ae2c9SAndreas Gohr $user = $m[1]; 328c39ae2c9SAndreas Gohr $repo = $m[2]; 329c39ae2c9SAndreas Gohr return 'https://bitbucket.org/' . $user . '/' . $repo . '.git'; 330c39ae2c9SAndreas Gohr } 331c39ae2c9SAndreas Gohr 332c39ae2c9SAndreas Gohr return false; 333c39ae2c9SAndreas Gohr } 334c39ae2c9SAndreas Gohr} 335c39ae2c9SAndreas Gohr 336c39ae2c9SAndreas Gohr 3379fb66494SAndreas Gohr$GitToolCLI = new GitToolCLI(); 3389fb66494SAndreas Gohr$GitToolCLI->run();