State.php 6.18 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	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
*/

19
namespace Q2A\Recalc;
20

21
class State
22 23 24 25 26 27 28 29
{
	public $state;
	public $operation;
	public $length;
	public $next;
	public $done;

	private $classes = array(
30 31 32 33 34 35 36 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 68 69 70 71
		'doreindexcontent' => '\Q2A\Recalc\ReindexContent',
		'doreindexcontent_pagereindex' => '\Q2A\Recalc\ReindexContentPageReindex',
		'doreindexcontent_postcount' => '\Q2A\Recalc\ReindexContentPostCount',
		'doreindexcontent_postreindex' => '\Q2A\Recalc\ReindexContentPostReindex',
		'doreindexposts_wordcount' => '\Q2A\Recalc\ReindexPostsWordCount',
		'dorecountposts' => '\Q2A\Recalc\RecountPosts',
		'dorecountposts_postcount' => '\Q2A\Recalc\RecountPostsPostCount',
		'dorecountposts_votecount' => '\Q2A\Recalc\RecountPostsVoteCount',
		'dorecountposts_acount' => '\Q2A\Recalc\RecountPostsACount',
		'dorecalcpoints' => '\Q2A\Recalc\RecalcPoints',
		'dorecalcpoints_usercount' => '\Q2A\Recalc\RecalcPointsUserCount',
		'dorecalcpoints_recalc' => '\Q2A\Recalc\RecalcPointsRecalc',
		'dorefillevents' => '\Q2A\Recalc\RefillEvents',
		'dorefillevents_qcount' => '\Q2A\Recalc\RefillEventsQCount',
		'dorefillevents_refill' => '\Q2A\Recalc\RefillEventsRefill',
		'dorecalccategories' => '\Q2A\Recalc\RecalcCategories',
		'dorecalccategories_postcount' => '\Q2A\Recalc\RecalcCategoriesPostCount',
		'dorecalccategories_postupdate' => '\Q2A\Recalc\RecalcCategoriesPostUpdate',
		'dorecalccategories_recount' => '\Q2A\Recalc\RecalcCategoriesRecount',
		'dorecalccategories_backpaths' => '\Q2A\Recalc\RecalcCategoriesBackPaths',
		'dodeletehidden' => '\Q2A\Recalc\DeleteHidden',
		'dodeletehidden_comments' => '\Q2A\Recalc\DeleteHiddenComments',
		'dodeletehidden_answers' => '\Q2A\Recalc\DeleteHiddenAnswers',
		'dodeletehidden_questions' => '\Q2A\Recalc\DeleteHiddenQuestions',
		'doblobstodisk' => '\Q2A\Recalc\BlobsToDisk',
		'doblobstodisk_move' => '\Q2A\Recalc\BlobsToDiskMove',
		'doblobstodb' => '\Q2A\Recalc\BlobsToDB',
		'doblobstodb_move' => '\Q2A\Recalc\BlobsToDBMove',
		'docachetrim' => '\Q2A\Recalc\CacheTrim',
		'docacheclear' => '\Q2A\Recalc\CacheClear',
		'docachetrim_process' => '\Q2A\Recalc\CacheClearProcess',
		'docacheclear_process' => '\Q2A\Recalc\CacheClearProcess',

		'doreindexposts_complete' => '\Q2A\Recalc\ReindexPostsComplete',
		'dorecountposts_complete' => '\Q2A\Recalc\RecountPostsComplete',
		'dorecalcpoints_complete' => '\Q2A\Recalc\RecalcPointsComplete',
		'dorefillevents_complete' => '\Q2A\Recalc\RefillEventsComplete',
		'dorecalccategories_complete' => '\Q2A\Recalc\RecalcCategoriesComplete',
		'dodeletehidden_complete' => '\Q2A\Recalc\DeleteHiddenComplete',
		'doblobstodisk_complete' => '\Q2A\Recalc\BlobsMoveComplete',
		'doblobstodb_complete' => '\Q2A\Recalc\BlobsMoveComplete',
		'docacheclear_complete' => '\Q2A\Recalc\CacheClearComplete',
72 73 74 75
	);

	/**
	 * Initialize the counts of resource usage.
76
	 * @param string $state
77 78 79 80 81 82
	 */
	public function __construct($state)
	{
		$this->setState($state);
	}

83 84 85 86
	/**
	 * Get the state.
	 * @return string
	 */
87 88 89 90 91
	public function getState()
	{
		return $this->state;
	}

92 93 94 95 96
	/**
	 * Set the state.
	 * @param string $state
	 * @return void
	 */
97 98 99 100 101 102
	public function setState($state = '')
	{
		$this->state = $state;
		list($this->operation, $this->length, $this->next, $this->done) = explode("\t", $state . "\t\t\t\t");
	}

103 104 105 106
	/**
	 * Update the state.
	 * @return void
	 */
107 108 109 110 111
	public function updateState()
	{
		$this->state = $this->operation . "\t" . $this->length . "\t" . $this->next . "\t" . $this->done;
	}

112 113 114 115
	/**
	 * Get the class that will handle this operation.
	 * @return string|null
	 */
116 117
	public function getOperationClass()
	{
118
		return isset($this->classes[$this->operation]) ? $this->classes[$this->operation] : null;
119 120
	}

121 122 123 124
	/**
	 * Whether all steps are completed.
	 * @return bool
	 */
125 126 127 128 129 130
	public function allDone()
	{
		return $this->done >= $this->length;
	}

	/**
131 132 133
	 * Change the $state to represent the beginning of a new $operation.
	 * @param string $newOperation
	 * @return void
134 135 136 137 138 139 140 141 142 143 144 145
	 */
	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();
	}

	/**
146
	 * Return how many steps there will be in recalculation operation.
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 185 186 187 188 189 190 191 192
	 * @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':
193
				$cacheDriver = \Q2A\Storage\CacheFactory::getCacheDriver();
194 195 196 197 198 199 200
				$cacheStats = $cacheDriver->getStats();
				return $cacheStats['files'];
		}

		return 0;
	}
}