Migrate to simple orm
parent
6e7818a967
commit
7b858e75b5
@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* ownCloud - Documents App
|
||||
*
|
||||
* @author Victor Dubiniuk
|
||||
* @copyright 2013 Victor Dubiniuk victor.dubiniuk@gmail.com
|
||||
*
|
||||
* This file is licensed under the Affero General Public License version 3 or
|
||||
* later.
|
||||
*/
|
||||
|
||||
namespace OCA\Documents;
|
||||
|
||||
class Db_Member extends Db{
|
||||
|
||||
const DB_TABLE = '`*PREFIX*documents_member`';
|
||||
|
||||
const ACTIVITY_THRESHOLD = 60; // 1 Minute
|
||||
|
||||
const MEMBER_STATUS_ACTIVE = 1;
|
||||
const MEMBER_STATUS_INACTIVE = 2;
|
||||
|
||||
protected $tableName = '`*PREFIX*documents_member`';
|
||||
|
||||
protected $insertStatement = 'INSERT INTO `*PREFIX*documents_member` (`es_id`, `uid`, `color`, `last_activity`)
|
||||
VALUES (?, ?, ?, ?)';
|
||||
|
||||
protected $loadStatement = 'SELECT * FROM `*PREFIX*documents_member` WHERE `member_id`= ?';
|
||||
|
||||
public function updateActivity($memberId){
|
||||
return $this->execute(
|
||||
'UPDATE ' . $this->tableName . ' SET `last_activity`=?, `status`=? WHERE `member_id`=?',
|
||||
array(
|
||||
time(),
|
||||
self::MEMBER_STATUS_ACTIVE,
|
||||
$memberId
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark members as inactive
|
||||
* @param string $esId - session Id
|
||||
* @return array - list of memberId that were marked as inactive
|
||||
*/
|
||||
public function updateByTimeout($esId){
|
||||
$time = $this->getInactivityPeriod();
|
||||
|
||||
$result = $this->execute('
|
||||
SELECT `member_id`
|
||||
FROM ' . self::DB_TABLE . '
|
||||
WHERE `es_id`= ?
|
||||
AND `last_activity`<?
|
||||
AND `status`=?
|
||||
',
|
||||
array(
|
||||
$esId,
|
||||
$time,
|
||||
self::MEMBER_STATUS_ACTIVE
|
||||
)
|
||||
);
|
||||
|
||||
$deactivated = $result->fetchAll();
|
||||
if (is_array($deactivated) && count($deactivated)){
|
||||
$deactivated = array_map(
|
||||
function($x){
|
||||
return ($x['member_id']);
|
||||
}, $deactivated
|
||||
);
|
||||
$this->deactivate($deactivated);
|
||||
} else {
|
||||
$deactivated = array();
|
||||
}
|
||||
|
||||
return $deactivated;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update members to inactive state
|
||||
* @param array $memberIds
|
||||
*/
|
||||
protected function deactivate($memberIds){
|
||||
$stmt = $this->buildInQuery('member_id', $memberIds);
|
||||
array_unshift($memberIds, self::MEMBER_STATUS_INACTIVE);
|
||||
$this->execute('
|
||||
UPDATE ' . $this->tableName . '
|
||||
SET `status`=?
|
||||
WHERE ' . $stmt,
|
||||
$memberIds
|
||||
);
|
||||
}
|
||||
|
||||
protected function getInactivityPeriod(){
|
||||
return time() - self::ACTIVITY_THRESHOLD;
|
||||
}
|
||||
}
|
@ -0,0 +1,163 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* ownCloud - Documents App
|
||||
*
|
||||
* @author Victor Dubiniuk
|
||||
* @copyright 2013 Victor Dubiniuk victor.dubiniuk@gmail.com
|
||||
*
|
||||
* This file is licensed under the Affero General Public License version 3 or
|
||||
* later.
|
||||
*/
|
||||
|
||||
namespace OCA\Documents;
|
||||
|
||||
/**
|
||||
* Session management
|
||||
*/
|
||||
class Db_Session extends \OCA\Documents\Db {
|
||||
|
||||
/**
|
||||
* DB table
|
||||
*/
|
||||
const DB_TABLE = '`*PREFIX*documents_session`';
|
||||
protected $tableName = '`*PREFIX*documents_session`';
|
||||
|
||||
protected $insertStatement = 'INSERT INTO `*PREFIX*documents_session` (`es_id`, `genesis_url`, `genesis_hash`, `owner`, `file_id`)
|
||||
VALUES (?, ?, ?, ?, ?)';
|
||||
|
||||
protected $loadStatement = 'SELECT * FROM `*PREFIX*documents_session` WHERE `es_id`= ?';
|
||||
|
||||
/**
|
||||
* Start a editing session or return an existing one
|
||||
* @param string $uid of the user starting a session
|
||||
* @param \OCA\Documents\File $file - file object
|
||||
* @return array
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function start($uid, File $file){
|
||||
list($ownerView, $path) = $file->getOwnerViewAndPath();
|
||||
|
||||
$oldSession = new Db_Session();
|
||||
$oldSession->loadBy('file_id', $file->getFileId());
|
||||
|
||||
//If there is no existing session we need to start a new one
|
||||
if (!$oldSession->hasData()){
|
||||
|
||||
//TODO: check if genesis document is a valid odt
|
||||
$genesisPath = $ownerView->storeDocument($ownerView, $path);
|
||||
if (!$genesisPath){
|
||||
throw new \Exception('Unable to copy document. Check permissions and make sure you have enought free space.');
|
||||
}
|
||||
|
||||
$hash = $ownerView->getHashByGenesis($genesisPath);
|
||||
|
||||
$newSession = new Db_Session(array(
|
||||
$genesisPath, $hash, $file->getOwner(), $file->getFileId()
|
||||
));
|
||||
|
||||
if (!$newSession->insert()){
|
||||
throw new \Exception('Failed to add session into database');
|
||||
}
|
||||
}
|
||||
|
||||
$session = $oldSession
|
||||
->loadBy('file_id', $file->getFileId())
|
||||
->getData()
|
||||
;
|
||||
|
||||
$member = new Db_Member(array(
|
||||
$session['es_id'],
|
||||
$uid,
|
||||
Helper::getRandomColor()
|
||||
));
|
||||
if ($member->insert()){
|
||||
$session['member_id'] = (string) $member->getLastInsertId();
|
||||
} else {
|
||||
throw new \Exception('Failed to add member into database');
|
||||
}
|
||||
|
||||
$session['permissions'] = $ownerView->getFilePermissions($path);
|
||||
|
||||
return $session;
|
||||
}
|
||||
|
||||
public static function cleanUp($esId){
|
||||
$session = new Db_Session();
|
||||
$session->deleteBy('es_id', $esId);
|
||||
|
||||
$member = new Db_Member();
|
||||
$member->deleteBy('es_id', $esId);
|
||||
|
||||
$op= new Db_Op();
|
||||
$op->deleteBy('es_id', $esId);
|
||||
}
|
||||
|
||||
public function insert(){
|
||||
$esId = $this->getUniqueSessionId();
|
||||
array_unshift($this->data, $esId);
|
||||
return parent::insert($this->data);
|
||||
}
|
||||
|
||||
public function getInfo($esId){
|
||||
$result = $this->execute('
|
||||
SELECT `s`.*, COUNT(`m`.`member_id`) AS `users`
|
||||
FROM ' . $this->tableName . ' AS `s`
|
||||
LEFT JOIN `*PREFIX*documents_member` AS `m` ON `s`.`es_id`=`m`.`es_id`
|
||||
AND `m`.`status`=' . Db_Member::MEMBER_STATUS_ACTIVE . '
|
||||
AND `m`.`uid` != ?
|
||||
WHERE `s`.`es_id` = ?
|
||||
GROUP BY `m`.`es_id`
|
||||
',
|
||||
array(
|
||||
\OCP\User::getUser(),
|
||||
$esId
|
||||
)
|
||||
);
|
||||
|
||||
$info = $result->fetchRow();
|
||||
if (!is_array($info)){
|
||||
$info = array();
|
||||
}
|
||||
return $info;
|
||||
}
|
||||
|
||||
public function getInfoByFileId($fileIds){
|
||||
if (!is_array($fileIds)){
|
||||
return array();
|
||||
}
|
||||
|
||||
$stmt = $this->buildInQuery('file_id', $fileIds);
|
||||
|
||||
$result = $this->execute('
|
||||
SELECT `s`.*, COUNT(`m`.`member_id`) AS `users`
|
||||
FROM ' . $this->tableName . ' AS `s`
|
||||
LEFT JOIN `*PREFIX*documents_member` AS `m` ON `s`.`es_id`=`m`.`es_id`
|
||||
AND `m`.`status`=' . Db_Member::MEMBER_STATUS_ACTIVE . '
|
||||
WHERE `s`.`file_id` ' . $stmt .'
|
||||
GROUP BY `m`.`es_id`',
|
||||
$fileIds
|
||||
);
|
||||
|
||||
$info = $result->fetchAll();
|
||||
if (!is_array($info)){
|
||||
$info = array();
|
||||
}
|
||||
return $info;
|
||||
}
|
||||
|
||||
protected function getUniqueSessionId(){
|
||||
$testSession = new Db_Session();
|
||||
do{
|
||||
// this prevents branching for stable5 for now:
|
||||
// OC_Util::generate_random_bytes was camelCased
|
||||
if (method_exists('\OC_Util', 'generate_random_bytes')){
|
||||
$id = \OC_Util::generate_random_bytes(30);
|
||||
} else {
|
||||
$id = \OC_Util::generateRandomBytes(30);
|
||||
}
|
||||
}while ($testSession->load($id)->hasData());
|
||||
|
||||
return $id;
|
||||
}
|
||||
}
|
@ -1,133 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* ownCloud - Documents App
|
||||
*
|
||||
* @author Victor Dubiniuk
|
||||
* @copyright 2013 Victor Dubiniuk victor.dubiniuk@gmail.com
|
||||
*
|
||||
* This file is licensed under the Affero General Public License version 3 or
|
||||
* later.
|
||||
*/
|
||||
|
||||
namespace OCA\Documents;
|
||||
|
||||
class Member extends Db{
|
||||
|
||||
const DB_TABLE = '`*PREFIX*documents_member`';
|
||||
|
||||
const ACTIVITY_THRESHOLD = 60; // 10 Minutes
|
||||
|
||||
const MEMBER_STATUS_ACTIVE = 1;
|
||||
const MEMBER_STATUS_INACTIVE = 2;
|
||||
|
||||
public static function add($esId, $uid, $color){
|
||||
$query = \OCP\DB::prepare('
|
||||
INSERT INTO ' . self::DB_TABLE . ' (`es_id`, `uid`, `color`, `last_activity`)
|
||||
VALUES (?, ?, ?, ?)
|
||||
');
|
||||
$query->execute(array(
|
||||
$esId,
|
||||
$uid,
|
||||
$color,
|
||||
time()
|
||||
));
|
||||
|
||||
return \OCP\DB::insertid(self::DB_TABLE);
|
||||
}
|
||||
|
||||
public static function getMember($id){
|
||||
$query = \OCP\DB::prepare('SELECT * FROM ' . self::DB_TABLE . ' WHERE `member_id`= ?');
|
||||
$result = $query->execute(array($id));
|
||||
return $result->fetchRow();
|
||||
}
|
||||
|
||||
public static function getMembersAsArray($ids){
|
||||
$memberCount = count($ids);
|
||||
if (!$memberCount || !is_array($ids)){
|
||||
return array();
|
||||
}
|
||||
|
||||
$stmt = self::buildPlaceholders($ids);
|
||||
$query = \OCP\DB::prepare('SELECT * FROM ' . self::DB_TABLE . ' WHERE `member_id` IN (' . $stmt . ')');
|
||||
$result = $query->execute($ids);
|
||||
return $result->fetchAll();
|
||||
}
|
||||
|
||||
public static function updateMemberActivity($memberId){
|
||||
$query = \OCP\DB::prepare('UPDATE ' . self::DB_TABLE . ' SET `last_activity`=?, `status`=? WHERE `member_id`=?');
|
||||
$query->execute(array(
|
||||
time(),
|
||||
self::MEMBER_STATUS_ACTIVE,
|
||||
$memberId
|
||||
));
|
||||
}
|
||||
|
||||
public static function getMembersByEsId($esId, $lastActivity = null){
|
||||
if (is_null($lastActivity)){
|
||||
$activeSince = self::getInactivityPeriod();
|
||||
} else {
|
||||
$activeSince = $lastActivity;
|
||||
}
|
||||
|
||||
$query = \OCP\DB::prepare('SELECT * FROM ' . self::DB_TABLE . ' WHERE `es_id`= ? AND `last_activity` > ?');
|
||||
$result = $query->execute(array($esId, $activeSince));
|
||||
return $result->fetchAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark memebers as inactive
|
||||
* @param string $esId - session Id
|
||||
* @return array - list of memberId that were marked as inactive
|
||||
*/
|
||||
public static function cleanSession($esId){
|
||||
$time = self::getInactivityPeriod();
|
||||
|
||||
$query = \OCP\DB::prepare('
|
||||
SELECT `member_id`
|
||||
FROM ' . self::DB_TABLE . '
|
||||
WHERE `es_id`= ?
|
||||
AND `last_activity`<?
|
||||
AND `status`=?
|
||||
');
|
||||
$result = $query->execute(array(
|
||||
$esId,
|
||||
$time,
|
||||
self::MEMBER_STATUS_ACTIVE
|
||||
));
|
||||
$deactivated = $result->fetchAll();
|
||||
|
||||
self::deactivate($esId, $time);
|
||||
|
||||
return $deactivated;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update members to inactive state
|
||||
* @param string $esId
|
||||
* @param timestamp $time
|
||||
*/
|
||||
protected static function deactivate($esId, $time){
|
||||
$query = \OCP\DB::prepare('
|
||||
UPDATE ' . self::DB_TABLE . '
|
||||
SET `status`=?
|
||||
WHERE `es_id`=?
|
||||
AND `last_activity`<?
|
||||
');
|
||||
$query->execute(array(
|
||||
self::MEMBER_STATUS_INACTIVE,
|
||||
$esId,
|
||||
$time
|
||||
));
|
||||
}
|
||||
|
||||
public static function deleteBySessionId($esId){
|
||||
$query = \OCP\DB::prepare('DELETE FROM ' . self::DB_TABLE . ' WHERE `es_id` = ?');
|
||||
$query->execute(array($esId));
|
||||
}
|
||||
|
||||
protected static function getInactivityPeriod(){
|
||||
return time() - self::ACTIVITY_THRESHOLD;
|
||||
}
|
||||
|
||||
}
|
@ -1,183 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* ownCloud - Documents App
|
||||
*
|
||||
* @author Victor Dubiniuk
|
||||
* @copyright 2013 Victor Dubiniuk victor.dubiniuk@gmail.com
|
||||
*
|
||||
* This file is licensed under the Affero General Public License version 3 or
|
||||
* later.
|
||||
*/
|
||||
|
||||
namespace OCA\Documents;
|
||||
|
||||
class Session extends Db {
|
||||
|
||||
const DB_TABLE = '`*PREFIX*documents_session`';
|
||||
|
||||
public static function start($uid, File $file){
|
||||
list($ownerView, $path) = $file->getOwnerViewAndPath();
|
||||
$session = Session::getSessionByFileId( $file->getFileId() );
|
||||
|
||||
//If there is no existing session we need to start a new one
|
||||
if (!$session || empty($session)){
|
||||
|
||||
$genesisPath = $ownerView->storeDocument($ownerView, $path);
|
||||
|
||||
if (!$genesisPath){
|
||||
throw new \Exception('Unable to copy document. Check permissions and make sure you have enought free space.');
|
||||
}
|
||||
|
||||
$hash = $ownerView->getHashByGenesis($genesisPath);
|
||||
$session = Session::add(
|
||||
$genesisPath, $hash, $file->getOwner(), $file->getFileId()
|
||||
);
|
||||
}
|
||||
|
||||
$session['permissions'] = $ownerView->getFilePermissions($path);
|
||||
$session['member_id'] = (string) Member::add($session['es_id'], $uid, Helper::getRandomColor());
|
||||
|
||||
return $session;
|
||||
}
|
||||
|
||||
public static function add($genesis, $hash, $owner, $fileId){
|
||||
$query = \OCP\DB::prepare('
|
||||
INSERT INTO ' . self::DB_TABLE . ' (`es_id`, `genesis_url`, `genesis_hash`, `owner`, `file_id`)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
');
|
||||
|
||||
$data = array(
|
||||
'es_id' => self::getUniqueSessionId(),
|
||||
'genesis_url' => $genesis,
|
||||
'genesis_hash' => $hash,
|
||||
'owner' => $owner,
|
||||
'file_id' => $fileId
|
||||
);
|
||||
$result = $query->execute(array_values($data));
|
||||
|
||||
if ($result){
|
||||
return $data;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function getAll(){
|
||||
$query = \OCP\DB::prepare('SELECT * FROM ' . self::DB_TABLE);
|
||||
$result = $query->execute();
|
||||
return $result->fetchAll();
|
||||
}
|
||||
|
||||
public static function getSession($id){
|
||||
$query = \OCP\DB::prepare('SELECT * FROM ' . self::DB_TABLE . ' WHERE `es_id`= ?');
|
||||
$result = $query->execute(array($id));
|
||||
return $result->fetchRow();
|
||||
}
|
||||
|
||||
public static function getInfo($esId){
|
||||
|
||||
$query = \OCP\DB::prepare('
|
||||
SELECT `s`.*, COUNT(`m`.`member_id`) AS `users`
|
||||
FROM ' . self::DB_TABLE . ' AS `s`
|
||||
LEFT JOIN `*PREFIX*documents_member` AS `m` ON `s`.`es_id`=`m`.`es_id`
|
||||
AND `m`.`status`=' . Member::MEMBER_STATUS_ACTIVE . '
|
||||
AND `m`.`uid` != ?
|
||||
WHERE `s`.`es_id` = ?
|
||||
GROUP BY `m`.`es_id`
|
||||
');
|
||||
$result = $query->execute(
|
||||
array(
|
||||
\OCP\User::getUser(),
|
||||
$esId
|
||||
)
|
||||
);
|
||||
|
||||
$info = $result->fetchRow();
|
||||
if (!is_array($info)){
|
||||
$info = array();
|
||||
}
|
||||
return $info;
|
||||
}
|
||||
|
||||
public static function getSessionByFileId($fileId){
|
||||
$sessions = self::getSessionsByFileIds(array($fileId));
|
||||
if (count($sessions) > 1){
|
||||
Helper::errorLog('documents', 'more than one session found for file id ' . $fileId);
|
||||
}
|
||||
|
||||
if (count($sessions)){
|
||||
return $sessions[0];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function getSessionsByFileIds($fileIds){
|
||||
if (!is_array($fileIds)){
|
||||
$fileIds = array($fileIds);
|
||||
}
|
||||
|
||||
$stmt = self::buildPlaceholders($fileIds);
|
||||
$query = \OCP\DB::prepare('SELECT * FROM ' . self::DB_TABLE . ' WHERE `file_id` IN (' . $stmt . ')');
|
||||
$result = $query->execute($fileIds);
|
||||
$sessions = $result->fetchAll();
|
||||
if (!is_array($sessions)){
|
||||
$sessions = array();
|
||||
}
|
||||
return $sessions;
|
||||
}
|
||||
|
||||
public static function getInfoByFileid($fileIds){
|
||||
if (!is_array($fileIds)){
|
||||
return array();
|
||||
}
|
||||
|
||||
$stmt = self::buildPlaceholders($fileIds);
|
||||
if (!$stmt){
|
||||
return array();
|
||||
}
|
||||
|
||||
$query = \OCP\DB::prepare('
|
||||
SELECT `s`.*, COUNT(`m`.`member_id`) AS `users`
|
||||
FROM ' . self::DB_TABLE . ' AS `s`
|
||||
LEFT JOIN `*PREFIX*documents_member` AS `m` ON `s`.`es_id`=`m`.`es_id`
|
||||
AND `m`.`status`=' . Member::MEMBER_STATUS_ACTIVE . '
|
||||
WHERE `s`.`file_id` IN (' . $stmt . ')
|
||||
GROUP BY `m`.`es_id`
|
||||
');
|
||||
$result = $query->execute($fileIds);
|
||||
|
||||
$info = $result->fetchAll();
|
||||
if (!is_array($info)){
|
||||
$info = array();
|
||||
}
|
||||
return $info;
|
||||
}
|
||||
|
||||
public static function cleanUp($esId){
|
||||
self::delete($esId);
|
||||
Member::deleteBySessionId($esId);
|
||||
Op::deleteBySessionId($esId);
|
||||
}
|
||||
|
||||
public static function delete($esId){
|
||||
$query = \OCP\DB::prepare('DELETE FROM ' . self::DB_TABLE . ' WHERE `es_id` = ?');
|
||||
$query->execute(array($esId));
|
||||
}
|
||||
|
||||
protected static function getUniqueSessionId(){
|
||||
do{
|
||||
// this prevents branching for stable5 for now:
|
||||
// OC_Util::generate_random_bytes was camelCased
|
||||
if (method_exists('\OC_Util', 'generate_random_bytes')){
|
||||
$id = \OC_Util::generate_random_bytes(30);
|
||||
} else {
|
||||
$id = \OC_Util::generateRandomBytes(30);
|
||||
}
|
||||
}while (self::getSession($id));
|
||||
|
||||
return $id;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue