<?php
/*
* Copyright 2008 Blandware (http://www.blandware.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* This file is used to init DB (for any DBMS).
*
* @package AtleapLite
* @author Roman Puchkovskiy
* @author Rinat Bikov
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
*/
/**
*/
require 'server_options.php';
/**
* Including common entry file header
*/
require 'include/entry_common.php';
require_once 'MDB2.php';
require_once 'Log.php';
/**
* Initialization of log object
*/
$logLevel = PEAR_LOG_INFO;
$logFileName = 'logs/atleaplite_script.log';
$log = &Log::singleton('file', $logFileName, '',array(), $logLevel);
/**
* Connects to MDB2.
*
* @global string DSN to use
* @return object MDB2 instance
*/
function &connect() {
global $dsn;
$mdb2 =& MDB2::factory($dsn);
if (PEAR::isError($mdb2)) {
die($mdb2->getMessage());
}
return $mdb2;
}
/**
* Executes a query.
*
* @param object $mdb2 MDB2 instance
* @param string $query query
* @return mixed query result
*/
function &executeQuery($mdb2, $query) {
echo "Executing query '$query'...\n";
$res =& $mdb2->query($query);
if (PEAR::isError($res)) {
die($res->getMessage());
}
return $res;
}
/**
* Executes all queries (separated with semicolon) from a file.
*
* @global object log
* @param object $mdb2 MDB2 instance
* @param string $filename file name
*/
function executeQueriesFromFile($mdb2, $filename) {
echo "*** Going to execute queries from '$filename'\n";
global $log;
$text = file_get_contents($filename);
if ($text === false) {
die("Cannot read file '$filename'");
}
$queries = explode(";\r\n", $text);
$countQueries = count($queries);
$log->debug("executeQueriesFromFile(mdb2, filename): count of queries ';\\n' = $countQueries");
if ($countQueries <= 1){
$queries = explode(";\n", $text);
$countQueries = count($queries);
$log->debug("executeQueriesFromFile(mdb2, filename): count of queries ';\\r\\n' = $countQueries");
}
foreach ($queries as $query) {
$query = trim($query);
if ($query != '') {
executeQuery($mdb2, $query);
}
}
echo "*** Successfully executed all queries from '$filename'";
}
if (!isset($argc)) {
// refuse to execute not under CLI
exit;
}
if ($argc < 2){
die("Use command line arguments with paths of query files for executing the script 'init.php'\n");
}
$mdb2 =& connect();
for ($i = 1; $i < $argc; $i++){
executeQueriesFromFile($mdb2, $argv[$i]);
}
$mdb2->disconnect();
?>