Source for file OnpubDatabase.php
Documentation is available at OnpubDatabase.php
* Manage an Onpub database.
* @author {@link mailto:corey@onpub.com Corey H.M. Taylor}
* @copyright Onpub (TM). Copyright 2012, Onpub.com.
* {@link http://onpub.com/}
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
* All the methods in this class which query the database use the database
* connection provided by the PDO object required by this constructor.
* Currently, Onpub only supports MySQL as a database for storing content.
* Therefore, when constructing the PDO object, only the
* {@link PHP_MANUAL#ref.pdo-mysql PDO_MYSQL} driver is supported
* as a PDO {@link PHP_MANUAL#ref.pdo-mysql.connection data source}.
* @param PDO $pdo A {@link PHP_MANUAL#function.pdo-construct PHP Data Object}.
$this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, TRUE);
* Get the name of the currently connected-to MySQL database.
* @return mixed NULL if no database is currently selected. Otherwise, the
* name of the MySQL that's currently being used.
$result =
$this->pdo->query('SELECT DATABASE() AS current');
if (!($row =
$result->fetch(PDO::FETCH_ASSOC))) {
* Delete the Onpub schema.
* Calling this method will delete all Onpub content and schema in the
* PDO-connected database! Use with caution.
* @return mixed TRUE if the schema was successfully deleted. An array
* of PDOException objects will be returned if any errors occured.
$sqlfile =
file('../api/sql/deleteonpubtables.sql');
// advance past all comments
while (strpos($sqlfile[$line], '--') !==
FALSE) {
while ($sqlfile[$line] ==
"\n") {
for ($i =
$line; $i <
sizeof($sqlfile); $i++
) {
while (strpos($sqlfile[$i], ';') ===
FALSE) {
if (($i +
1) <
sizeof($sqlfile)) {
while ($sqlfile[$i +
1] ==
"\n") {
$result =
$this->pdo->exec($query);
catch
(PDOException $e) {
* Install the {@link http://onpub.com/pdfs/onpub_schema.pdf Onpub schema}.
* Calling this method will install the Onpub tables in the PDO-connected
* @param int $version Optional argument to specify what version of the Onpub
* schema to install. If NULL (the default), the latest version of the schema
* will be added to the database.
* @return mixed TRUE if the schema was successfully installed. An array
* of PDOException objects will be returned if any errors occured.
public function install($version =
NULL)
$sqlfile =
file('../api/sql/createonpubtables-rev0.sql');
// advance past all comments
while (strpos($sqlfile[$line], '--') !==
FALSE) {
while ($sqlfile[$line] ==
"\n") {
for ($i =
$line; $i <
sizeof($sqlfile); $i++
) {
while (strpos($sqlfile[$i], ';') ===
FALSE) {
if (($i +
1) <
sizeof($sqlfile)) {
while ($sqlfile[$i +
1] ==
"\n") {
$result =
$this->pdo->exec($query);
catch
(PDOException $e) {
* Gets a list of MySQL databases that the logged-in user has access to.
* System database names are excluded from the list.
* @return array Array will be empty if user has no database access.
* Otherwise array will contain the names of the MySQL databases she has
$result =
$this->pdo->query('SHOW DATABASES');
$rows =
$result->fetchAll(PDO::FETCH_ASSOC);
$excludes =
array('mysql', 'performance_schema', 'information_schema');
foreach ($rows as $row) {
if (!in_array($row['Database'], $excludes))
$dbs[] =
$row['Database'];
* Check the status of the Onpub schema.
* @return mixed The version of the schema in the database as an int. An array
* of PDOException objects will be returned if the schema is incomplete or
$queryOptions->setPage(1, 1);
$oaamaps->select($queryOptions);
catch
(PDOException $e) {
$oarticles->select($queryOptions);
catch
(PDOException $e) {
$oauthors->select($queryOptions);
catch
(PDOException $e) {
$oimages->select($queryOptions);
catch
(PDOException $e) {
$osamaps->select($queryOptions);
catch
(PDOException $e) {
$osections->select($queryOptions);
catch
(PDOException $e) {
$owebsites->select($queryOptions);
catch
(PDOException $e) {
$owsmaps->select($queryOptions);
catch
(PDOException $e) {
* Verify the results of a call to {@link PHP_MANUAL#function.pdostatement-execute PDOStatement->execute()}.
* Used internally to verify whether or not a PDOStatement->execute() call was
* successful or not. If the call returned FALSE then an exception is
* thrown with an error message and error code, explaining what went wrong.
* If the execute() call fails during a running database transaction,
* {@link PHP_MANUAL#function.pdo-rollback PDOStatement->rollback()} is called to
* roll back the transaction to put the database back in the state it was
* before the error occured; then the appropriate exception is thrown.
* @param PDO $pdo The PDO object which called {@link PHP_MANUAL#function.pdo-prepare prepare()}.
* @param mixed $result The value execute() returned when it was called.
* @param bool $isTransaction TRUE if execute() was called during a running
* database transaction, FALSE otherwise.
* @param array $errorInfo The array returned by {@link PHP_MANUAL#function.pdostatement-errorinfo PDOStatement->errorInfo()}.
* @throws PDOException if the execute() call failed.
public static function verifyExecute(PDO $pdo, $result, $isTransaction, $errorInfo)
$status =
$pdo->rollBack();
$errorInfo =
$pdo->errorInfo();
$e =
new PDOException($errorInfo[2], $errorInfo[1]);
$e->errorInfo =
$errorInfo;
$e =
new PDOException($errorInfo[2], $errorInfo[1]);
$e->errorInfo =
$errorInfo;
$e =
new PDOException($errorInfo[2], $errorInfo[1]);
$e->errorInfo =
$errorInfo;
* Verify the results of a call to {@link PHP_MANUAL#function.pdo-exec PDO->exec()}.
* Used internally to verify whether or not a PDO->exec() call was
* successful or not. If the call returned FALSE then an exception is
* thrown with an error message and error code, explaining what went wrong.
* If the exec() call fails during a running database transaction,
* {@link PHP_MANUAL#function.pdo-rollback PDO->rollback()} is called to
* roll back the transaction to put the database back in the state it was
* before the error occured; then the appropriate exception is thrown.
* @param PDO $pdo The PDO object which called exec().
* @param mixed $result The value exec() returned when it was called.
* @param bool $isTransaction TRUE if exec() was called during a running
* database transaction, FALSE otherwise.
* @throws PDOException if the exec() call failed.
public static function verifyExec(PDO $pdo, $result, $isTransaction)
$errorInfo =
$pdo->errorInfo();
$status =
$pdo->rollBack();
$errorInfo =
$pdo->errorInfo();
$e =
new PDOException($errorInfo[2], $errorInfo[1]);
$e->errorInfo =
$errorInfo;
$e =
new PDOException($errorInfo[2], $errorInfo[1]);
$e->errorInfo =
$errorInfo;
$errorInfo =
$pdo->errorInfo();
$e =
new PDOException($errorInfo[2], $errorInfo[1]);
$e->errorInfo =
$errorInfo;
* Verify the results of a call to {@link PHP_MANUAL#function.pdo-query PDO->query()}.
* Used internally to verify whether or not a PDO->query() call was
* successful or not. If the call returned FALSE then an exception is
* thrown with an error message and error code, explaining what went wrong.
* If the query() call fails during a running database transaction,
* {@link PHP_MANUAL#function.pdo-rollback PDO->rollback()} is called to
* roll back the transaction to put the database back in the state it was
* before the error occured; then the appropriate exception is thrown.
* @param PDO $pdo The PDO object which called query().
* @param mixed $result The value query() returned when it was called.
* @param bool $isTransaction TRUE if query() was called during a running
* database transaction, FALSE otherwise.
* @throws PDOException if the query() call failed.
public static function verifyQuery(PDO $pdo, $result, $isTransaction)
$errorInfo =
$pdo->errorInfo();
$status =
$pdo->rollBack();
$errorInfo =
$pdo->errorInfo();
$e =
new PDOException($errorInfo[2], $errorInfo[1]);
$e->errorInfo =
$errorInfo;
$e =
new PDOException($errorInfo[2], $errorInfo[1]);
$e->errorInfo =
$errorInfo;
$errorInfo =
$pdo->errorInfo();
$e =
new PDOException($errorInfo[2], $errorInfo[1]);
$e->errorInfo =
$errorInfo;
* Verify the results of a call to {@link PHP_MANUAL#function.pdo-beginTransaction PDO->beginTransaction()} or {@link PHP_MANUAL#function.pdo-commit PDO->commit()}.
* Used internally to verify whether or not a PDO->beginTransaction() or a
* PDO->rollback() call was successful or not. If the call returned FALSE
* then an exception is thrown with an error message and error code,
* explaining what went wrong.
* @param PDO $pdo The PDO object which called beginTransaction() or rollback().
* @param mixed $result The value beginTransaction() or rollback() returned when it was called.
* @throws PDOException if the beginTransaction() or rollback() call failed.
$errorInfo =
$pdo->errorInfo();
$e =
new PDOException($errorInfo[2], $errorInfo[1]);
$e->errorInfo =
$errorInfo;
* Verify the results of a call to {@link PHP_MANUAL#function.pdo-prepare PDO->prepare()}.
* Used internally to verify whether or not a PDO->prepare() call was
* successful or not. If the call returned FALSE then an exception is
* thrown with an error message and error code, explaining what went wrong.
* If the prepare() call fails during a running database transaction,
* {@link PHP_MANUAL#function.pdo-rollback PDO->rollback()} is called to
* roll back the transaction to put the database back in the state it was
* before the error occured; then the appropriate exception is thrown.
* @param PDO $pdo The PDO object which called prepare().
* @param mixed $result The value prepare() returned when it was called.
* @param bool $isTransaction TRUE if prepare() was called during a running
* database transaction, FALSE otherwise.
* @throws PDOException if the prepare() call failed.
public static function verifyPrepare(PDO $pdo, $result, $isTransaction)
$errorInfo =
$pdo->errorInfo();
$status =
$pdo->rollBack();
$errorInfo =
$pdo->errorInfo();
$e =
new PDOException($errorInfo[2], $errorInfo[1]);
$e->errorInfo =
$errorInfo;
$e =
new PDOException($errorInfo[2], $errorInfo[1]);
$e->errorInfo =
$errorInfo;
$errorInfo =
$pdo->errorInfo();
$e =
new PDOException($errorInfo[2], $errorInfo[1]);
$e->errorInfo =
$errorInfo;
* Decode a UTF8-encoded string to a latin1-encoded string.
* @param string $in_str UTF8-encoded string.
* @return string latin1-encoded string.
// utf8Decode is courtesy nospam@jra.nu and was found
// at: http://us4.php.net/utf8-decode in the comments section
// Replace ? with a unique string
$new_str =
str_replace("?", "q0u0e0s0t0i0o0n", $in_str);
// if it contains ? marks
if (strpos($new_str, "?") !==
FALSE) {
// Something went wrong, set new_str to the original string.
// If not then all is well, put the ?-marks back where is belongs
$new_str =
str_replace("q0u0e0s0t0i0o0n", "?", $new_str);
Documentation generated on Fri, 08 Feb 2013 04:02:20 -0500 by phpDocumentor 1.4.4