Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Appliy coding standard to Versioning

  • Loading branch information...
commit b5e27934106eaff570cd1cce7867d53c551fa34c 1 parent 5674584
@white-gecko white-gecko authored
Showing with 128 additions and 149 deletions.
  1. +128 −149 library/Erfurt/Versioning.php
View
277 library/Erfurt/Versioning.php
@@ -1,26 +1,26 @@
<?php
/**
- * This file is part of the {@link http://aksw.org/Projects/Erfurt Erfurt} project.
+ * This file is part of the {@link http://erfurt-framework.org Erfurt} project.
*
- * @copyright Copyright (c) 2012, {@link http://aksw.org AKSW}
- * @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
+ * @copyright Copyright (c) 2013, {@link http://aksw.org AKSW}
+ * @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
*/
/*
* database tables:
* CREATE TABLE ef_versioning_actions(
- * id INT NOT NULL AUTO_INCREMENT,
- * model VARCHAR(255) NOT NULL,
+ * id INT NOT NULL AUTO_INCREMENT,
+ * model VARCHAR(255) NOT NULL,
* useruri VARCHAR(255) NOT NULL,
- * resource VARCHAR(255),
- * tstamp INT NOT NULL,
- * action_type INT NOT NULL,
- * parent INT DEFAULT NULL,
+ * resource VARCHAR(255),
+ * tstamp INT NOT NULL,
+ * action_type INT NOT NULL,
+ * parent INT DEFAULT NULL,
* payload_id INT DEFAULT NULL
* );
- *
+ *
* CREATE TABLE ef_versioning_payloads(
- * id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
+ * id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
* statement_hash LONGTEXT
* );
*
@@ -32,7 +32,7 @@
* @package Erfurt
* @author Philipp Frischmuth <prischmuth@googlemail.com>
* @author Norman Heino <norman.heino@gmail.com>
- * @copyright Copyright (c) 2012, {@link http://aksw.org AKSW}
+ * @copyright Copyright (c) 2013, {@link http://aksw.org AKSW}
* @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
*/
class Erfurt_Versioning
@@ -43,21 +43,21 @@ class Erfurt_Versioning
const STATEMENT_CHANGED = 21;
const STATEMENT_REMOVED = 22;
const STATEMENTS_ROLLBACK = 23;
-
+
protected $_currentAction = null;
protected $_currentActionParent = null;
-
+
protected $_versioningEnabled = false;
-
+
protected $_limit = 10;
-
+
protected $_store = null;
-
+
protected $_user = null;
-
+
protected $_eventsRegistered = false;
-
+
/**
* Enables or disables versioning.
*
@@ -77,11 +77,11 @@ public function enableVersioning($versioningEnabled = true)
$eventDispatcher->register('onAddMultipleStatements', $this);
$eventDispatcher->register('onDeleteMatchingStatements', $this);
$eventDispatcher->register('onDeleteMultipleStatements', $this);
-
+
$this->_eventsRegistered = true;
}
}
-
+
/**
* Stopping current action if possible throws Exception else
*/
@@ -90,7 +90,7 @@ public function endAction()
if (!$this->isVersioningEnabled()) {
return;
}
-
+
// no action to end?
if (null === $this->_currentAction) {
throw new Exception('Action not started');
@@ -116,15 +116,16 @@ private function _abortAction()
// do nothing
}
}
-
+
/**
* abort current action
* TODO: is it ok to have this method also public?
*/
- public function abortAction(){
+ public function abortAction()
+ {
$this->_abortAction();
- }
-
+ }
+
/**
* Probably shortcut?
*/
@@ -133,14 +134,14 @@ public function getLastModifiedForResource($resourceUri, $graphUri)
$this->_checkSetup();
$history = $this->getHistoryForResource($resourceUri, $graphUri);
-
+
if (is_array($history) && count($history) > 0) {
return $history[0];
} else {
return null;
}
}
-
+
/**
* get the versioning actions for a specific model
*
@@ -155,16 +156,16 @@ public function getHistoryForGraph($graphUri, $page = 1)
'FROM ef_versioning_actions WHERE
model = \'' . $graphUri . '\'
ORDER BY tstamp DESC';
-
+
$result = $this->_sqlQuery(
- $sql,
- $this->getLimit() + 1,
+ $sql,
+ $this->getLimit() + 1,
$page * $this->getLimit() - $this->getLimit()
);
-
+
return $result;
}
-
+
/**
* In difference to getHistoryForGraph, this method do result history
* actions but the last changed resources
@@ -175,15 +176,14 @@ public function getHistoryForGraph($graphUri, $page = 1)
public function getConciseHistoryForGraph($graphUri, $page = 1)
{
$this->_checkSetup();
-
- $sql = 'SELECT useruri, resource, MAX(tstamp) FROM ef_versioning_actions WHERE
- model = \'' . $graphUri . '\'
- GROUP BY useruri, resource
- ORDER BY 3 DESC';
-
+ $sql = 'SELECT useruri, resource, MAX(tstamp) FROM ef_versioning_actions WHERE
+ model = \'' . $graphUri . '\'
+ GROUP BY useruri, resource
+ ORDER BY 3 DESC';
+
$result = $this->_sqlQuery(
- $sql,
- $this->getLimit() + 1,
+ $sql,
+ $this->getLimit() + 1,
$page * $this->getLimit() - $this->getLimit()
);
@@ -207,13 +207,12 @@ public function getModifiedResources($graphUri, $timestamp = 0)
model = \'' . $graphUri . '\' AND
tstamp >= \'' . $timestamp . '\'
ORDER BY tstamp DESC';
-
+
$result = $this->_sqlQuery($sql);
return $result;
}
-
/**
* get the versioning actions for a specific resource of a model
*
@@ -224,25 +223,25 @@ public function getModifiedResources($graphUri, $timestamp = 0)
public function getHistoryForResource($resourceUri, $graphUri, $page = 1)
{
$this->_checkSetup();
-
+
$sql = 'SELECT v2.id, v2.useruri, v2.tstamp, v2.action_type
FROM ef_versioning_actions AS v1, ef_versioning_actions AS v2
- WHERE
- v1.model = \'' . $graphUri . '\' AND
+ WHERE
+ v1.model = \'' . $graphUri . '\' AND
v1.resource = \'' . $resourceUri . '\' AND
v2.id = v1.parent
UNION
SELECT id, useruri, tstamp, action_type
FROM ef_versioning_actions
- WHERE
- model = \'' . $graphUri . '\' AND
+ WHERE
+ model = \'' . $graphUri . '\' AND
resource = \'' . $resourceUri . '\' AND
parent IS NULL
ORDER BY tstamp DESC';
$result = $this->_sqlQuery(
- $sql,
- $this->getLimit() + 1,
+ $sql,
+ $this->getLimit() + 1,
$page * $this->getLimit() - $this->getLimit()
);
@@ -255,19 +254,19 @@ public function getHistoryForResourceList($resources, $graphUri, $page = 1)
$sql = 'SELECT id, resource, useruri, tstamp, action_type ' .
'FROM ef_versioning_actions WHERE
- model = \'' . $graphUri . '\' AND ( resource = \'' . implode ('\' OR resource = \'' ,$resources) . '\' )
+ model = \'' . $graphUri . '\' AND ( resource = \'' . implode('\' OR resource = \'', $resources) . '\' )
AND parent IS NULL
ORDER BY tstamp DESC';
-
+
$result = $this->_sqlQuery(
- $sql,
- $this->getLimit() + 1,
+ $sql,
+ $this->getLimit() + 1,
($page - 1) * $this->getLimit()
);
-
+
return $result;
- }
-
+ }
+
public function getHistoryForUser($userUri, $page = 1)
{
$this->_checkSetup();
@@ -276,20 +275,20 @@ public function getHistoryForUser($userUri, $page = 1)
'FROM ef_versioning_actions WHERE
useruri = \'' . $userUri . '\'
ORDER BY tstamp DESC';
-
+
$result = $this->_sqlQuery(
- $sql,
- $this->getLimit() + 1,
+ $sql,
+ $this->getLimit() + 1,
$page * $this->getLimit() - $this->getLimit()
);
-
+
return $result;
}
-
- /*
- * Gets latest changes for user on all resources for dashboard
- */
- public function getHistoryForUserDash($userUri)
+
+ /**
+ * Gets latest changes for user on all resources for dashboard
+ */
+ public function getHistoryForUserDash($userUri)
{
$this->_checkSetup();
@@ -297,34 +296,34 @@ public function getHistoryForUserDash($userUri)
'FROM ef_versioning_actions WHERE
useruri = \'' . $userUri . '\'
ORDER BY tstamp DESC';
-
+
$result = $this->_sqlQuery(
- $sql,
- $this->getLimit() + 1,
+ $sql,
+ $this->getLimit() + 1,
$this->getLimit() - $this->getLimit()
);
-
+
return $result;
}
-
+
public function getLimit()
{
return $this->_limit;
}
-
+
/**
* Returns whether an action is currently running or not.
- *
+ *
* @return bool Returns true iff an action is currently started, else false.
*/
public function isActionStarted()
{
return (null !== $this->_currentAction);
}
-
+
/**
* Returns whether versioning is currently enabled or not.
- *
+ *
* @return bool Returns true iff versioning is enabled, false else.
*/
public function isVersioningEnabled()
@@ -337,10 +336,10 @@ public function setLimit($limit)
if ($limit <= 0) {
throw new Exception('Invalid value for limit. Must be postive integer.');
}
-
+
$this->_limit = (int) $limit;
}
-
+
public function onAddStatement(Erfurt_Event $event)
{
if (!$this->isVersioningEnabled()) {
@@ -350,7 +349,6 @@ public function onAddStatement(Erfurt_Event $event)
$this->_checkSetup();
if (is_array($event->statement)) {
-
$payload = array (
$event->statement['subject'] => array (
$event->statement['predicate'] => array (
@@ -366,7 +364,7 @@ public function onAddStatement(Erfurt_Event $event)
// do nothing
}
}
-
+
public function onAddMultipleStatements(Erfurt_Event $event)
{
if (!$this->isVersioningEnabled()) {
@@ -383,7 +381,7 @@ public function onAddMultipleStatements(Erfurt_Event $event)
// do nothing
}
}
-
+
public function onDeleteMatchingStatements(Erfurt_Event $event)
{
if (!$this->isVersioningEnabled()) {
@@ -391,7 +389,6 @@ public function onDeleteMatchingStatements(Erfurt_Event $event)
}
$this->_checkSetup();
-
$graphUri = $event->graphUri;
if (isset($event->statements)) {
@@ -409,12 +406,9 @@ public function onDeleteMultipleStatements(Erfurt_Event $event)
}
$this->_checkSetup();
-
$graphUri = $event->graphUri;
-
$this->_execAddPayloadsAndActions($graphUri, self::STATEMENT_REMOVED, $event->statements);
}
-
/**
* Restores a change made to the store directly identified by an actionid inside
@@ -426,51 +420,40 @@ public function onDeleteMultipleStatements(Erfurt_Event $event)
* @param integer $actionid identifies the action to restore
* @return boolean true if everythings goes fine false otherwise
*/
- public function rollbackAction($actionId)
+ public function rollbackAction($actionId)
{
$this->_checkSetup();
$actionsSql = 'SELECT action_type, payload_id, model, parent FROM ef_versioning_actions WHERE ' .
'( id = ' . ((int)$actionId) . ' OR parent = ' . ((int)$actionId) . ' ) ' .
'AND payload_id IS NOT NULL';
-
+
$result = $this->_sqlQuery($actionsSql);
-
+
if ( count($result) == 0 || $result[0]['payload_id'] === null ) {
$this->_abortAction();
$dedicatedException = 'No valid entry in ef_versioning_actions for action ID';
throw new Exception('No rollback possible (' . $dedicatedException . ')');
-
- return false;
-
} else {
-
foreach ($result as $i) {
-
$type = (int) $i['action_type'];
$modelUri = isset($i['model']) ? $i['model'] : null;
$payloadID = (int) $i['payload_id'];
$payloadsSql = 'SELECT statement_hash FROM ef_versioning_payloads WHERE id = ' .
$payloadID;
-
+
$payloadResult = $this->_sqlQuery($payloadsSql);
if (count($payloadResult) !== 1) {
-
$dedicatedException = 'No valid entry in ef_versioning_payloads for payload ID';
throw new Exception('No rollback possible (' . $dedicatedException . ')');
-
- return false;
-
} else {
-
if (isset($payloadResult[0]['statement_hash'])) {
$payload = unserialize($payloadResult[0]['statement_hash']);
} else {
$payload = null;
}
-
if ($type === self::STATEMENT_ADDED) {
$this->_getStore()->deleteMultipleStatements($modelUri, $payload);
@@ -479,16 +462,13 @@ public function rollbackAction($actionId)
} else {
// do nothing
}
-
}
-
}
return true;
-
}
}
-
+
/**
* Starts a log action to which subsequent statement modifications are added.
*
@@ -501,7 +481,11 @@ public function startAction($actionSpec)
// action already running?
if (null !== $this->_currentAction) {
- throw new Exception('Action already started (type='.$this->_currentAction['type'].', modeluri='.$this->_currentAction['modeluri'].', resourceuri='.$this->_currentAction['resourceuri'].')');
+ throw new Exception(
+ 'Action already started (type=' . $this->_currentAction['type'] . ', ' .
+ 'modeluri=' . $this->_currentAction['modeluri'] . ', ' .
+ 'resourceuri=' . $this->_currentAction['resourceuri'] . ')'
+ );
} elseif ($this->isVersioningEnabled() ) {
$actionType = $actionSpec['type'];
$graphUri = $actionSpec['modeluri'];
@@ -512,7 +496,7 @@ public function startAction($actionSpec)
// do nothing
}
}
-
+
public function setUserUri($uri)
{
$this->_user = $uri;
@@ -528,11 +512,11 @@ public function getDetailsForAction($id)
{
$this->_checkSetup();
- $detailsSql = 'SELECT actions.action_type, payloads.statement_hash ' .
- ' FROM ef_versioning_actions AS actions, ' .
+ $detailsSql = 'SELECT actions.action_type, payloads.statement_hash ' .
+ ' FROM ef_versioning_actions AS actions, ' .
' ef_versioning_payloads AS payloads ' .
- 'WHERE ' .
- '( actions.id = ' . $id . ' OR actions.parent = ' . $id . ' ) ' .
+ 'WHERE ' .
+ '( actions.id = ' . $id . ' OR actions.parent = ' . $id . ' ) ' .
'AND actions.payload_id IS NOT NULL ' .
'AND actions.payload_id = payloads.id ';
@@ -545,7 +529,7 @@ public function getDetailsForAction($id)
* Deletes all history information on a specific model
* use with caution
*/
- public function deleteHistoryForModel($graphUri)
+ public function deleteHistoryForModel($graphUri)
{
$this->_checkSetup();
@@ -565,7 +549,7 @@ public function deleteHistoryForModel($graphUri)
foreach ($result as $r ) {
$idArray[] = $r['payload_id'];
- }
+ }
sort($idArray, SORT_NUMERIC);
@@ -599,57 +583,54 @@ public function deleteHistoryForModel($graphUri)
$sqldeletePayload = 'DELETE FROM ef_versioning_payloads WHERE ';
if ( ($i + 100) < $sizeOfRanges ) {
- $sqldeletePayload .= implode ('OR',array_slice($ranges,$i,100));
+ $sqldeletePayload .= implode('OR', array_slice($ranges, $i, 100));
} else {
$length = ( $sizeOfRanges ) % 100;
- $sqldeletePayload .= implode ('OR',array_slice($ranges,$i,$length));
+ $sqldeletePayload .= implode('OR', array_slice($ranges, $i, $length));
}
$resultPayload = $this->_sqlQuery($sqldeletePayload);
}
}
-
// finally delete actions
$sqldeleteAction = 'DELETE FROM ef_versioning_actions WHERE
model = \'' . $graphUri . '\' OR resource = \'' . $graphUri . '\'';
$resultAction = $this->_sqlQuery($sqldeleteAction);
-
-
}
-
+
private function _execAddAction($graphUri, $resource, $actionType, $payloadId = null)
{
if ($this->_user === null) {
$this->_user = $this->_getAuth()->getIdentity()->getUri();
- }
+ }
$userUri = $this->_user;
-
+
$actionsSql = 'INSERT INTO ef_versioning_actions (model, useruri, resource, tstamp, action_type, parent';
-
+
if (null !== $payloadId) {
$actionsSql .= ', payload_id)';
} else {
$actionsSql .= ')';
}
-
+
if (null !== $this->_currentActionParent) {
$actionParent = $this->_currentActionParent;
} else {
$actionParent = 'NULL';
}
- $actionsSql .= ' VALUES (\'' .
+ $actionsSql .= ' VALUES (\'' .
addslashes($graphUri) . '\', \'' .
- addslashes($userUri) . '\', \'' .
- addslashes($resource) . '\', \'' . time() . '\', ' .
+ addslashes($userUri) . '\', \'' .
+ addslashes($resource) . '\', \'' . time() . '\', ' .
addslashes($actionType) . ', ' . $actionParent;
-
+
if (null !== $payloadId) {
$actionsSql .= ', ' . $payloadId . ')';
} else {
$actionsSql .= ')';
- }
+ }
$this->_sqlQuery($actionsSql);
@@ -658,55 +639,53 @@ private function _execAddAction($graphUri, $resource, $actionType, $payloadId =
return $parentActionId;
}
}
-
+
private function _execAddPayload($payload)
{
$payloadsSql = 'INSERT INTO ef_versioning_payloads (statement_hash) VALUES (\'' .
addslashes(serialize($payload)) . '\')';
-
+
$this->_sqlQuery($payloadsSql);
$payloadId = $this->_getStore()->lastInsertId();
-
+
return $payloadId;
}
-
- private function _execAddPayloadsAndActions($graphUri, $actionType, $statements)
+
+ private function _execAddPayloadsAndActions($graphUri, $actionType, $statements)
{
foreach ($statements as $s => $poArray) {
foreach ($poArray as $p => $oArray) {
foreach ($oArray as $i => $oSpec) {
$statement = array($s => array($p => array($oSpec)));
-
$payloadId = $this->_execAddPayload($statement);
-
$this->_execAddAction($graphUri, $s, $actionType, $payloadId);
}
}
}
}
-
+
protected function _getStore()
{
if (null === $this->_store) {
$this->_store = Erfurt_App::getInstance()->getStore();
}
-
+
return $this->_store;
}
-
+
protected function _getAuth()
{
$app = Erfurt_App::getInstance();
return $app->getAuth();
}
-
+
/**
* late setup function for time saving and mocking in test cases
*/
- private function _checkSetup()
+ private function _checkSetup()
{
$this->_initialize();
- }
+ }
private function _initialize()
{
@@ -714,7 +693,7 @@ private function _initialize()
if (!$this->_getStore()->isSqlSupported()) {
throw new Exception('For versioning support store adapter needs to implement the SQL interface.');
}
-
+
$existingTableNames = $this->_getStore()->listTables();
if (!in_array('ef_versioning_actions', $existingTableNames)) {
@@ -728,34 +707,34 @@ private function _initialize()
'parent' => 'INT DEFAULT NULL',
'payload_id' => 'INT DEFAULT NULL'
);
-
+
$this->_getStore()->createTable('ef_versioning_actions', $columnSpec);
}
-
+
if (!in_array('ef_versioning_payloads', $existingTableNames)) {
$columnSpec = array(
'id' => 'INT PRIMARY KEY AUTO_INCREMENT',
'statement_hash' => 'LONGTEXT'
);
-
+
$this->_getStore()->createTable('ef_versioning_payloads', $columnSpec);
- }
+ }
}
-
+
protected function _sqlQuery($sql, $limit = PHP_INT_MAX, $offset = 0)
{
try {
$result = $this->_getStore()->sqlQuery($sql, $limit, $offset);
} catch (Erfurt_Exception $e) {
$this->_checkSetup();
-
+
try {
$result = $this->_getStore()->sqlQuery($sql, $limit, $offset);
- } catch (Erfurt_Exception $e2) {
- throw new Erfurt_Exception('Erfurt_Versioning _sqlQuery failed: ' . $e2->getMessage() . $sql);
+ } catch (Erfurt_Exception $f) {
+ throw new Erfurt_Exception('Erfurt_Versioning _sqlQuery failed: ' . $f->getMessage() . $sql);
}
}
-
+
return $result;
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.