Posts tagged ‘MySQL’

Concrete5 :: interacting with the database

The C5 API has a class Database in the core package (libraries/database.php). Alas, this is not the documentation we’re looking for (we’re looking for query, etc. methods, which are not present there).

Actually the methods used come directly from the included ADODB librarary. You can download a documentation here.

Usage:

//Load the Database Layer.
$db = Loader::db();
//quote the string we’ll be inserting (use get_magic_quotes_gpc() as second parameter to avoid double escaping!)
$source = $db->qstr($source, get_magic_quotes_gpc());
$content = $db->qstr($content);
$sql = “insert into IdImportWizardSources (source, content) “;
$sql .= “values ($source,$content)”;
if ($db->Execute($sql)===false) {
//error handling
echo ‘error inserting: ‘ .$db->ErrorMsg();
}

Concrete5 and db.xml

How does the installation of a db.xml file in the package root directory work?

I need to create tables for my Dashboard Application (Import Wizard), which will not be used by blocks – the Import Wizard does not contain any blocks.

In the Concrete5 forum, a function installDB was mentioned. It’s defined in the InstallController class like this:

protected function installDB() {

$installDirectory = $this->installData['DIR_BASE_CORE'] . ‘/config’;
$file = $installDirectory . ‘/db.xml’;
if (!file_exists($file)) {
throw new Exception(t(‘Unable to locate database import file.’));
}

$db = Loader::db();
$db->ensureEncoding();
$err = Package::installDB($file);

}

It ensures the db.xml file exists and after some other setup steps and check finally calls Package::installDB (Package is a model):

public static function installDB($xmlFile) {

if (!file_exists($xmlFile)) {
return false;
}

// currently this is just done from xml

$db = Loader::db();

// this sucks – but adodb generates errors at the beginning because it attempts
// to find a table that doesn’t exist!

$handler = $db->IgnoreErrors();
if ($db->getDebug() == false) {
ob_start();
}

$schema = $db->getADOSChema();
$sql = $schema->ParseSchema($xmlFile);

$db->IgnoreErrors($handler);

if (!$sql) {
$result->message = $db->ErrorMsg();
return $result;
}

$r = $schema->ExecuteSchema();

if ($db->getDebug() == false) {
$dbLayerErrorMessage = ob_get_contents();
ob_end_clean();
}

$result = new stdClass;
$result->result = false;

if ($dbLayerErrorMessage != ”) {
$result->message = $dbLayerErrorMessage;
return $result;
} if (!$r) {
$result->message = $db->ErrorMsg();
return $result;
}

$result->result = true;

$db->CacheFlush();
return $result;

}

The Code is Copyright by Concrete5 Team.

It looks like I could just drop a db.xml file in my package directory and it will be installed. But how should I name my table?

By convention, there’s different prefixes:

  • atX (atAddress, atBoolean, atDateTime) = attribute types
  • btX (btForm, btFlashContent, btSearch) = block types

But there’s none for helper tables, to be used just for the Dashboard wizard.

Here’s a link to the AdoDB XML Schemas (AXMLS) (the language db.xml is written in). I came up with the following:

<?xml version=”1.0″?>
<schema version=”0.3″>
<table name=”IdImportWizardSources”>
<field name=”ID” type=”I”>
<key />
<unsigned />
<autoincrement />
</field>
<field name=”source” type=”X” />
<field name=”retrieved” type=”T”>
<deftimestamp />
</field>
<field name=”content” type=”B” />

</table>
</schema>

Here I define a new table named IdImportWizardSources (Id for IdeaDay), and several field values using keywords.

Notable is the retrieved field which is automatically set to the current timestamp (deftimestamp). content has a type of BLOB which should be able to store up to 4 GB with MySQL longblob type – unfortunately the amount you can store at once also depends on your MySQL max_packet_size. (Which is usually set to around 16 MB).

The table is imported and set up automatically by Concrete, without a single line o’code.

Server Management Software

Wir sind dazu bereit, den nächsten Schritt zu gehen und die Verwaltung des Webservers von manuell auf semi-manuell umzustellen.

Unser Mailsystem wird nach wie vor vom Websystem abgekoppelt laufen – auf Zimbra-Basis.

Für den Webserver selber rüsten wir eine Server Management Software nach. Sie muß einen typischen LAMP Server unterstützen (Linux – Debian / Apache / MySQL / PHP)

Hier sind die Kandidaten:

  1. SysCP

    Gut dokumentiert, aktive Community, interessanter Funktionsumfang (inklusive Resellern die ihre Kunden selbst administrieren können usw.), deutsches User Interface verfügbar.

    Natürlich Open Source.

  2. Kloxo / HyperVM

    Interessant für unser Setup (OpenVZ, mehrere VPMs, …); allerdings evtl. nur auf englisch und massiver Overkill. Es heißt, dass es als Open Source released wurde, jedoch ist auf der Firmenseite etwas von Lizenzkosten die Rede …

  3. http://www.virtualmin.com/

    Virtualmin ist eine weitere Möglichkeit. Hier sticht ebenfalls keine Sprachunterstützung heraus, das System ist komplex und deckt z.B. detaillierte CPU Leistung usw. Statistiken ab.

    Unterstützt einen hohen Grad von Sicherheit bei PHP – die Prozesse werden unter dem jeweiligen User ausgeführt, mit suexec und mod_fastcgi. Siehe auch: http://en.wikipedia.org/wiki/Webmin

  4. OpenLSM

    Eine indische Software mit einer schönen, aber leider nicht so informativen Webseite. In der Feauture Liste schaut das Produkt gut aus – Anlegen von Resellern, Monitoring, … Unterstützt auch Subversion.

  5. VHCS

    Obwohl auf anderen Seiten behauptet wird, dass das Projekt tot sei, ist es nicht so. Die Jungs arbeiten gerade an Release 2.6, der Web 2.0 in VHCS bringen soll. Das Interface schaut übersichtlich und aufgeräumt aus.

  6. OpenPanel

    Schaut sehr, sehr gut aus – ein JavaScript zentriertes UserInterface. Leider ist es seit Juli 2008 in Beta 0.9.5 stehen geblieben. Eine 1.0 steht fast vor der Tür – sie wird Debian 5.0 unterstützen. Definitiv einen Blick wert – auch wenn es erstmal nur in Englisch ist.
    Wird von  PanelSix entwickelt, hier ist die Doku.

Hier ist noch ein Überblick auf Wikipedia (EN).

MySQL

[25-Feb-2010 16:15:46] PHP Warning: mysql_real_escape_string() [<a href='function.mysql-real-escape-string'>function.mysql-real-escape-string</a>]: Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2) in … on line 38
[25-Feb-2010 16:15:46] PHP Warning: mysql_real_escape_string() [<a href='function.mysql-real-escape-string'>function.mysql-real-escape-string</a>]: A link to the server could not be established in … on line 38
(…)

Lösung des Problems:
die Funktion mysql-real-escape-string erfordert eigentlich zwei Parameter: zuerst den $unescaped_string, dann die resource $link_identifier. Lässt man zweiteres weg, versucht MySQL die aktuell zuletzt geöffnete Verbindung zu benutzen.

Ich habe in meinem Skript den Zugang zur Datenbank erst danach geöffnet. (Das ist der wichtigste Unterschied zwischen mysql_escape_string und mysql_real_escape_string – ersteres erfordert KEINEN Zugriff auf die Datenbank!)