qa-app-post-create.php 9.47 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-post-create.php
	Version: See define()s at top of qa-include/qa-base.php
	Description: Creating questions, answers 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.
Scott Vivian committed
18

Gideon Greenspan committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
	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
*/

	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-maxima.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-hotness.php';
	require_once QA_INCLUDE_DIR.'qa-util-string.php';
Scott Vivian committed
37 38


Gideon Greenspan committed
39 40 41 42 43 44 45
	function qa_combine_notify_email($userid, $notify, $email)
/*
	Return value to store in database combining $notify and $email values entered by user $userid (or null for anonymous)
*/
	{
		return $notify ? (empty($email) ? (isset($userid) ? '@' : null) : $email) : null;
	}
Scott Vivian committed
46 47


Gideon Greenspan committed
48
	function qa_question_create($followanswer, $userid, $handle, $cookieid, $title, $content, $format, $text, $tagstring, $notify, $email,
Gideon Greenspan committed
49
		$categoryid=null, $extravalue=null, $queued=false, $name=null)
Gideon Greenspan committed
50 51 52 53 54 55 56 57 58
/*
	Add a question (application level) - create record, update appropriate counts, index it, send notifications.
	If question is follow-on from an answer, $followanswer should contain answer database record, otherwise null.
	See qa-app-posts.php for a higher-level function which is easier to use.
*/
	{
		require_once QA_INCLUDE_DIR.'qa-db-selects.php';

		$postid=qa_db_post_create($queued ? 'Q_QUEUED' : 'Q', @$followanswer['postid'], $userid, isset($userid) ? null : $cookieid,
Gideon Greenspan committed
59 60
			qa_remote_ip_address(), $title, $content, $format, $tagstring, qa_combine_notify_email($userid, $notify, $email),
			$categoryid, isset($userid) ? null : $name);
Scott Vivian committed
61

Gideon Greenspan committed
62 63 64 65
		if (isset($extravalue))	{
			require_once QA_INCLUDE_DIR.'qa-db-metas.php';
			qa_db_postmeta_set($postid, 'qa_q_extra', $extravalue);
		}
Scott Vivian committed
66

Gideon Greenspan committed
67 68
		qa_db_posts_calc_category_path($postid);
		qa_db_hotness_update($postid);
Scott Vivian committed
69

Gideon Greenspan committed
70 71 72 73
		if ($queued) {
			qa_db_queuedcount_update();

		} else {
Gideon Greenspan committed
74
			qa_post_index($postid, 'Q', $postid, @$followanswer['postid'], $title, $content, $format, $text, $tagstring, $categoryid);
Gideon Greenspan committed
75
			qa_update_counts_for_q($postid);
Gideon Greenspan committed
76 77
			qa_db_points_update_ifuser($userid, 'qposts');
		}
Scott Vivian committed
78

Gideon Greenspan committed
79 80 81 82 83 84 85 86 87 88 89
		qa_report_event($queued ? 'q_queue' : 'q_post', $userid, $handle, $cookieid, array(
			'postid' => $postid,
			'parentid' => @$followanswer['postid'],
			'parent' => $followanswer,
			'title' => $title,
			'content' => $content,
			'format' => $format,
			'text' => $text,
			'tags' => $tagstring,
			'categoryid' => $categoryid,
			'extra' => $extravalue,
Gideon Greenspan committed
90
			'name' => $name,
Gideon Greenspan committed
91 92 93
			'notify' => $notify,
			'email' => $email,
		));
Scott Vivian committed
94

Gideon Greenspan committed
95 96
		return $postid;
	}
Scott Vivian committed
97 98


Gideon Greenspan committed
99
	function qa_update_counts_for_q($postid)
Gideon Greenspan committed
100 101 102
/*
	Perform various common cached count updating operations to reflect changes in the question whose id is $postid
*/
Gideon Greenspan committed
103 104 105
	{
		if (isset($postid)) // post might no longer exist
			qa_db_category_path_qcount_update(qa_db_post_get_category_path($postid));
Scott Vivian committed
106

Gideon Greenspan committed
107 108 109 110 111
		qa_db_qcount_update();
		qa_db_unaqcount_update();
		qa_db_unselqcount_update();
		qa_db_unupaqcount_update();
	}
Scott Vivian committed
112 113


Gideon Greenspan committed
114 115 116 117 118 119 120 121 122 123
	function qa_array_filter_by_keys($inarray, $keys)
/*
	Return an array containing the elements of $inarray whose key is in $keys
*/
	{
		$outarray=array();

		foreach ($keys as $key)
			if (isset($inarray[$key]))
				$outarray[$key]=$inarray[$key];
Scott Vivian committed
124

Gideon Greenspan committed
125 126 127
		return $outarray;
	}

Scott Vivian committed
128

Gideon Greenspan committed
129 130 131 132 133 134 135
	function qa_suspend_post_indexing($suspend=true)
/*
	Suspend the indexing (and unindexing) of posts via qa_post_index(...) and qa_post_unindex(...)
	if $suspend is true, otherwise reinstate it. A counter is kept to allow multiple calls.
*/
	{
		global $qa_post_indexing_suspended;
Scott Vivian committed
136

Gideon Greenspan committed
137 138
		$qa_post_indexing_suspended+=($suspend ? 1 : -1);
	}
Scott Vivian committed
139 140


Gideon Greenspan committed
141
	function qa_post_index($postid, $type, $questionid, $parentid, $title, $content, $format, $text, $tagstring, $categoryid)
Gideon Greenspan committed
142
/*
Gideon Greenspan committed
143 144
	Add post $postid (which comes under $questionid) of $type (Q/A/C) to the database index, with $title, $text,
	$tagstring and $categoryid. Calls through to all installed search modules.
Gideon Greenspan committed
145 146 147
*/
	{
		global $qa_post_indexing_suspended;
Scott Vivian committed
148

Gideon Greenspan committed
149 150
		if ($qa_post_indexing_suspended>0)
			return;
Scott Vivian committed
151

Gideon Greenspan committed
152
	//	Send through to any search modules for indexing
Scott Vivian committed
153

Gideon Greenspan committed
154 155
		$searches=qa_load_modules_with('search', 'index_post');
		foreach ($searches as $search)
Gideon Greenspan committed
156
			$search->index_post($postid, $type, $questionid, $parentid, $title, $content, $format, $text, $tagstring, $categoryid);
Gideon Greenspan committed
157 158
	}

Scott Vivian committed
159

Gideon Greenspan committed
160
	function qa_answer_create($userid, $handle, $cookieid, $content, $format, $text, $notify, $email, $question, $queued=false, $name=null)
Gideon Greenspan committed
161 162 163 164 165 166 167
/*
	Add an answer (application level) - create record, update appropriate counts, index it, send notifications.
	$question should contain database record for the question this is an answer to.
	See qa-app-posts.php for a higher-level function which is easier to use.
*/
	{
		$postid=qa_db_post_create($queued ? 'A_QUEUED' : 'A', $question['postid'], $userid, isset($userid) ? null : $cookieid,
Gideon Greenspan committed
168 169
			qa_remote_ip_address(), null, $content, $format, null, qa_combine_notify_email($userid, $notify, $email),
			$question['categoryid'], isset($userid) ? null : $name);
Scott Vivian committed
170

Gideon Greenspan committed
171
		qa_db_posts_calc_category_path($postid);
Scott Vivian committed
172

Gideon Greenspan committed
173 174
		if ($queued) {
			qa_db_queuedcount_update();
Scott Vivian committed
175

Gideon Greenspan committed
176
		} else {
Gideon Greenspan committed
177
			if ($question['type']=='Q') // don't index answer if parent question is hidden or queued
Gideon Greenspan committed
178
				qa_post_index($postid, 'A', $question['postid'], $question['postid'], null, $content, $format, $text, null, $question['categoryid']);
Scott Vivian committed
179

Gideon Greenspan committed
180
			qa_update_q_counts_for_a($question['postid']);
Gideon Greenspan committed
181 182
			qa_db_points_update_ifuser($userid, 'aposts');
		}
Scott Vivian committed
183

Gideon Greenspan committed
184 185 186 187 188 189 190 191
		qa_report_event($queued ? 'a_queue' : 'a_post', $userid, $handle, $cookieid, array(
			'postid' => $postid,
			'parentid' => $question['postid'],
			'parent' => $question,
			'content' => $content,
			'format' => $format,
			'text' => $text,
			'categoryid' => $question['categoryid'],
Gideon Greenspan committed
192
			'name' => $name,
Gideon Greenspan committed
193 194 195
			'notify' => $notify,
			'email' => $email,
		));
Scott Vivian committed
196

Gideon Greenspan committed
197 198
		return $postid;
	}
Scott Vivian committed
199 200


Gideon Greenspan committed
201
	function qa_update_q_counts_for_a($questionid)
Gideon Greenspan committed
202 203 204
/*
	Perform various common cached count updating operations to reflect changes in an answer of question $questionid
*/
Gideon Greenspan committed
205 206 207 208 209 210 211
	{
		qa_db_post_acount_update($questionid);
		qa_db_hotness_update($questionid);
		qa_db_acount_update();
		qa_db_unaqcount_update();
		qa_db_unupaqcount_update();
	}
Gideon Greenspan committed
212

Scott Vivian committed
213

Gideon Greenspan committed
214
	function qa_comment_create($userid, $handle, $cookieid, $content, $format, $text, $notify, $email, $question, $parent, $commentsfollows, $queued=false, $name=null)
Gideon Greenspan committed
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
/*
	Add a comment (application level) - create record, update appropriate counts, index it, send notifications.
	$question should contain database record for the question this is part of (as direct or comment on Q's answer).
	If this is a comment on an answer, $answer should contain database record for the answer, otherwise null.
	$commentsfollows should contain database records for all previous comments on the same question or answer,
	but it can also contain other records that are ignored.
	See qa-app-posts.php for a higher-level function which is easier to use.
*/
	{
		require_once QA_INCLUDE_DIR.'qa-app-emails.php';
		require_once QA_INCLUDE_DIR.'qa-app-options.php';
		require_once QA_INCLUDE_DIR.'qa-app-format.php';
		require_once QA_INCLUDE_DIR.'qa-util-string.php';

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

Gideon Greenspan committed
232
		$postid=qa_db_post_create($queued ? 'C_QUEUED' : 'C', $parent['postid'], $userid, isset($userid) ? null : $cookieid,
Gideon Greenspan committed
233 234
			qa_remote_ip_address(), null, $content, $format, null, qa_combine_notify_email($userid, $notify, $email),
			$question['categoryid'], isset($userid) ? null : $name);
Scott Vivian committed
235

Gideon Greenspan committed
236
		qa_db_posts_calc_category_path($postid);
Scott Vivian committed
237

Gideon Greenspan committed
238 239
		if ($queued) {
			qa_db_queuedcount_update();
Scott Vivian committed
240

Gideon Greenspan committed
241
		} else {
Gideon Greenspan committed
242
			if ( ($question['type']=='Q') && (($parent['type']=='Q') || ($parent['type']=='A')) ) // only index if antecedents fully visible
Gideon Greenspan committed
243
				qa_post_index($postid, 'C', $question['postid'], $parent['postid'], null, $content, $format, $text, null, $question['categoryid']);
Scott Vivian committed
244

Gideon Greenspan committed
245 246 247
			qa_db_points_update_ifuser($userid, 'cposts');
			qa_db_ccount_update();
		}
Scott Vivian committed
248

Gideon Greenspan committed
249
		$thread=array();
Scott Vivian committed
250

Gideon Greenspan committed
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
		foreach ($commentsfollows as $comment)
			if (($comment['type']=='C') && ($comment['parentid']==$parent['postid'])) // find just those for this parent, fully visible
				$thread[]=$comment;

		qa_report_event($queued ? 'c_queue' : 'c_post', $userid, $handle, $cookieid, array(
			'postid' => $postid,
			'parentid' => $parent['postid'],
			'parenttype' => $parent['basetype'],
			'parent' => $parent,
			'questionid' => $question['postid'],
			'question' => $question,
			'thread' => $thread,
			'content' => $content,
			'format' => $format,
			'text' => $text,
			'categoryid' => $question['categoryid'],
Gideon Greenspan committed
267
			'name' => $name,
Gideon Greenspan committed
268 269 270
			'notify' => $notify,
			'email' => $email,
		));
Scott Vivian committed
271

Gideon Greenspan committed
272 273
		return $postid;
	}
Scott Vivian committed
274

Gideon Greenspan committed
275 276 277 278

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