本文整理汇总了PHP中SqlUtility::prefixQuery方法的典型用法代码示例。如果您正苦于以下问题:PHP SqlUtility::prefixQuery方法的具体用法?PHP SqlUtility::prefixQuery怎么用?PHP SqlUtility::prefixQuery使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SqlUtility
的用法示例。
在下文中一共展示了SqlUtility::prefixQuery方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: queryFromFile
function queryFromFile($sql_file_path)
{
$sqlUtility = new SqlUtility();
global $db, $progress, $errors;
$tables = array();
if (!file_exists($sql_file_path)) {
$progress[] = $sql_file_path . ': file not exists.';
return false;
}
$sql_query = trim(fread(fopen($sql_file_path, 'r'), filesize($sql_file_path)));
$sqlUtility->splitSqlFile($pieces, $sql_query);
foreach ($pieces as $piece) {
$piece = trim($piece);
// [0] contains the prefixed query
// [4] contains unprefixed table name
if ($_POST['tb_prefix'] || $_POST['tb_prefix'] == '') {
$prefixed_query = $sqlUtility->prefixQuery($piece, $_POST['tb_prefix']);
} else {
$prefixed_query = $piece;
}
if ($prefixed_query != false) {
$prefixed_query[1] = strtoupper($prefixed_query[1]);
$table = $_POST['tb_prefix'] . $prefixed_query[4];
if ($prefixed_query[1] == 'CREATE TABLE') {
if (mysql_query($prefixed_query[0], $db) !== false) {
$progress[] = 'Table <strong>' . $table . '</strong> created successfully.';
} else {
if (mysql_errno($db) == 1050) {
$progress[] = 'Table <strong>' . $table . '</strong> already exists. Skipping.';
} else {
$errors[] = 'Table <strong>' . $table . '</strong> creation failed.';
}
}
} elseif ($prefixed_query[1] == 'INSERT INTO') {
mysql_query($prefixed_query[0], $db);
} elseif ($prefixed_query[1] == 'DELETE FROM') {
mysql_query($prefixed_query[0], $db);
} elseif ($prefixed_query[1] == 'REPLACE INTO') {
mysql_query($prefixed_query[0], $db);
} elseif ($prefixed_query[1] == 'ALTER TABLE') {
if (mysql_query($prefixed_query[0], $db) !== false) {
$progress[] = 'Table <strong>' . $table . '</strong> altered successfully.';
} else {
if (mysql_errno($db) == 1060) {
$progress[] = 'Table <strong>' . $table . '</strong> fields already exists. Skipping.';
} elseif (mysql_errno($db) == 1091) {
$progress[] = 'Table <strong>' . $table . '</strong> fields already dropped. Skipping.';
} else {
$errors[] = 'Table <strong>' . $table . '</strong> alteration failed.';
}
}
} elseif ($prefixed_query[1] == 'DROP TABLE') {
mysql_query($prefixed_query[1] . ' ' . $table, $db);
} elseif ($prefixed_query[1] == 'UPDATE') {
mysql_query($prefixed_query[0], $db);
}
}
}
return true;
}
示例2: installSQLAutomatically
/**
* Executes SQL file which xoops_version of $module specifies. This
* function is usefull for installers, but it's impossible to control
* for detail.
*
* @static
* @param XoopsModule $module
* @param Legacy_ModuleInstallLog $log
* @note FOR THE CUSTOM-INSTALLER
*/
function installSQLAutomatically(&$module, &$log)
{
$sqlfileInfo =& $module->getInfo('sqlfile');
$dirname = $module->getVar('dirname');
if (!isset($sqlfileInfo[XOOPS_DB_TYPE])) {
return;
}
$sqlfile = $sqlfileInfo[XOOPS_DB_TYPE];
$sqlfilepath = XOOPS_MODULE_PATH . "/{$dirname}/{$sqlfile}";
if (isset($module->modinfo['cube_style']) && $module->modinfo['cube_style'] == true) {
require_once XOOPS_MODULE_PATH . "/legacy/admin/class/Legacy_SQLScanner.class.php";
$scanner = new Legacy_SQLScanner();
$scanner->setDB_PREFIX(XOOPS_DB_PREFIX);
$scanner->setDirname($module->get('dirname'));
if (!$scanner->loadFile($sqlfilepath)) {
$log->addError(XCube_Utils::formatMessage(_AD_LEGACY_ERROR_SQL_FILE_NOT_FOUND, $sqlfile));
return false;
}
$scanner->parse();
$sqls = $scanner->getSQL();
$root =& XCube_Root::getSingleton();
$db =& $root->mController->getDB();
//
// TODO The following variable exists for rollback, but it is not implemented.
//
foreach ($sqls as $sql) {
if (!$db->query($sql)) {
$log->addError($db->error());
return;
}
}
$log->addReport(_AD_LEGACY_MESSAGE_DATABASE_SETUP_FINISHED);
} else {
require_once XOOPS_ROOT_PATH . '/class/database/sqlutility.php';
$reservedTables = array('avatar', 'avatar_users_link', 'block_module_link', 'xoopscomments', 'config', 'configcategory', 'configoption', 'image', 'imagebody', 'imagecategory', 'imgset', 'imgset_tplset_link', 'imgsetimg', 'groups', 'groups_users_link', 'group_permission', 'online', 'bannerclient', 'banner', 'bannerfinish', 'priv_msgs', 'ranks', 'session', 'smiles', 'users', 'newblocks', 'modules', 'tplfile', 'tplset', 'tplsource', 'xoopsnotifications');
$root =& XCube_Root::getSingleton();
$db =& $root->mController->mDB;
$sql_query = fread(fopen($sqlfilepath, 'r'), filesize($sqlfilepath));
$sql_query = trim($sql_query);
SqlUtility::splitMySqlFile($pieces, $sql_query);
$created_tables = array();
foreach ($pieces as $piece) {
// [0] contains the prefixed query
// [4] contains unprefixed table name
$prefixed_query = SqlUtility::prefixQuery($piece, $db->prefix());
if (!$prefixed_query) {
$log->addError("{$piece} is not a valid SQL!");
return;
}
// check if the table name is reserved
if (!in_array($prefixed_query[4], $reservedTables)) {
// not reserved, so try to create one
if (!$db->query($prefixed_query[0])) {
$log->addError($db->error());
return;
} else {
if (!in_array($prefixed_query[4], $created_tables)) {
$log->addReport(' Table ' . $db->prefix($prefixed_query[4]) . ' created.');
$created_tables[] = $prefixed_query[4];
} else {
$log->addReport(' Data inserted to table ' . $db->prefix($prefixed_query[4]));
}
}
} else {
// the table name is reserved, so halt the installation
$log->addError($prefixed_query[4] . " is a reserved table!");
return;
}
}
}
}
示例3: executeSQL
/**
* Execute module's SQL file - if any
*
* @return bool
*/
function executeSQL()
{
$error = false;
$sqlfile =& $this->getInfo('sqlfile');
if ($sqlfile != false && is_array($sqlfile)) {
$reservedTables = array('avatar', 'avatar_users_link', 'block_module_link', 'xoopscomments', 'config', 'configcategory', 'configoption', 'image', 'imagebody', 'imagecategory', 'imgset', 'imgset_tplset_link', 'imgsetimg', 'groups', 'groups_users_link', 'group_permission', 'online', 'bannerclient', 'banner', 'bannerfinish', 'ranks', 'session', 'smiles', 'users', 'newblocks', 'modules', 'tplfile', 'tplset', 'tplsource', 'xoopsnotifications', 'banner', 'bannerclient', 'bannerfinish');
$sql_file_path = XOOPS_ROOT_PATH . "/modules/" . $this->getVar('dirname') . "/" . $sqlfile[XOOPS_DB_TYPE];
if (!file_exists($sql_file_path)) {
$this->setErrors("SQL file not found at <b>{$sql_file_path}</b>");
$error = true;
} else {
$this->setMessage("SQL file found at <b>{$sql_file_path}</b>.<br /> Creating tables...");
include_once XOOPS_ROOT_PATH . '/class/database/sqlutility.php';
$sql_query = fread(fopen($sql_file_path, 'r'), filesize($sql_file_path));
$sql_query = trim($sql_query);
SqlUtility::splitMySqlFile($pieces, $sql_query);
$created_tables = array();
foreach ($pieces as $piece) {
// [0] contains the prefixed query
// [4] contains unprefixed table name
$prefixed_query = SqlUtility::prefixQuery($piece, $GLOBALS['xoopsDB']->prefix());
if (!$prefixed_query) {
$this->setErrors("<b>{$piece}</b> is not a valid SQL!");
$error = true;
break;
}
// check if the table name is reserved
if (!in_array($prefixed_query[4], $reservedTables)) {
// not reserved, so try to create one
if (!$GLOBALS['xoopsDB']->query($prefixed_query[0])) {
$this->setErrors($GLOBALS['xoopsDB']->error());
$error = true;
break;
} else {
if (!in_array($prefixed_query[4], $created_tables)) {
$this->setMessage(' Table <b>' . $GLOBALS['xoopsDB']->prefix($prefixed_query[4]) . '</b> created.');
$created_tables[] = $prefixed_query[4];
} else {
$this->setMessage(' Data inserted to table <b>' . $GLOBALS['xoopsDB']->prefix($prefixed_query[4]) . '</b>.');
}
}
} else {
// the table name is reserved, so halt the installation
$this->setErrors('<b>' . $prefixed_query[4] . "</b> is a reserved table!");
$error = true;
break;
}
}
// if there was an error, delete the tables created so far, so the next installation will not fail
if ($error == true) {
foreach ($created_tables as $ct) {
//echo $ct;
$GLOBALS['xoopsDB']->query("DROP TABLE " . $GLOBALS['xoopsDB']->prefix($ct));
}
}
}
}
return $error;
}
示例4: queryFromFile
function queryFromFile($sql_file_path, $table_prefix)
{
global $db, $progress, $errors;
$tables = array();
if (!file_exists($sql_file_path))
return false;
$sql_query = trim(fread(fopen($sql_file_path, 'r'), filesize($sql_file_path)));
SqlUtility::splitSqlFile($pieces, $sql_query);
foreach ($pieces as $piece)
{
$piece = trim($piece);
// [0] contains the prefixed query
// [4] contains unprefixed table name
if ($table_prefix || ($table_prefix == ''))
$prefixed_query = SqlUtility::prefixQuery($piece, $table_prefix);
else
$prefixed_query = $piece;
if ($prefixed_query != false )
{
$table = $table_prefix.$prefixed_query[4];
if($prefixed_query[1] == 'CREATE TABLE')
{
if (mysql_query($prefixed_query[0],$db) !== false)
$progress[] = 'Table <b>'.$table . '</b> created successfully.';
else
{
if (mysql_errno($db) == 1050)
$progress[] = 'Table <b>'.$table . '</b> already exists. Skipping.';
else
$errors[] = 'Table <b>' . $table . '</b> creation failed.';
}
}
elseif($prefixed_query[1] == 'INSERT INTO')
mysql_query($prefixed_query[0],$db);
elseif($prefixed_query[1] == 'REPLACE INTO')
mysql_query($prefixed_query[0],$db);
elseif($prefixed_query[1] == 'ALTER TABLE')
mysql_query($prefixed_query[0],$db);
elseif($prefixed_query[1] == 'DROP TABLE')
mysql_query($prefixed_query[1] . ' ' .$table,$db);
}
}
return TRUE;
}
示例5: ucfirst
function pico_oninstall_base($module, $mydirname)
{
// transations on module install
global $ret;
// TODO :-D
// for Cube 2.1
if (defined('XOOPS_CUBE_LEGACY')) {
$root =& XCube_Root::getSingleton();
$root->mDelegateManager->add('Legacy.Admin.Event.ModuleInstall.' . ucfirst($mydirname) . '.Success', 'pico_message_append_oninstall');
$ret = array();
} else {
if (!is_array($ret)) {
$ret = array();
}
}
$db =& Database::getInstance();
$mid = $module->getVar('mid');
// TABLES (loading mysql.sql)
$sql_file_path = dirname(__FILE__) . '/sql/mysql.sql';
$prefix_mod = $db->prefix() . '_' . $mydirname;
if (file_exists($sql_file_path)) {
$ret[] = "SQL file found at <b>" . htmlspecialchars($sql_file_path) . "</b>.<br /> Creating tables...";
if (file_exists(XOOPS_ROOT_PATH . '/class/database/oldsqlutility.php')) {
include_once XOOPS_ROOT_PATH . '/class/database/oldsqlutility.php';
$sqlutil = new OldSqlUtility();
} else {
include_once XOOPS_ROOT_PATH . '/class/database/sqlutility.php';
$sqlutil = new SqlUtility();
}
$sql_query = trim(file_get_contents($sql_file_path));
$sqlutil->splitMySqlFile($pieces, $sql_query);
$created_tables = array();
if (is_array($pieces)) {
foreach ($pieces as $piece) {
$prefixed_query = $sqlutil->prefixQuery($piece, $prefix_mod);
if (!$prefixed_query) {
$ret[] = "Invalid SQL <b>" . htmlspecialchars($piece) . "</b><br />";
return false;
}
if (!$db->query($prefixed_query[0])) {
$ret[] = '<b>' . htmlspecialchars($db->error()) . '</b><br />';
//var_dump( $db->error() ) ;
return false;
} else {
if (!in_array($prefixed_query[4], $created_tables)) {
$ret[] = 'Table <b>' . htmlspecialchars($prefix_mod . '_' . $prefixed_query[4]) . '</b> created.<br />';
$created_tables[] = $prefixed_query[4];
} else {
$ret[] = 'Data inserted to table <b>' . htmlspecialchars($prefix_mod . '_' . $prefixed_query[4]) . '</b>.</br />';
}
}
}
}
}
// TEMPLATES
$tplfile_handler =& xoops_gethandler('tplfile');
$tpl_path = dirname(__FILE__) . '/templates';
if ($handler = @opendir($tpl_path . '/')) {
while (($file = readdir($handler)) !== false) {
if (substr($file, 0, 1) == '.') {
continue;
}
$file_path = $tpl_path . '/' . $file;
if (is_file($file_path)) {
$mtime = intval(@filemtime($file_path));
$tplfile =& $tplfile_handler->create();
$tplfile->setVar('tpl_source', file_get_contents($file_path), true);
$tplfile->setVar('tpl_refid', $mid);
$tplfile->setVar('tpl_tplset', 'default');
$tplfile->setVar('tpl_file', $mydirname . '_' . $file);
$tplfile->setVar('tpl_desc', '', true);
$tplfile->setVar('tpl_module', $mydirname);
$tplfile->setVar('tpl_lastmodified', $mtime);
$tplfile->setVar('tpl_lastimported', 0);
$tplfile->setVar('tpl_type', 'module');
if (!$tplfile_handler->insert($tplfile)) {
$ret[] = '<span style="color:#ff0000;">ERROR: Could not insert template <b>' . htmlspecialchars($mydirname . '_' . $file) . '</b> to the database.</span><br />';
} else {
$tplid = $tplfile->getVar('tpl_id');
$ret[] = 'Template <b>' . htmlspecialchars($mydirname . '_' . $file) . '</b> added to the database. (ID: <b>' . $tplid . '</b>)<br />';
// generate compiled file
include_once XOOPS_ROOT_PATH . '/class/xoopsblock.php';
include_once XOOPS_ROOT_PATH . '/class/template.php';
if (!xoops_template_touch($tplid)) {
$ret[] = '<span style="color:#ff0000;">ERROR: Failed compiling template <b>' . htmlspecialchars($mydirname . '_' . $file) . '</b>.</span><br />';
} else {
$ret[] = 'Template <b>' . htmlspecialchars($mydirname . '_' . $file) . '</b> compiled.</span><br />';
}
}
}
}
closedir($handler);
}
include_once XOOPS_ROOT_PATH . '/class/xoopsblock.php';
include_once XOOPS_ROOT_PATH . '/class/template.php';
xoops_template_clear_module_cache($mid);
return true;
}
示例6: ucfirst
function xpwiki_oninstall_base($module, $mydirname)
{
// transations on module install
global $ret;
// TODO :-D
// for Cube 2.1
if (defined('XOOPS_CUBE_LEGACY')) {
$root =& XCube_Root::getSingleton();
$root->mDelegateManager->add('Legacy.Admin.Event.ModuleInstall.' . ucfirst($mydirname) . '.Success', 'xpwiki_message_append_oninstall');
$root->mDelegateManager->add('Legacy.Admin.Event.ModuleInstall.' . ucfirst($mydirname) . '.Fail', 'xpwiki_message_append_oninstall');
$ret = array();
} else {
if (!is_array($ret)) {
$ret = array();
}
}
$db =& Database::getInstance();
$mid = $module->getVar('mid');
// TABLES (loading mysql.sql)
$sql_file_path = dirname(__FILE__) . '/sql/mysql.sql';
$prefix_mod = $db->prefix() . '_' . $mydirname;
if (file_exists($sql_file_path)) {
$ret[] = "SQL file found at <b>" . htmlspecialchars($sql_file_path, ENT_COMPAT, _CHARSET) . "</b>.<br /> Creating tables...";
if (is_file(XOOPS_ROOT_PATH . '/class/database/oldsqlutility.php')) {
include_once XOOPS_ROOT_PATH . '/class/database/oldsqlutility.php';
$sqlutil = new OldSqlUtility();
} else {
include_once XOOPS_ROOT_PATH . '/class/database/sqlutility.php';
$sqlutil = new SqlUtility();
}
$sql_query = trim(file_get_contents($sql_file_path));
// [ MySQL Version >= 5 ] BLOB and TEXT columns cannot be assigned a default value.
if (is_object($db->conn) && get_class($db->conn) === 'mysqli') {
$mysql_ver = mysqli_get_server_info($db->conn);
} else {
$mysql_ver = mysql_get_server_info();
}
if (@$mysql_ver[0] >= 5) {
$sql_query = str_replace(' default \'\'', '', $sql_query);
}
// [ MySQL Version >= 4 ] ENGINE is the preferred term from MySQL 4.0.18 on and TYPE is deprecated.
if (@$mysql_ver[0] >= 4) {
$sql_query = str_replace(' TYPE=MyISAM', ' ENGINE=MyISAM', $sql_query);
}
$sqlutil->splitMySqlFile($pieces, $sql_query);
$created_tables = array();
foreach ($pieces as $piece) {
$prefixed_query = $sqlutil->prefixQuery($piece, $prefix_mod);
if (!$prefixed_query) {
$ret[] = "Invalid SQL <b>" . htmlspecialchars($piece, ENT_COMPAT, _CHARSET) . "</b><br />";
return false;
}
if (!$db->query($prefixed_query[0])) {
$ret[] = '<b>' . htmlspecialchars($db->error(), ENT_COMPAT, _CHARSET) . '</b><br />';
//var_dump( $db->error() ) ;
return false;
} else {
if (!in_array($prefixed_query[4], $created_tables)) {
$ret[] = 'Table <b>' . htmlspecialchars($prefix_mod . '_' . $prefixed_query[4], ENT_COMPAT, _CHARSET) . '</b> created.<br />';
$created_tables[] = $prefixed_query[4];
} else {
$ret[] = 'Data inserted to table <b>' . htmlspecialchars($prefix_mod . '_' . $prefixed_query[4], ENT_COMPAT, _CHARSET) . '</b>.</br />';
}
}
}
}
// TEMPLATES
$tplfile_handler =& xoops_gethandler('tplfile');
$tpl_path = dirname(__FILE__) . '/templates';
if ($handler = @opendir($tpl_path . '/')) {
while (($file = readdir($handler)) !== false) {
if (substr($file, 0, 1) == '.') {
continue;
}
$file_path = $tpl_path . '/' . $file;
if (is_file($file_path) && substr($file, -5) == '.html') {
$mtime = intval(@filemtime($file_path));
$tplfile =& $tplfile_handler->create();
$tplfile->setVar('tpl_source', file_get_contents($file_path), true);
$tplfile->setVar('tpl_refid', $mid);
$tplfile->setVar('tpl_tplset', 'default');
$tplfile->setVar('tpl_file', $mydirname . '_' . $file);
$tplfile->setVar('tpl_desc', '', true);
$tplfile->setVar('tpl_module', $mydirname);
$tplfile->setVar('tpl_lastmodified', $mtime);
$tplfile->setVar('tpl_lastimported', 0);
$tplfile->setVar('tpl_type', 'module');
if (!$tplfile_handler->insert($tplfile)) {
$ret[] = '<span style="color:#ff0000;">ERROR: Could not insert template <b>' . htmlspecialchars($mydirname . '_' . $file, ENT_COMPAT, _CHARSET) . '</b> to the database.</span><br />';
} else {
$tplid = $tplfile->getVar('tpl_id');
$ret[] = 'Template <b>' . htmlspecialchars($mydirname . '_' . $file, ENT_COMPAT, _CHARSET) . '</b> added to the database. (ID: <b>' . $tplid . '</b>)<br />';
// generate compiled file
include_once XOOPS_ROOT_PATH . '/class/xoopsblock.php';
include_once XOOPS_ROOT_PATH . '/class/template.php';
if (!xoops_template_touch($tplid)) {
$ret[] = '<span style="color:#ff0000;">ERROR: Failed compiling template <b>' . htmlspecialchars($mydirname . '_' . $file, ENT_COMPAT, _CHARSET) . '</b>.</span><br />';
} else {
$ret[] = 'Template <b>' . htmlspecialchars($mydirname . '_' . $file, ENT_COMPAT, _CHARSET) . '</b> compiled.</span><br />';
}
//.........这里部分代码省略.........
示例7: queryFromFile
/**
* perform queries from SQL dump file in a batch
*
* @param string $file file path to an SQL dump file
*
* @return bool FALSE if failed reading SQL file or TRUE
* if the file has been read and queries executed
* @deprecated since version 2.6.0 - alpha 3. Switch to doctrine connector.
*/
public function queryFromFile($file)
{
$this->deprecated();
if (false !== ($fp = fopen($file, 'r'))) {
$sql_queries = trim(fread($fp, filesize($file)));
SqlUtility::splitMySqlFile($pieces, $sql_queries);
foreach ($pieces as $query) {
// [0] contains the prefixed query
// [4] contains unprefixed table name
$prefixed_query = SqlUtility::prefixQuery(trim($query), $this->prefix());
if ($prefixed_query != false) {
$this->query($prefixed_query[0]);
}
}
return true;
}
return false;
}
示例8: queryFromFile
function queryFromFile($sql_file_path)
{
$tables = array();
if (!file_exists($sql_file_path)) {
return false;
}
$sql_query = trim(fread(fopen($sql_file_path, 'r'), filesize($sql_file_path)));
SqlUtility::splitMySqlFile($pieces, $sql_query);
$this->db->connect();
foreach ($pieces as $piece) {
$piece = trim($piece);
// [0] contains the prefixed query
// [4] contains unprefixed table name
$prefixed_query = SqlUtility::prefixQuery($piece, $this->db->prefix());
if ($prefixed_query != false) {
$table = $this->db->prefix($prefixed_query[4]);
if ($prefixed_query[1] == 'CREATE TABLE') {
if ($this->db->query($prefixed_query[0]) != false) {
if (!isset($this->s_tables['create'][$table])) {
$this->s_tables['create'][$table] = 1;
}
} else {
if (!isset($this->f_tables['create'][$table])) {
$this->f_tables['create'][$table] = 1;
}
}
} elseif ($prefixed_query[1] == 'INSERT INTO') {
if ($this->db->query($prefixed_query[0]) != false) {
if (!isset($this->s_tables['insert'][$table])) {
$this->s_tables['insert'][$table] = 1;
} else {
$this->s_tables['insert'][$table]++;
}
} else {
if (!isset($this->f_tables['insert'][$table])) {
$this->f_tables['insert'][$table] = 1;
} else {
$this->f_tables['insert'][$table]++;
}
}
} elseif ($prefixed_query[1] == 'ALTER TABLE') {
if ($this->db->query($prefixed_query[0]) != false) {
if (!isset($this->s_tables['alter'][$table])) {
$this->s_tables['alter'][$table] = 1;
}
} else {
if (!isset($this->s_tables['alter'][$table])) {
$this->f_tables['alter'][$table] = 1;
}
}
} elseif ($prefixed_query[1] == 'DROP TABLE') {
if ($this->db->query('DROP TABLE ' . $table) != false) {
if (!isset($this->s_tables['drop'][$table])) {
$this->s_tables['drop'][$table] = 1;
}
} else {
if (!isset($this->s_tables['drop'][$table])) {
$this->f_tables['drop'][$table] = 1;
}
}
}
}
}
return true;
}
示例9: xoonips_sql_queries
/**
* @split $sqls to individual queries, add prefix to table, and query sqls
* output some informations to stdout.
*
* @param string $sqls string of sqls.
* @return boolean true if succeed
*
*/
function xoonips_sql_queries($sqls)
{
global $xoopsDB;
$textutil =& xoonips_getutility('text');
$pieces = array();
SqlUtility::splitMySqlFile($pieces, $sqls);
$created_tables = array();
$errs = array();
$msgs = array();
$error = false;
$ret = '';
foreach ($pieces as $piece) {
// [0] contains the prefixed query
// [4] contains unprefixed table name
$prefixed_query = SqlUtility::prefixQuery($piece, $xoopsDB->prefix());
if (!$prefixed_query) {
$errs[] = '<b>' . $piece . '</b> is not a valid SQL!';
$error = true;
break;
}
if (!$xoopsDB->query($prefixed_query[0])) {
$errs[] = $xoopsDB->error() . ' of SQL ' . $textutil->html_special_chars($prefixed_query[0]);
$error = true;
break;
}
if (strncmp('CREATE', strtoupper($prefixed_query[0]), 6) == 0 && !in_array($prefixed_query[4], $created_tables)) {
$msgs[] = ' Table <b>' . $xoopsDB->prefix($prefixed_query[4]) . '</b> created.';
$created_tables[] = $prefixed_query[4];
}
}
if ($error) {
// if there was an error, delete the tables created so far,
// so the next installation will not fail
foreach ($created_tables as $ct) {
$xoopsDB->query('DROP TABLE ' . $xoopsDB->prefix($ct));
}
// set error messages
foreach ($errs as $er) {
$ret .= ' ' . $er . '<br />';
}
unset($msgs);
unset($errs);
}
echo $ret;
return !$error;
}
示例10: xsns_oninstall
function xsns_oninstall($module, $mydirname)
{
global $ret;
if( defined( 'XOOPS_CUBE_LEGACY' ) ) {
$root =& XCube_Root::getSingleton();
$root->mDelegateManager->add( 'Legacy.Admin.Event.ModuleInstall.' . ucfirst($mydirname) . '.Success' , 'xsns_message_append_oninstall' ) ;
$root->mDelegateManager->add( 'Legacy.Admin.Event.ModuleInstall.' . ucfirst($mydirname) . '.Fail' , 'xsns_message_append_oninstall' ) ;
$ret = array() ;
}
else{
if( !is_array($ret) ){
$ret = array() ;
}
}
$constpref = '_MI_'.strtoupper($mydirname);
if(strlen($mydirname) > 15){
$ret[] = constant($constpref.'_INSTERR').'<br />';
}
$db =& Database::getInstance() ;
$mid = $module->getVar('mid') ;
// Tables
$sql_ver = floatval(substr(mysql_get_server_info(), 0, 3));
if($sql_ver < 4){
$sql_file = 'mysql3.sql';
}
elseif($sql_ver == 4.0){
$sql_file = 'mysql40.sql';
}
else{
$sql_file = 'mysql.sql';
}
$sql_file_path = realpath(dirname(__FILE__).'/sql/'.$sql_file);
$prefix_mod = $db->prefix() . '_' . $mydirname ;
if( file_exists( $sql_file_path ) ) {
$ret[] = "SQL file found at <b>".htmlspecialchars($sql_file_path)."</b>.<br /> Creating tables...";
if( file_exists( XOOPS_ROOT_PATH.'/class/database/oldsqlutility.php' ) ) {
include_once XOOPS_ROOT_PATH.'/class/database/oldsqlutility.php' ;
$sqlutil = new OldSqlUtility ;
} else {
include_once XOOPS_ROOT_PATH.'/class/database/sqlutility.php' ;
$sqlutil = new SqlUtility ;
}
$sql_query = trim( file_get_contents( $sql_file_path ) ) ;
$sqlutil->splitMySqlFile( $pieces , $sql_query ) ;
$created_tables = array() ;
if( is_array( $pieces ) ) {
foreach( $pieces as $piece ) {
$prefixed_query = $sqlutil->prefixQuery( $piece , $prefix_mod ) ;
if( ! $prefixed_query ) {
$ret[] = "Invalid SQL <b>".htmlspecialchars($piece)."</b><br />";
return false ;
}
if( ! $db->query( $prefixed_query[0] ) ) {
$ret[] = '<b>'.htmlspecialchars( $db->error() ).'</b><br />' ;
return false ;
}
else {
if( ! in_array( $prefixed_query[4] , $created_tables ) ) {
$ret[] = 'Table <b>'.htmlspecialchars($prefix_mod.'_'.$prefixed_query[4]).'</b> created.<br />'; $created_tables[] = $prefixed_query[4];
}
else {
$ret[] = 'Data inserted to table <b>'.htmlspecialchars($prefix_mod.'_'.$prefixed_query[4]).'</b>.</br />';
}
}
}
}
}
// Templates
$tplfile_handler =& xoops_gethandler( 'tplfile' ) ;
$tpl_path = dirname(__FILE__).'/templates' ;
if( $handler = @opendir( $tpl_path . '/' ) ) {
while( ( $file = readdir( $handler ) ) !== false ) {
if( substr( $file , 0 , 1 ) == '.' || !preg_match('/(\.html$)|(\.css$)/i', $file)){
continue ;
}
$file_path = $tpl_path . '/' . $file ;
if( is_file( $file_path ) ) {
$mtime = intval( @filemtime( $file_path ) ) ;
$tplfile =& $tplfile_handler->create() ;
$tplfile->setVar( 'tpl_source' , file_get_contents( $file_path ) , true ) ;
$tplfile->setVar( 'tpl_refid' , $mid ) ;
$tplfile->setVar( 'tpl_tplset' , 'default' ) ;
$tplfile->setVar( 'tpl_file' , $mydirname . '_' . $file ) ;
$tplfile->setVar( 'tpl_desc' , '' , true ) ;
$tplfile->setVar( 'tpl_module' , $mydirname ) ;
$tplfile->setVar( 'tpl_lastmodified' , $mtime ) ;
$tplfile->setVar( 'tpl_lastimported' , 0 ) ;
$tplfile->setVar( 'tpl_type' , 'module' ) ;
if( ! $tplfile_handler->insert( $tplfile ) ) {
$ret[] = '<span style="color:#ff0000;">ERROR: Could not insert template <b>'.htmlspecialchars($mydirname.'_'.$file).'</b> to the database.</span><br />';
} else {
$tplid = $tplfile->getVar( 'tpl_id' ) ;
$ret[] = 'Template <b>'.htmlspecialchars($mydirname.'_'.$file).'</b> added to the database. (ID: <b>'.$tplid.'</b>)<br />';
//.........这里部分代码省略.........
示例11: install
/**
* install a module
*
* @param string $mod module dirname
* @param boolean $force force query
*
* @return bool|XoopsModule|XoopsObject
*/
public function install($mod = '', $force = false)
{
$xoops = Xoops::getInstance();
$module_handler = $xoops->getHandlerModule();
$mod = trim($mod);
try {
$cnt = $module_handler->getCount(new Criteria('dirname', $mod));
} catch (DBALException $e) {
$cnt = 0;
}
if ($cnt == 0) {
/* @var $module XoopsModule */
$module = $module_handler->create();
$module->loadInfoAsVar($mod);
$module->setVar('weight', 1);
$module->setVar('isactive', 1);
$module->setVar('last_update', time());
$install_script = $module->getInfo('onInstall');
if ($install_script && trim($install_script) != '') {
XoopsLoad::loadFile($xoops->path('modules/' . $mod . '/' . trim($install_script)));
}
$func = "xoops_module_pre_install_{$mod}";
// If pre install function is defined, execute
if (function_exists($func)) {
$result = $func($module);
if (!$result) {
$this->error[] = sprintf(XoopsLocale::EF_NOT_EXECUTED, $func);
$this->error = array_merge($this->error, $module->getErrors());
return false;
} else {
$this->trace[] = sprintf(XoopsLocale::SF_EXECUTED, "<strong>{$func}</strong>");
$this->trace = array_merge($this->trace, $module->getMessages());
}
}
// Create tables
$created_tables = array();
if (count($this->error) == 0) {
$schema_file = $module->getInfo('schema');
$sql_file = $module->getInfo('sqlfile');
if (!empty($schema_file)) {
$schema_file_path = \XoopsBaseConfig::get('root-path') . '/modules/' . $mod . '/' . $schema_file;
if (!XoopsLoad::fileExists($schema_file_path)) {
$this->error[] = sprintf(SystemLocale::EF_SQL_FILE_NOT_FOUND, "<strong>{$schema_file}</strong>");
return false;
}
$importer = new ImportSchema();
$importSchema = $importer->importSchemaArray(Yaml::read($schema_file_path));
$synchronizer = new SingleDatabaseSynchronizer($xoops->db());
$synchronizer->updateSchema($importSchema, true);
} elseif (is_array($sql_file) && !empty($sql_file[\XoopsBaseConfig::get('db-type')])) {
$xoops->deprecated('Install SQL files are deprecated since 2.6.0. Convert to portable Schemas');
$sql_file_path = \XoopsBaseConfig::get('root-path') . '/modules/' . $mod . '/' . $sql_file[\XoopsBaseConfig::get('db-type')];
if (!XoopsLoad::fileExists($sql_file_path)) {
$this->error[] = sprintf(SystemLocale::EF_SQL_FILE_NOT_FOUND, "<strong>{$sql_file_path}</strong>");
return false;
} else {
$this->trace[] = sprintf(SystemLocale::SF_SQL_FILE_FOUND, "<strong>{$sql_file_path}</strong>");
$this->trace[] = SystemLocale::MANAGING_TABLES;
$sql_query = fread(fopen($sql_file_path, 'r'), filesize($sql_file_path));
$sql_query = trim($sql_query);
SqlUtility::splitMySqlFile($pieces, $sql_query);
foreach ($pieces as $piece) {
// [0] contains the prefixed query
// [4] contains unprefixed table name
$prefixed_query = SqlUtility::prefixQuery($piece, $xoops->db()->prefix());
if (!$prefixed_query) {
$this->error[]['sub'] = '<span class="red">' . sprintf(XoopsLocale::EF_INVALID_SQL, '<strong>' . $piece . '</strong>') . '</span>';
break;
}
// check if the table name is reserved
if (!in_array($prefixed_query[4], $this->reservedTables) || $mod == 'system') {
// not reserved, so try to create one
try {
$result = $xoops->db()->query($prefixed_query[0]);
} catch (Exception $e) {
$xoops->events()->triggerEvent('core.exception', $e);
$result = false;
}
if (!$result) {
$this->error[] = $xoops->db()->errorInfo();
break;
} else {
if (!in_array($prefixed_query[4], $created_tables)) {
$this->trace[]['sub'] = sprintf(XoopsLocale::SF_TABLE_CREATED, '<strong>' . $xoops->db()->prefix($prefixed_query[4]) . '</strong>');
$created_tables[] = $prefixed_query[4];
} else {
$this->trace[]['sub'] = sprintf(XoopsLocale::SF_DATA_INSERTED_TO_TABLE, '<strong>' . $xoops->db()->prefix($prefixed_query[4]) . '</strong>');
}
}
} else {
// the table name is reserved, so halt the installation
$this->error[]['sub'] = sprintf(SystemLocale::EF_TABLE_IS_RESERVED, '<strong>' . $prefixed_query[4] . '</strong>');
//.........这里部分代码省略.........
示例12: queryFromFile
public static function queryFromFile($sql_file_path, $table_prefix = null, $in_plain_msg = true)
{
global $progress, $errors, $msg;
$tables = array();
if (!file_exists($sql_file_path)) {
return false;
}
$sql_query = trim(fread(fopen($sql_file_path, 'r'), filesize($sql_file_path)));
SqlUtility::splitSqlFile($pieces, $sql_query);
foreach ($pieces as $piece) {
$piece = trim($piece);
// [0] contains the prefixed query
// [4] contains unprefixed table name
if ($table_prefix || $table_prefix == '') {
$prefixed_query = SqlUtility::prefixQuery($piece, $table_prefix);
} else {
$prefixed_query = $piece;
}
if ($prefixed_query != false) {
$table = $table_prefix . $prefixed_query[4];
if ($prefixed_query[1] == 'CREATE TABLE' || $prefixed_query[1] == 'CREATE TABLE IF NOT EXISTS') {
$result = queryDB($prefixed_query[0], array(), FALSE, FALSE);
if (count($result) > 0) {
if ($in_plain_msg) {
$progress[] = 'Table <b>' . $table . '</b> created successfully.';
} else {
$msg->addFeedback(array('TABLE_CREATED', $table));
}
} else {
if (at_db_errno() == 1050) {
//// NOTE STATIC ERRNO --- if (mysql_errno($db) == 1050) {
if ($in_plain_msg) {
$progress[] = 'Table <b>' . $table . '</b> already exists. Skipping.';
} else {
$msg->addFeedback(array('TABLE_EXIST', $table));
}
} else {
if ($in_plain_msg) {
$errors[] = 'Table <b>' . $table . '</b> creation failed.';
} else {
$msg->addError(array('CREATE_TABLE_FAIL', $table));
}
}
}
} elseif ($prefixed_query[1] == 'INSERT INTO') {
queryDB($prefixed_query[0], array(), FALSE, FALSE);
} elseif ($prefixed_query[1] == 'REPLACE INTO') {
queryDB($prefixed_query[0], array(), FALSE, FALSE);
} elseif ($prefixed_query[1] == 'ALTER TABLE') {
$result = queryDB($prefixed_query[0], array());
if ($result > 0) {
if ($in_plain_msg) {
$progress[] = 'Table <strong>' . $table . '</strong> altered successfully.';
} else {
$msg->addFeedback(array('TABLE_ALTERED', $table));
}
} else {
if (at_db_errno() == 1060) {
////// NOTE STATIC ERRNO --- if (mysql_errno($db) == 1060) {
if ($in_plain_msg) {
$progress[] = 'Table <strong>' . $table . '</strong> fields already exists. Skipping.';
} else {
$msg->addFeedback(array('TABLE_FIELD_EXIST', $table));
}
////// NOTE STATIC ERRNO --- } elseif (mysql_errno($db) == 1091) {
} elseif (at_db_errno() == 1091) {
if ($in_plain_msg) {
$progress[] = 'Table <strong>' . $table . '</strong> fields already dropped. Skipping.';
} else {
$msg->addFeedback(array('TABLE_FIELD_DROPPED', $table));
}
} else {
if ($in_plain_msg) {
$errors[] = 'Table <strong>' . $table . '</strong> alteration failed.';
} else {
$msg->addError(array('ALTER_TABLE_FAIL', $table));
}
}
}
} elseif ($prefixed_query[1] == 'DROP TABLE') {
queryDB($prefixed_query[1] . ' ' . $table, array());
} elseif ($prefixed_query[1] == 'UPDATE') {
queryDB($prefixed_query[0], array());
}
}
}
return true;
}
示例13: xoops_cp_header
} else {
xoops_cp_header();
ss_adminMenu(-1, _AM_SS_IMPORT);
$error = false;
$db =& Database::getInstance();
$reservedTables = array('avatar', 'avatar_users_link', 'block_module_link', 'comments', 'config', 'configcategory', 'configoption', 'image', 'imagebody', 'imagecategory', 'imgset', 'imgset_tplset_link', 'imgsetimg', 'groups', 'groups_users_link', 'group_permission', 'online', 'bannerclient', 'banner', 'bannerfinish', 'priv_msgs', 'ranks', 'session', 'smiles', 'users', 'newblocks', 'modules', 'tplfile', 'tplset', 'tplsource');
$msgs[] = "SQL file found at <b>{$sql_file_path}</b>.<br /> Importing Q&A";
include_once XOOPS_ROOT_PATH . '/class/database/sqlutility.php';
$sql_query = fread(fopen($sql_file_path, 'r'), filesize($sql_file_path));
$sql_query = trim($sql_query);
SqlUtility::splitMySqlFile($pieces, $sql_query);
$created_tables = array();
foreach ($pieces as $piece) {
// [0] contains the prefixed query
// [4] contains unprefixed table name
$prefixed_query = SqlUtility::prefixQuery($piece, $db->prefix());
if (!$prefixed_query) {
$errs[] = "<b>{$piece}</b> is not a valid SQL!";
$error = true;
break;
}
// check if the table name is reserved
if (!in_array($prefixed_query[4], $reservedTables)) {
// not reserved, so try to create one
if (!$db->query($prefixed_query[0])) {
$errs[] = $db->error();
$error = true;
break;
} else {
if (!in_array($prefixed_query[4], $created_tables)) {
$msgs[] = ' Updating <b>' . $db->prefix($prefixed_query[4]) . '</b> table.';
示例14: bulletin_oninstall_base
function bulletin_oninstall_base($module, $mydirname)
{
// transations on module install
global $ret;
$db =& Database::getInstance();
$mid = $module->getVar('mid');
// for Cube 2.1
if (defined('XOOPS_CUBE_LEGACY')) {
$isCube = true;
$root =& XCube_Root::getSingleton();
$root->mDelegateManager->add('Legacy.Admin.Event.ModuleInstall.' . ucfirst($mydirname) . '.Success', 'bulletin_message_append_oninstall');
$ret = array();
} else {
$isCube = false;
if (!is_array($ret)) {
$ret = array();
}
}
// transations on module installation
$bulletin_posting_permissions = array(1, 2, 3, 7);
$gperm_handler = xoops_gethandler('groupperm');
foreach ($bulletin_posting_permissions as $itemid) {
$gperm =& $gperm_handler->create();
$gperm->setVar('gperm_groupid', 1);
$gperm->setVar('gperm_name', 'bulletin_permit');
$gperm->setVar('gperm_modid', $mid);
$gperm->setVar('gperm_itemid', $itemid);
$gperm_handler->insert($gperm);
}
// TABLES (loading mysql.sql)
$sql_file_path = dirname(__FILE__) . '/sql/mysql.sql';
$prefix_mod = $db->prefix() . '_' . $mydirname;
if (file_exists($sql_file_path)) {
$ret[] = "SQL file found at <b>" . htmlspecialchars($sql_file_path) . "</b>.<br /> Creating tables...<br />";
if (file_exists(XOOPS_ROOT_PATH . '/class/database/oldsqlutility.php')) {
include_once XOOPS_ROOT_PATH . '/class/database/oldsqlutility.php';
$sqlutil = new OldSqlUtility();
} else {
include_once XOOPS_ROOT_PATH . '/class/database/sqlutility.php';
$sqlutil = new SqlUtility();
}
$sql_query = trim(file_get_contents($sql_file_path));
$sqlutil->splitMySqlFile($pieces, $sql_query);
$created_tables = array();
foreach ($pieces as $piece) {
$prefixed_query = $sqlutil->prefixQuery($piece, $prefix_mod);
if (!$prefixed_query) {
$ret[] = "Invalid SQL <b>" . htmlspecialchars($piece) . "</b><br />";
return false;
}
if (!$db->query($prefixed_query[0])) {
$ret[] = '<b>' . htmlspecialchars($db->error()) . '</b><br />';
var_dump($db->error());
return false;
} else {
if (!in_array($prefixed_query[4], $created_tables)) {
$ret[] = ' Table <b>' . htmlspecialchars($prefix_mod . '_' . $prefixed_query[4]) . '</b> created.<br />';
$created_tables[] = $prefixed_query[4];
} else {
$ret[] = ' Data inserted to table <b>' . htmlspecialchars($prefix_mod . '_' . $prefixed_query[4]) . '</b>.</br />';
}
}
}
}
// TEMPLATES
$tplfile_handler =& xoops_gethandler('tplfile');
$tpl_path = dirname(__FILE__) . '/templates';
if ($handler = @opendir($tpl_path . '/')) {
while (($file = readdir($handler)) !== false) {
if (substr($file, 0, 1) == '.') {
continue;
}
$file_path = $tpl_path . '/' . $file;
if (is_file($file_path) && substr($file, -5) == '.html') {
$mtime = intval(@filemtime($file_path));
$tplfile =& $tplfile_handler->create();
$tplfile->setVar('tpl_source', file_get_contents($file_path), true);
$tplfile->setVar('tpl_refid', $mid);
$tplfile->setVar('tpl_tplset', 'default');
$tplfile->setVar('tpl_file', $mydirname . '_' . $file);
$tplfile->setVar('tpl_desc', '', true);
$tplfile->setVar('tpl_module', $mydirname);
$tplfile->setVar('tpl_lastmodified', $mtime);
$tplfile->setVar('tpl_lastimported', 0);
$tplfile->setVar('tpl_type', 'module');
if (!$tplfile_handler->insert($tplfile)) {
$ret[] = '<span style="color:#ff0000;">ERROR: Could not insert template <b>' . htmlspecialchars($mydirname . '_' . $file) . '</b> to the database.</span><br />';
} else {
$tplid = $tplfile->getVar('tpl_id');
$ret[] = 'Template <b>' . htmlspecialchars($mydirname . '_' . $file) . '</b> added to the database. (ID: <b>' . $tplid . '</b>)<br />';
// generate compiled file
include_once XOOPS_ROOT_PATH . '/class/xoopsblock.php';
include_once XOOPS_ROOT_PATH . '/class/template.php';
if (!xoops_template_touch($tplid)) {
$ret[] = '<span style="color:#ff0000;">ERROR: Failed compiling template <b>' . htmlspecialchars($mydirname . '_' . $file) . '</b>.</span><br />';
} else {
$ret[] = 'Template <b>' . htmlspecialchars($mydirname . '_' . $file) . '</b> compiled.</span><br />';
}
}
}
//.........这里部分代码省略.........
示例15: queryFromFile
function queryFromFile($sql_file_path, $table_prefix)
{
global $db, $progress, $errors;
include_once AC_INCLUDE_PATH . 'classes/DAO/DAO.class.php';
$dao = new DAO();
$tables = array();
if (!file_exists($sql_file_path)) {
return false;
}
$sql_query = trim(fread(fopen($sql_file_path, 'r'), filesize($sql_file_path)));
SqlUtility::splitSqlFile($pieces, $sql_query);
foreach ($pieces as $piece) {
$piece = trim($piece);
// [0] contains the prefixed query
// [4] contains unprefixed table name
if ($table_prefix || $table_prefix == '') {
$prefixed_query = SqlUtility::prefixQuery($piece, $table_prefix);
} else {
$prefixed_query = $piece;
}
if ($prefixed_query != false) {
$table = $table_prefix . $prefixed_query[4];
$prefixed_query[1] = strtoupper($prefixed_query[1]);
if (strtoupper($prefixed_query[1]) == 'CREATE TABLE') {
if ($dao->execute($prefixed_query[0]) !== false) {
$progress[] = 'Table <b>' . $table . '</b> created successfully.';
} else {
if (mysql_errno($db) == 1050) {
$progress[] = 'Table <b>' . $table . '</b> already exists. Skipping.';
} else {
$errors[] = 'Table <b>' . $table . '</b> creation failed.';
}
}
} elseif ($prefixed_query[1] == 'INSERT INTO' || $prefixed_query[1] == 'ALTER TABLE' || $prefixed_query[1] == 'DROP TABLE' || $prefixed_query[1] == 'UPDATE') {
$dao->execute($prefixed_query[0]);
}
}
}
return TRUE;
}