post-update.php 47.3 KB
Newer Older
Scott committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	Description: Changing questions, answer and comments (application level)


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

Scott committed
22
if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
23
	header('Location: ../../');
Scott committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
	exit;
}

require_once QA_INCLUDE_DIR . 'app/post-create.php';
require_once QA_INCLUDE_DIR . 'app/updates.php';
require_once QA_INCLUDE_DIR . 'db/post-create.php';
require_once QA_INCLUDE_DIR . 'db/post-update.php';
require_once QA_INCLUDE_DIR . 'db/points.php';
require_once QA_INCLUDE_DIR . 'db/hotness.php';


define('QA_POST_STATUS_NORMAL', 0);
define('QA_POST_STATUS_HIDDEN', 1);
define('QA_POST_STATUS_QUEUED', 2);


/**
 * Change the fields of a question (application level) to $title, $content, $format, $tagstring, $notify, $extravalue
 * and $name, then reindex based on $text. For backwards compatibility if $name is null then the name will not be
 * changed. Pass the question's database record before changes in $oldquestion and details of the user doing this in
 * $userid, $handle and $cookieid. Set $remoderate to true if the question should be requeued for moderation if
45
 * modified. Set $silent to true to not mark the question as edited. Reports event as appropriate. See /qa-include/app/posts.php
Scott committed
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 72 73 74 75 76 77 78
 * for a higher-level function which is easier to use.
 * @param $oldquestion
 * @param $title
 * @param $content
 * @param $format
 * @param $text
 * @param $tagstring
 * @param $notify
 * @param $userid
 * @param $handle
 * @param $cookieid
 * @param $extravalue
 * @param $name
 * @param bool $remoderate
 * @param bool $silent
 */
function qa_question_set_content($oldquestion, $title, $content, $format, $text, $tagstring, $notify, $userid, $handle, $cookieid, $extravalue = null, $name = null, $remoderate = false, $silent = false)
{
	qa_post_unindex($oldquestion['postid']);

	$wasqueued = ($oldquestion['type'] == 'Q_QUEUED');
	$titlechanged = strcmp($oldquestion['title'], $title) !== 0;
	$contentchanged = strcmp($oldquestion['content'], $content) !== 0 || strcmp($oldquestion['format'], $format) !== 0;
	$tagschanged = strcmp($oldquestion['tags'], $tagstring) !== 0;
	$setupdated = ($titlechanged || $contentchanged || $tagschanged) && (!$wasqueued) && !$silent;

	qa_db_post_set_content($oldquestion['postid'], $title, $content, $format, $tagstring, $notify,
		$setupdated ? $userid : null, $setupdated ? qa_remote_ip_address() : null,
		($titlechanged || $contentchanged) ? QA_UPDATE_CONTENT : QA_UPDATE_TAGS, $name);

	if (isset($extravalue)) {
		require_once QA_INCLUDE_DIR . 'db/metas.php';
		qa_db_postmeta_set($oldquestion['postid'], 'qa_q_extra', $extravalue);
Scott committed
79 80
	}

Scott committed
81 82
	if ($setupdated && $remoderate) {
		require_once QA_INCLUDE_DIR . 'app/posts.php';
Scott committed
83

Scott committed
84 85 86
		$answers = qa_post_get_question_answers($oldquestion['postid']);
		$commentsfollows = qa_post_get_question_commentsfollows($oldquestion['postid']);
		$closepost = qa_post_get_question_closepost($oldquestion['postid']);
Scott committed
87

Scott committed
88 89
		foreach ($answers as $answer)
			qa_post_unindex($answer['postid']);
Scott committed
90

Scott committed
91 92 93
		foreach ($commentsfollows as $comment) {
			if ($comment['basetype'] == 'C')
				qa_post_unindex($comment['postid']);
Scott committed
94 95
		}

Scott committed
96 97
		if (@$closepost['parentid'] == $oldquestion['postid'])
			qa_post_unindex($closepost['postid']);
Scott committed
98

Scott committed
99 100 101 102
		qa_db_post_set_type($oldquestion['postid'], 'Q_QUEUED');
		qa_update_counts_for_q($oldquestion['postid']);
		qa_db_queuedcount_update();
		qa_db_points_update_ifuser($oldquestion['userid'], array('qposts', 'aselects'));
Scott committed
103

Scott committed
104 105
		if ($oldquestion['flagcount'])
			qa_db_flaggedcount_update();
Scott committed
106

Scott committed
107 108
	} elseif ($oldquestion['type'] == 'Q') { // not hidden or queued
		qa_post_index($oldquestion['postid'], 'Q', $oldquestion['postid'], $oldquestion['parentid'], $title, $content, $format, $text, $tagstring, $oldquestion['categoryid']);
109 110 111
		if ($tagschanged) {
			qa_db_tagcount_update();
		}
Scott committed
112
	}
Scott committed
113

Scott committed
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
	$eventparams = array(
		'postid' => $oldquestion['postid'],
		'title' => $title,
		'content' => $content,
		'format' => $format,
		'text' => $text,
		'tags' => $tagstring,
		'extra' => $extravalue,
		'name' => $name,
		'oldquestion' => $oldquestion,
	);

	qa_report_event('q_edit', $userid, $handle, $cookieid, $eventparams + array(
		'silent' => $silent,
		'oldtitle' => $oldquestion['title'],
		'oldcontent' => $oldquestion['content'],
		'oldformat' => $oldquestion['format'],
		'oldtags' => $oldquestion['tags'],
		'titlechanged' => $titlechanged,
		'contentchanged' => $contentchanged,
		'tagschanged' => $tagschanged,
	));

	if ($setupdated && $remoderate)
		qa_report_event('q_requeue', $userid, $handle, $cookieid, $eventparams);
}


/**
 * Set the selected answer (application level) of $oldquestion to $selchildid. Pass details of the user doing this
 * in $userid, $handle and $cookieid, and the database records for the selected and deselected answers in $answers.
 * Handles user points values and notifications.
146
 * See /qa-include/app/posts.php for a higher-level function which is easier to use.
Scott committed
147 148 149 150 151 152 153 154 155 156 157
 * @param $userid
 * @param $handle
 * @param $cookieid
 * @param $oldquestion
 * @param $selchildid
 * @param $answers
 */
function qa_question_set_selchildid($userid, $handle, $cookieid, $oldquestion, $selchildid, $answers)
{
	$oldselchildid = $oldquestion['selchildid'];

158 159 160
	$lastip = qa_remote_ip_address();

	qa_db_post_set_selchildid($oldquestion['postid'], isset($selchildid) ? $selchildid : null, $userid, $lastip);
Scott committed
161 162 163 164 165 166 167 168 169 170 171
	qa_db_points_update_ifuser($oldquestion['userid'], 'aselects');
	qa_db_unselqcount_update();

	if (isset($oldselchildid) && isset($answers[$oldselchildid])) {
		qa_db_points_update_ifuser($answers[$oldselchildid]['userid'], 'aselecteds');

		qa_report_event('a_unselect', $userid, $handle, $cookieid, array(
			'parentid' => $oldquestion['postid'],
			'parent' => $oldquestion,
			'postid' => $oldselchildid,
			'answer' => $answers[$oldselchildid],
Scott committed
172
		));
173 174 175 176 177 178 179 180 181

		if (!empty($oldquestion['closed']) && empty($oldquestion['closedbyid'])) {
			qa_db_post_set_closed($oldquestion['postid'], null, $userid, $lastip);

			qa_report_event('q_reopen', $userid, $handle, $cookieid, array(
				'postid' => $oldquestion['postid'],
				'oldquestion' => $oldquestion,
			));
		}
Scott committed
182 183
	}

Scott committed
184 185
	if (isset($selchildid)) {
		qa_db_points_update_ifuser($answers[$selchildid]['userid'], 'aselecteds');
Scott committed
186

Scott committed
187 188 189 190 191 192
		qa_report_event('a_select', $userid, $handle, $cookieid, array(
			'parentid' => $oldquestion['postid'],
			'parent' => $oldquestion,
			'postid' => $selchildid,
			'answer' => $answers[$selchildid],
		));
193 194 195 196 197 198 199 200 201 202 203

		if (empty($oldquestion['closed'])) {
			qa_db_post_set_closed($oldquestion['postid'], null, $userid, $lastip);

			qa_report_event('q_close', $userid, $handle, $cookieid, array(
				'postid' => $oldquestion['postid'],
				'oldquestion' => $oldquestion,
				'reason' => 'answer-selected',
				'originalid' => $answers[$selchildid],
			));
		}
Scott committed
204
	}
Scott committed
205 206 207 208 209 210
}


/**
 * Reopen $oldquestion if it was closed. Pass details of the user doing this in $userid, $handle and $cookieid, and the
 * $oldclosepost (to match $oldquestion['closedbyid']) if any.
211
 * See /qa-include/app/posts.php for a higher-level function which is easier to use.
Scott committed
212 213 214 215 216 217 218 219 220 221 222 223 224 225
 * @param $oldquestion
 * @param $oldclosepost
 * @param $userid
 * @param $handle
 * @param $cookieid
 */
function qa_question_close_clear($oldquestion, $oldclosepost, $userid, $handle, $cookieid)
{
	if (isset($oldquestion['closedbyid'])) {
		qa_db_post_set_closed($oldquestion['postid'], null, $userid, qa_remote_ip_address());

		if (isset($oldclosepost) && ($oldclosepost['parentid'] == $oldquestion['postid'])) {
			qa_post_unindex($oldclosepost['postid']);
			qa_db_post_delete($oldclosepost['postid']);
Scott committed
226 227
		}

Scott committed
228
		qa_report_event('q_reopen', $userid, $handle, $cookieid, array(
Scott committed
229 230 231 232
			'postid' => $oldquestion['postid'],
			'oldquestion' => $oldquestion,
		));
	}
Scott committed
233 234 235 236 237 238
}


/**
 * Close $oldquestion as a duplicate of the question with id $originalpostid. Pass details of the user doing this in
 * $userid, $handle and $cookieid, and the $oldclosepost (to match $oldquestion['closedbyid']) if any. See
239
 * /qa-include/app/posts.php for a higher-level function which is easier to use.
Scott committed
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
 * @param $oldquestion
 * @param $oldclosepost
 * @param $originalpostid
 * @param $userid
 * @param $handle
 * @param $cookieid
 */
function qa_question_close_duplicate($oldquestion, $oldclosepost, $originalpostid, $userid, $handle, $cookieid)
{
	qa_question_close_clear($oldquestion, $oldclosepost, $userid, $handle, $cookieid);

	qa_db_post_set_closed($oldquestion['postid'], $originalpostid, $userid, qa_remote_ip_address());

	qa_report_event('q_close', $userid, $handle, $cookieid, array(
		'postid' => $oldquestion['postid'],
		'oldquestion' => $oldquestion,
		'reason' => 'duplicate',
		'originalid' => $originalpostid,
	));
}


/**
 * Close $oldquestion with the reason given in $note. Pass details of the user doing this in $userid, $handle and
 * $cookieid, and the $oldclosepost (to match $oldquestion['closedbyid']) if any.
265
 * See /qa-include/app/posts.php for a higher-level function which is easier to use.
Scott committed
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
 * @param $oldquestion
 * @param $oldclosepost
 * @param $note
 * @param $userid
 * @param $handle
 * @param $cookieid
 */
function qa_question_close_other($oldquestion, $oldclosepost, $note, $userid, $handle, $cookieid)
{
	qa_question_close_clear($oldquestion, $oldclosepost, $userid, $handle, $cookieid);

	$postid = qa_db_post_create('NOTE', $oldquestion['postid'], $userid, isset($userid) ? null : $cookieid,
		qa_remote_ip_address(), null, $note, '', null, null, $oldquestion['categoryid']);

	qa_db_posts_calc_category_path($postid);

	if ($oldquestion['type'] == 'Q')
		qa_post_index($postid, 'NOTE', $oldquestion['postid'], $oldquestion['postid'], null, $note, '', $note, null, $oldquestion['categoryid']);

	qa_db_post_set_closed($oldquestion['postid'], $postid, $userid, qa_remote_ip_address());

	qa_report_event('q_close', $userid, $handle, $cookieid, array(
		'postid' => $oldquestion['postid'],
		'oldquestion' => $oldquestion,
		'reason' => 'other',
		'note' => $note,
	));
}


/**
 * Set $oldquestion to hidden if $hidden is true, visible/normal if otherwise. All other parameters are as for qa_question_set_status(...)
298
 * @deprecated Replaced by qa_question_set_status.
Scott committed
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
 * @param $oldquestion
 * @param $hidden
 * @param $userid
 * @param $handle
 * @param $cookieid
 * @param $answers
 * @param $commentsfollows
 * @param $closepost
 */
function qa_question_set_hidden($oldquestion, $hidden, $userid, $handle, $cookieid, $answers, $commentsfollows, $closepost = null)
{
	qa_question_set_status($oldquestion, $hidden ? QA_POST_STATUS_HIDDEN : QA_POST_STATUS_NORMAL, $userid, $handle, $cookieid, $answers, $commentsfollows, $closepost);
}


/**
 * Set the status (application level) of $oldquestion to $status, one of the QA_POST_STATUS_* constants above. Pass
 * details of the user doing this in $userid, $handle and $cookieid, the database records for all answers to the
 * question in $answers, the database records for all comments on the question or the question's answers in
 * $commentsfollows ($commentsfollows can also contain records for follow-on questions which are ignored), and
 * $closepost to match $oldquestion['closedbyid'] (if any). Handles indexing, user points, cached counts and event
320
 * reports. See /qa-include/app/posts.php for a higher-level function which is easier to use.
Scott committed
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
 * @param $oldquestion
 * @param $status
 * @param $userid
 * @param $handle
 * @param $cookieid
 * @param $answers
 * @param $commentsfollows
 * @param $closepost
 */
function qa_question_set_status($oldquestion, $status, $userid, $handle, $cookieid, $answers, $commentsfollows, $closepost = null)
{
	require_once QA_INCLUDE_DIR . 'app/format.php';
	require_once QA_INCLUDE_DIR . 'app/updates.php';

	$washidden = ($oldquestion['type'] == 'Q_HIDDEN');
	$wasqueued = ($oldquestion['type'] == 'Q_QUEUED');
	$wasrequeued = $wasqueued && isset($oldquestion['updated']);

	qa_post_unindex($oldquestion['postid']);

	foreach ($answers as $answer) {
		qa_post_unindex($answer['postid']);
Scott committed
343 344
	}

Scott committed
345 346 347
	foreach ($commentsfollows as $comment) {
		if ($comment['basetype'] == 'C')
			qa_post_unindex($comment['postid']);
Scott committed
348 349
	}

Scott committed
350 351
	if (@$closepost['parentid'] == $oldquestion['postid'])
		qa_post_unindex($closepost['postid']);
Scott committed
352

Scott committed
353 354
	$setupdated = false;
	$event = null;
Scott committed
355

Scott committed
356 357 358 359
	if ($status == QA_POST_STATUS_QUEUED) {
		$newtype = 'Q_QUEUED';
		if (!$wasqueued)
			$event = 'q_requeue'; // same event whether it was hidden or shown before
Scott committed
360

Scott committed
361 362 363 364
	} elseif ($status == QA_POST_STATUS_HIDDEN) {
		$newtype = 'Q_HIDDEN';
		if (!$washidden) {
			$event = $wasqueued ? 'q_reject' : 'q_hide';
Scott committed
365
			if (!$wasqueued)
Scott committed
366 367
				$setupdated = true;
		}
Scott committed
368

Scott committed
369 370 371 372 373 374 375 376
	} elseif ($status == QA_POST_STATUS_NORMAL) {
		$newtype = 'Q';
		if ($wasqueued)
			$event = 'q_approve';
		elseif ($washidden) {
			$event = 'q_reshow';
			$setupdated = true;
		}
Scott committed
377

Scott committed
378 379
	} else
		qa_fatal_error('Unknown status in qa_question_set_status(): ' . $status);
Scott committed
380

Scott committed
381
	qa_db_post_set_type($oldquestion['postid'], $newtype, $setupdated ? $userid : null, $setupdated ? qa_remote_ip_address() : null, QA_UPDATE_VISIBLE);
Scott committed
382

Scott committed
383 384 385
	if ($wasqueued && $status == QA_POST_STATUS_NORMAL && qa_opt('moderate_update_time')) { // ... for approval of a post, can set time to now instead
		if ($wasrequeued) // reset edit time to now if there was one, since we're approving the edit...
			qa_db_post_set_updated($oldquestion['postid'], null);
Scott committed
386

Scott committed
387 388 389
		else { // ... otherwise we're approving original created post
			qa_db_post_set_created($oldquestion['postid'], null);
			qa_db_hotness_update($oldquestion['postid']);
Scott committed
390
		}
Scott committed
391
	}
Scott committed
392

Scott committed
393 394
	qa_update_counts_for_q($oldquestion['postid']);
	qa_db_points_update_ifuser($oldquestion['userid'], array('qposts', 'aselects'));
Scott committed
395

Scott committed
396 397
	if ($wasqueued || ($status == QA_POST_STATUS_QUEUED))
		qa_db_queuedcount_update();
Scott committed
398

Scott committed
399 400
	if ($oldquestion['flagcount'])
		qa_db_flaggedcount_update();
Scott committed
401

Scott committed
402 403 404
	if ($status == QA_POST_STATUS_NORMAL) {
		qa_post_index($oldquestion['postid'], 'Q', $oldquestion['postid'], $oldquestion['parentid'], $oldquestion['title'], $oldquestion['content'],
			$oldquestion['format'], qa_viewer_text($oldquestion['content'], $oldquestion['format']), $oldquestion['tags'], $oldquestion['categoryid']);
Scott committed
405

Scott committed
406 407 408 409 410 411
		foreach ($answers as $answer) {
			if ($answer['type'] == 'A') { // even if question visible, don't index hidden or queued answers
				qa_post_index($answer['postid'], $answer['type'], $oldquestion['postid'], $answer['parentid'], null,
					$answer['content'], $answer['format'], qa_viewer_text($answer['content'], $answer['format']), null, $answer['categoryid']);
			}
		}
Scott committed
412

Scott committed
413 414 415
		foreach ($commentsfollows as $comment) {
			if ($comment['type'] == 'C') {
				$answer = @$answers[$comment['parentid']];
Scott committed
416

Scott committed
417
				if (!isset($answer) || $answer['type'] == 'A') { // don't index comment if it or its parent is hidden
Scott committed
418 419
					qa_post_index($comment['postid'], $comment['type'], $oldquestion['postid'], $comment['parentid'], null,
						$comment['content'], $comment['format'], qa_viewer_text($comment['content'], $comment['format']), null, $comment['categoryid']);
Scott committed
420
				}
Scott committed
421 422
			}
		}
Scott committed
423

Scott committed
424 425 426
		if ($closepost['parentid'] == $oldquestion['postid']) {
			qa_post_index($closepost['postid'], $closepost['type'], $oldquestion['postid'], $closepost['parentid'], null,
				$closepost['content'], $closepost['format'], qa_viewer_text($closepost['content'], $closepost['format']), null, $closepost['categoryid']);
Scott committed
427
		}
Scott committed
428
	}
Scott committed
429

Scott committed
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
	$eventparams = array(
		'postid' => $oldquestion['postid'],
		'parentid' => $oldquestion['parentid'],
		'parent' => isset($oldquestion['parentid']) ? qa_db_single_select(qa_db_full_post_selectspec(null, $oldquestion['parentid'])) : null,
		'title' => $oldquestion['title'],
		'content' => $oldquestion['content'],
		'format' => $oldquestion['format'],
		'text' => qa_viewer_text($oldquestion['content'], $oldquestion['format']),
		'tags' => $oldquestion['tags'],
		'categoryid' => $oldquestion['categoryid'],
		'name' => $oldquestion['name'],
	);

	if (isset($event)) {
		qa_report_event($event, $userid, $handle, $cookieid, $eventparams + array(
Scott committed
445 446 447 448
				'oldquestion' => $oldquestion,
			));
	}

Scott committed
449 450 451
	if ($wasqueued && ($status == QA_POST_STATUS_NORMAL) && !$wasrequeued) {
		require_once QA_INCLUDE_DIR . 'db/selects.php';
		require_once QA_INCLUDE_DIR . 'util/string.php';
Scott committed
452

Scott committed
453 454 455 456
		qa_report_event('q_post', $oldquestion['userid'], $oldquestion['handle'], $oldquestion['cookieid'], $eventparams + array(
			'notify' => isset($oldquestion['notify']),
			'email' => qa_email_validate($oldquestion['notify']) ? $oldquestion['notify'] : null,
			'delayed' => $oldquestion['created'],
Scott committed
457 458
		));
	}
Scott committed
459 460 461 462 463 464 465 466 467
}


/**
 * Sets the category (application level) of $oldquestion to $categoryid. Pass details of the user doing this in
 * $userid, $handle and $cookieid, the database records for all answers to the question in $answers, the database
 * records for all comments on the question or the question's answers in $commentsfollows ($commentsfollows can also
 * contain records for follow-on questions which are ignored), and $closepost to match $oldquestion['closedbyid'] (if any).
 * Set $silent to true to not mark the question as edited. Handles cached counts and event reports and will reset category
468
 * IDs and paths for all answers and comments. See /qa-include/app/posts.php for a higher-level function which is easier to use.
Scott committed
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
 * @param $oldquestion
 * @param $categoryid
 * @param $userid
 * @param $handle
 * @param $cookieid
 * @param $answers
 * @param $commentsfollows
 * @param $closepost
 * @param bool $silent
 */
function qa_question_set_category($oldquestion, $categoryid, $userid, $handle, $cookieid, $answers, $commentsfollows, $closepost = null, $silent = false)
{
	$oldpath = qa_db_post_get_category_path($oldquestion['postid']);

	qa_db_post_set_category($oldquestion['postid'], $categoryid, $silent ? null : $userid, $silent ? null : qa_remote_ip_address());
	qa_db_posts_calc_category_path($oldquestion['postid']);

	$newpath = qa_db_post_get_category_path($oldquestion['postid']);

	qa_db_category_path_qcount_update($oldpath);
	qa_db_category_path_qcount_update($newpath);

	$otherpostids = array();
	foreach ($answers as $answer) {
		$otherpostids[] = $answer['postid'];
	}
Scott committed
495

Scott committed
496 497 498 499
	foreach ($commentsfollows as $comment) {
		if ($comment['basetype'] == 'C')
			$otherpostids[] = $comment['postid'];
	}
Scott committed
500

Scott committed
501 502
	if (@$closepost['parentid'] == $oldquestion['postid'])
		$otherpostids[] = $closepost['postid'];
503

Scott committed
504
	qa_db_posts_set_category_path($otherpostids, $newpath);
505

Scott committed
506 507 508 509 510
	$searchmodules = qa_load_modules_with('search', 'move_post');
	foreach ($searchmodules as $searchmodule) {
		$searchmodule->move_post($oldquestion['postid'], $categoryid);
		foreach ($otherpostids as $otherpostid) {
			$searchmodule->move_post($otherpostid, $categoryid);
Scott committed
511 512 513
		}
	}

Scott committed
514 515 516 517 518 519 520 521 522 523 524 525 526
	qa_report_event('q_move', $userid, $handle, $cookieid, array(
		'postid' => $oldquestion['postid'],
		'oldquestion' => $oldquestion,
		'categoryid' => $categoryid,
		'oldcategoryid' => $oldquestion['categoryid'],
	));
}


/**
 * Permanently delete a question (application level) from the database. The question must not have any answers or
 * comments on it. Pass details of the user doing this in $userid, $handle and $cookieid, and $closepost to match
 * $oldquestion['closedbyid'] (if any). Handles unindexing, votes, points, cached counts and event reports.
527
 * See /qa-include/app/posts.php for a higher-level function which is easier to use.
Scott committed
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
 * @param $oldquestion
 * @param $userid
 * @param $handle
 * @param $cookieid
 * @param $oldclosepost
 */
function qa_question_delete($oldquestion, $userid, $handle, $cookieid, $oldclosepost = null)
{
	require_once QA_INCLUDE_DIR . 'db/votes.php';

	if ($oldquestion['type'] != 'Q_HIDDEN')
		qa_fatal_error('Tried to delete a non-hidden question');

	$params = array(
		'postid' => $oldquestion['postid'],
		'oldquestion' => $oldquestion,
	);

	qa_report_event('q_delete_before', $userid, $handle, $cookieid, $params);

	if (isset($oldclosepost) && ($oldclosepost['parentid'] == $oldquestion['postid'])) {
		qa_db_post_set_closed($oldquestion['postid'], null); // for foreign key constraint
		qa_post_unindex($oldclosepost['postid']);
		qa_db_post_delete($oldclosepost['postid']);
Scott committed
552 553
	}

Scott committed
554 555
	$useridvotes = qa_db_uservote_post_get($oldquestion['postid']);
	$oldpath = qa_db_post_get_category_path($oldquestion['postid']);
Scott committed
556

Scott committed
557 558 559 560 561
	qa_post_unindex($oldquestion['postid']);
	qa_db_post_delete($oldquestion['postid']); // also deletes any related voteds due to foreign key cascading
	qa_update_counts_for_q(null);
	qa_db_category_path_qcount_update($oldpath); // don't do inside qa_update_counts_for_q() since post no longer exists
	qa_db_points_update_ifuser($oldquestion['userid'], array('qposts', 'aselects', 'qvoteds', 'upvoteds', 'downvoteds'));
Scott committed
562

Scott committed
563 564 565
	foreach ($useridvotes as $voteruserid => $vote) {
		// could do this in one query like in qa_db_users_recalc_points() but this will do for now - unlikely to be many votes
		qa_db_points_update_ifuser($voteruserid, ($vote > 0) ? 'qupvotes' : 'qdownvotes');
Scott committed
566 567
	}

Scott committed
568 569
	qa_report_event('q_delete', $userid, $handle, $cookieid, $params);
}
Scott committed
570 571


Scott committed
572 573 574 575 576 577 578 579 580 581 582
/**
 * Set the author (application level) of $oldquestion to $userid and also pass $handle and $cookieid
 * of user. Updates points and reports events as appropriate.
 * @param $oldquestion
 * @param $userid
 * @param $handle
 * @param $cookieid
 */
function qa_question_set_userid($oldquestion, $userid, $handle, $cookieid)
{
	require_once QA_INCLUDE_DIR . 'db/votes.php';
Scott committed
583

Scott committed
584
	$postid = $oldquestion['postid'];
Scott committed
585

Scott committed
586 587 588
	qa_db_post_set_userid($postid, $userid);
	qa_db_uservote_remove_own($postid);
	qa_db_post_recount_votes($postid);
Scott committed
589

Scott committed
590 591
	qa_db_points_update_ifuser($oldquestion['userid'], array('qposts', 'aselects', 'qvoteds', 'upvoteds', 'downvoteds'));
	qa_db_points_update_ifuser($userid, array('qposts', 'aselects', 'qvoteds', 'qupvotes', 'qdownvotes', 'upvoteds', 'downvoteds'));
Scott committed
592

Scott committed
593 594 595 596 597
	qa_report_event('q_claim', $userid, $handle, $cookieid, array(
		'postid' => $postid,
		'oldquestion' => $oldquestion,
	));
}
Scott committed
598 599


Scott committed
600 601 602 603 604 605 606
/**
 * Remove post $postid from our index and update appropriate word counts. Calls through to all search modules.
 * @param $postid
 */
function qa_post_unindex($postid)
{
	global $qa_post_indexing_suspended;
Scott committed
607

Scott committed
608 609
	if ($qa_post_indexing_suspended > 0)
		return;
Scott committed
610

Scott committed
611
	// Send through to any search modules for unindexing
Scott committed
612

Scott committed
613 614 615
	$searchmodules = qa_load_modules_with('search', 'unindex_post');
	foreach ($searchmodules as $searchmodule) {
		$searchmodule->unindex_post($postid);
Scott committed
616
	}
Scott committed
617 618 619 620 621 622 623 624 625
}


/**
 * Change the fields of an answer (application level) to $content, $format, $notify and $name, then reindex based on
 * $text. For backwards compatibility if $name is null then the name will not be changed. Pass the answer's database
 * record before changes in $oldanswer, the question's in $question, and details of the user doing this in $userid,
 * $handle and $cookieid. Set $remoderate to true if the question should be requeued for moderation if modified. Set
 * $silent to true to not mark the question as edited. Handle indexing and event reports as appropriate. See
626
 * /qa-include/app/posts.php for a higher-level function which is easier to use.
Scott committed
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
 * @param $oldanswer
 * @param $content
 * @param $format
 * @param $text
 * @param $notify
 * @param $userid
 * @param $handle
 * @param $cookieid
 * @param $question
 * @param $name
 * @param bool $remoderate
 * @param bool $silent
 */
function qa_answer_set_content($oldanswer, $content, $format, $text, $notify, $userid, $handle, $cookieid, $question, $name = null, $remoderate = false, $silent = false)
{
	qa_post_unindex($oldanswer['postid']);

	$wasqueued = ($oldanswer['type'] == 'A_QUEUED');
	$contentchanged = strcmp($oldanswer['content'], $content) || strcmp($oldanswer['format'], $format);
	$setupdated = $contentchanged && (!$wasqueued) && !$silent;

	qa_db_post_set_content($oldanswer['postid'], $oldanswer['title'], $content, $format, $oldanswer['tags'], $notify,
		$setupdated ? $userid : null, $setupdated ? qa_remote_ip_address() : null, QA_UPDATE_CONTENT, $name);

	if ($setupdated && $remoderate) {
		require_once QA_INCLUDE_DIR . 'app/posts.php';

		$commentsfollows = qa_post_get_answer_commentsfollows($oldanswer['postid']);

		foreach ($commentsfollows as $comment) {
Scott committed
657
			if ($comment['basetype'] == 'C' && $comment['parentid'] == $oldanswer['postid'])
Scott committed
658 659 660
				qa_post_unindex($comment['postid']);
		}

Scott committed
661
		qa_db_post_set_type($oldanswer['postid'], 'A_QUEUED');
Scott committed
662
		qa_update_q_counts_for_a($question['postid']);
Scott committed
663
		qa_db_queuedcount_update();
Scott committed
664 665 666 667 668
		qa_db_points_update_ifuser($oldanswer['userid'], array('aposts', 'aselecteds'));

		if ($oldanswer['flagcount'])
			qa_db_flaggedcount_update();

Scott committed
669
	} elseif ($oldanswer['type'] == 'A' && $question['type'] == 'Q') { // don't index if question or answer are hidden/queued
Scott committed
670
		qa_post_index($oldanswer['postid'], 'A', $question['postid'], $oldanswer['parentid'], null, $content, $format, $text, null, $oldanswer['categoryid']);
Scott committed
671 672
	}

Scott committed
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
	$eventparams = array(
		'postid' => $oldanswer['postid'],
		'parentid' => $oldanswer['parentid'],
		'parent' => $question,
		'content' => $content,
		'format' => $format,
		'text' => $text,
		'name' => $name,
		'oldanswer' => $oldanswer,
	);

	qa_report_event('a_edit', $userid, $handle, $cookieid, $eventparams + array(
		'silent' => $silent,
		'oldcontent' => $oldanswer['content'],
		'oldformat' => $oldanswer['format'],
		'contentchanged' => $contentchanged,
	));

	if ($setupdated && $remoderate)
		qa_report_event('a_requeue', $userid, $handle, $cookieid, $eventparams);
}


/**
 * Set $oldanswer to hidden if $hidden is true, visible/normal if otherwise. All other parameters are as for qa_answer_set_status(...)
698
 * @deprecated Replaced by qa_answer_set_status.
Scott committed
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
 * @param $oldanswer
 * @param $hidden
 * @param $userid
 * @param $handle
 * @param $cookieid
 * @param $question
 * @param $commentsfollows
 */
function qa_answer_set_hidden($oldanswer, $hidden, $userid, $handle, $cookieid, $question, $commentsfollows)
{
	qa_answer_set_status($oldanswer, $hidden ? QA_POST_STATUS_HIDDEN : QA_POST_STATUS_NORMAL, $userid, $handle, $cookieid, $question, $commentsfollows);
}


/**
 * Set the status (application level) of $oldanswer to $status, one of the QA_POST_STATUS_* constants above. Pass
 * details of the user doing this in $userid, $handle and $cookieid, the database record for the question in $question,
 * and the database records for all comments on the answer in $commentsfollows ($commentsfollows can also contain other
717
 * records which are ignored). Handles indexing, user points, cached counts and event reports. See /qa-include/app/posts.php for
Scott committed
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
 * a higher-level function which is easier to use.
 * @param $oldanswer
 * @param $status
 * @param $userid
 * @param $handle
 * @param $cookieid
 * @param $question
 * @param $commentsfollows
 */
function qa_answer_set_status($oldanswer, $status, $userid, $handle, $cookieid, $question, $commentsfollows)
{
	require_once QA_INCLUDE_DIR . 'app/format.php';

	$washidden = ($oldanswer['type'] == 'A_HIDDEN');
	$wasqueued = ($oldanswer['type'] == 'A_QUEUED');
	$wasrequeued = $wasqueued && isset($oldanswer['updated']);

	qa_post_unindex($oldanswer['postid']);

	foreach ($commentsfollows as $comment) {
Scott committed
738
		if ($comment['basetype'] == 'C' && $comment['parentid'] == $oldanswer['postid'])
Scott committed
739 740
			qa_post_unindex($comment['postid']);
	}
Scott committed
741

Scott committed
742 743
	$setupdated = false;
	$event = null;
Scott committed
744

Scott committed
745 746 747 748
	if ($status == QA_POST_STATUS_QUEUED) {
		$newtype = 'A_QUEUED';
		if (!$wasqueued)
			$event = 'a_requeue'; // same event whether it was hidden or shown before
Scott committed
749

Scott committed
750 751 752 753 754 755 756
	} elseif ($status == QA_POST_STATUS_HIDDEN) {
		$newtype = 'A_HIDDEN';
		if (!$washidden) {
			$event = $wasqueued ? 'a_reject' : 'a_hide';
			if (!$wasqueued)
				$setupdated = true;
		}
Scott committed
757

Scott committed
758 759 760
		if ($question['selchildid'] == $oldanswer['postid']) { // remove selected answer
			qa_question_set_selchildid(null, null, null, $question, null, array($oldanswer['postid'] => $oldanswer));
		}
Scott committed
761

Scott committed
762 763 764 765 766 767 768
	} elseif ($status == QA_POST_STATUS_NORMAL) {
		$newtype = 'A';
		if ($wasqueued)
			$event = 'a_approve';
		elseif ($washidden) {
			$event = 'a_reshow';
			$setupdated = true;
Scott committed
769 770
		}

Scott committed
771 772
	} else
		qa_fatal_error('Unknown status in qa_answer_set_status(): ' . $status);
Scott committed
773

Scott committed
774
	qa_db_post_set_type($oldanswer['postid'], $newtype, $setupdated ? $userid : null, $setupdated ? qa_remote_ip_address() : null, QA_UPDATE_VISIBLE);
Scott committed
775

Scott committed
776 777 778 779 780
	if ($wasqueued && ($status == QA_POST_STATUS_NORMAL) && qa_opt('moderate_update_time')) { // ... for approval of a post, can set time to now instead
		if ($wasrequeued)
			qa_db_post_set_updated($oldanswer['postid'], null);
		else
			qa_db_post_set_created($oldanswer['postid'], null);
Scott committed
781 782
	}

Scott committed
783 784
	qa_update_q_counts_for_a($question['postid']);
	qa_db_points_update_ifuser($oldanswer['userid'], array('aposts', 'aselecteds'));
Scott committed
785

Scott committed
786 787
	if ($wasqueued || $status == QA_POST_STATUS_QUEUED)
		qa_db_queuedcount_update();
Scott committed
788

Scott committed
789 790
	if ($oldanswer['flagcount'])
		qa_db_flaggedcount_update();
Scott committed
791

Scott committed
792
	if ($question['type'] == 'Q' && $status == QA_POST_STATUS_NORMAL) { // even if answer visible, don't index if question is hidden or queued
Scott committed
793 794
		qa_post_index($oldanswer['postid'], 'A', $question['postid'], $oldanswer['parentid'], null, $oldanswer['content'],
			$oldanswer['format'], qa_viewer_text($oldanswer['content'], $oldanswer['format']), null, $oldanswer['categoryid']);
Scott committed
795

Scott committed
796
		foreach ($commentsfollows as $comment) {
Scott committed
797
			if ($comment['type'] == 'C' && $comment['parentid'] == $oldanswer['postid']) { // and don't index hidden/queued comments
Scott committed
798 799 800 801
				qa_post_index($comment['postid'], $comment['type'], $question['postid'], $comment['parentid'], null, $comment['content'],
					$comment['format'], qa_viewer_text($comment['content'], $comment['format']), null, $comment['categoryid']);
			}
		}
Scott committed
802 803
	}

Scott committed
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
	$eventparams = array(
		'postid' => $oldanswer['postid'],
		'parentid' => $oldanswer['parentid'],
		'parent' => $question,
		'content' => $oldanswer['content'],
		'format' => $oldanswer['format'],
		'text' => qa_viewer_text($oldanswer['content'], $oldanswer['format']),
		'categoryid' => $oldanswer['categoryid'],
		'name' => $oldanswer['name'],
	);

	if (isset($event)) {
		qa_report_event($event, $userid, $handle, $cookieid, $eventparams + array(
				'oldanswer' => $oldanswer,
			));
	}
Scott committed
820

Scott committed
821 822
	if ($wasqueued && ($status == QA_POST_STATUS_NORMAL) && !$wasrequeued) {
		require_once QA_INCLUDE_DIR . 'util/string.php';
Scott committed
823

Scott committed
824 825 826 827
		qa_report_event('a_post', $oldanswer['userid'], $oldanswer['handle'], $oldanswer['cookieid'], $eventparams + array(
			'notify' => isset($oldanswer['notify']),
			'email' => qa_email_validate($oldanswer['notify']) ? $oldanswer['notify'] : null,
			'delayed' => $oldanswer['created'],
Scott committed
828
		));
Scott committed
829 830 831 832 833 834 835 836
	}
}


/**
 * Permanently delete an answer (application level) from the database. The answer must not have any comments or
 * follow-on questions. Pass the database record for the question in $question and details of the user doing this
 * in $userid, $handle and $cookieid. Handles unindexing, votes, points, cached counts and event reports.
837
 * See /qa-include/app/posts.php for a higher-level function which is easier to use.
Scott committed
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
 * @param $oldanswer
 * @param $question
 * @param $userid
 * @param $handle
 * @param $cookieid
 */
function qa_answer_delete($oldanswer, $question, $userid, $handle, $cookieid)
{
	require_once QA_INCLUDE_DIR . 'db/votes.php';

	if ($oldanswer['type'] != 'A_HIDDEN')
		qa_fatal_error('Tried to delete a non-hidden answer');

	$useridvotes = qa_db_uservote_post_get($oldanswer['postid']);

	$params = array(
		'postid' => $oldanswer['postid'],
		'parentid' => $oldanswer['parentid'],
		'oldanswer' => $oldanswer,
	);

	qa_report_event('a_delete_before', $userid, $handle, $cookieid, $params);

	qa_post_unindex($oldanswer['postid']);
	qa_db_post_delete($oldanswer['postid']); // also deletes any related voteds due to cascading

	if ($question['selchildid'] == $oldanswer['postid']) {
		qa_db_post_set_selchildid($question['postid'], null);
		qa_db_points_update_ifuser($question['userid'], 'aselects');
		qa_db_unselqcount_update();
Scott committed
868 869
	}

Scott committed
870 871
	qa_update_q_counts_for_a($question['postid']);
	qa_db_points_update_ifuser($oldanswer['userid'], array('aposts', 'aselecteds', 'avoteds', 'upvoteds', 'downvoteds'));
Scott committed
872

Scott committed
873 874 875 876
	foreach ($useridvotes as $voteruserid => $vote) {
		// could do this in one query like in qa_db_users_recalc_points() but this will do for now - unlikely to be many votes
		qa_db_points_update_ifuser($voteruserid, ($vote > 0) ? 'aupvotes' : 'adownvotes');
	}
Scott committed
877

Scott committed
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
	qa_report_event('a_delete', $userid, $handle, $cookieid, $params);
}


/**
 * Set the author (application level) of $oldanswer to $userid and also pass $handle and $cookieid
 * of user. Updates points and reports events as appropriate.
 * @param $oldanswer
 * @param $userid
 * @param $handle
 * @param $cookieid
 */
function qa_answer_set_userid($oldanswer, $userid, $handle, $cookieid)
{
	require_once QA_INCLUDE_DIR . 'db/votes.php';

	$postid = $oldanswer['postid'];

	qa_db_post_set_userid($postid, $userid);
	qa_db_uservote_remove_own($postid);
	qa_db_post_recount_votes($postid);

	qa_db_points_update_ifuser($oldanswer['userid'], array('aposts', 'aselecteds', 'avoteds', 'upvoteds', 'downvoteds'));
	qa_db_points_update_ifuser($userid, array('aposts', 'aselecteds', 'avoteds', 'aupvotes', 'adownvotes', 'upvoteds', 'downvoteds'));

	qa_report_event('a_claim', $userid, $handle, $cookieid, array(
		'postid' => $postid,
		'parentid' => $oldanswer['parentid'],
		'oldanswer' => $oldanswer,
	));
}


/**
 * Change the fields of a comment (application level) to $content, $format, $notify and $name, then reindex based on
 * $text. For backwards compatibility if $name is null then the name will not be changed. Pass the comment's database
 * record before changes in $oldcomment, details of the user doing this in $userid, $handle and $cookieid, the
 * antecedent question in $question and the answer's database record in $answer if this is a comment on an answer,
 * otherwise null. Set $remoderate to true if the question should be requeued for moderation if modified. Set $silent
917
 * to true to not mark the question as edited. Handles unindexing and event reports. See /qa-include/app/posts.php for a
Scott committed
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
 * higher-level function which is easier to use.
 * @param $oldcomment
 * @param $content
 * @param $format
 * @param $text
 * @param $notify
 * @param $userid
 * @param $handle
 * @param $cookieid
 * @param $question
 * @param $parent
 * @param $name
 * @param bool $remoderate
 * @param bool $silent
 */
function qa_comment_set_content($oldcomment, $content, $format, $text, $notify, $userid, $handle, $cookieid, $question, $parent, $name = null, $remoderate = false, $silent = false)
{
	if (!isset($parent))
		$parent = $question; // for backwards compatibility with old answer parameter

	qa_post_unindex($oldcomment['postid']);

	$wasqueued = ($oldcomment['type'] == 'C_QUEUED');
	$contentchanged = strcmp($oldcomment['content'], $content) || strcmp($oldcomment['format'], $format);
	$setupdated = $contentchanged && (!$wasqueued) && !$silent;

	qa_db_post_set_content($oldcomment['postid'], $oldcomment['title'], $content, $format, $oldcomment['tags'], $notify,
		$setupdated ? $userid : null, $setupdated ? qa_remote_ip_address() : null, QA_UPDATE_CONTENT, $name);

	if ($setupdated && $remoderate) {
		qa_db_post_set_type($oldcomment['postid'], 'C_QUEUED');
Scott committed
949
		qa_db_ccount_update();
Scott committed
950 951
		qa_db_queuedcount_update();
		qa_db_points_update_ifuser($oldcomment['userid'], array('cposts'));
952

Scott committed
953 954
		if ($oldcomment['flagcount'])
			qa_db_flaggedcount_update();
Scott committed
955

Scott committed
956 957
	} elseif ($oldcomment['type'] == 'C' && $question['type'] == 'Q' && ($parent['type'] == 'Q' || $parent['type'] == 'A')) { // all must be visible
		qa_post_index($oldcomment['postid'], 'C', $question['postid'], $oldcomment['parentid'], null, $content, $format, $text, null, $oldcomment['categoryid']);
Scott committed
958 959
	}

Scott committed
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
	$eventparams = array(
		'postid' => $oldcomment['postid'],
		'parentid' => $oldcomment['parentid'],
		'parenttype' => $parent['basetype'],
		'parent' => $parent,
		'questionid' => $question['postid'],
		'question' => $question,
		'content' => $content,
		'format' => $format,
		'text' => $text,
		'name' => $name,
		'oldcomment' => $oldcomment,
	);

	qa_report_event('c_edit', $userid, $handle, $cookieid, $eventparams + array(
		'silent' => $silent,
		'oldcontent' => $oldcomment['content'],
		'oldformat' => $oldcomment['format'],
		'contentchanged' => $contentchanged,
	));

	if ($setupdated && $remoderate)
		qa_report_event('c_requeue', $userid, $handle, $cookieid, $eventparams);
}


/**
 * Convert an answer to a comment (application level) and set its fields to $content, $format, $notify and $name. For
 * backwards compatibility if $name is null then the name will not be changed. Pass the answer's database record before
 * changes in $oldanswer, the new comment's $parentid to be, details of the user doing this in $userid, $handle and
 * $cookieid, the antecedent question's record in $question, the records for all answers to that question in $answers,
 * and the records for all comments on the (old) answer and questions following from the (old) answer in
 * $commentsfollows ($commentsfollows can also contain other records which are ignored). Set $remoderate to true if the
 * question should be requeued for moderation if modified. Set $silent to true to not mark the question as edited.
 * Handles indexing (based on $text), user points, cached counts and event reports.
 * @param $oldanswer
 * @param $parentid
 * @param $content
 * @param $format
 * @param $text
 * @param $notify
 * @param $userid
 * @param $handle
 * @param $cookieid
 * @param $question
 * @param $answers
 * @param $commentsfollows
 * @param $name
 * @param bool $remoderate
 * @param bool $silent
 */
function qa_answer_to_comment($oldanswer, $parentid, $content, $format, $text, $notify, $userid, $handle, $cookieid, $question, $answers, $commentsfollows, $name = null, $remoderate = false, $silent = false)
{
	require_once QA_INCLUDE_DIR . 'db/votes.php';

	$parent = isset($answers[$parentid]) ? $answers[$parentid] : $question;

	qa_post_unindex($oldanswer['postid']);

	$wasqueued = ($oldanswer['type'] == 'A_QUEUED');
	$contentchanged = strcmp($oldanswer['content'], $content) || strcmp($oldanswer['format'], $format);
	$setupdated = $contentchanged && (!$wasqueued) && !$silent;

	if ($setupdated && $remoderate)
		$newtype = 'C_QUEUED';
	else
		$newtype = substr_replace($oldanswer['type'], 'C', 0, 1);

	qa_db_post_set_type($oldanswer['postid'], $newtype, ($wasqueued || $silent) ? null : $userid,
		($wasqueued || $silent) ? null : qa_remote_ip_address(), QA_UPDATE_TYPE);
	qa_db_post_set_parent($oldanswer['postid'], $parentid);
	qa_db_post_set_content($oldanswer['postid'], $oldanswer['title'], $content, $format, $oldanswer['tags'], $notify,
		$setupdated ? $userid : null, $setupdated ? qa_remote_ip_address() : null, QA_UPDATE_CONTENT, $name);

	foreach ($commentsfollows as $commentfollow) {
		if ($commentfollow['parentid'] == $oldanswer['postid']) // do same thing for comments and follows
			qa_db_post_set_parent($commentfollow['postid'], $parentid);
Scott committed
1037 1038
	}

Scott committed
1039 1040
	qa_update_q_counts_for_a($question['postid']);
	qa_db_ccount_update();
1041
	qa_db_points_update_ifuser($oldanswer['userid'], array('aposts', 'aselecteds', 'cposts', 'avoteds', 'cvoteds'));
Scott committed
1042

Scott committed
1043 1044 1045 1046 1047
	$useridvotes = qa_db_uservote_post_get($oldanswer['postid']);
	foreach ($useridvotes as $voteruserid => $vote) {
		// could do this in one query like in qa_db_users_recalc_points() but this will do for now - unlikely to be many votes
		qa_db_points_update_ifuser($voteruserid, ($vote > 0) ? 'aupvotes' : 'adownvotes');
	}
Scott committed
1048

Scott committed
1049 1050
	if ($setupdated && $remoderate) {
		qa_db_queuedcount_update();
Scott committed
1051

Scott committed
1052 1053
		if ($oldanswer['flagcount'])
			qa_db_flaggedcount_update();
Scott committed
1054

Scott committed
1055
	} elseif ($oldanswer['type'] == 'A' && $question['type'] == 'Q' && ($parent['type'] == 'Q' || $parent['type'] == 'A')) // only if all fully visible
Scott committed
1056
		qa_post_index($oldanswer['postid'], 'C', $question['postid'], $parentid, null, $content, $format, $text, null, $oldanswer['categoryid']);
Scott committed
1057

Scott committed
1058 1059 1060
	if ($question['selchildid'] == $oldanswer['postid']) { // remove selected answer
		qa_question_set_selchildid(null, null, null, $question, null, array($oldanswer['postid'] => $oldanswer));
	}
Scott committed
1061

Scott committed
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
	$eventparams = array(
		'postid' => $oldanswer['postid'],
		'parentid' => $parentid,
		'parenttype' => $parent['basetype'],
		'parent' => $parent,
		'questionid' => $question['postid'],
		'question' => $question,
		'content' => $content,
		'format' => $format,
		'text' => $text,
		'name' => $name,
		'oldanswer' => $oldanswer,
	);

	qa_report_event('a_to_c', $userid, $handle, $cookieid, $eventparams + array(
		'silent' => $silent,
		'oldcontent' => $oldanswer['content'],
		'oldformat' => $oldanswer['format'],
		'contentchanged' => $contentchanged,
	));

	if ($setupdated && $remoderate) {
		// a-to-c conversion can be detected by presence of $event['oldanswer'] instead of $event['oldcomment']
		qa_report_event('c_requeue', $userid, $handle, $cookieid, $eventparams);
	}
}


/**
 * Set $oldcomment to hidden if $hidden is true, visible/normal if otherwise. All other parameters are as for qa_comment_set_status(...)
1092
 * @deprecated Replaced by qa_comment_set_status.
Scott committed
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
 * @param $oldcomment
 * @param $hidden
 * @param $userid
 * @param $handle
 * @param $cookieid
 * @param $question
 * @param $parent
 */
function qa_comment_set_hidden($oldcomment, $hidden, $userid, $handle, $cookieid, $question, $parent)
{
	qa_comment_set_status($oldcomment, $hidden ? QA_POST_STATUS_HIDDEN : QA_POST_STATUS_NORMAL, $userid, $handle, $cookieid, $question, $parent);
}


/**
 * Set the status (application level) of $oldcomment to $status, one of the QA_POST_STATUS_* constants above. Pass the
 * antecedent question's record in $question, details of the user doing this in $userid, $handle and $cookieid, and the
 * answer's database record in $answer if this is a comment on an answer, otherwise null. Handles indexing, user
1111
 * points, cached counts and event reports. See /qa-include/app/posts.php for a higher-level function which is easier to use.
Scott committed
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
 * @param $oldcomment
 * @param $status
 * @param $userid
 * @param $handle
 * @param $cookieid
 * @param $question
 * @param $parent
 */
function qa_comment_set_status($oldcomment, $status, $userid, $handle, $cookieid, $question, $parent)
{
	require_once QA_INCLUDE_DIR . 'app/format.php';

	if (!isset($parent))
		$parent = $question; // for backwards compatibility with old answer parameter

	$washidden = ($oldcomment['type'] == 'C_HIDDEN');
	$wasqueued = ($oldcomment['type'] == 'C_QUEUED');
	$wasrequeued = $wasqueued && isset($oldcomment['updated']);

	qa_post_unindex($oldcomment['postid']);

	$setupdated = false;
	$event = null;

	if ($status == QA_POST_STATUS_QUEUED) {
		$newtype = 'C_QUEUED';
		if (!$wasqueued)
			$event = 'c_requeue'; // same event whether it was hidden or shown before

	} elseif ($status == QA_POST_STATUS_HIDDEN) {
		$newtype = 'C_HIDDEN';
		if (!$washidden) {
			$event = $wasqueued ? 'c_reject' : 'c_hide';
Scott committed
1145
			if (!$wasqueued)
Scott committed
1146
				$setupdated = true;
Scott committed
1147 1148
		}

Scott committed
1149 1150 1151 1152 1153 1154 1155 1156
	} elseif ($status == QA_POST_STATUS_NORMAL) {
		$newtype = 'C';
		if ($wasqueued)
			$event = 'c_approve';
		elseif ($washidden) {
			$event = 'c_reshow';
			$setupdated = true;
		}
Scott committed
1157

Scott committed
1158 1159
	} else
		qa_fatal_error('Unknown status in qa_comment_set_status(): ' . $status);
Scott committed
1160

Scott committed
1161
	qa_db_post_set_type($oldcomment['postid'], $newtype, $setupdated ? $userid : null, $setupdated ? qa_remote_ip_address() : null, QA_UPDATE_VISIBLE);
Scott committed
1162

Scott committed
1163 1164 1165 1166 1167 1168
	if ($wasqueued && ($status == QA_POST_STATUS_NORMAL) && qa_opt('moderate_update_time')) { // ... for approval of a post, can set time to now instead
		if ($wasrequeued)
			qa_db_post_set_updated($oldcomment['postid'], null);
		else
			qa_db_post_set_created($oldcomment['postid'], null);
	}
Scott committed
1169

Scott committed
1170 1171
	qa_db_ccount_update();
	qa_db_points_update_ifuser($oldcomment['userid'], array('cposts'));
Scott committed
1172

Scott committed
1173 1174
	if ($wasqueued || $status == QA_POST_STATUS_QUEUED)
		qa_db_queuedcount_update();
Scott committed
1175

Scott committed
1176 1177
	if ($oldcomment['flagcount'])
		qa_db_flaggedcount_update();
Scott committed
1178

Scott committed
1179 1180 1181 1182
	if ($question['type'] == 'Q' && ($parent['type'] == 'Q' || $parent['type'] == 'A') && $status == QA_POST_STATUS_NORMAL) {
		// only index if none of the things it depends on are hidden or queued
		qa_post_index($oldcomment['postid'], 'C', $question['postid'], $oldcomment['parentid'], null, $oldcomment['content'],
			$oldcomment['format'], qa_viewer_text($oldcomment['content'], $oldcomment['format']), null, $oldcomment['categoryid']);
Scott committed
1183 1184
	}

Scott committed
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
	$eventparams = array(
		'postid' => $oldcomment['postid'],
		'parentid' => $oldcomment['parentid'],
		'parenttype' => $parent['basetype'],
		'parent' => $parent,
		'questionid' => $question['postid'],
		'question' => $question,
		'content' => $oldcomment['content'],
		'format' => $oldcomment['format'],
		'text' => qa_viewer_text($oldcomment['content'], $oldcomment['format']),
		'categoryid' => $oldcomment['categoryid'],
		'name' => $oldcomment['name'],
	);

	if (isset($event)) {
		qa_report_event($event, $userid, $handle, $cookieid, $eventparams + array(
Scott committed
1201
			'oldcomment' => $oldcomment,
Scott committed
1202
		));
Scott committed
1203 1204
	}

Scott committed
1205 1206 1207
	if ($wasqueued && $status == QA_POST_STATUS_NORMAL && !$wasrequeued) {
		require_once QA_INCLUDE_DIR . 'db/selects.php';
		require_once QA_INCLUDE_DIR . 'util/string.php';
Scott committed
1208

Scott committed
1209 1210
		$commentsfollows = qa_db_single_select(qa_db_full_child_posts_selectspec(null, $oldcomment['parentid']));
		$thread = array();
Scott committed
1211

Scott committed
1212 1213 1214 1215
		foreach ($commentsfollows as $comment) {
			if ($comment['type'] == 'C' && $comment['parentid'] == $parent['postid'])
				$thread[] = $comment;
		}
Scott committed
1216

Scott committed
1217 1218 1219 1220 1221
		qa_report_event('c_post', $oldcomment['userid'], $oldcomment['handle'], $oldcomment['cookieid'], $eventparams + array(
			'thread' => $thread,
			'notify' => isset($oldcomment['notify']),
			'email' => qa_email_validate($oldcomment['notify']) ? $oldcomment['notify'] : null,
			'delayed' => $oldcomment['created'],
Scott committed
1222 1223
		));
	}
Scott committed
1224 1225 1226 1227 1228 1229 1230
}


/**
 * Permanently delete a comment in $oldcomment (application level) from the database. Pass the database question in $question
 * and the answer's database record in $answer if this is a comment on an answer, otherwise null. Pass details of the user
 * doing this in $userid, $handle and $cookieid. Handles unindexing, points, cached counts and event reports.
1231
 * See /qa-include/app/posts.php for a higher-level function which is easier to use.
Scott committed
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
 * @param $oldcomment
 * @param $question
 * @param $parent
 * @param $userid
 * @param $handle
 * @param $cookieid
 */
function qa_comment_delete($oldcomment, $question, $parent, $userid, $handle, $cookieid)
{
	if (!isset($parent))
		$parent = $question; // for backwards compatibility with old answer parameter

	if ($oldcomment['type'] != 'C_HIDDEN')
		qa_fatal_error('Tried to delete a non-hidden comment');

	$params = array(
		'postid' => $oldcomment['postid'],
		'parentid' => $oldcomment['parentid'],
		'oldcomment' => $oldcomment,
		'parenttype' => $parent['basetype'],
		'questionid' => $question['postid'],
	);

	qa_report_event('c_delete_before', $userid, $handle, $cookieid, $params);

	qa_post_unindex($oldcomment['postid']);
	qa_db_post_delete($oldcomment['postid']);
	qa_db_points_update_ifuser($oldcomment['userid'], array('cposts'));
	qa_db_ccount_update();

	qa_report_event('c_delete', $userid, $handle, $cookieid, $params);
}


/**
 * Set the author (application level) of $oldcomment to $userid and also pass $handle and $cookieid
 * of user. Updates points and reports events as appropriate.
 * @param $oldcomment
 * @param $userid
 * @param $handle
 * @param $cookieid
 */
function qa_comment_set_userid($oldcomment, $userid, $handle, $cookieid)
{
	require_once QA_INCLUDE_DIR . 'db/votes.php';

	$postid = $oldcomment['postid'];

	qa_db_post_set_userid($postid, $userid);
	qa_db_uservote_remove_own($postid);
	qa_db_post_recount_votes($postid);

	qa_db_points_update_ifuser($oldcomment['userid'], array('cposts'));
	qa_db_points_update_ifuser($userid, array('cposts'));

	qa_report_event('c_claim', $userid, $handle, $cookieid, array(
		'postid' => $postid,
		'parentid' => $oldcomment['parentid'],
		'oldcomment' => $oldcomment,
	));
}