State.php 5.59 KB
Newer Older
1 2 3 4 5
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

6 7
	File: qa-include/Q2A/Recalc/State.php
	Description: Class holding state of the current recalculation process.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27


	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	More about this license: http://www.question2answer.org/license.php
*/

if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
	header('Location: ../../');
	exit;
}

28
class Q2A_Recalc_State
29 30 31 32 33 34 35 36
{
	public $state;
	public $operation;
	public $length;
	public $next;
	public $done;

	private $classes = array(
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
		'doreindexcontent' => 'ReindexContent',
		'doreindexcontent_pagereindex' => 'ReindexContentPageReindex',
		'doreindexcontent_postcount' => 'ReindexContentPostCount',
		'doreindexcontent_postreindex' => 'ReindexContentPostReindex',
		'doreindexposts_wordcount' => 'ReindexPostsWordCount',
		'dorecountposts' => 'RecountPosts',
		'dorecountposts_postcount' => 'RecountPostsPostCount',
		'dorecountposts_votecount' => 'RecountPostsVoteCount',
		'dorecountposts_acount' => 'RecountPostsAcount',
		'dorecalcpoints' => 'RecalcPoints',
		'dorecalcpoints_usercount' => 'RecalcPointsUserCount',
		'dorecalcpoints_recalc' => 'RecalcPointsRecalc',
		'dorefillevents' => 'RefillEvents',
		'dorefillevents_qcount' => 'RefillEventsQcount',
		'dorefillevents_refill' => 'RefillEventsRefill',
		'dorecalccategories' => 'RecalcCategories',
		'dorecalccategories_postcount' => 'RecalcCategoriesPostCount',
		'dorecalccategories_postupdate' => 'RecalcCategoriesPostUpdate',
		'dorecalccategories_recount' => 'RecalcCategoriesRecount',
		'dorecalccategories_backpaths' => 'RecalcCategoriesBackPaths',
		'dodeletehidden' => 'DeleteHidden',
		'dodeletehidden_comments' => 'DeleteHiddenComments',
		'dodeletehidden_answers' => 'DeleteHiddenAnswers',
		'dodeletehidden_questions' => 'DeletehiddenQuestions',
		'doblobstodisk' => 'BlobsToDisk',
		'doblobstodisk_move' => 'BlobsToDiskMove',
		'doblobstodb' => 'BlobsToDB',
		'doblobstodb_move' => 'BlobsToDBMove',
		'docachetrim' => 'CacheTrim',
		'docacheclear' => 'CacheClear',
		'docachetrim_process' => 'CacheClearProcess',
68 69 70 71 72 73 74 75
		'docacheclear_process' => 'CacheClearProcess',

		'doreindexposts_complete' => 'ReindexPostsComplete',
		'dorecountposts_complete' => 'RecountPostsComplete',
		'dorecalcpoints_complete' => 'RecalcPointsComplete',
		'dorefillevents_complete' => 'RefillEventsComplete',
		'dorecalccategories_complete' => 'RecalcCategoriesComplete',
		'dodeletehidden_complete' => 'DeleteHiddenComplete',
Scott committed
76 77
		'doblobstodisk_complete' => 'BlobsMoveComplete',
		'doblobstodb_complete' => 'BlobsMoveComplete',
78
		'docacheclear_complete' => 'CacheClearComplete',
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
	);

	/**
	 * Initialize the counts of resource usage.
	 */
	public function __construct($state)
	{
		$this->setState($state);
	}

	public function getState()
	{
		return $this->state;
	}

	public function setState($state = '')
	{
		$this->state = $state;
		list($this->operation, $this->length, $this->next, $this->done) = explode("\t", $state . "\t\t\t\t");
	}

	public function updateState()
	{
		$this->state = $this->operation . "\t" . $this->length . "\t" . $this->next . "\t" . $this->done;
	}

	public function getOperationClass()
	{
107
		return isset($this->classes[$this->operation]) ? 'Q2A_Recalc_' . $this->classes[$this->operation] : null;
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
	}

	public function allDone()
	{
		return $this->done >= $this->length;
	}

	/**
	 * Change the $state to represent the beginning of a new $operation
	 * @param $newOperation
	 */
	public function transition($newOperation)
	{
		$this->operation = $newOperation;
		$this->length = $this->stageLength();
		$this->next = (QA_FINAL_EXTERNAL_USERS && ($newOperation == 'dorecalcpoints_recalc')) ? '' : 0;
		$this->done = 0;

		$this->updateState();
	}

	/**
	 * Return how many steps there will be in recalculation $operation
	 * @return int
	 */
	private function stageLength()
	{
		switch ($this->operation) {
			case 'doreindexcontent_pagereindex':
				return qa_db_count_pages();

			case 'doreindexcontent_postreindex':
				return qa_opt('cache_qcount') + qa_opt('cache_acount') + qa_opt('cache_ccount');

			case 'doreindexposts_wordcount':
				return qa_db_count_words();

			case 'dorecalcpoints_recalc':
				return qa_opt('cache_userpointscount');

			case 'dorecountposts_votecount':
			case 'dorecountposts_acount':
			case 'dorecalccategories_postupdate':
				return qa_db_count_posts();

			case 'dorefillevents_refill':
				return qa_opt('cache_qcount') + qa_db_count_posts('Q_HIDDEN');

			case 'dorecalccategories_recount':
			case 'dorecalccategories_backpaths':
				return qa_db_count_categories();

			case 'dodeletehidden_comments':
				return count(qa_db_posts_get_for_deleting('C'));

			case 'dodeletehidden_answers':
				return count(qa_db_posts_get_for_deleting('A'));

			case 'dodeletehidden_questions':
				return count(qa_db_posts_get_for_deleting('Q'));

			case 'doblobstodisk_move':
				return qa_db_count_blobs_in_db();

			case 'doblobstodb_move':
				return qa_db_count_blobs_on_disk();

			case 'docachetrim_process':
			case 'docacheclear_process':
				$cacheDriver = Q2A_Storage_CacheFactory::getCacheDriver();
				$cacheStats = $cacheDriver->getStats();
				return $cacheStats['files'];
		}

		return 0;
	}
}