#!/usr/bin/env php <?php /* * Copyright 2011 Aaron Seigo <*****@*****.**> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details * * You should have received a copy of the GNU Library General Public * License along with this program; if not, write to the * Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ include_once 'config.php'; include_once "{$common_includePath}/db.php"; $db = SynchrotronDBConnection::copy('write', $synchrotron_dbs['default']); $db->db_username = $db_writeusername; $db->db_password = $db_writepassword; db_register($db); $old_time = time() - 60 * 15; $db = db_connection('write'); db_query($db, "DELETE FROM accesses WHERE ts < {$old_time};");
if ($db_connection instanceof SynchrotronDBConnection) { $synchrotron_dbs[$db_connection->identifier] = $db_connection; return true; } return false; } // REGISTER A DEFAULT DB global $db_localOnly; $defaultDB = new SynchrotronDBConnection('default'); $defaultDB->db_host = $db_host; $defaultDB->db_port = $db_port; $defaultDB->db_name = $db_name; $defaultDB->db_username = $db_username; $defaultDB->db_password = $db_password; $defaultDB->localConnection = $db_localOnly; db_register($defaultDB); /* ********************************************************************** * database connections ********************************************************************** */ function db_connection($identifier = 'default') { global $synchrotron_dbs; $db = $synchrotron_dbs[$identifier]; if (!$db) { $db = SynchrotronDBConnection::Copy($identifier, $synchrotron_dbs['default']); } if ($db instanceof SynchrotronDBConnection) { if (!$db->resource) { if ($db->persistent) {
function cloneDatabase($targetdbname) { set_time_limit(0); $newname = HEURIST_DB_PREFIX . $targetdbname; //create new empty database if (!db_create($newname)) { return false; } echo_flush("<p>Create Database Structure (tables)</p>"); if (db_script($newname, HEURIST_DIR . "admin/setup/dbcreate/blankDBStructure.sql")) { echo_flush('<p style="padding-left:20px">SUCCESS</p>'); } else { db_drop($newname); return false; } // Remove initial values from empty database mysql_connection_insert($newname); mysql_query('delete from sysIdentification where 1'); mysql_query('delete from sysTableLastUpdated where 1'); mysql_query('delete from sysUsrGrpLinks where 1'); mysql_query('delete from sysUGrps where ugr_ID>=0'); mysql_query('delete from defLanguages where 1'); echo_flush("<p>Copy data</p>"); // db_clone function in /common/php/db_utils.php does all the work if (db_clone(DATABASE, $newname)) { echo_flush('<p style="padding-left:20px">SUCCESS</p>'); } else { db_drop($newname); return false; } //cleanup database to avoid issues with addition of constraints //1. cleanup missed trm_InverseTermId mysql_query('update defTerms t1 left join defTerms t2 on t1.trm_InverseTermId=t2.trm_ID set t1.trm_InverseTermId=null where t1.trm_ID>0 and t2.trm_ID is NULL'); //2. remove missed recent records mysql_query('delete FROM usrRecentRecords where rre_RecID is not null and rre_RecID not in (select rec_ID from Records)'); //3. remove missed rrc_SourceRecID and rrc_TargetRecID mysql_query('delete FROM recRelationshipsCache where rrc_SourceRecID is not null and rrc_SourceRecID not in (select rec_ID from Records)'); mysql_query('delete FROM recRelationshipsCache where rrc_TargetRecID is not null and rrc_TargetRecID not in (select rec_ID from Records)'); //4. cleanup orphaned details mysql_query('delete FROM recDetails where dtl_RecID is not null and dtl_RecID not in (select rec_ID from Records)'); //5. cleanup missed references to uploaded files mysql_query('delete FROM recDetails where dtl_UploadedFileID is not null and dtl_UploadedFileID not in (select ulf_ID from recUploadedFiles)'); $sHighLoadWarning = "<p><h4>Note: </h4>Failure to clone a database may result from high server load. Please try again, and if the problem continues contact the Heurist developers at info heuristnetwork dot org</p>"; // 4. add contrainsts, procedure and triggers echo_flush("<p>Addition of Referential Constraints</p>"); if (db_script($newname, dirname(__FILE__) . "/../dbcreate/addReferentialConstraints.sql")) { echo_flush('<p style="padding-left:20px">SUCCESS</p>'); } else { db_drop($newname); print $sHighLoadWarning; return false; } echo_flush("<p>Addition of Procedures and Triggers</p>"); if (db_script($newname, dirname(__FILE__) . "/../dbcreate/addProceduresTriggers.sql")) { echo_flush('<p style="padding-left:20px">SUCCESS</p>'); } else { db_drop($newname); print $sHighLoadWarning; return false; } // 5. remove registration info and assign originID for definitions mysql_connection_insert($newname); $sourceRegID = 0; $res = mysql_query('select sys_dbRegisteredID from sysIdentification where 1'); if ($res) { $row = mysql_fetch_row($res); if ($row) { $sourceRegID = $row[0]; } } //print "<p>".$sourceRegID."</p>"; // RESET register db ID $query1 = "update sysIdentification set sys_dbRegisteredID=0, sys_hmlOutputDirectory=null, sys_htmlOutputDirectory=null, sys_SyncDefsWithDB=null, sys_MediaFolders=null where 1"; $res1 = mysql_query($query1); if (mysql_error()) { //(mysql_num_rows($res1) == 0) print "<p><h4>Warning</h4><b>Unable to reset sys_dbRegisteredID in sysIdentification table. (" . mysql_error() . ")<br> Please reset the registration ID manually</b></p>"; } //assign origin ID db_register($newname, $sourceRegID); // Index new database for Elasticsearch //TODO: Needs error report, trap error and warn or abort clone buildAllIndices($targetdbname); // Copy the images and the icons directories //TODO: Needs error report, trap error and warn or abort clone recurse_copy(HEURIST_UPLOAD_ROOT . HEURIST_DBNAME, HEURIST_UPLOAD_ROOT . $targetdbname); // Update file path in target database with absolute paths $query1 = "update recUploadedFiles set ulf_FilePath='" . HEURIST_UPLOAD_ROOT . $targetdbname . "/' where ulf_FilePath='" . HEURIST_UPLOAD_ROOT . HEURIST_DBNAME . "/' and ulf_ID>0"; $res1 = mysql_query($query1); if (mysql_error()) { //(mysql_num_rows($res1) == 0) print "<p><h4>Warning</h4><b>Unable to set database files path to new path</b>" . "<br>Query was:" . $query1 . "<br>Please get your system administrator to fix this problem BEFORE editing the database (your edits will affect the original database)</p>"; } // Success! echo "<hr><p> </p><h2>New database '{$targetdbname}' created successfully</h2>"; print "<p>Please access your new database through this link: <a href='" . HEURIST_BASE_URL . "?db=" . $targetdbname . "' title='' target=\"_new\"><strong>" . $targetdbname . "</strong></a></p>"; return true; }
function registerDatabase() { $heuristDBname = rawurlencode(HEURIST_DBNAME); global $dbID, $dbName, $ownerGrpID, $indexdb_user_id, $usrEmail, $usrPassword, $usrName, $usrFirstName, $usrLastName, $dbDescription; $serverURL = HEURIST_BASE_URL . "?db=" . $heuristDBname; $serverURL = substr($serverURL, 7); // to avoid conversion path to localhost if masterindex on the same server $usrEmail = rawurlencode($usrEmail); $usrName = rawurlencode($usrName); $usrFirstName = rawurlencode($usrFirstName); $usrLastName = rawurlencode($usrLastName); $usrPassword = rawurlencode($usrPassword); $dbDescriptionEncoded = rawurlencode($dbDescription); $reg_url = HEURIST_INDEX_BASE_URL . "admin/setup/dbproperties/getNextDBRegistrationID.php" . "?db=Heurist_Master_Index&dbReg=" . $heuristDBname . "&dbVer=" . HEURIST_DBVERSION . "&dbTitle=" . $dbDescriptionEncoded . "&usrPassword="******"&usrName=" . $usrName . "&usrFirstName=" . $usrFirstName . "&usrLastName=" . $usrLastName . "&usrEmail=" . $usrEmail . "&serverURL=" . rawurlencode($serverURL); //$data = loadRemoteURLContent($reg_url); $data = loadRemoteURLContentSpecial($reg_url); //without proxy if (!$data) { die("Unable to contact Heurist master index, possibly due to timeout or proxy setting<br />" . "URL requested: <a href='{$reg_url}'>{$reg_url}</a>"); } if ($data) { $dbID = intval($data); // correct return of data is just the registration number. we probably need a // better formatted return with some tags to ensure we are getting the right thing } if ($dbID == 0) { // Unable to allocate a new database identifier $decodedData = explode(',', $data); if (count($decodedData) > 0) { $msg = $decodedData[1]; } else { $msg = "Problem allocating a database identifier from the Heurist master index, " . "returned the following instead of a registration number:\n" . substr($data, 0, 25) . " ... \nPlease contact <a href=mailto:info@HeuristNetwork.org>Heurist developers</a> for advice"; } echo $msg . "<br />"; return; } else { if ($dbID == -1) { // old title update function, should no longer be called $res = mysql_query("update sysIdentification set `sys_dbDescription`='" . mysql_real_escape_string($dbDescription) . "' where 1"); echo "<div class='input-row'><div class='input-header-cell'>" . "Database description (updated):</div><div class='input-cell'>" . $dbDescription . "</div></div>"; } else { // We have got a new dbID, set the assigned dbID in sysIdentification $res = mysql_query("update sysIdentification set `sys_dbRegisteredID`='{$dbID}', " . "`sys_dbDescription`='" . mysql_real_escape_string($dbDescription) . "' where 1"); if ($res) { echo "<div class='input-row'><div class='input-header-cell'>Database:</div>" . "<div class='input-cell'>" . DATABASE . "</div></div>"; echo "<div class='input-row'><div class='input-header-cell'>" . "Registration successful, database ID allocated is</div>" . "<div class='input-cell'>" . $dbID . "</div></div>"; echo "<div class='input-row'><div class='input-header-cell'></div>" . "<div class='input-cell'>Basic description: " . $dbDescription . "</div></div>"; $url = HEURIST_INDEX_BASE_URL . "records/edit/editRecord.html?recID=" . $dbID . "&db=Heurist_Master_Index"; echo "<div class='input-row'><div class='input-header-cell'>Collection metadata:</div>" . "<div class='input-cell'><a href={$url} target=_blank>Click here to edit</a> " . "(login - if asked - as yourself) </div></div>"; // Update original DB ID and original db code for all existing record types, fields and terms // which don't have them (meaning that they were defined within this database) // Record types $result = db_register(DATABASE, $dbID); if (!$result) { echo "<div class=wrap><div id=errorMsg>Unable to set all values for originating DB information for " . DATABASE . " - one of the update queries failed</div></div>"; } ?> <script> // automatically call Heurist_Master_Index metadata edit form for this database window.open("<?php echo $url; ?> ",'_blank'); </script> <?php } else { $msg = "<div class=wrap><div id=errorMsg><span>Unable to write database identification record</span>" . "this database might be incorrectly set up<br />" . "Please contact <a href=mailto:info@HeuristNetwork.org>Heurist developers</a> for advice</div></div>"; echo $msg; return; } // unable to write db identification record } } // successful new DB ID }