function recount() { $app = JFactory::getApplication (); $state = $app->getUserState ( 'com_kunena.admin.recount', null ); if ($state === null) { // First run $query = "SELECT MAX(id) FROM #__kunena_messages"; $db = JFactory::getDBO(); $db->setQuery ( $query ); $state = new StdClass(); $state->step = 0; $state->maxId = (int) $db->loadResult (); $state->start = 0; } $this->checkTimeout(); while (1) { $count = mt_rand(95000, 105000); switch ($state->step) { case 0: // Update topic statistics kimport('kunena.forum.topic.helper'); KunenaForumTopicHelper::recount(false, $state->start, $state->start+$count); $state->start += $count; //$app->enqueueMessage ( JText::sprintf('COM_KUNENA_ADMIN_RECOUNT_TOPICS', min($state->start, $state->maxId), $state->maxId) ); break; case 1: // Update usertopic statistics kimport('kunena.forum.topic.user.helper'); KunenaForumTopicUserHelper::recount(false, $state->start, $state->start+$count); $state->start += $count; //$app->enqueueMessage ( JText::sprintf('COM_KUNENA_ADMIN_RECOUNT_USERTOPICS', min($state->start, $state->maxId), $state->maxId) ); break; case 2: // Update user statistics kimport('kunena.user.helper'); KunenaUserHelper::recount(); //$app->enqueueMessage ( JText::sprintf('COM_KUNENA_ADMIN_RECOUNT_USER') ); break; case 3: // Update category statistics kimport('kunena.forum.category.helper'); KunenaForumCategoryHelper::recount(); //$app->enqueueMessage ( JText::sprintf('COM_KUNENA_ADMIN_RECOUNT_CATEGORY') ); break; default: $app->setUserState ( 'com_kunena.admin.recount', null ); $app->enqueueMessage (JText::_('COM_KUNENA_RECOUNTFORUMS_DONE')); $this->setRedirect(KunenaRoute::_('index.php?option=com_kunena', false)); return; } if (!$state->start || $state->start > $state->maxId) { $state->step++; $state->start = 0; } if ($this->checkTimeout()) break; } $app->setUserState ( 'com_kunena.admin.recount', $state ); $this->setRedirect(KunenaRoute::_('index.php?option=com_kunena&view=recount&task=recount', false)); }
/** * Test getTopics() */ public function testGetTopics() { list($count, $topics) = KunenaForumTopicHelper::getLatestTopics(false, 0, 20); $topicusers = KunenaForumTopicUserHelper::getTopics($topics); foreach ($topics as $topic) { $this->assertTrue(isset($topicusers[$topic->id])); $this->assertEquals($topic->id, $topicusers[$topic->id]->topic_id); } }
/** * Returns KunenaForumTopicUser object * * @access public * @param identifier The user topic to load - Can be only an integer. * @return KunenaForumTopicUser The user topic object. * @since 1.7 */ static public function get($topic = null, $user = null, $reload = false) { if ($topic instanceof KunenaForumTopic) { $topic = $topic->id; } $topic = intval ( $topic ); $user = KunenaUserHelper::get($user); if ($topic < 1) return new KunenaForumTopicUser (null, $user); if ($reload || empty ( self::$_instances [$user->userid][$topic] )) { self::$_instances [$user->userid][$topic] = self::$_topics [$topic][$user->userid] = array_pop(KunenaForumTopicUserHelper::getTopics ( $topic, $user )); } return self::$_instances [$user->userid][$topic]; }
function recountCategories() { $app = JFactory::getApplication(); $state = $app->getUserState('com_kunena.install.recount', null); // Only perform this stage if database needs recounting (upgrade from older version) $version = $this->getVersion(); if (version_compare($version->version, '2.0.0-DEV', ">")) { return true; } if ($state === null) { // First run $query = "SELECT MAX(id) FROM #__kunena_messages"; $this->db->setQuery($query); $state = new StdClass(); $state->step = 0; $state->maxId = (int) $this->db->loadResult(); $state->start = 0; } while (1) { $count = mt_rand(95000, 105000); switch ($state->step) { case 0: // Update topic statistics KunenaForumTopicHelper::recount(false, $state->start, $state->start + $count); $state->start += $count; $this->addStatus(JText::sprintf('COM_KUNENA_MIGRATE_RECOUNT_TOPICS', min($state->start, $state->maxId), $state->maxId), true, '', 'recount'); break; case 1: // Update usertopic statistics KunenaForumTopicUserHelper::recount(false, $state->start, $state->start + $count); $state->start += $count; $this->addStatus(JText::sprintf('COM_KUNENA_MIGRATE_RECOUNT_USERTOPICS', min($state->start, $state->maxId), $state->maxId), true, '', 'recount'); break; case 2: // Update user statistics KunenaUserHelper::recount(); $this->addStatus(JText::sprintf('COM_KUNENA_MIGRATE_RECOUNT_USER'), true, '', 'recount'); break; case 3: // Update category statistics KunenaForumCategoryHelper::recount(); $this->addStatus(JText::sprintf('COM_KUNENA_MIGRATE_RECOUNT_CATEGORY'), true, '', 'recount'); break; default: $app->setUserState('com_kunena.install.recount', null); $this->addStatus(JText::_('COM_KUNENA_MIGRATE_RECOUNT_DONE'), true, '', 'recount'); return true; } if (!$state->start || $state->start > $state->maxId) { $state->step++; $state->start = 0; } if ($this->checkTimeout()) { break; } } $app->setUserState('com_kunena.install.recount', $state); return false; }
/** * Perform recount on statistics in smaller chunks. * * @return void * @throws Exception */ public function dorecount() { $ajax = $this->input->getWord('format', 'html') == 'json'; if (!JSession::checkToken('request')) { $this->setResponse(array('success' => false, 'header' => JText::_('COM_KUNENA_AJAX_ERROR'), 'message' => JText::_('COM_KUNENA_AJAX_DETAILS_BELOW'), 'error' => JText::_('COM_KUNENA_ERROR_TOKEN')), $ajax); $this->setRedirect(KunenaRoute::_($this->baseurl, false)); return; } $state = $this->app->getUserState('com_kunena.admin.recount', null); try { $this->checkTimeout(); while (1) { // Topic count per run. // TODO: count isn't accurate as it can overflow total. $count = mt_rand(4500, 5500); switch ($state->step) { case 0: if ($state->topics) { // Update topic statistics KunenaForumTopicHelper::recount(false, $state->start, $state->start + $count); $state->start += $count; $msg = JText::sprintf('COM_KUNENA_ADMIN_RECOUNT_TOPICS_X', round(min(100 * $state->start / $state->maxId + 1, 100)) . '%'); } break; case 1: if ($state->usertopics) { // Update user's topic statistics KunenaForumTopicUserHelper::recount(false, $state->start, $state->start + $count); $state->start += $count; $msg = JText::sprintf('COM_KUNENA_ADMIN_RECOUNT_USERTOPICS_X', round(min(100 * $state->start / $state->maxId + 1, 100)) . '%'); } break; case 2: if ($state->categories) { // Update category statistics KunenaForumCategoryHelper::recount(); KunenaForumCategoryHelper::fixAliases(); $msg = JText::sprintf('COM_KUNENA_ADMIN_RECOUNT_CATEGORIES_X', '100%'); } break; case 3: if ($state->users) { // Update user statistics KunenaUserHelper::recount(); $msg = JText::sprintf('COM_KUNENA_ADMIN_RECOUNT_USERS_X', '100%'); } break; case 4: if ($state->polls) { // Update user statistics KunenaForumTopicPollHelper::recount(); $msg = JText::sprintf('COM_KUNENA_ADMIN_RECOUNT_POLLS_X', '100%'); } break; default: $header = JText::_('COM_KUNENA_RECOUNTFORUMS_DONE'); $msg = JText::_('COM_KUNENA_AJAX_REQUESTED_RECOUNTED'); $this->app->setUserState('com_kunena.admin.recount', null); $this->setResponse(array('success' => true, 'status' => '100%', 'header' => $header, 'message' => $msg), $ajax); $this->setRedirect(KunenaRoute::_($this->baseurl, false), $header); return; } $state->current = min($state->current + $count, $state->total); if (!$state->start || $state->start > $state->maxId) { $state->step++; $state->start = 0; } if ($this->checkTimeout()) { break; } } $state->reload++; $this->app->setUserState('com_kunena.admin.recount', $state); } catch (Exception $e) { if (!$ajax) { throw $e; } $this->setResponse(array('success' => false, 'status' => sprintf("%2.1f%%", 99 * $state->current / ($state->total + 1)), 'header' => JText::_('COM_KUNENA_AJAX_ERROR'), 'message' => JText::_('COM_KUNENA_AJAX_DETAILS_BELOW'), 'error' => $e->getMessage()), $ajax); } $token = JSession::getFormToken() . '=1'; $redirect = KunenaRoute::_("{$this->baseurl}&task=dorecount&i={$state->reload}&{$token}", false); $this->setResponse(array('success' => true, 'status' => sprintf("%2.1f%%", 99 * $state->current / ($state->total + 1)), 'header' => JText::_('COM_KUNENA_AJAX_RECOUNT_WAIT'), 'message' => $msg, 'href' => $redirect), $ajax); }
/** * @param mixed $ids * @param mixed $user * * @return KunenaForumTopicUser[] */ public static function getUserTopics($ids = false, $user = null) { if ($ids === false) { $ids = array_keys(self::$_instances); } return KunenaForumTopicUserHelper::getTopics($ids, $user); }
/** * @param KunenaForumMessage $message * @param int $postdelta * * @return bool */ public function update($message = null, $postdelta = 0) { // Update post count $this->posts += $postdelta; $exists = $message && $message->exists(); if (!$this->exists()) { if (!$exists) { $this->setError(JText::_('COM_KUNENA_LIB_TOPIC_NOT_EXISTS')); return false; } $this->id = $message->id; } if ($exists && $message->thread == $this->id && $message->hold == $this->hold) { // If message belongs into this topic and has same state, we may need to update cache $this->updatePostInfo($message->id, $message->time, $message->userid, $message->message, $message->name); } elseif (!$this->moved_id) { if (!isset($this->hold)) { $this->hold = KunenaForum::TOPIC_DELETED; } // If message isn't visible anymore, check if we need to update cache if (!$exists || $this->first_post_id == $message->id) { // If message got deleted and was cached, we need to find new first post $db = JFactory::getDBO(); $query = "SELECT * FROM #__kunena_messages AS m INNER JOIN #__kunena_messages_text AS t ON t.mesid=m.id\n\t\t\t\t\tWHERE m.thread={$db->quote($this->id)} AND m.hold={$this->hold} ORDER BY m.time ASC, m.id ASC"; $db->setQuery($query, 0, 1); $first = $db->loadObject(); KunenaError::checkDatabaseError(); if ($first) { $this->first_post_time = 0; $this->updatePostInfo($first->id, $first->time, $first->userid, $first->message, $first->name); } else { $this->updatePostInfo(false); } } if (!$exists || $this->last_post_id == $message->id) { // If topic got deleted and was cached, we need to find new last post $db = JFactory::getDBO(); $query = "SELECT * FROM #__kunena_messages AS m INNER JOIN #__kunena_messages_text AS t ON t.mesid=m.id\n\t\t\t\t\tWHERE m.thread={$db->quote($this->id)} AND m.hold={$this->hold} ORDER BY m.time DESC, m.id DESC"; $db->setQuery($query, 0, 1); $last = $db->loadObject(); KunenaError::checkDatabaseError(); if ($last) { $this->last_post_time = 0; $this->updatePostInfo($last->id, $last->time, $last->userid, $last->message, $last->name); } else { $this->updatePostInfo(false); } } } if (!$this->first_post_id || !$this->last_post_id) { // If topic has no visible posts, mark it deleted and recount $this->hold = $exists ? $message->hold : KunenaForum::TOPIC_DELETED; $this->recount(); } if (!($message && $message->exists()) && !$this->posts) { return $this->delete(); } if (!$this->save()) { return false; } if ($exists && $message->userid && abs($postdelta) <= 1) { // Update user topic $usertopic = $this->getUserTopic($message->userid); if (!$usertopic->update($message, $postdelta)) { $this->setError($usertopic->getError()); } // Update post count from user $user = KunenaUserHelper::get($message->userid); $user->posts += $postdelta; if (!$user->save()) { $this->setError($user->getError()); } } else { KunenaForumTopicUserHelper::recount($this->id); // FIXME: optimize KunenaUserHelper::recount(); } return true; }
public static function getInstance($id = null, $user = null, $reload = false) { return KunenaForumTopicUserHelper::get($id, $user, $reload); }
function recount() { $state = $this->app->getUserState('com_kunena.admin.recount', null); if ($state === null) { // First run: get last message id (if topics were created with <K2.0) $query = "SELECT MAX(id) FROM #__kunena_messages"; $db = JFactory::getDBO(); $db->setQuery($query); $state = new StdClass(); $state->step = 0; $state->maxId = (int) $db->loadResult(); $state->start = 0; $state->reload = 0; $state->topics = JRequest::getBool('topics', false); $state->usertopics = JRequest::getBool('usertopics', false); $state->categories = JRequest::getBool('categories', false); $state->users = JRequest::getBool('users', false); } $this->checkTimeout(); while (1) { $count = mt_rand(95000, 105000); switch ($state->step) { case 0: if ($state->topics) { // Update topic statistics KunenaForumTopicHelper::recount(false, $state->start, $state->start + $count); $state->start += $count; //$this->app->enqueueMessage ( JText::sprintf('COM_KUNENA_ADMIN_RECOUNT_TOPICS', min($state->start, $state->maxId), $state->maxId) ); } break; case 1: if ($state->usertopics) { // Update usertopic statistics KunenaForumTopicUserHelper::recount(false, $state->start, $state->start + $count); $state->start += $count; //$this->app->enqueueMessage ( JText::sprintf('COM_KUNENA_ADMIN_RECOUNT_USERTOPICS', min($state->start, $state->maxId), $state->maxId) ); } break; case 2: if ($state->categories) { // Update category statistics KunenaForumCategoryHelper::recount(); //$this->app->enqueueMessage ( JText::sprintf('COM_KUNENA_ADMIN_RECOUNT_CATEGORY') ); } break; case 3: if ($state->users) { // Update user statistics KunenaUserHelper::recount(); //$this->app->enqueueMessage ( JText::sprintf('COM_KUNENA_ADMIN_RECOUNT_USER') ); } break; default: $this->app->setUserState('com_kunena.admin.recount', null); $this->app->enqueueMessage(JText::_('COM_KUNENA_RECOUNTFORUMS_DONE')); $this->setRedirect(KunenaRoute::_($this->baseurl, false)); return; } if (!$state->start || $state->start > $state->maxId) { $state->step++; $state->start = 0; } if ($this->checkTimeout()) { break; } } $state->reload++; $this->app->setUserState('com_kunena.admin.recount', $state); $this->setRedirect(KunenaRoute::_("{$this->baseurl}&task=recount&i={$state->reload}", false)); }
/** * Free up memory by cleaning up all cached items. */ public static function cleanup() { self::$_instances = array(); self::$_topics = array(); }