1
0
mirror of https://github.com/wbbaddons/Tims-Chat.git synced 2024-12-22 21:40:08 +00:00

Adding cleanup-cronjob (Currently missing the PIP)

Moving getPackageID into the Util
This commit is contained in:
Tim Düsterhus 2012-03-08 22:07:46 +01:00
parent 8b0dd6e391
commit 6e89b04751
10 changed files with 142 additions and 63 deletions

View File

@ -2,7 +2,6 @@
namespace wcf\acp\form;
use \wcf\system\exception\UserInputException;
use \wcf\system\language\I18nHandler;
use \wcf\system\package\PackageDependencyHandler;
use \wcf\system\WCF;
/**
@ -99,7 +98,7 @@ public function save() {
$roomID = $returnValues['returnValues']->roomID;
if (!I18nHandler::getInstance()->isPlainValue('title')) {
I18nHandler::getInstance()->save('title', 'wcf.chat.room.title'.$roomID, 'wcf.chat.room', PackageDependencyHandler::getInstance()->getPackageID('timwolla.wcf.chat'));
I18nHandler::getInstance()->save('title', 'wcf.chat.room.title'.$roomID, 'wcf.chat.room', \wcf\util\ChatUtil::getPackageID());
// update title
$chatRoomEditor->update(array(
@ -108,7 +107,7 @@ public function save() {
}
if (!I18nHandler::getInstance()->isPlainValue('topic')) {
I18nHandler::getInstance()->save('topic', 'wcf.chat.room.topic'.$roomID, 'wcf.chat.room', PackageDependencyHandler::getInstance()->getPackageID('timwolla.wcf.chat'));
I18nHandler::getInstance()->save('topic', 'wcf.chat.room.topic'.$roomID, 'wcf.chat.room', \wcf\util\ChatUtil::getPackageID());
// update topic
$chatRoomEditor->update(array(

View File

@ -1,7 +1,6 @@
<?php
namespace wcf\acp\form;
use \wcf\system\language\I18nHandler;
use \wcf\system\package\PackageDependencyHandler;
use \wcf\system\WCF;
/**
@ -64,20 +63,20 @@ public function save() {
$this->title = 'wcf.chat.room.title'.$this->roomObj->roomID;
if (I18nHandler::getInstance()->isPlainValue('title')) {
I18nHandler::getInstance()->remove($this->title, PackageDependencyHandler::getInstance()->getPackageID('timwolla.wcf.chat'));
I18nHandler::getInstance()->remove($this->title, \wcf\util\ChatUtil::getPackageID());
$this->title = I18nHandler::getInstance()->getValue('title');
}
else {
I18nHandler::getInstance()->save('title', $this->title, 'wcf.chat.room', PackageDependencyHandler::getInstance()->getPackageID('timwolla.wcf.chat'));
I18nHandler::getInstance()->save('title', $this->title, 'wcf.chat.room', \wcf\util\ChatUtil::getPackageID());
}
$this->topic = 'wcf.chat.room.topic'.$this->roomObj->roomID;
if (I18nHandler::getInstance()->isPlainValue('topic')) {
I18nHandler::getInstance()->remove($this->topic, PackageDependencyHandler::getInstance()->getPackageID('timwolla.wcf.chat'));
I18nHandler::getInstance()->remove($this->topic, \wcf\util\ChatUtil::getPackageID());
$this->topic = I18nHandler::getInstance()->getValue('topic');
}
else {
I18nHandler::getInstance()->save('topic', $this->topic, 'wcf.chat.room', PackageDependencyHandler::getInstance()->getPackageID('timwolla.wcf.chat'));
I18nHandler::getInstance()->save('topic', $this->topic, 'wcf.chat.room', \wcf\util\ChatUtil::getPackageID());
}
\wcf\system\acl\ACLHandler::getInstance()->save($this->roomID, $this->objectTypeID);
@ -105,8 +104,8 @@ public function readData() {
parent::readData();
if (!count($_POST)) {
I18nHandler::getInstance()->setOptions('title', PackageDependencyHandler::getInstance()->getPackageID('timwolla.wcf.chat'), $this->roomObj->title, 'wcf.chat.room.title\d+');
I18nHandler::getInstance()->setOptions('topic', PackageDependencyHandler::getInstance()->getPackageID('timwolla.wcf.chat'), $this->roomObj->topic, 'wcf.chat.room.topic\d+');
I18nHandler::getInstance()->setOptions('title', \wcf\util\ChatUtil::getPackageID(), $this->roomObj->title, 'wcf.chat.room.title\d+');
I18nHandler::getInstance()->setOptions('topic', \wcf\util\ChatUtil::getPackageID(), $this->roomObj->topic, 'wcf.chat.room.topic\d+');
$this->title = $this->roomObj->title;
$this->topic = $this->roomObj->topic;

View File

@ -23,17 +23,19 @@ class ChatMessageEditor extends \wcf\data\DatabaseObjectEditor {
* @param integer $lifetime Delete messages older that this time.
* @return integer Number of deleted messages.
*/
public static function cleanup($lifetime = CHAT_ARCHIVETIME) {
public static function prune($lifetime = CHAT_ARCHIVETIME) {
$baseClass = self::$baseClass;
$sql = "SELECT
".static::getDatabaseIndexName()."
".$baseClass::getDatabaseTableIndexName()."
FROM
".static::getDatabaseTableName()."
".$baseClass::getDatabaseTableName()."
WHERE
time < ?";
$statement = \wcf\system\WCF::getDB()->prepareStatement($sql);
$statement->execute(TIME_NOW - $lifetime);
time < ?";
$stmt = \wcf\system\WCF::getDB()->prepareStatement($sql);
$stmt->execute(array(TIME_NOW - $lifetime));
$objectIDs = array();
while ($objectIDs[] = $statement->fetchColumn());
return static::deleteAll($objectIDs);
while ($objectIDs[] = $stmt->fetchColumn());
return self::deleteAll($objectIDs);
}
}

View File

@ -43,7 +43,7 @@ public function __toString() {
* @return integer
*/
public function countUsers() {
$packageID = \wcf\system\package\PackageDependencyHandler::getInstance()->getPackageID('timwolla.wcf.chat');
$packageID = \wcf\util\ChatUtil::getPackageID();
$sql = "SELECT
count(*) as count
@ -100,7 +100,7 @@ public function getTitle() {
* @return array<\wcf\data\user\User>
*/
public function getUsers() {
$packageID = \wcf\system\package\PackageDependencyHandler::getInstance()->getPackageID('timwolla.wcf.chat');
$packageID = \wcf\util\ChatUtil::getPackageID();
$sql = "SELECT
userID

View File

@ -17,19 +17,13 @@ class ChatRoomEditor extends \wcf\data\DatabaseObjectEditor implements \wcf\data
*/
protected static $baseClass = '\wcf\data\chat\room\ChatRoom';
/**
* Clears the room cache.
*/
public static function resetCache() {
\wcf\system\cache\CacheHandler::getInstance()->clear(WCF_DIR.'cache', 'cache.chatrooms.php');
}
/**
* @see \wcf\data\DatabaseObjectEditor::deleteAll()
*/
public static function deleteAll(array $objectIDs = array()) {
parent::deleteAll($objectIDs);
$packageID = \wcf\system\package\PackageDependencyHandler::getInstance()->getPackageID('timwolla.wcf.chat');
$packageID = \wcf\util\ChatUtil::getPackageID();
WCF::getDB()->beginTransaction();
foreach ($objectIDs as $objectID) {
@ -40,4 +34,40 @@ public static function deleteAll(array $objectIDs = array()) {
return count($objectIDs);
}
/**
* Deletes temporary rooms that are unused.
*
* @return integer Number of deleted rooms
*/
public static function prune() {
$baseClass = self::$baseClass;
$sql = "SELECT
".$baseClass::getDatabaseTableIndexName()."
FROM
".$baseClass::getDatabaseTableName()."
WHERE
permanent = ?
AND roomID NOT IN(
SELECT
fieldValue AS roomID
FROM
wcf".WCF_N."_user_storage
WHERE
packageID = ?
AND field = ?)";
$stmt = \wcf\system\WCF::getDB()->prepareStatement($sql);
$stmt->execute(array(0, \wcf\util\ChatUtil::getPackageID(), 'roomID'));
$objectIDs = array();
while ($objectIDs[] = $stmt->fetchColumn());
return self::deleteAll($objectIDs);
}
/**
* Clears the room cache.
*/
public static function resetCache() {
\wcf\system\cache\CacheHandler::getInstance()->clear(WCF_DIR.'cache', 'cache.chatrooms.php');
}
}

View File

@ -88,7 +88,7 @@ public function readData() {
try {
\wcf\util\ChatUtil::writeUserData(array('lastSeen' => end($this->newestMessages)->messageID));
}
catch (\wcf\system\SystemException $e) {
catch (\wcf\system\exception\SystemException $e) {
\wcf\util\ChatUtil::writeUserData(array('lastSeen' => 0));
}
@ -161,12 +161,15 @@ public function show() {
if (!WCF::getUser()->userID) {
throw new \wcf\system\exception\PermissionDeniedException();
}
\wcf\system\menu\page\PageMenu::getInstance()->setActiveMenuItem('wcf.header.menu.chat');
// remove index breadcrumb
WCF::getBreadcrumbs()->remove(0);
parent::show();
// break if not ajax
// break if not using ajax
if ($this->useTemplate) exit;
@header('Content-type: application/json');

View File

@ -31,9 +31,9 @@ public function getData(array $cacheResource) {
FROM wcf".WCF_N."_acl_option acl_option,
wcf".WCF_N."_acl_option_to_group option_to_group
".$conditionBuilder;
$statement = WCF::getDB()->prepareStatement($sql);
$statement->execute($conditionBuilder->getParameters());
while ($row = $statement->fetchArray()) {
$stmt = WCF::getDB()->prepareStatement($sql);
$stmt->execute($conditionBuilder->getParameters());
while ($row = $stmt->fetchArray()) {
if (!isset($data[$row['roomID']][$row['permission']])) $data[$row['roomID']][$row['permission']] = $row['optionValue'];
else $data[$row['roomID']][$row['permission']] = $row['optionValue'] || $data[$row['roomID']][$row['permission']];
}

View File

@ -27,7 +27,7 @@ class ChatPermissionHandler extends \wcf\system\SingletonFactory {
* @see \wcf\system\SingletonFactory::init()
*/
protected function init() {
$packageID = PackageDependencyHandler::getInstance()->getPackageID('timwolla.wcf.chat');
$packageID = \wcf\util\ChatUtil::getPackageID();
$ush = \wcf\system\user\storage\UserStorageHandler::getInstance();
// get groups permissions
@ -58,9 +58,9 @@ protected function init() {
FROM wcf".WCF_N."_acl_option acl_option,
wcf".WCF_N."_acl_option_to_user option_to_user
".$conditionBuilder;
$statement = WCF::getDB()->prepareStatement($sql);
$statement->execute($conditionBuilder->getParameters());
while ($row = $statement->fetchArray()) {
$stmt = WCF::getDB()->prepareStatement($sql);
$stmt->execute($conditionBuilder->getParameters());
while ($row = $stmt->fetchArray()) {
$userPermissions[$row['roomID']][$row['permission']] = $row['optionValue'];
}
@ -97,7 +97,7 @@ public function getPermission(\wcf\data\chat\room\ChatRoom $room, $permission) {
* Clears the cache.
*/
public static function clearCache() {
$packageID = PackageDependencyHandler::getInstance()->getPackageID('timwolla.wcf.chat');
$packageID = \wcf\util\ChatUtil::getPackageID();
$ush = \wcf\system\user\storage\UserStorageHandler::getInstance();
$ush->resetAll('chatUserPermissions', $packageID);

View File

@ -0,0 +1,21 @@
<?php
namespace wcf\system\cronjob;
/**
* Vaporizes unneeded data.
*
* @author Tim Düsterhus
* @copyright 2010-2012 Tim Düsterhus
* @license Creative Commons Attribution-NonCommercial-ShareAlike <http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode>
* @package timwolla.wcf.chat
* @subpackage system.cronjob
*/
class ChatCleanupCronjob implements ICronjob {
/**
* @see wcf\system\ICronjob::execute()
*/
public function execute(\wcf\data\cronjob\Cronjob $cronjob) {
\wcf\data\chat\message\ChatMessageEditor::prune();
\wcf\data\chat\room\ChatRoomEditor::prune();
}
}

View File

@ -22,8 +22,43 @@ class ChatUtil {
*/
const TIME_MODIFIER_REGEX = '((?:[0-9]+[s|h|d|w|m|y|S|H|D|W|M|Y]?,?)+)';
/**
* Package identifier of Tims Chat.
*
* @var string
*/
const PACKAGE_IDENTIFIER = 'timwolla.wcf.chat';
public static $serialize = array('color' => true);
/**
* Cached packageID of Tims Chat.
*
* @var integer
*/
private static $packageID = null;
/**
* Returns the packageID of Tims Chat.
*/
public static function getPackageID() {
if (self::$packageID === null) {
self::$packageID = PackageDependencyHandler::getInstance()->getPackageID(self::PACKAGE_IDENTIFIER);
}
return self::$packageID;
}
/**
* Returns a random number.
*
* @return integer
*/
public static function /* int */ getRandomNumber() {
return 4; // chosen by a fair dice roll
// guaranteed to be random
}
/**
* Creates a gradient out of two colors represented by an integer.
* The first byte is red, the second byte is green, the third one is blue.
@ -58,7 +93,7 @@ public static function gradient($string, $start, $end) {
*/
public static function readUserData($field) {
$ush = UserStorageHandler::getInstance();
$packageID = PackageDependencyHandler::getInstance()->getPackageID('timwolla.wcf.chat');
$packageID = self::getPackageID();
// load storage
$ush->loadStorage(array(WCF::getUser()->userID), $packageID);
@ -79,30 +114,6 @@ public static function readUserData($field) {
else return $data[WCF::getUser()->userID];
}
/**
* Returns a random number.
*
* @return integer
*/
public static function /* int */ getRandomNumber() {
return 4; // chosen by a fair dice roll
// guaranteed to be random
}
/**
* Writes user data
*
* @param array $data
*/
public static function writeUserData(array $data) {
$ush = UserStorageHandler::getInstance();
$packageID = PackageDependencyHandler::getInstance()->getPackageID('timwolla.wcf.chat');
foreach($data as $key => $value) {
$ush->update(WCF::getUser()->userID, $key, (isset(static::$serialize[$key])) ? serialize($value) : $value, $packageID);
}
}
/**
* Splits a string into smaller chunks.
* UTF-8 safe version of str_split().
@ -175,4 +186,18 @@ public static function timeModifier($time) {
return (int) round($result, 0);
}
/**
* Writes user data
*
* @param array $data
*/
public static function writeUserData(array $data) {
$ush = UserStorageHandler::getInstance();
$packageID = self::getPackageID();
foreach($data as $key => $value) {
$ush->update(WCF::getUser()->userID, $key, (isset(static::$serialize[$key])) ? serialize($value) : $value, $packageID);
}
}
}