qa-app-recalc.php 22.1 KB
Newer Older
Gideon Greenspan committed
1
<?php
Scott Vivian committed
2

Gideon Greenspan committed
3 4 5 6 7
/*
	Question2Answer (c) Gideon Greenspan

	http://www.question2answer.org/

Scott Vivian committed
8

Gideon Greenspan committed
9 10 11 12 13 14 15 16 17
	File: qa-include/qa-app-recalc.php
	Version: See define()s at top of qa-include/qa-base.php
	Description: Managing database recalculations (clean-up operations) and status messages


	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.
Scott Vivian committed
18

Gideon Greenspan committed
19 20 21 22 23 24 25
	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
*/
Scott Vivian committed
26

Gideon Greenspan committed
27 28
/*
	A full list of redundant (non-normal) information in the database that can be recalculated:
Scott Vivian committed
29

Gideon Greenspan committed
30 31
	Recalculated in doreindexcontent:
	================================
Gideon Greenspan committed
32 33 34 35 36
	^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
Gideon Greenspan committed
37
	^options (title=cache_*): cached values for various things (e.g. counting questions)
Scott Vivian committed
38

Gideon Greenspan committed
39 40 41
	Recalculated in dorecountposts:
	==============================
	^posts (upvotes, downvotes, netvotes, hotness, acount, amaxvotes, flagcount): number of votes, hotness, answers, answer votes, flags
Scott Vivian committed
42

Gideon Greenspan committed
43 44 45 46
	Recalculated in dorecalcpoints:
	===============================
	^userpoints (all except bonus): points calculation for all users
	^options (title=cache_userpointscount):
Scott Vivian committed
47

Gideon Greenspan committed
48 49 50 51 52 53
	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
Scott Vivian committed
54

Gideon Greenspan committed
55 56 57 58
	Recalculated in dorebuildupdates:
	=================================
	^sharedevents (all): per-entity event streams (see big comment in qa-db-favorites.php)
	^userevents (all): per-subscriber event streams
Scott Vivian committed
59

Gideon Greenspan committed
60 61 62 63 64 65 66 67 68 69 70 71 72
	[but these are not entirely redundant since they can contain historical information no longer in ^posts]
*/

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

	require_once QA_INCLUDE_DIR.'qa-db-recalc.php';
	require_once QA_INCLUDE_DIR.'qa-db-post-create.php';
	require_once QA_INCLUDE_DIR.'qa-db-points.php';
	require_once QA_INCLUDE_DIR.'qa-db-selects.php';
	require_once QA_INCLUDE_DIR.'qa-db-admin.php';
Gideon Greenspan committed
73
	require_once QA_INCLUDE_DIR.'qa-db-users.php';
Gideon Greenspan committed
74 75 76 77 78 79 80 81 82 83 84 85
	require_once QA_INCLUDE_DIR.'qa-app-options.php';
	require_once QA_INCLUDE_DIR.'qa-app-post-create.php';
	require_once QA_INCLUDE_DIR.'qa-app-post-update.php';


	function qa_recalc_perform_step(&$state)
/*
	Advance the recalculation operation represented by $state by a single step.
	$state can also be the name of a recalculation operation on its own.
*/
	{
		$continue=false;
Scott Vivian committed
86

Gideon Greenspan committed
87
		@list($operation, $length, $next, $done)=explode("\t", $state);
Scott Vivian committed
88

Gideon Greenspan committed
89
		switch ($operation) {
Gideon Greenspan committed
90 91
			case 'doreindexcontent':
				qa_recalc_transition($state, 'doreindexcontent_pagereindex');
Gideon Greenspan committed
92
				break;
Scott Vivian committed
93

Gideon Greenspan committed
94 95
			case 'doreindexcontent_pagereindex':
				$pages=qa_db_pages_get_for_reindexing($next, 10);
Scott Vivian committed
96

Gideon Greenspan committed
97 98
				if (count($pages)) {
					require_once QA_INCLUDE_DIR.'qa-app-format.php';
Scott Vivian committed
99

Gideon Greenspan committed
100
					$lastpageid=max(array_keys($pages));
Scott Vivian committed
101

Gideon Greenspan committed
102 103 104 105 106
					foreach ($pages as $pageid => $page)
						if (!($page['flags'] & QA_PAGE_FLAGS_EXTERNAL)) {
							$searchmodules=qa_load_modules_with('search', 'unindex_page');
							foreach ($searchmodules as $searchmodule)
								$searchmodule->unindex_page($pageid);
Scott Vivian committed
107

Gideon Greenspan committed
108 109 110 111 112 113 114 115
							$searchmodules=qa_load_modules_with('search', 'index_page');
							if (count($searchmodules)) {
								$indextext=qa_viewer_text($page['content'], 'html');

								foreach ($searchmodules as $searchmodule)
									$searchmodule->index_page($pageid, $page['tags'], $page['heading'], $page['content'], 'html', $indextext);
							}
						}
Scott Vivian committed
116

Gideon Greenspan committed
117 118 119
					$next=1+$lastpageid;
					$done+=count($pages);
					$continue=true;
Scott Vivian committed
120

Gideon Greenspan committed
121 122 123
				} else
					qa_recalc_transition($state, 'doreindexcontent_postcount');
				break;
Scott Vivian committed
124

Gideon Greenspan committed
125
			case 'doreindexcontent_postcount':
Gideon Greenspan committed
126 127 128 129
				qa_db_qcount_update();
				qa_db_acount_update();
				qa_db_ccount_update();

Gideon Greenspan committed
130
				qa_recalc_transition($state, 'doreindexcontent_postreindex');
Gideon Greenspan committed
131
				break;
Scott Vivian committed
132

Gideon Greenspan committed
133
			case 'doreindexcontent_postreindex':
Gideon Greenspan committed
134
				$posts=qa_db_posts_get_for_reindexing($next, 10);
Scott Vivian committed
135

Gideon Greenspan committed
136 137 138 139
				if (count($posts)) {
					require_once QA_INCLUDE_DIR.'qa-app-format.php';

					$lastpostid=max(array_keys($posts));
Scott Vivian committed
140

Gideon Greenspan committed
141 142
					qa_db_prepare_for_reindexing($next, $lastpostid);
					qa_suspend_update_counts();
Scott Vivian committed
143

Gideon Greenspan committed
144 145
					foreach ($posts as $postid => $post) {
						qa_post_unindex($postid);
Gideon Greenspan committed
146
						qa_post_index($postid, $post['type'], $post['questionid'], $post['parentid'], $post['title'], $post['content'],
Gideon Greenspan committed
147 148
							$post['format'], qa_viewer_text($post['content'], $post['format']), $post['tags'], $post['categoryid']);
					}
Scott Vivian committed
149

Gideon Greenspan committed
150 151 152 153 154 155 156 157 158
					$next=1+$lastpostid;
					$done+=count($posts);
					$continue=true;

				} else {
					qa_db_truncate_indexes($next);
					qa_recalc_transition($state, 'doreindexposts_wordcount');
				}
				break;
Scott Vivian committed
159

Gideon Greenspan committed
160 161
			case 'doreindexposts_wordcount':
				$wordids=qa_db_words_prepare_for_recounting($next, 1000);
Scott Vivian committed
162

Gideon Greenspan committed
163 164
				if (count($wordids)) {
					$lastwordid=max($wordids);
Scott Vivian committed
165

Gideon Greenspan committed
166
					qa_db_words_recount($next, $lastwordid);
Scott Vivian committed
167

Gideon Greenspan committed
168 169 170
					$next=1+$lastwordid;
					$done+=count($wordids);
					$continue=true;
Scott Vivian committed
171

Gideon Greenspan committed
172 173 174 175 176
				} else {
					qa_db_tagcount_update(); // this is quick so just do it here
					qa_recalc_transition($state, 'doreindexposts_complete');
				}
				break;
Scott Vivian committed
177

Gideon Greenspan committed
178 179 180
			case 'dorecountposts':
				qa_recalc_transition($state, 'dorecountposts_postcount');
				break;
Scott Vivian committed
181

Gideon Greenspan committed
182 183 184 185 186 187 188 189 190
			case 'dorecountposts_postcount':
				qa_db_qcount_update();
				qa_db_acount_update();
				qa_db_ccount_update();
				qa_db_unaqcount_update();
				qa_db_unselqcount_update();

				qa_recalc_transition($state, 'dorecountposts_votecount');
				break;
Scott Vivian committed
191

Gideon Greenspan committed
192 193
			case 'dorecountposts_votecount':
				$postids=qa_db_posts_get_for_recounting($next, 1000);
Scott Vivian committed
194

Gideon Greenspan committed
195 196
				if (count($postids)) {
					$lastpostid=max($postids);
Scott Vivian committed
197

Gideon Greenspan committed
198
					qa_db_posts_votes_recount($next, $lastpostid);
Scott Vivian committed
199

Gideon Greenspan committed
200 201 202 203 204 205 206
					$next=1+$lastpostid;
					$done+=count($postids);
					$continue=true;

				} else
					qa_recalc_transition($state, 'dorecountposts_acount');
				break;
Scott Vivian committed
207

Gideon Greenspan committed
208 209
			case 'dorecountposts_acount':
				$postids=qa_db_posts_get_for_recounting($next, 1000);
Scott Vivian committed
210

Gideon Greenspan committed
211 212
				if (count($postids)) {
					$lastpostid=max($postids);
Scott Vivian committed
213

Gideon Greenspan committed
214
					qa_db_posts_answers_recount($next, $lastpostid);
Scott Vivian committed
215

Gideon Greenspan committed
216 217 218 219 220 221 222 223 224
					$next=1+$lastpostid;
					$done+=count($postids);
					$continue=true;

				} else {
					qa_db_unupaqcount_update();
					qa_recalc_transition($state, 'dorecountposts_complete');
				}
				break;
Scott Vivian committed
225

Gideon Greenspan committed
226 227 228
			case 'dorecalcpoints':
				qa_recalc_transition($state, 'dorecalcpoints_usercount');
				break;
Scott Vivian committed
229

Gideon Greenspan committed
230 231
			case 'dorecalcpoints_usercount':
				qa_db_userpointscount_update(); // for progress update - not necessarily accurate
Gideon Greenspan committed
232
				qa_db_uapprovecount_update(); // needs to be somewhere and this is the most appropriate place
Gideon Greenspan committed
233 234
				qa_recalc_transition($state, 'dorecalcpoints_recalc');
				break;
Scott Vivian committed
235

Gideon Greenspan committed
236
			case 'dorecalcpoints_recalc':
Gideon Greenspan committed
237 238 239 240
				$recalccount=10;
				$userids=qa_db_users_get_for_recalc_points($next, $recalccount+1); // get one extra so we know where to start from next
				$gotcount=count($userids);
				$recalccount=min($recalccount, $gotcount); // can't recalc more than we got
Scott Vivian committed
241

Gideon Greenspan committed
242 243
				if ($recalccount>0) {
					$lastuserid=$userids[$recalccount-1];
Scott Vivian committed
244
					qa_db_users_recalc_points($next, $lastuserid);
Gideon Greenspan committed
245
					$done+=$recalccount;
Scott Vivian committed
246

Gideon Greenspan committed
247 248 249 250 251
				} else
					$lastuserid=$next; // for truncation

				if ($gotcount>$recalccount) { // more left to do
					$next=$userids[$recalccount]; // start next round at first one not recalculated
Gideon Greenspan committed
252
					$continue=true;
Gideon Greenspan committed
253

Gideon Greenspan committed
254
				} else {
Gideon Greenspan committed
255
					qa_db_truncate_userpoints($lastuserid);
Gideon Greenspan committed
256 257 258 259
					qa_db_userpointscount_update(); // quick so just do it here
					qa_recalc_transition($state, 'dorecalcpoints_complete');
				}
				break;
Scott Vivian committed
260

Gideon Greenspan committed
261 262 263
			case 'dorefillevents':
				qa_recalc_transition($state, 'dorefillevents_qcount');
				break;
Scott Vivian committed
264

Gideon Greenspan committed
265 266 267 268
			case 'dorefillevents_qcount':
				qa_db_qcount_update();
				qa_recalc_transition($state, 'dorefillevents_refill');
				break;
Scott Vivian committed
269

Gideon Greenspan committed
270 271
			case 'dorefillevents_refill':
				$questionids=qa_db_qs_get_for_event_refilling($next, 1);
Scott Vivian committed
272

Gideon Greenspan committed
273 274 275 276
				if (count($questionids)) {
					require_once QA_INCLUDE_DIR.'qa-app-events.php';
					require_once QA_INCLUDE_DIR.'qa-app-updates.php';
					require_once QA_INCLUDE_DIR.'qa-util-sort.php';
Scott Vivian committed
277

Gideon Greenspan committed
278
					$lastquestionid=max($questionids);
Scott Vivian committed
279

Gideon Greenspan committed
280 281 282 283 284 285 286 287 288
					foreach ($questionids as $questionid) {

					//	Retrieve all posts relating to this question

						list($question, $childposts, $achildposts)=qa_db_select_with_pending(
							qa_db_full_post_selectspec(null, $questionid),
							qa_db_full_child_posts_selectspec(null, $questionid),
							qa_db_full_a_child_posts_selectspec(null, $questionid)
						);
Scott Vivian committed
289

Gideon Greenspan committed
290
					//	Merge all posts while preserving keys as postids
Scott Vivian committed
291

Gideon Greenspan committed
292 293 294 295 296 297 298
						$posts=array($questionid => $question);

						foreach ($childposts as $postid => $post)
							$posts[$postid]=$post;

						foreach ($achildposts as $postid => $post)
							$posts[$postid]=$post;
Scott Vivian committed
299

Gideon Greenspan committed
300
					//	Creation and editing of each post
Scott Vivian committed
301

Gideon Greenspan committed
302 303
						foreach ($posts as $postid => $post) {
							$followonq=($post['basetype']=='Q') && ($postid!=$questionid);
Scott Vivian committed
304

Gideon Greenspan committed
305 306 307 308 309 310 311 312
							if ($followonq)
								$updatetype=QA_UPDATE_FOLLOWS;
							elseif ( ($post['basetype']=='C') && (@$posts[$post['parentid']]['basetype']=='Q') )
								$updatetype=QA_UPDATE_C_FOR_Q;
							elseif ( ($post['basetype']=='C') && (@$posts[$post['parentid']]['basetype']=='A') )
								$updatetype=QA_UPDATE_C_FOR_A;
							else
								$updatetype=null;
Scott Vivian committed
313

Gideon Greenspan committed
314
							qa_create_event_for_q_user($questionid, $postid, $updatetype, $post['userid'], @$posts[$post['parentid']]['userid'], $post['created']);
Scott Vivian committed
315

Gideon Greenspan committed
316 317 318
							if (isset($post['updated']) && !$followonq)
								qa_create_event_for_q_user($questionid, $postid, $post['updatetype'], $post['lastuserid'], $post['userid'], $post['updated']);
						}
Scott Vivian committed
319

Gideon Greenspan committed
320 321 322 323
					//	Tags and categories of question

						qa_create_event_for_tags($question['tags'], $questionid, null, $question['userid'], $question['created']);
						qa_create_event_for_category($question['categoryid'], $questionid, null, $question['userid'], $question['created']);
Scott Vivian committed
324

Gideon Greenspan committed
325
					//	Collect comment threads
Scott Vivian committed
326

Gideon Greenspan committed
327
						$parentidcomments=array();
Scott Vivian committed
328

Gideon Greenspan committed
329 330 331
						foreach ($posts as $postid => $post)
							if ($post['basetype']=='C')
								$parentidcomments[$post['parentid']][$postid]=$post;
Scott Vivian committed
332

Gideon Greenspan committed
333 334 335 336
					//	For each comment thread, notify all previous comment authors of each comment in the thread (could get slow)

						foreach ($parentidcomments as $parentid => $comments) {
							$keyuserids=array();
Scott Vivian committed
337

Gideon Greenspan committed
338
							qa_sort_by($comments, 'created');
Scott Vivian committed
339

Gideon Greenspan committed
340 341 342
							foreach ($comments as $comment) {
								foreach ($keyuserids as $keyuserid => $dummy)
									if ( ($keyuserid != $comment['userid']) && ($keyuserid != @$posts[$parentid]['userid']) )
Gideon Greenspan committed
343
										qa_db_event_create_not_entity($keyuserid, $questionid, $comment['postid'], QA_UPDATE_FOLLOWS, $comment['userid'], $comment['created']);
Gideon Greenspan committed
344 345 346 347 348 349

								if (isset($comment['userid']))
									$keyuserids[$comment['userid']]=true;
							}
						}
					}
Scott Vivian committed
350

Gideon Greenspan committed
351 352 353 354 355 356 357
					$next=1+$lastquestionid;
					$done+=count($questionids);
					$continue=true;

				} else
					qa_recalc_transition($state, 'dorefillevents_complete');
				break;
Scott Vivian committed
358

Gideon Greenspan committed
359 360 361
			case 'dorecalccategories':
				qa_recalc_transition($state, 'dorecalccategories_postcount');
				break;
Scott Vivian committed
362

Gideon Greenspan committed
363 364 365
			case 'dorecalccategories_postcount':
				qa_db_acount_update();
				qa_db_ccount_update();
Scott Vivian committed
366

Gideon Greenspan committed
367 368
				qa_recalc_transition($state, 'dorecalccategories_postupdate');
				break;
Scott Vivian committed
369

Gideon Greenspan committed
370 371
			case 'dorecalccategories_postupdate':
				$postids=qa_db_posts_get_for_recategorizing($next, 100);
Scott Vivian committed
372

Gideon Greenspan committed
373 374
				if (count($postids)) {
					$lastpostid=max($postids);
Scott Vivian committed
375

Gideon Greenspan committed
376 377
					qa_db_posts_recalc_categoryid($next, $lastpostid);
					qa_db_posts_calc_category_path($next, $lastpostid);
Scott Vivian committed
378

Gideon Greenspan committed
379 380 381
					$next=1+$lastpostid;
					$done+=count($postids);
					$continue=true;
Scott Vivian committed
382

Gideon Greenspan committed
383 384 385 386
				} else {
					qa_recalc_transition($state, 'dorecalccategories_recount');
				}
				break;
Scott Vivian committed
387

Gideon Greenspan committed
388 389
			case 'dorecalccategories_recount':
				$categoryids=qa_db_categories_get_for_recalcs($next, 10);
Scott Vivian committed
390

Gideon Greenspan committed
391 392
				if (count($categoryids)) {
					$lastcategoryid=max($categoryids);
Scott Vivian committed
393

Gideon Greenspan committed
394 395
					foreach ($categoryids as $categoryid)
						qa_db_ifcategory_qcount_update($categoryid);
Scott Vivian committed
396

Gideon Greenspan committed
397 398 399
					$next=1+$lastcategoryid;
					$done+=count($categoryids);
					$continue=true;
Scott Vivian committed
400

Gideon Greenspan committed
401 402 403 404
				} else {
					qa_recalc_transition($state, 'dorecalccategories_backpaths');
				}
				break;
Scott Vivian committed
405

Gideon Greenspan committed
406 407 408 409 410
			case 'dorecalccategories_backpaths':
				$categoryids=qa_db_categories_get_for_recalcs($next, 10);

				if (count($categoryids)) {
					$lastcategoryid=max($categoryids);
Scott Vivian committed
411

Gideon Greenspan committed
412
					qa_db_categories_recalc_backpaths($next, $lastcategoryid);
Scott Vivian committed
413

Gideon Greenspan committed
414 415 416
					$next=1+$lastcategoryid;
					$done+=count($categoryids);
					$continue=true;
Scott Vivian committed
417

Gideon Greenspan committed
418 419 420 421
				} else {
					qa_recalc_transition($state, 'dorecalccategories_complete');
				}
				break;
Scott Vivian committed
422

Gideon Greenspan committed
423 424 425
			case 'dodeletehidden':
				qa_recalc_transition($state, 'dodeletehidden_comments');
				break;
Scott Vivian committed
426

Gideon Greenspan committed
427 428
			case 'dodeletehidden_comments':
				$posts=qa_db_posts_get_for_deleting('C', $next, 1);
Scott Vivian committed
429

Gideon Greenspan committed
430 431
				if (count($posts)) {
					require_once QA_INCLUDE_DIR.'qa-app-posts.php';
Scott Vivian committed
432

Gideon Greenspan committed
433 434 435
					$postid=$posts[0];

					qa_post_delete($postid);
Scott Vivian committed
436

Gideon Greenspan committed
437 438 439
					$next=1+$postid;
					$done++;
					$continue=true;
Scott Vivian committed
440

Gideon Greenspan committed
441 442 443
				} else
					qa_recalc_transition($state, 'dodeletehidden_answers');
				break;
Scott Vivian committed
444

Gideon Greenspan committed
445 446
			case 'dodeletehidden_answers':
				$posts=qa_db_posts_get_for_deleting('A', $next, 1);
Scott Vivian committed
447

Gideon Greenspan committed
448 449
				if (count($posts)) {
					require_once QA_INCLUDE_DIR.'qa-app-posts.php';
Scott Vivian committed
450

Gideon Greenspan committed
451
					$postid=$posts[0];
Scott Vivian committed
452

Gideon Greenspan committed
453
					qa_post_delete($postid);
Scott Vivian committed
454

Gideon Greenspan committed
455 456 457
					$next=1+$postid;
					$done++;
					$continue=true;
Scott Vivian committed
458

Gideon Greenspan committed
459 460 461 462 463 464
				} else
					qa_recalc_transition($state, 'dodeletehidden_questions');
				break;

			case 'dodeletehidden_questions':
				$posts=qa_db_posts_get_for_deleting('Q', $next, 1);
Scott Vivian committed
465

Gideon Greenspan committed
466 467
				if (count($posts)) {
					require_once QA_INCLUDE_DIR.'qa-app-posts.php';
Scott Vivian committed
468

Gideon Greenspan committed
469
					$postid=$posts[0];
Scott Vivian committed
470

Gideon Greenspan committed
471
					qa_post_delete($postid);
Scott Vivian committed
472

Gideon Greenspan committed
473 474 475
					$next=1+$postid;
					$done++;
					$continue=true;
Scott Vivian committed
476

Gideon Greenspan committed
477 478 479
				} else
					qa_recalc_transition($state, 'dodeletehidden_complete');
				break;
Scott Vivian committed
480

Gideon Greenspan committed
481 482 483
			case 'doblobstodisk':
				qa_recalc_transition($state, 'doblobstodisk_move');
				break;
Scott Vivian committed
484

Gideon Greenspan committed
485 486
			case 'doblobstodisk_move':
				$blob=qa_db_get_next_blob_in_db($next);
Scott Vivian committed
487

Gideon Greenspan committed
488 489 490
				if (isset($blob)) {
					require_once QA_INCLUDE_DIR.'qa-app-blobs.php';
					require_once QA_INCLUDE_DIR.'qa-db-blobs.php';
Scott Vivian committed
491

Gideon Greenspan committed
492 493
					if (qa_write_blob_file($blob['blobid'], $blob['content'], $blob['format']))
						qa_db_blob_set_content($blob['blobid'], null);
Scott Vivian committed
494

Gideon Greenspan committed
495 496 497
					$next=1+$blob['blobid'];
					$done++;
					$continue=true;
Scott Vivian committed
498

Gideon Greenspan committed
499 500 501
				} else
					qa_recalc_transition($state, 'doblobstodisk_complete');
				break;
Scott Vivian committed
502

Gideon Greenspan committed
503 504 505
			case 'doblobstodb':
				qa_recalc_transition($state, 'doblobstodb_move');
				break;
Scott Vivian committed
506

Gideon Greenspan committed
507 508
			case 'doblobstodb_move':
				$blob=qa_db_get_next_blob_on_disk($next);
Scott Vivian committed
509

Gideon Greenspan committed
510 511 512
				if (isset($blob)) {
					require_once QA_INCLUDE_DIR.'qa-app-blobs.php';
					require_once QA_INCLUDE_DIR.'qa-db-blobs.php';
Scott Vivian committed
513

Gideon Greenspan committed
514 515 516
					$content=qa_read_blob_file($blob['blobid'], $blob['format']);
					qa_db_blob_set_content($blob['blobid'], $content);
					qa_delete_blob_file($blob['blobid'], $blob['format']);
Scott Vivian committed
517

Gideon Greenspan committed
518 519 520
					$next=1+$blob['blobid'];
					$done++;
					$continue=true;
Scott Vivian committed
521

Gideon Greenspan committed
522 523 524
				} else
					qa_recalc_transition($state, 'doblobstodb_complete');
				break;
Gideon Greenspan committed
525 526 527 528 529

			default:
				$state='';
				break;
		}
Scott Vivian committed
530

Gideon Greenspan committed
531
		if ($continue)
Gideon Greenspan committed
532
			$state=$operation."\t".$length."\t".$next."\t".$done;
Scott Vivian committed
533

Gideon Greenspan committed
534 535
		return $continue && ($done<$length);
	}
Scott Vivian committed
536

Gideon Greenspan committed
537 538 539 540 541 542

	function qa_recalc_transition(&$state, $operation)
/*
	Change the $state to represent the beginning of a new $operation
*/
	{
Gideon Greenspan committed
543 544 545
		$length=qa_recalc_stage_length($operation);
		$next=(QA_FINAL_EXTERNAL_USERS && ($operation=='dorecalcpoints_recalc')) ? '' : 0;
		$done=0;
Scott Vivian committed
546

Gideon Greenspan committed
547
		$state=$operation."\t".$length."\t".$next."\t".$done;
Gideon Greenspan committed
548 549
	}

Scott Vivian committed
550

Gideon Greenspan committed
551 552 553 554 555 556
	function qa_recalc_stage_length($operation)
/*
	Return how many steps there will be in recalculation $operation
*/
	{
		switch ($operation) {
Gideon Greenspan committed
557 558 559
			case 'doreindexcontent_pagereindex':
				$length=qa_db_count_pages();
				break;
Scott Vivian committed
560

Gideon Greenspan committed
561
			case 'doreindexcontent_postreindex':
Gideon Greenspan committed
562 563
				$length=qa_opt('cache_qcount')+qa_opt('cache_acount')+qa_opt('cache_ccount');
				break;
Scott Vivian committed
564

Gideon Greenspan committed
565 566 567
			case 'doreindexposts_wordcount':
				$length=qa_db_count_words();
				break;
Scott Vivian committed
568

Gideon Greenspan committed
569 570 571
			case 'dorecalcpoints_recalc':
				$length=qa_opt('cache_userpointscount');
				break;
Scott Vivian committed
572

Gideon Greenspan committed
573 574 575 576 577
			case 'dorecountposts_votecount':
			case 'dorecountposts_acount':
			case 'dorecalccategories_postupdate':
				$length=qa_db_count_posts();
				break;
Scott Vivian committed
578

Gideon Greenspan committed
579 580 581
			case 'dorefillevents_refill':
				$length=qa_opt('cache_qcount')+qa_db_count_posts('Q_HIDDEN');
				break;
Scott Vivian committed
582

Gideon Greenspan committed
583 584 585 586
			case 'dorecalccategories_recount':
			case 'dorecalccategories_backpaths':
				$length=qa_db_count_categories();
				break;
Scott Vivian committed
587

Gideon Greenspan committed
588 589 590
			case 'dodeletehidden_comments':
				$length=count(qa_db_posts_get_for_deleting('C'));
				break;
Scott Vivian committed
591

Gideon Greenspan committed
592 593 594
			case 'dodeletehidden_answers':
				$length=count(qa_db_posts_get_for_deleting('A'));
				break;
Scott Vivian committed
595

Gideon Greenspan committed
596 597 598
			case 'dodeletehidden_questions':
				$length=count(qa_db_posts_get_for_deleting('Q'));
				break;
Scott Vivian committed
599

Gideon Greenspan committed
600 601 602
			case 'doblobstodisk_move':
				$length=qa_db_count_blobs_in_db();
				break;
Scott Vivian committed
603

Gideon Greenspan committed
604 605 606
			case 'doblobstodb_move':
				$length=qa_db_count_blobs_on_disk();
				break;
Scott Vivian committed
607

Gideon Greenspan committed
608 609 610 611
			default:
				$length=0;
				break;
		}
Scott Vivian committed
612

Gideon Greenspan committed
613 614 615
		return $length;
	}

Scott Vivian committed
616

Gideon Greenspan committed
617 618 619 620 621
	function qa_recalc_get_message($state)
/*
	Return a string which gives a user-viewable version of $state
*/
	{
Gideon Greenspan committed
622
		@list($operation, $length, $next, $done)=explode("\t", $state);
Scott Vivian committed
623

Gideon Greenspan committed
624 625
		$done=(int)$done;
		$length=(int)$length;
Scott Vivian committed
626

Gideon Greenspan committed
627
		switch ($operation) {
Gideon Greenspan committed
628
			case 'doreindexcontent_postcount':
Gideon Greenspan committed
629 630 631 632 633
			case 'dorecountposts_postcount':
			case 'dorecalccategories_postcount':
			case 'dorefillevents_qcount':
				$message=qa_lang('admin/recalc_posts_count');
				break;
Scott Vivian committed
634

Gideon Greenspan committed
635 636 637 638 639 640 641 642
			case 'doreindexcontent_pagereindex':
				$message=strtr(qa_lang('admin/reindex_pages_reindexed'), array(
					'^1' => number_format($done),
					'^2' => number_format($length)
				));
				break;

			case 'doreindexcontent_postreindex':
Gideon Greenspan committed
643 644 645 646 647
				$message=strtr(qa_lang('admin/reindex_posts_reindexed'), array(
					'^1' => number_format($done),
					'^2' => number_format($length)
				));
				break;
Scott Vivian committed
648

Gideon Greenspan committed
649 650 651 652 653 654
			case 'doreindexposts_wordcount':
				$message=strtr(qa_lang('admin/reindex_posts_wordcounted'), array(
					'^1' => number_format($done),
					'^2' => number_format($length)
				));
				break;
Scott Vivian committed
655

Gideon Greenspan committed
656 657 658 659 660 661
			case 'dorecountposts_votecount':
				$message=strtr(qa_lang('admin/recount_posts_votes_recounted'), array(
					'^1' => number_format($done),
					'^2' => number_format($length)
				));
				break;
Scott Vivian committed
662

Gideon Greenspan committed
663 664 665 666 667 668
			case 'dorecountposts_acount':
				$message=strtr(qa_lang('admin/recount_posts_as_recounted'), array(
					'^1' => number_format($done),
					'^2' => number_format($length)
				));
				break;
Scott Vivian committed
669

Gideon Greenspan committed
670 671 672
			case 'doreindexposts_complete':
				$message=qa_lang('admin/reindex_posts_complete');
				break;
Scott Vivian committed
673

Gideon Greenspan committed
674 675 676
			case 'dorecountposts_complete':
				$message=qa_lang('admin/recount_posts_complete');
				break;
Scott Vivian committed
677

Gideon Greenspan committed
678 679 680
			case 'dorecalcpoints_usercount':
				$message=qa_lang('admin/recalc_points_usercount');
				break;
Scott Vivian committed
681

Gideon Greenspan committed
682 683 684 685 686 687
			case 'dorecalcpoints_recalc':
				$message=strtr(qa_lang('admin/recalc_points_recalced'), array(
					'^1' => number_format($done),
					'^2' => number_format($length)
				));
				break;
Scott Vivian committed
688

Gideon Greenspan committed
689 690 691
			case 'dorecalcpoints_complete':
				$message=qa_lang('admin/recalc_points_complete');
				break;
Scott Vivian committed
692

Gideon Greenspan committed
693 694 695 696 697 698
			case 'dorefillevents_refill':
				$message=strtr(qa_lang('admin/refill_events_refilled'), array(
					'^1' => number_format($done),
					'^2' => number_format($length)
				));
				break;
Scott Vivian committed
699

Gideon Greenspan committed
700 701 702
			case 'dorefillevents_complete':
				$message=qa_lang('admin/refill_events_complete');
				break;
Scott Vivian committed
703

Gideon Greenspan committed
704 705 706 707 708 709
			case 'dorecalccategories_postupdate':
				$message=strtr(qa_lang('admin/recalc_categories_updated'), array(
					'^1' => number_format($done),
					'^2' => number_format($length)
				));
				break;
Scott Vivian committed
710

Gideon Greenspan committed
711 712 713 714 715 716
			case 'dorecalccategories_recount':
				$message=strtr(qa_lang('admin/recalc_categories_recounting'), array(
					'^1' => number_format($done),
					'^2' => number_format($length)
				));
				break;
Scott Vivian committed
717

Gideon Greenspan committed
718 719 720 721 722 723
			case 'dorecalccategories_backpaths':
				$message=strtr(qa_lang('admin/recalc_categories_backpaths'), array(
					'^1' => number_format($done),
					'^2' => number_format($length)
				));
				break;
Scott Vivian committed
724

Gideon Greenspan committed
725 726 727
			case 'dorecalccategories_complete':
				$message=qa_lang('admin/recalc_categories_complete');
				break;
Scott Vivian committed
728

Gideon Greenspan committed
729 730 731 732 733 734
			case 'dodeletehidden_comments':
				$message=strtr(qa_lang('admin/hidden_comments_deleted'), array(
					'^1' => number_format($done),
					'^2' => number_format($length)
				));
				break;
Scott Vivian committed
735

Gideon Greenspan committed
736 737 738 739 740 741
			case 'dodeletehidden_answers':
				$message=strtr(qa_lang('admin/hidden_answers_deleted'), array(
					'^1' => number_format($done),
					'^2' => number_format($length)
				));
				break;
Scott Vivian committed
742

Gideon Greenspan committed
743 744 745 746 747 748 749 750 751 752
			case 'dodeletehidden_questions':
				$message=strtr(qa_lang('admin/hidden_questions_deleted'), array(
					'^1' => number_format($done),
					'^2' => number_format($length)
				));
				break;

			case 'dodeletehidden_complete':
				$message=qa_lang('admin/delete_hidden_complete');
				break;
Scott Vivian committed
753

Gideon Greenspan committed
754 755 756 757 758 759 760
			case 'doblobstodisk_move':
			case 'doblobstodb_move':
				$message=strtr(qa_lang('admin/blobs_move_moved'), array(
					'^1' => number_format($done),
					'^2' => number_format($length)
				));
				break;
Scott Vivian committed
761

Gideon Greenspan committed
762 763 764 765
			case 'doblobstodisk_complete':
			case 'doblobstodb_complete':
				$message=qa_lang('admin/blobs_move_complete');
				break;
Scott Vivian committed
766

Gideon Greenspan committed
767 768 769 770
			default:
				$message='';
				break;
		}
Scott Vivian committed
771

Gideon Greenspan committed
772 773 774 775 776 777 778
		return $message;
	}


/*
	Omit PHP closing tag to help avoid accidental output
*/