post-update.php 11.2 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:  Database functions for changing a question, answer or comment


	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 45 46 47 48 49 50 51 52 53
	exit;
}


require_once QA_INCLUDE_DIR . 'app/updates.php';


/**
 * Update the selected answer in the database for $questionid to $selchildid, and optionally record that $lastuserid did it from $lastip
 * @param $questionid
 * @param $selchildid
 * @param $lastuserid
 * @param $lastip
 */
function qa_db_post_set_selchildid($questionid, $selchildid, $lastuserid = null, $lastip = null)
{
	qa_db_query_sub(
		"UPDATE ^posts AS x, (SELECT selchildid FROM ^posts WHERE postid=#) AS a " .
		"SET x.updated=NULL, x.updatetype=NULL, x.lastuserid=NULL, x.lastip=NULL WHERE " . // if previous answer's last edit was to be selected, remove that
		"x.postid=a.selchildid AND x.updatetype=$",
		$questionid, QA_UPDATE_SELECTED
	);

	qa_db_query_sub(
		'UPDATE ^posts SET selchildid=# WHERE postid=#',
		$selchildid, $questionid
	);

	if (isset($selchildid) && isset($lastuserid) && isset($lastip)) {
		qa_db_query_sub(
54 55
			"UPDATE ^posts SET updated=NOW(), updatetype=$, lastuserid=$, lastip=UNHEX($) WHERE postid=#",
			QA_UPDATE_SELECTED, $lastuserid, bin2hex(@inet_pton($lastip)), $selchildid
Scott committed
56
		);
Scott committed
57
	}
Scott committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71
}


/**
 * Set $questionid to be closed by post $closedbyid (null if not closed) in the database, and optionally record that
 * $lastuserid did it from $lastip
 * @param $questionid
 * @param $closedbyid
 * @param $lastuserid
 * @param $lastip
 */
function qa_db_post_set_closed($questionid, $closedbyid, $lastuserid = null, $lastip = null)
{
	if (isset($lastuserid) || isset($lastip)) {
Scott committed
72
		qa_db_query_sub(
73 74
			"UPDATE ^posts SET closedbyid=#, updated=NOW(), updatetype=$, lastuserid=$, lastip=UNHEX($) WHERE postid=#",
			$closedbyid, QA_UPDATE_CLOSED, $lastuserid, bin2hex(@inet_pton($lastip)), $questionid
Scott committed
75
		);
Scott committed
76
	} else {
Scott committed
77
		qa_db_query_sub(
Scott committed
78 79
			'UPDATE ^posts SET closedbyid=# WHERE postid=#',
			$closedbyid, $questionid
Scott committed
80 81
		);
	}
Scott committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95
}


/**
 * Set the type in the database of $postid to $type, and optionally record that $lastuserid did it from $lastip
 * @param $postid
 * @param $type
 * @param $lastuserid
 * @param $lastip
 * @param string $updatetype
 */
function qa_db_post_set_type($postid, $type, $lastuserid = null, $lastip = null, $updatetype = QA_UPDATE_TYPE)
{
	if (isset($lastuserid) || isset($lastip)) {
Scott committed
96
		qa_db_query_sub(
97 98
			'UPDATE ^posts SET type=$, updated=NOW(), updatetype=$, lastuserid=$, lastip=UNHEX($) WHERE postid=#',
			$type, $updatetype, $lastuserid, bin2hex(@inet_pton($lastip)), $postid
Scott committed
99 100 101 102 103
		);
	} else {
		qa_db_query_sub(
			'UPDATE ^posts SET type=$ WHERE postid=#',
			$type, $postid
Scott committed
104 105
		);
	}
Scott committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
}


/**
 * Set the parent in the database of $postid to $parentid, and optionally record that $lastuserid did it from $lastip
 * (if at least one is specified)
 * @param $postid
 * @param $parentid
 * @param $lastuserid
 * @param $lastip
 */
function qa_db_post_set_parent($postid, $parentid, $lastuserid = null, $lastip = null)
{
	if (isset($lastuserid) || isset($lastip)) {
		qa_db_query_sub(
121 122
			"UPDATE ^posts SET parentid=#, updated=NOW(), updatetype=$, lastuserid=$, lastip=UNHEX($) WHERE postid=#",
			$parentid, QA_UPDATE_PARENT, $lastuserid, bin2hex(@inet_pton($lastip)), $postid
Scott committed
123 124 125 126 127 128
		);
	} else {
		qa_db_query_sub(
			'UPDATE ^posts SET parentid=# WHERE postid=#',
			$parentid, $postid
		);
Scott committed
129
	}
Scott committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
}


/**
 * Set the text fields in the database of $postid to $title, $content, $tagstring, $notify and $name, and record that
 * $lastuserid did it from $lastip (if at least one is specified) with $updatetype. For backwards compatibility if $name
 * is null then the name will not be changed.
 * @param $postid
 * @param $title
 * @param $content
 * @param $format
 * @param $tagstring
 * @param $notify
 * @param $lastuserid
 * @param $lastip
 * @param string $updatetype
 * @param $name
 */
function qa_db_post_set_content($postid, $title, $content, $format, $tagstring, $notify, $lastuserid = null, $lastip = null, $updatetype = QA_UPDATE_CONTENT, $name = null)
{
	if (isset($lastuserid) || isset($lastip)) {
		// use COALESCE() for name since $name=null means it should not be modified (for backwards compatibility)
Scott committed
152
		qa_db_query_sub(
153 154
			'UPDATE ^posts SET title=$, content=$, format=$, tags=$, name=COALESCE($, name), notify=$, updated=NOW(), updatetype=$, lastuserid=$, lastip=UNHEX($) WHERE postid=#',
			$title, $content, $format, $tagstring, $name, $notify, $updatetype, $lastuserid, bin2hex(@inet_pton($lastip)), $postid
Scott committed
155 156 157 158 159
		);
	} else {
		qa_db_query_sub(
			'UPDATE ^posts SET title=$, content=$, format=$, tags=$, name=COALESCE($, name), notify=$ WHERE postid=#',
			$title, $content, $format, $tagstring, $name, $notify, $postid
Scott committed
160 161
		);
	}
Scott committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
}


/**
 * Set the author in the database of $postid to $userid, and set the lastuserid to $userid as well if appropriate
 * @param $postid
 * @param $userid
 */
function qa_db_post_set_userid($postid, $userid)
{
	qa_db_query_sub(
		'UPDATE ^posts SET userid=$, lastuserid=IF(updated IS NULL, lastuserid, COALESCE(lastuserid,$)) WHERE postid=#',
		$userid, $userid, $postid
	);
}


/**
 * Set the (exact) category in the database of $postid to $categoryid, and optionally record that $lastuserid did it from
 * $lastip (if at least one is specified)
 * @param $postid
 * @param $categoryid
 * @param $lastuserid
 * @param $lastip
 */
function qa_db_post_set_category($postid, $categoryid, $lastuserid = null, $lastip = null)
{
	if (isset($lastuserid) || isset($lastip)) {
		qa_db_query_sub(
191 192
			"UPDATE ^posts SET categoryid=#, updated=NOW(), updatetype=$, lastuserid=$, lastip=UNHEX($) WHERE postid=#",
			$categoryid, QA_UPDATE_CATEGORY, $lastuserid, bin2hex(@inet_pton($lastip)), $postid
Scott committed
193 194 195 196 197 198
		);
	} else {
		qa_db_query_sub(
			'UPDATE ^posts SET categoryid=# WHERE postid=#',
			$categoryid, $postid
		);
Scott committed
199
	}
Scott committed
200 201 202 203 204 205 206 207 208 209 210 211
}


/**
 * Set the category path in the database of each of $postids to $path retrieved via qa_db_post_get_category_path()
 * @param $postids
 * @param $path
 */
function qa_db_posts_set_category_path($postids, $path)
{
	if (count($postids)) {
		// requires QA_CATEGORY_DEPTH=4
Scott committed
212
		qa_db_query_sub(
Scott committed
213 214
			'UPDATE ^posts SET categoryid=#, catidpath1=#, catidpath2=#, catidpath3=# WHERE postid IN (#)',
			$path['categoryid'], $path['catidpath1'], $path['catidpath2'], $path['catidpath3'], $postids
Scott committed
215 216
		);
	}
Scott committed
217
}
Scott committed
218 219


Scott committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
/**
 * Set the created date of $postid to $created, which is a unix timestamp. If created is null, set to now.
 * @param $postid
 * @param $created
 */
function qa_db_post_set_created($postid, $created)
{
	if (isset($created)) {
		qa_db_query_sub(
			'UPDATE ^posts SET created=FROM_UNIXTIME(#) WHERE postid=#',
			$created, $postid
		);
	} else {
		qa_db_query_sub(
			'UPDATE ^posts SET created=NOW() WHERE postid=#',
Scott committed
235
			$postid
Scott committed
236
		);
Scott committed
237
	}
Scott committed
238
}
Scott committed
239 240


Scott committed
241 242 243 244 245 246 247 248 249 250 251 252 253
/**
 * Set the last updated date of $postid to $updated, which is a unix timestamp. If updated is null, set to now.
 * @param $postid
 * @param $updated
 */
function qa_db_post_set_updated($postid, $updated)
{
	if (isset($updated)) {
		qa_db_query_sub(
			'UPDATE ^posts SET updated=FROM_UNIXTIME(#) WHERE postid=#',
			$updated, $postid
		);
	} else {
Scott committed
254
		qa_db_query_sub(
Scott committed
255
			'UPDATE ^posts SET updated=NOW() WHERE postid=#',
Scott committed
256 257 258
			$postid
		);
	}
Scott committed
259 260 261 262 263 264 265 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 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
}


/**
 * Deletes post $postid from the database (will also delete any votes on the post due to foreign key cascading)
 * @param $postid
 */
function qa_db_post_delete($postid)
{
	qa_db_query_sub(
		'DELETE FROM ^posts WHERE postid=#',
		$postid
	);
}


/**
 * Return an array of wordids that were indexed in the database for the title of $postid
 * @param $postid
 * @return array
 */
function qa_db_titlewords_get_post_wordids($postid)
{
	return qa_db_read_all_values(qa_db_query_sub(
		'SELECT wordid FROM ^titlewords WHERE postid=#',
		$postid
	));
}


/**
 * Remove all entries in the database index of title words for $postid
 * @param $postid
 */
function qa_db_titlewords_delete_post($postid)
{
	qa_db_query_sub(
		'DELETE FROM ^titlewords WHERE postid=#',
		$postid
	);
}


/**
 * Return an array of wordids that were indexed in the database for the content of $postid
 * @param $postid
 * @return array
 */
function qa_db_contentwords_get_post_wordids($postid)
{
	return qa_db_read_all_values(qa_db_query_sub(
		'SELECT wordid FROM ^contentwords WHERE postid=#',
		$postid
	));
}


/**
 * Remove all entries in the database index of content words for $postid
 * @param $postid
 */
function qa_db_contentwords_delete_post($postid)
{
	qa_db_query_sub(
		'DELETE FROM ^contentwords WHERE postid=#',
		$postid
	);
}


/**
 * Return an array of wordids that were indexed in the database for the individual words in tags of $postid
 * @param $postid
 * @return array
 */
function qa_db_tagwords_get_post_wordids($postid)
{
	return qa_db_read_all_values(qa_db_query_sub(
		'SELECT wordid FROM ^tagwords WHERE postid=#',
		$postid
	));
}


/**
 * Remove all entries in the database index of individual words in tags of $postid
 * @param $postid
 */
function qa_db_tagwords_delete_post($postid)
{
	qa_db_query_sub(
		'DELETE FROM ^tagwords WHERE postid=#',
		$postid
	);
}


/**
 * Return an array of wordids that were indexed in the database for the whole tags of $postid
 * @param $postid
 * @return array
 */
function qa_db_posttags_get_post_wordids($postid)
{
	return qa_db_read_all_values(qa_db_query_sub(
		'SELECT wordid FROM ^posttags WHERE postid=#',
		$postid
	));
}


/**
 * Remove all entries in the database index of whole tags for $postid
 * @param $postid
 */
function qa_db_posttags_delete_post($postid)
{
	qa_db_query_sub(
		'DELETE FROM ^posttags WHERE postid=#',
		$postid
	);
}


/**
 * Return the array $postids containing only those elements which are the postid of a question in the database
 * @param $postids
 * @return array
 */
function qa_db_posts_filter_q_postids($postids)
{
	if (count($postids)) {
Scott committed
391
		return qa_db_read_all_values(qa_db_query_sub(
Scott committed
392 393
			"SELECT postid FROM ^posts WHERE type='Q' AND postid IN (#)",
			$postids
Scott committed
394 395 396
		));
	}

Scott committed
397 398
	return array();
}
Scott committed
399 400


Scott committed
401 402 403 404 405 406 407 408
/**
 * Return an array of all the userids of authors of posts in the array $postids
 * @param $postids
 * @return array
 */
function qa_db_posts_get_userids($postids)
{
	if (count($postids)) {
Scott committed
409
		return qa_db_read_all_values(qa_db_query_sub(
Scott committed
410 411
			"SELECT DISTINCT userid FROM ^posts WHERE postid IN (#) AND userid IS NOT NULL",
			$postids
Scott committed
412 413 414
		));
	}

Scott committed
415 416
	return array();
}
Scott committed
417

Scott committed
418 419 420 421 422 423 424

/**
 * Update the cached count of the number of flagged posts in the database
 */
function qa_db_flaggedcount_update()
{
	if (qa_should_update_counts()) {
Scott committed
425
		qa_db_query_sub(
Scott committed
426 427 428 429
			"INSERT INTO ^options (title, content) " .
			"SELECT 'cache_flaggedcount', COUNT(*) FROM ^posts " .
			"WHERE flagcount > 0 AND type IN ('Q', 'A', 'C') " .
			"ON DUPLICATE KEY UPDATE content = VALUES(content)"
Scott committed
430 431
		);
	}
Scott committed
432
}