Permalink
Browse files

NRFE-140: split up into classes

  • Loading branch information...
1 parent 74f2914 commit 9b55cefdf9ea56a4e3c192fc6fae58f4dde1e667 @cweiske cweiske committed Dec 29, 2011
View
24 README.rst
@@ -11,6 +11,30 @@ Deployed eine ``README.rst``-Datei in ein Wiki, z.B. Confluence
:confluence-page: aida_rsttest
+===========
+Einrichtung
+===========
+::
+
+ $ cp config.php.dist config.php
+ $ emacs config.php
+ .. change user and password
+
+
+===========================
+Meta-Angaben in rST-Dateien
+===========================
+Man kann in den .rst-Dateien Meta-Angaben hinterlegen, damit man die Parameter
+nicht alle auf der Kommandozeile angeben muss.
+
+Beispiel::
+
+ .. meta::
+ :deploy-target: confluence
+ :confluence-host: http://confluence.example.org
+ :confluence-space: IT
+ :confluence-page: rstpagetest
+
==========
Verwendung
==========
View
11 config.php.dist
@@ -1,9 +1,6 @@
<?php
-$cflHost = 'http://FIXME';
-$cflUser = 'FIXME';
-$cflPass = 'FIXME';
-$cflSpace = 'FIXME';
-$cflTitle = 'FIXME';
-$markerBegin = "{html}<!-- BEGIN deploy-content -->{html}\n";
-$markerEnd = "{html}<!-- END deploy-content -->{html}\n";
+$options = array(
+ 'user' => 'FIXME',
+ 'password' => 'FIXME'
+);
?>
View
117 deploy-rst.php
@@ -1,114 +1,17 @@
#!/usr/bin/env php
<?php
-require_once __DIR__ . '/config.php';
-
-function err($msg)
+function __autoload($class)
{
- file_put_contents("php://stderr", $msg . "\n");
-}
-
-function run($cmd)
-{
- $handle = popen($cmd, 'r');
- if (!is_resource($handle)) {
- return array('', 1);
- }
-
- $output = stream_get_contents($handle);
- $retval = pclose($handle);
-
- return array($output, $retval);
-}
-
-if ($argc <= 1) {
- err('Please pass a file name');
- exit(1);
-}
-$file = $argv[1];
-if (!file_exists($file)) {
- err('File does not exist');
- exit(2);
-}
-
-require_once 'System.php';
-
-$cmdRst2c = System::which('rst2confluence');
-if ($cmdRst2c === false) {
- err('rst2confluence not found');
- exit(2);
+ $file = str_replace(array('_', '\\'), '/', $class) . '.php';
+ require_once $file;
}
-$cmdCli = System::which('confluence-cli');
-if ($cmdCli === false) {
- err('confluence-cli not found');
- exit(2);
+if (is_dir(__DIR__ . '/src/netresearch/DeployRst')) {
+ set_include_path(
+ get_include_path() . PATH_SEPARATOR . __DIR__ . '/src'
+ );
}
-list($rcDoc, $retval) = run($cmdRst2c . ' ' . escapeshellarg($file));
-if ($retval !== 0) {
- err('Error converting rst to confluence format');
- exit(20);
-}
-
-//we cannot pipe it, see https://studio.plugins.atlassian.com/browse/CSOAP-122
-$tmpfile = tempnam(sys_get_temp_dir(), 'deploy-confluence-');
-$cmd = sprintf(
- 'confluence-cli --server %s --user %s --password %s --action getPageSource --space %s --title %s --file %s --quiet',
- escapeshellarg($cflHost),
- escapeshellarg($cflUser),
- escapeshellarg($cflPass),
- escapeshellarg($cflSpace),
- escapeshellarg($cflTitle),
- escapeshellarg($tmpfile)
-);
-list($lastline, $retval) = run($cmd);
-$curDoc = file_get_contents($tmpfile);
-unlink($file);
-
-//list($curDoc, $retval) = run($cmd);
-if ($retval !== 0) {
- err('Error fetching confluence document source' . "\n" . $lastline);
- exit(21);
-}
-if (strlen($curDoc) == 0) {
- err('Document is empty. This might be a bug');
- exit(22);
-}
-
-$begin = strpos($curDoc, $markerBegin);
-$end = strpos($curDoc, $markerEnd);
-
-if ($begin === false && $end === false) {
- //add it to the end
- $newDoc = $curDoc . "\n\n" . $markerBegin . $rcDoc . $markerEnd;
-} else if ($begin === false || $end === false) {
- err('Begin or end marker not found');
- exit(23);
-} else if ($end < $begin) {
- err('Begin marker after end marker');
- exit(24);
-} else {
- //replace it
- $newDoc = substr($curDoc, 0, $begin)
- . $markerBegin . $rcDoc . $markerEnd
- . substr($curDoc, $end + strlen($markerEnd));
-}
-
-
-//we cannot pipe because of https://studio.plugins.atlassian.com/browse/CSOAP-121
-$file = tempnam(sys_get_temp_dir(), 'deploy-confluence-');
-file_put_contents($file, $newDoc);
-$cmd = sprintf(
- 'confluence-cli --server %s --user %s --password %s --action storePage --space %s --title %s --file %s --quiet',
- escapeshellarg($cflHost),
- escapeshellarg($cflUser),
- escapeshellarg($cflPass),
- escapeshellarg($cflSpace),
- escapeshellarg($cflTitle),
- escapeshellarg($file)
-);
-list($lastline, $retval) = run($cmd);
-if ($retval !== 0) {
- err('Error storing new document in confluence' . "\n" . $lastline);
- exit(30);
-}
+require_once __DIR__ . '/config.php';
+$cli = new netresearch\DeployRst\Cli($options);
+$cli->run();
?>
View
61 src/netresearch/DeployRst/Cli.php
@@ -0,0 +1,61 @@
+<?php
+namespace netresearch\DeployRst;
+
+class Cli
+{
+ public $file;
+ public $metas;
+ public $options;
+
+ public function __construct($options = array())
+ {
+ $this->options = $options;
+ }
+
+ public function run()
+ {
+ try {
+ $this->loadParams();
+ $this->loadMeta();
+ $this->runDriver();
+ } catch (Exception $e) {
+ echo $e->getMessage() . "\n";
+ exit($e->getCode());
+ }
+ }
+
+ protected function loadParams()
+ {
+ if ($GLOBALS['argc'] <= 1) {
+ throw new Exception('Please pass a file name', 1);
+ }
+ $this->file = $GLOBALS['argv'][1];
+ if (!file_exists($this->file)) {
+ throw new Exception('File does not exist', 2);
+ }
+ }
+
+ protected function loadMeta()
+ {
+ $this->metas = Rst::extractMeta($this->file);
+ if (!isset($this->metas['deploy-target'])) {
+ throw new Exception('No deploy-target meta directive found', 3);
+ }
+ }
+
+ protected function runDriver()
+ {
+ $class = '\\netresearch\DeployRst\\Driver_' . ucfirst($this->metas['deploy-target']);
+ if (!class_exists($class)) {
+ throw new Exception(
+ 'No wiki driver found for target ' . $this->metas['deploy-target']
+ );
+ }
+
+ $driver = new $class($this->file, $this->metas, $this->options);
+ $driver->run();
+ }
+
+}
+
+?>
View
27 src/netresearch/DeployRst/Driver.php
@@ -0,0 +1,27 @@
+<?php
+namespace netresearch\DeployRst;
+
+class Driver
+{
+ protected function loadSetting($name)
+ {
+ if (isset($this->options[$name])) {
+ return $this->options[$name];
+ }
+ if (isset($this->metas[$name])) {
+ return $this->metas[$name];
+ }
+
+ throw new Exception(
+ 'Required setting "' . $name . '" not found.' . "\n"
+ . 'Add it as meta variable in your rST file like' . "\n"
+ . ".. meta::\n"
+ . ' :' . $name . ": value-of-$name\n"
+ . 'or pass it as command line option',
+ 100
+ );
+ }
+
+}
+
+?>
View
159 src/netresearch/DeployRst/Driver/Confluence.php
@@ -0,0 +1,159 @@
+<?php
+namespace netresearch\DeployRst;
+
+class Driver_Confluence extends Driver
+{
+ protected $options;
+ protected $file;
+ protected $metas;
+
+ protected $cflHost;
+ protected $cflUser;
+ protected $cflPass;
+ protected $cflSpace;
+ protected $cflPage;
+
+ public $markerBegin = "{html}<!-- BEGIN deploy-content -->{html}\n";
+ public $markerEnd = "{html}<!-- END deploy-content -->{html}\n";
+
+
+ public function __construct($file, $metas, $options)
+ {
+ $this->options = $options;
+ $this->file = $file;
+ $this->metas = $metas;
+ $this->loadTools();
+ $this->loadParameters();
+ }
+
+ public function run()
+ {
+ $this->storePage(
+ $this->embedIntoPage(
+ $this->getCurrentPage(),
+ $this->convertRst()
+ )
+ );
+ }
+
+ public function loadTools()
+ {
+ require_once 'System.php';
+
+ $this->cmd['rst2c'] = \System::which('rst2confluence');
+ if ($this->cmd['rst2c'] === false) {
+ throw new Exception('rst2confluence not found', 10);
+ }
+ $this->cmd['cflcli'] = \System::which('confluence-cli');
+ if ($this->cmd['cflcli'] === false) {
+ throw new Exception('confluence-cli not found', 11);
+ }
+ }
+
+ protected function loadParameters()
+ {
+ $this->cflHost = $this->loadSetting('confluence-host');
+ $this->cflSpace = $this->loadSetting('confluence-space');
+ $this->cflPage = $this->loadSetting('confluence-page');
+ $this->cflUser = $this->loadSetting('user');
+ $this->cflPass = $this->loadSetting('password');
+ }
+
+
+ public function convertRst()
+ {
+ list($rcDoc, $retval) = Exec::run(
+ $this->cmd['rst2c'] . ' ' . escapeshellarg($this->file)
+ );
+ if ($retval !== 0) {
+ throw new Exception('Error converting rst to confluence format', 20);
+ }
+
+ return $rcDoc;
+ }
+
+ public function getCurrentPage()
+ {
+ //we cannot pipe it, see https://studio.plugins.atlassian.com/browse/CSOAP-122
+ $tmpfile = tempnam(sys_get_temp_dir(), 'deploy-confluence-');
+ $cmd = sprintf(
+ $this->cmd['cflcli']
+ . ' --server %s --user %s --password %s'
+ . ' --action getPageSource --space %s --title %s --file %s --quiet',
+ escapeshellarg($this->cflHost),
+ escapeshellarg($this->cflUser),
+ escapeshellarg($this->cflPass),
+ escapeshellarg($this->cflSpace),
+ escapeshellarg($this->cflPage),
+ escapeshellarg($tmpfile)
+ );
+ list($lastline, $retval) = Exec::run($cmd);
+ $curDoc = file_get_contents($tmpfile);
+ unlink($tmpfile);
+
+ //list($curDoc, $retval) = run($cmd);
+ if ($retval !== 0) {
+ throw new Exception(
+ 'Error fetching confluence document source' . "\n" . $lastline,
+ 21
+ );
+ }
+ if (strlen($curDoc) == 0) {
+ throw new Exception('Document is empty. This might be a bug', 22);
+ }
+
+ return $curDoc;
+ }
+
+
+ public function embedIntoPage($curDoc, $newCont)
+ {
+ $begin = strpos($curDoc, $this->markerBegin);
+ $end = strpos($curDoc, $this->markerEnd);
+
+ if ($begin === false && $end === false) {
+ //add it to the end
+ $newDoc = $curDoc . "\n\n"
+ . $this->markerBegin . $newCont . $this->markerEnd;
+ } else if ($begin === false || $end === false) {
+ throw new Exception('Begin or end marker not found', 23);
+ } else if ($end < $begin) {
+ throw new Exception('Begin marker after end marker', 24);
+ } else {
+ //replace it
+ $newDoc = substr($curDoc, 0, $begin)
+ . $this->markerBegin . $newCont . $this->markerEnd
+ . substr($curDoc, $end + strlen($this->markerEnd));
+ }
+
+ return $newDoc;
+ }
+
+ public function storePage($newDoc)
+ {
+ //we cannot pipe because of https://studio.plugins.atlassian.com/browse/CSOAP-121
+ $tmpfile = tempnam(sys_get_temp_dir(), 'deploy-confluence-');
+ file_put_contents($tmpfile, $newDoc);
+ $cmd = sprintf(
+ $this->cmd['cflcli']
+ . ' --server %s --user %s --password %s'
+ . ' --action storePage --space %s --title %s --file %s --quiet',
+ escapeshellarg($this->cflHost),
+ escapeshellarg($this->cflUser),
+ escapeshellarg($this->cflPass),
+ escapeshellarg($this->cflSpace),
+ escapeshellarg($this->cflPage),
+ escapeshellarg($tmpfile)
+ );
+ list($lastline, $retval) = Exec::run($cmd);
+
+ unlink($tmpfile);
+ if ($retval !== 0) {
+ throw new Exception(
+ 'Error storing new document in confluence' . "\n" . $lastline, 30
+ );
+ }
+ }
+}
+
+?>
View
8 src/netresearch/DeployRst/Exception.php
@@ -0,0 +1,8 @@
+<?php
+namespace netresearch\DeployRst;
+
+class Exception extends \Exception
+{
+}
+
+?>
View
21 src/netresearch/DeployRst/Exec.php
@@ -0,0 +1,21 @@
+<?php
+namespace netresearch\DeployRst;
+
+class Exec
+{
+ public static function run($cmd)
+ {
+ $handle = popen($cmd, 'r');
+ if (!is_resource($handle)) {
+ return array('', 1);
+ }
+
+ $output = stream_get_contents($handle);
+ $retval = pclose($handle);
+
+ return array($output, $retval);
+ }
+
+}
+
+?>
View
38 src/netresearch/DeployRst/Rst.php
@@ -0,0 +1,38 @@
+<?php
+namespace netresearch\DeployRst;
+
+
+class Rst
+{
+ /**
+ * Extract meta data from rST source
+ *
+ * @param string $rst rST source content
+ *
+ * @return array Meta data
+ */
+ public static function extractMeta($file)
+ {
+ $lines = file($file);
+ $inmeta = false;
+ $metas = array();
+ foreach ($lines as $line) {
+ $tline = trim($line);
+ if ($inmeta) {
+ $parts = explode(':', $tline, 3);
+ if (count($parts) != 3 || $parts[0] != '') {
+ break;
+ }
+ $metas[trim($parts[1])] = trim($parts[2]);
+ }
+ if ($tline == '.. meta::') {
+ $inmeta = true;
+ }
+ }
+
+ return $metas;
+ }
+
+}
+
+?>

0 comments on commit 9b55cef

Please sign in to comment.