RecalcMain.php 3.62 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 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
<?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
*/

/*
	A full list of redundant (non-normal) information in the database that can be recalculated:

	Recalculated in doreindexcontent:
	================================
	^titlewords (all): index of words in titles of posts
	^contentwords (all): index of words in content of posts
	^tagwords (all): index of words in tags of posts (a tag can contain multiple words)
	^posttags (all): index tags of posts
	^words (all): list of words used for indexes
	^options (title=cache_*): cached values for various things (e.g. counting questions)

	Recalculated in dorecountposts:
	==============================
	^posts (upvotes, downvotes, netvotes, hotness, acount, amaxvotes, flagcount): number of votes, hotness, answers, answer votes, flags

	Recalculated in dorecalcpoints:
	===============================
	^userpoints (all except bonus): points calculation for all users
	^options (title=cache_userpointscount):

	Recalculated in dorecalccategories:
	===================================
	^posts (categoryid): assign to answers and comments based on their antecedent question
	^posts (catidpath1, catidpath2, catidpath3): hierarchical path to category ids (requires QA_CATEGORY_DEPTH=4)
	^categories (qcount): number of (visible) questions in each category
	^categories (backpath): full (backwards) path of slugs to that category

	Recalculated in dorebuildupdates:
	=================================
	^sharedevents (all): per-entity event streams (see big comment in /qa-include/db/favorites.php)
	^userevents (all): per-subscriber event streams

	[but these are not entirely redundant since they can contain historical information no longer in ^posts]
*/

55
namespace Q2A\Recalc;
56

57
class RecalcMain
58 59 60 61 62
{
	protected $state;

	/**
	 * Initialize the counts of resource usage.
63
	 * @param string $state
64 65 66
	 */
	public function __construct($state)
	{
67 68 69 70 71 72 73 74 75 76
		require_once QA_INCLUDE_DIR . 'db/recalc.php';
		require_once QA_INCLUDE_DIR . 'db/post-create.php';
		require_once QA_INCLUDE_DIR . 'db/points.php';
		require_once QA_INCLUDE_DIR . 'db/selects.php';
		require_once QA_INCLUDE_DIR . 'db/admin.php';
		require_once QA_INCLUDE_DIR . 'db/users.php';
		require_once QA_INCLUDE_DIR . 'app/options.php';
		require_once QA_INCLUDE_DIR . 'app/post-create.php';
		require_once QA_INCLUDE_DIR . 'app/post-update.php';

77
		$this->state = new \Q2A\Recalc\State($state);
78 79
	}

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

89 90 91 92
	/**
	 * Do the recalculation.
	 * @return bool
	 */
93 94
	public function performStep()
	{
95
		$step = AbstractStep::factory($this->state);
96

97
		if (!$step || $step->isFinalStep()) {
98
			$this->state->setState();
99 100 101
			return false;
		}

102
		$continue = $step->doStep();
103
		if ($continue) {
104
			$this->state->updateState();
105 106
		}

107
		return $continue && !$this->state->allDone();
108 109 110
	}

	/**
111
	 * Return a string which gives a user-viewable version of $state.
112 113 114 115
	 * @return string
	 */
	public function getMessage()
	{
116
		$step = AbstractStep::factory($this->state);
117
		return $step ? $step->getMessage() : '';
118 119
	}
}