changeset 479:86ee2b42a995

Move CLI helper functions to msitegen, again.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 07 Dec 2013 10:40:22 +0200
parents 8dde27202989
children 26033a4b754a
files createdb.php msitegen.inc.php
diffstat 2 files changed, 91 insertions(+), 72 deletions(-) [+]
line wrap: on
line diff
--- a/createdb.php	Sat Dec 07 10:18:52 2013 +0200
+++ b/createdb.php	Sat Dec 07 10:40:22 2013 +0200
@@ -3,13 +3,7 @@
 require_once "mconfig.inc.php";
 require_once "msite.inc.php";
 
-
-// Check if we are running from commandline or not
-if (php_sapi_name() != "cli" || !empty($_SERVER["REMOTE_ADDR"]))
-{
-  header("Status: 404 Not Found");
-  die();
-}
+stCheckCLIExec();
 
 $dbVersion = 10;
 
@@ -278,70 +272,6 @@
 //
 // Helper functions
 //
-function stCArg($index)
-{
-  global $argc, $argv;
-  if ($index < $argc)
-    return $argv[$index];
-  else
-    return FALSE;
-}
-
-function stCArgLC($index)
-{
-  global $argc, $argv;
-  if ($index < $argc)
-    return strtolower($argv[$index]);
-  else
-    return FALSE;
-}
-
-
-function stCSQLError($sql)
-{
-  global $db;
-  die("Error executing SQL query: ".implode("; ", $db->errorInfo())." in statement \"".$sql."\"\n");
-  exit;
-}
-
-
-function stConnectDB($dbspec)
-{
-  global $db;
-  try {
-    $db = new PDO($dbspec);
-  }
-  catch (PDOException $e) {
-    error_log("Could not connect to SQL database '".$dbspec."': ".$e->getMessage().".");
-    return FALSE;
-  }
-  return TRUE;
-}
-
-
-function stGetDBMeta($name)
-{
-  global $db;
-
-  if (($item = stFetchSQL("SELECT * FROM dbmeta WHERE key=".$db->quote($name))) === FALSE)
-    return FALSE;
-  
-  return stGetSQLSettingData($item);
-}
-
-
-function stSetDBMeta($name, $value)
-{
-  global $db;
-
-  if (($item = stFetchSQL("SELECT * FROM dbmeta WHERE key=".$db->quote($name))) === FALSE)
-    return FALSE;
-
-  $sql = "UPDATE dbmeta SET ".stGetSettingSQL($item, $value)." WHERE key=".$db->quote($name);
-  return stExecSQL($sql);
-}
-
-
 function stCreateOneTable($name, $schema)
 {
   return (stExecSQL("CREATE TABLE IF NOT EXISTS ".$name." (".$schema.")") !== FALSE) ? TRUE : FALSE;
@@ -481,7 +411,7 @@
 
 
 // Try to connect to database
-if (!stConnectDB($spec))
+if (!stCLIConnectSQLDB($spec))
   die("Could not connect to SQL database '".$spec."'.\n");
 
 echo "Using database spec '".$spec."'.\n";
--- a/msitegen.inc.php	Sat Dec 07 10:18:52 2013 +0200
+++ b/msitegen.inc.php	Sat Dec 07 10:40:22 2013 +0200
@@ -676,4 +676,93 @@
   return $str;
 }
 
+
+//
+// CLI related helper functions
+//
+
+// Check if we are running from commandline or not
+function stCheckCLIExec($fail = TRUE)
+{
+  if (php_sapi_name() != "cli" || !empty($_SERVER["REMOTE_ADDR"]))
+  {
+    if ($fail)
+    {
+      header("Status: 404 Not Found");
+      die();
+    }
+    else
+      return TRUE;
+  }
+  else
+    return FALSE;
+}
+
+
+function stCArg($index)
+{
+  global $argc, $argv;
+  if ($index < $argc)
+    return $argv[$index];
+  else
+    return FALSE;
+}
+
+
+function stCArgLC($index)
+{
+  global $argc, $argv;
+  if ($index < $argc)
+    return strtolower($argv[$index]);
+  else
+    return FALSE;
+}
+
+
+function stCSQLError($sql)
+{
+  global $db;
+  die("Error executing SQL query: ".implode("; ", $db->errorInfo())." in statement \"".$sql."\"\n");
+  exit;
+}
+
+
+function stCLIConnectSQLDB($dbspec)
+{
+  global $db;
+  try {
+    $db = new PDO($dbspec);
+  }
+  catch (PDOException $e) {
+    error_log("Could not connect to SQL database '".$dbspec."': ".$e->getMessage().".");
+    return FALSE;
+  }
+  return TRUE;
+}
+
+
+function stGetDBMeta($name)
+{
+  global $db;
+
+  if (($item = stFetchSQL("SELECT * FROM dbmeta WHERE key=".$db->quote($name))) === FALSE)
+    return FALSE;
+  
+  return stGetSQLSettingData($item);
+}
+
+
+function stSetDBMeta($name, $value)
+{
+  global $db;
+
+  if (($item = stFetchSQL("SELECT * FROM dbmeta WHERE key=".$db->quote($name))) === FALSE)
+    return FALSE;
+
+  $sql = "UPDATE dbmeta SET ".stGetSettingSQL($item, $value)." WHERE key=".$db->quote($name);
+  return stExecSQL($sql);
+}
+
+
+
 ?>
\ No newline at end of file