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)); }
function doprune() { $app = JFactory::getApplication (); if (!JRequest::checkToken()) { $app->enqueueMessage ( JText::_ ( 'COM_KUNENA_ERROR_TOKEN' ), 'error' ); $this->setRedirect(KunenaRoute::_($this->baseurl, false)); return; } $category = KunenaForumCategoryHelper::get(JRequest::getInt ( 'prune_forum', 0 )); if (!$category->authorise('admin')) { $app->enqueueMessage ( JText::_ ( 'COM_KUNENA_CHOOSEFORUMTOPRUNE' ), 'error' ); $this->setRedirect(KunenaRoute::_($this->baseurl, false)); return; } // Convert days to seconds for timestamp functions... $prune_days = JRequest::getInt ( 'prune_days', 36500 ); $prune_date = JFactory::getDate()->toUnix() - ($prune_days * 86400); $trashdelete = JRequest::getInt( 'trashdelete', 0); // Get up to 100 oldest topics to be deleted $params = array( 'orderby'=>'tt.last_post_time ASC', 'where'=>"AND tt.last_post_time<{$prune_date} AND ordering=0", ); list($count, $topics) = KunenaForumTopicHelper::getLatestTopics($category->id, 0, 100, $params); $deleted = 0; foreach ( $topics as $topic ) { $deleted++; if ( $trashdelete ) $topic->delete(false); else $topic->trash(); } KunenaUserHelper::recount(); KunenaForumCategoryHelper::recount(); KunenaForumMessageAttachmentHelper::cleanup(); if ( $trashdelete ) $app->enqueueMessage ( "" . JText::_('COM_KUNENA_FORUMPRUNEDFOR') . " " . $prune_days . " " . JText::_('COM_KUNENA_PRUNEDAYS') . "; " . JText::_('COM_KUNENA_PRUNEDELETED') . " {$deleted}/{$count} " . JText::_('COM_KUNENA_PRUNETHREADS') ); else $app->enqueueMessage ( "" . JText::_('COM_KUNENA_FORUMPRUNEDFOR') . " " . $prune_days . " " . JText::_('COM_KUNENA_PRUNEDAYS') . "; " . JText::_('COM_KUNENA_PRUNETRASHED') . " {$deleted}/{$count} " . JText::_('COM_KUNENA_PRUNETHREADS') ); $this->setRedirect(KunenaRoute::_($this->baseurl, false)); }
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); }
/** * Method to delete the KunenaForumCategory object from the database * * @access public * @return boolean True on success * @since 1.6 */ public function delete() { if (!$this->exists()) { return true; } // Create the user table object $table = &$this->getTable (); $result = $table->delete ( $this->id ); if (! $result) { $this->setError ( $table->getError () ); } $this->_exists = false; $access = KunenaFactory::getAccessControl(); $access->clearCache(); $db = JFactory::getDBO (); // Delete moderators $queries[] = "DELETE FROM #__kunena_moderation WHERE catid={$db->quote($this->id)}"; // Delete user topics $queries[] = "DELETE FROM #__kunena_user_topics WHERE category_id={$db->quote($this->id)}"; // Delete user categories $queries[] = "DELETE FROM #__kunena_user_categories WHERE category_id={$db->quote($this->id)}"; // Delete user read $queries[] = "DELETE FROM #__kunena_user_read WHERE category_id={$db->quote($this->id)}"; // Delete thank yous $queries[] = "DELETE t FROM #__kunena_thankyou AS t INNER JOIN #__kunena_messages AS m ON m.id=t.postid WHERE m.catid={$db->quote($this->id)}"; // Delete poll users $queries[] = "DELETE p FROM #__kunena_polls_users AS p INNER JOIN #__kunena_topics AS tt ON tt.poll_id=p.pollid WHERE tt.category_id={$db->quote($this->id)} AND tt.moved_id=0"; // Delete poll options $queries[] = "DELETE p FROM #__kunena_polls_options AS p INNER JOIN #__kunena_topics AS tt ON tt.poll_id=p.pollid WHERE tt.category_id={$db->quote($this->id)} AND tt.moved_id=0"; // Delete polls $queries[] = "DELETE p FROM #__kunena_polls AS p INNER JOIN #__kunena_topics AS tt ON tt.poll_id=p.id WHERE tt.category_id={$db->quote($this->id)} AND tt.moved_id=0"; // Delete messages $queries[] = "DELETE m, t FROM #__kunena_messages AS m INNER JOIN #__kunena_messages_text AS t ON m.id=t.mesid WHERE m.catid={$db->quote($this->id)}"; // TODO: delete attachments foreach ($queries as $query) { $db->setQuery($query); $db->query(); KunenaError::checkDatabaseError (); } KunenaUserHelper::recount(); KunenaForumCategoryHelper::recount(); return $result; }
/** * @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; }
function restore() { if (!JSession::checkToken('post')) { $this->app->enqueueMessage(JText::_('COM_KUNENA_ERROR_TOKEN'), 'error'); $this->app->redirect(KunenaRoute::_($this->baseurl, false)); } $cid = JRequest::getVar('cid', array(), 'post', 'array'); // Array of integers JArrayHelper::toInteger($cid); $type = JRequest::getCmd('type', 'topics', 'post'); if (empty($cid)) { $this->app->enqueueMessage(JText::_('COM_KUNENA_A_NO_MESSAGES_SELECTED'), 'notice'); $this->app->redirect(KunenaRoute::_($this->baseurl, false)); } $nb_items = 0; if ($type == 'messages') { $messages = KunenaForumMessageHelper::getMessages($cid, 'none'); foreach ($messages as $target) { if ($target->publish(KunenaForum::PUBLISHED)) { $nb_items++; } else { $this->app->enqueueMessage($target->getError(), 'notice'); } } } elseif ($type == 'topics') { $topics = KunenaForumTopicHelper::getTopics($cid, 'none'); foreach ($topics as $target) { if ($target->publish(KunenaForum::PUBLISHED)) { $nb_items++; } else { $this->app->enqueueMessage($target->getError(), 'notice'); } } } else { // Error... } if ($nb_items > 0) { $this->app->enqueueMessage(JText::sprintf('COM_KUNENA_TRASH_ITEMS_RESTORE_DONE', $nb_items)); } KunenaUserHelper::recount(); KunenaForumTopicHelper::recount(); KunenaForumCategoryHelper::recount(); $this->app->redirect(KunenaRoute::_($this->baseurl, false)); }
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)); }
function restore() { $app = JFactory::getApplication (); if (! JRequest::checkToken ()) { $app->enqueueMessage ( JText::_ ( 'COM_KUNENA_ERROR_TOKEN' ), 'error' ); $app->redirect ( KunenaRoute::_($this->baseurl, false) ); } $kunena_db = JFactory::getDBO (); $cid = JRequest::getVar ( 'cid', array (), 'post', 'array' ); $topics = JRequest::getInt ( 'topics', 0, 'post' ); $messages = JRequest::getInt ( 'messages', 0, 'post' ); if (empty ( $cid )) { $app->enqueueMessage ( JText::_ ( 'COM_KUNENA_A_NO_MESSAGES_SELECTED' ), 'notice' ); $app->redirect ( KunenaRoute::_($this->baseurl, false) ); } $msg = JText::_('COM_KUNENA_TRASH_RESTORE_DONE'); if ( $messages ) { $messages = KunenaForumMessageHelper::getMessages($cid); foreach ( $messages as $target ) { if ( $target->authorise('undelete') && $target->publish(KunenaForum::PUBLISHED) ) { $app->enqueueMessage ( $msg ); } else { $app->enqueueMessage ( $target->getError(), 'notice' ); } } } elseif ( $topics ) { $topics = KunenaForumTopicHelper::getTopics($cid); foreach ( $topics as $target ) { if ( $target->authorise('undelete') && $target->publish(KunenaForum::PUBLISHED) ) { $app->enqueueMessage ( $msg ); } else { $app->enqueueMessage ( $target->getError(), 'notice' ); } } } else { // error } KunenaUserHelper::recount(); KunenaForumTopicHelper::recount(); KunenaForumCategoryHelper::recount (); $app->redirect(KunenaRoute::_($this->baseurl, false)); }
/** * Delete this category and all related information from the database. * * @return bool True on success */ public function delete() { if (!$this->exists()) { return true; } if (!parent::delete()) { return false; } $access = KunenaAccess::getInstance(); $access->clearCache(); $db = JFactory::getDBO(); $queries[] = "DELETE FROM #__kunena_aliases WHERE type='catid' AND item={$db->quote($this->id)}"; // Delete user topics $queries[] = "DELETE FROM #__kunena_user_topics WHERE category_id={$db->quote($this->id)}"; // Delete user categories $queries[] = "DELETE FROM #__kunena_user_categories WHERE category_id={$db->quote($this->id)}"; // Delete user read $queries[] = "DELETE FROM #__kunena_user_read WHERE category_id={$db->quote($this->id)}"; // Delete thank yous $queries[] = "DELETE t FROM #__kunena_thankyou AS t INNER JOIN #__kunena_messages AS m ON m.id=t.postid WHERE m.catid={$db->quote($this->id)}"; // Delete poll users $queries[] = "DELETE p FROM #__kunena_polls_users AS p INNER JOIN #__kunena_topics AS tt ON tt.poll_id=p.pollid WHERE tt.category_id={$db->quote($this->id)} AND tt.moved_id=0"; // Delete poll options $queries[] = "DELETE p FROM #__kunena_polls_options AS p INNER JOIN #__kunena_topics AS tt ON tt.poll_id=p.pollid WHERE tt.category_id={$db->quote($this->id)} AND tt.moved_id=0"; // Delete polls $queries[] = "DELETE p FROM #__kunena_polls AS p INNER JOIN #__kunena_topics AS tt ON tt.poll_id=p.id WHERE tt.category_id={$db->quote($this->id)} AND tt.moved_id=0"; // Delete messages $queries[] = "DELETE m, t FROM #__kunena_messages AS m INNER JOIN #__kunena_messages_text AS t ON m.id=t.mesid WHERE m.catid={$db->quote($this->id)}"; // TODO: delete attachments // TODO: delete keywords // Delete topics $queries[] = "DELETE FROM #__kunena_topics WHERE category_id={$db->quote($this->id)}"; foreach ($queries as $query) { $db->setQuery($query); $db->query(); KunenaError::checkDatabaseError(); } KunenaUserHelper::recount(); KunenaForumMessageThankyouHelper::recount(); $this->id = null; KunenaForumCategoryHelper::register($this); return true; }