Permalink
Browse files

Added a tool that clones all Phorum repositories to a local directory.

  • Loading branch information...
1 parent fcfa132 commit ef6c437cc9ce768e038e1d9ca645a59293d2029a @mmakaay mmakaay committed Apr 12, 2012
Showing with 152 additions and 0 deletions.
  1. +9 −0 README
  2. +94 −0 bin/clone_all_repositories
  3. +49 −0 lib/Phorum/Git.php
View
@@ -24,3 +24,12 @@ more in the future.
@DESCRIPTION@ the description from info.txt, word wrapped at 72 chars,
stripped HTML tags
+
+System requirements for using the tools (the names that were used
+are Ubuntu package names):
+
+ * php5-cli
+ * php5-curl
+ * zip
+ * git
+
@@ -0,0 +1,94 @@
+#!/usr/bin/env php
+<?php
+
+require_once dirname(__FILE__) . '/../lib/Phorum/Git.php';
+
+// ----------------------------------------------------------------------
+// Parse arguments
+// ----------------------------------------------------------------------
+
+$args = array();
+
+$verbose = FALSE;
+$export_dir = "/tmp";
+
+for ($i = 1; $i < $_SERVER['argc']; $i++)
+{
+ $opt = $_SERVER['argv'][$i];
+
+ // Process options.
+ switch ($opt)
+ {
+ case "-h":
+ usage();
+ exit;
+ break;
+
+ case "-v":
+ $verbose = TRUE;
+ break;
+
+ case "-d":
+ if (!isset($_SERVER['argv'][++$i])) {
+ die("Missing value for option -d!\n");
+ }
+ $export_dir = $_SERVER['argv'][$i];
+ if (!is_dir($export_dir)) {
+ die("$export_dir: export dir does not exist");
+ }
+ break;
+
+ default:
+ die("Unknown option: $opt\n");
+ }
+}
+
+// ----------------------------------------------------------------------
+// Export the Phorum repositories
+// ----------------------------------------------------------------------
+
+try
+{
+ if ($verbose) print "> Retrieving repository list ...\n";
+ $git = new Phorum_Git();
+ $repos= $git->getRepositories();
+ if ($verbose) print "> Number of repositories: " . count($repos) . "\n";
+
+ if ($verbose) print "> Cloning repositories ...\n";
+ foreach ($repos as $r)
+ {
+ if ($verbose) print "----- $r->name -----\n";
+
+ if (is_dir("$export_dir/$r->name"))
+ {
+ if ($verbose) print "SKIP, target already exists\n";
+ }
+ else
+ {
+ $ssh = "git@github.com:Phorum/" . escapeshellarg($r->name) . ".git";
+ system("cd $export_dir; git clone $ssh");
+ }
+ }
+}
+catch (Exception $e)
+{
+ die("Error: " . $e->getMessage() . "\n");
+}
+
+// ----------------------------------------------------------------------
+// Utility functions
+// ----------------------------------------------------------------------
+
+function usage()
+{
+ $program = basename($_SERVER['argv'][0]);
+
+ print "\n";
+ print "Usage: $program [OPTIONS]\n";
+ print "\n";
+ print " -h : Show this help.\n";
+ print " -v : Enable verbose output.\n";
+ print " -d <path> : The directory for cloning the Phorum git\n";
+ print " repositories to. By default, /tmp is used.\n";
+ print "\n";
+}
View
@@ -0,0 +1,49 @@
+<?php
+/**
+ * This script implements the Phorum_Git class.
+ *
+ * @author Maurice Makaay
+ * @copyright Phorum
+ * @package Phorum
+ * @category DevTools
+ */
+
+/**
+ * The Phorum_Git class.
+ *
+ * This class provides functionality for communicating with the
+ * Phorum github repositories.
+ *
+ * @package Phorum
+ */
+class Phorum_Git
+{
+ protected $_gitapi = "https://github.com/api/v2/json/";
+
+ protected function _api_get($request)
+ {
+ $url = $this->_gitapi . $request;
+ $ch = curl_init($url);
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
+ $output = curl_exec($ch);
+ curl_close($ch);
+
+ if (!$output) throw new Exception(
+ "HTTP request for URL '$url' failed");
+
+ $data = json_decode($output);
+ if ($data === NULL) throw new Exception(
+ "Could not decode JSON response for URL '$url'");
+
+ return $data;
+ }
+
+ /**
+ * Retrieve a list of Phorum repositories from github.
+ */
+ public function getRepositories()
+ {
+ return $this->_api_get("repos/show/Phorum")->repositories;
+ }
+}
+

0 comments on commit ef6c437

Please sign in to comment.