qa-db-admin.php 17.8 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-db-admin.php
	Version: See define()s at top of qa-include/qa-base.php
	Description: Database access functions which are specific to the admin center


	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 37 38 39
	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;
	}


	function qa_db_mysql_version()
/*
	Return the current version of MySQL
*/
	{
		return qa_db_read_one_value(qa_db_query_raw('SELECT VERSION()'));
	}
Scott Vivian committed
40 41


Gideon Greenspan committed
42 43
	function qa_db_table_size()
/*
Gideon Greenspan committed
44
	Return the total size in bytes of all relevant tables in the Q2A database
Gideon Greenspan committed
45 46
*/
	{
Gideon Greenspan committed
47 48 49 50 51 52 53
		if (defined('QA_MYSQL_USERS_PREFIX')) { // check if one of the prefixes is a prefix itself of the other
			if (stripos(QA_MYSQL_USERS_PREFIX, QA_MYSQL_TABLE_PREFIX)===0)
				$prefixes=array(QA_MYSQL_TABLE_PREFIX);
			elseif (stripos(QA_MYSQL_TABLE_PREFIX, QA_MYSQL_USERS_PREFIX)===0)
				$prefixes=array(QA_MYSQL_USERS_PREFIX);
			else
				$prefixes=array(QA_MYSQL_TABLE_PREFIX, QA_MYSQL_USERS_PREFIX);
Scott Vivian committed
54

Gideon Greenspan committed
55 56
		} else
			$prefixes=array(QA_MYSQL_TABLE_PREFIX);
Scott Vivian committed
57

Gideon Greenspan committed
58
		$size=0;
Gideon Greenspan committed
59 60 61 62 63 64 65 66
		foreach ($prefixes as $prefix) {
			$statuses=qa_db_read_all_assoc(qa_db_query_raw(
				"SHOW TABLE STATUS LIKE '".$prefix."%'"
			));

			foreach ($statuses as $status)
				$size+=$status['Data_length']+$status['Index_length'];
		}
Scott Vivian committed
67

Gideon Greenspan committed
68 69
		return $size;
	}
Scott Vivian committed
70 71


Gideon Greenspan committed
72 73 74 75 76 77 78
	function qa_db_count_posts($type=null, $fromuser=null)
/*
	Return a count of the number of posts of $type in database.
	Set $fromuser to true to only count non-anonymous posts, false to only count anonymous posts
*/
	{
		$wheresql='';
Scott Vivian committed
79

Gideon Greenspan committed
80 81
		if (isset($type))
			$wheresql.=' WHERE type='.qa_db_argument_to_mysql($type, true);
Scott Vivian committed
82

Gideon Greenspan committed
83 84
		if (isset($fromuser))
			$wheresql.=(strlen($wheresql) ? ' AND' : ' WHERE').' userid '.($fromuser ? 'IS NOT' : 'IS').' NULL';
Scott Vivian committed
85

Gideon Greenspan committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
		return qa_db_read_one_value(qa_db_query_sub(
			'SELECT COUNT(*) FROM ^posts'.$wheresql
		));
	}


	function qa_db_count_users()
/*
	Return number of registered users in database.
*/
	{
		return qa_db_read_one_value(qa_db_query_sub(
			'SELECT COUNT(*) FROM ^users'
		));
	}
Scott Vivian committed
101

Gideon Greenspan committed
102 103 104 105 106 107 108 109 110 111 112

	function qa_db_count_active_users($table)
/*
	Return number of active users in database $table
*/
	{
		switch ($table) {
			case 'posts':
			case 'uservotes':
			case 'userpoints':
				break;
Scott Vivian committed
113

Gideon Greenspan committed
114 115 116 117
			default:
				qa_fatal_error('qa_db_count_active_users() called for unknown table');
				break;
		}
Scott Vivian committed
118

Gideon Greenspan committed
119 120 121 122
		return qa_db_read_one_value(qa_db_query_sub(
			'SELECT COUNT(DISTINCT(userid)) FROM ^'.$table
		));
	}
Scott Vivian committed
123 124


Gideon Greenspan committed
125 126 127 128 129 130 131 132 133
	function qa_db_count_categories()
/*
	Return number of categories in the database
*/
	{
		return qa_db_read_one_value(qa_db_query_sub(
			'SELECT COUNT(*) FROM ^categories'
		));
	}
Scott Vivian committed
134 135


Gideon Greenspan committed
136 137
	function qa_db_count_categoryid_qs($categoryid)
/*
Scott Vivian committed
138
	Return number of questions in the database in $categoryid exactly, and not one of its subcategories
Gideon Greenspan committed
139 140 141 142 143 144 145
*/
	{
		return qa_db_read_one_value(qa_db_query_sub(
			"SELECT COUNT(*) FROM ^posts WHERE categoryid<=># AND type='Q'",
			$categoryid
		));
	}
Scott Vivian committed
146 147


Gideon Greenspan committed
148 149
	function qa_db_get_user_visible_postids($userid)
/*
Gideon Greenspan committed
150
	Return list of postids of visible or queued posts by $userid
Gideon Greenspan committed
151 152 153
*/
	{
		return qa_db_read_all_values(qa_db_query_sub(
Gideon Greenspan committed
154
			"SELECT postid FROM ^posts WHERE userid=# AND type IN ('Q', 'A', 'C', 'Q_QUEUED', 'A_QUEUED', 'C_QUEUED')",
Gideon Greenspan committed
155 156 157
			$userid
		));
	}
Scott Vivian committed
158 159


Gideon Greenspan committed
160 161
	function qa_db_get_ip_visible_postids($ip)
/*
Gideon Greenspan committed
162
	Return list of postids of visible or queued posts from $ip address
Gideon Greenspan committed
163 164 165
*/
	{
		return qa_db_read_all_values(qa_db_query_sub(
Gideon Greenspan committed
166
			"SELECT postid FROM ^posts WHERE createip=INET_ATON($) AND type IN ('Q', 'A', 'C', 'Q_QUEUED', 'A_QUEUED', 'C_QUEUED')",
Gideon Greenspan committed
167 168 169
			$ip
		));
	}
Scott Vivian committed
170 171


Gideon Greenspan committed
172 173 174 175 176 177 178 179 180 181 182 183 184
	function qa_db_postids_count_dependents($postids)
/*
	Return an array whose keys contain the $postids which exist, and whose elements contain the number of other posts depending on each one
*/
	{
		if (count($postids))
			return qa_db_read_all_assoc(qa_db_query_sub(
				"SELECT postid, COALESCE(childcount, 0) AS count FROM ^posts LEFT JOIN (SELECT parentid, COUNT(*) AS childcount FROM ^posts WHERE parentid IN (#) AND LEFT(type, 1) IN ('A', 'C') GROUP BY parentid) x ON postid=x.parentid WHERE postid IN (#)",
				$postids, $postids
			), 'postid', 'count');
		else
			return array();
	}
Scott Vivian committed
185 186


Gideon Greenspan committed
187
	function qa_db_get_unapproved_users($count)
Gideon Greenspan committed
188 189 190 191
/*
	Return an array of the (up to) $count most recently created users who are awaiting approval and have not been blocked.
	The array element for each user includes a 'profile' key whose value is an array of non-empty profile fields of the user.
*/
Gideon Greenspan committed
192 193
	{
		$results=qa_db_read_all_assoc(qa_db_query_sub(
Gideon Greenspan committed
194
			"SELECT ^users.userid, UNIX_TIMESTAMP(created) AS created, INET_NTOA(createip) AS createip, email, handle, flags, title, content FROM ^users LEFT JOIN ^userprofile ON ^users.userid=^userprofile.userid AND LENGTH(content)>0 WHERE level<# AND NOT (flags&#) ORDER BY created DESC LIMIT #",
Gideon Greenspan committed
195 196
			QA_USER_LEVEL_APPROVED, QA_USER_FLAGS_USER_BLOCKED, $count
		));
Scott Vivian committed
197

Gideon Greenspan committed
198
		$users=array();
Scott Vivian committed
199

Gideon Greenspan committed
200 201
		foreach ($results as $result) {
			$userid=$result['userid'];
Scott Vivian committed
202

Gideon Greenspan committed
203 204 205 206 207 208
			if (!isset($users[$userid])) {
				$users[$result['userid']]=$result;
				$users[$result['userid']]['profile']=array();
				unset($users[$userid]['title']);
				unset($users[$userid]['content']);
			}
Scott Vivian committed
209

Gideon Greenspan committed
210 211 212
			if (isset($result['title']) && isset($result['content']))
				$users[$userid]['profile'][$result['title']]=$result['content'];
		}
Scott Vivian committed
213

Gideon Greenspan committed
214 215
		return $users;
	}
Scott Vivian committed
216 217


Gideon Greenspan committed
218
	function qa_db_has_blobs_on_disk()
Gideon Greenspan committed
219 220 221
/*
	Return whether there are any blobs whose content has been stored as a file on disk
*/
Gideon Greenspan committed
222 223 224
	{
		return count(qa_db_read_all_values(qa_db_query_sub('SELECT blobid FROM ^blobs WHERE content IS NULL LIMIT 1'))) ? true : false;
	}
Scott Vivian committed
225 226


Gideon Greenspan committed
227
	function qa_db_has_blobs_in_db()
Gideon Greenspan committed
228 229 230
/*
	Return whether there are any blobs whose content has been stored in the database
*/
Gideon Greenspan committed
231 232 233
	{
		return count(qa_db_read_all_values(qa_db_query_sub('SELECT blobid FROM ^blobs WHERE content IS NOT NULL LIMIT 1'))) ? true : false;
	}
Gideon Greenspan committed
234

Scott Vivian committed
235

Gideon Greenspan committed
236 237 238 239 240 241 242 243 244 245
	function qa_db_category_last_pos($parentid)
/*
	Return the maximum position of the categories with $parentid
*/
	{
		return qa_db_read_one_value(qa_db_query_sub(
			'SELECT COALESCE(MAX(position), 0) FROM ^categories WHERE parentid<=>#',
			$parentid
		));
	}
Scott Vivian committed
246 247


Gideon Greenspan committed
248 249 250 251 252 253 254 255
	function qa_db_category_child_depth($categoryid)
/*
	Return how many levels of subcategory there are below $categoryid
*/
	{
		// This is potentially a very slow query since it counts all the multi-generational offspring of a particular category
		// But it's only used for admin purposes when moving a category around so I don't think it's worth making more efficient
		// (Incidentally, this could be done by keeping a count for every category of how many generations of offspring it has.)
Scott Vivian committed
256

Gideon Greenspan committed
257 258 259 260
		$result=qa_db_read_one_assoc(qa_db_query_sub(
			'SELECT COUNT(child1.categoryid) AS count1, COUNT(child2.categoryid) AS count2, COUNT(child3.categoryid) AS count3 FROM ^categories AS child1 LEFT JOIN ^categories AS child2 ON child2.parentid=child1.categoryid LEFT JOIN ^categories AS child3 ON child3.parentid=child2.categoryid WHERE child1.parentid=#;', // requires QA_CATEGORY_DEPTH=4
			$categoryid
		));
Scott Vivian committed
261

Gideon Greenspan committed
262 263 264
		for ($depth=QA_CATEGORY_DEPTH-1; $depth>=1; $depth--)
			if ($result['count'.$depth])
				return $depth;
Scott Vivian committed
265

Gideon Greenspan committed
266 267
		return 0;
	}
Scott Vivian committed
268 269


Gideon Greenspan committed
270 271 272 273 274 275 276 277 278 279 280
	function qa_db_category_create($parentid, $title, $tags)
/*
	Create a new category with $parentid, $title (=name) and $tags (=slug) in the database
*/
	{
		$lastpos=qa_db_category_last_pos($parentid);

		qa_db_query_sub(
			'INSERT INTO ^categories (parentid, title, tags, position) VALUES (#, $, $, #)',
			$parentid, $title, $tags, 1+$lastpos
		);
Scott Vivian committed
281

Gideon Greenspan committed
282
		$categoryid=qa_db_last_insert_id();
Scott Vivian committed
283

Gideon Greenspan committed
284
		qa_db_categories_recalc_backpaths($categoryid);
Scott Vivian committed
285

Gideon Greenspan committed
286 287
		return $categoryid;
	}
Scott Vivian committed
288 289


Gideon Greenspan committed
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
	function qa_db_categories_recalc_backpaths($firstcategoryid, $lastcategoryid=null)
/*
	Recalculate the backpath columns for all categories from $firstcategoryid to $lastcategoryid (if specified)
*/
	{
		if (!isset($lastcategoryid))
			$lastcategoryid=$firstcategoryid;

		qa_db_query_sub(
			"UPDATE ^categories AS x, (SELECT cat1.categoryid, CONCAT_WS('/', cat1.tags, cat2.tags, cat3.tags, cat4.tags) AS backpath FROM ^categories AS cat1 LEFT JOIN ^categories AS cat2 ON cat1.parentid=cat2.categoryid LEFT JOIN ^categories AS cat3 ON cat2.parentid=cat3.categoryid LEFT JOIN ^categories AS cat4 ON cat3.parentid=cat4.categoryid WHERE cat1.categoryid BETWEEN # AND #) AS a SET x.backpath=a.backpath WHERE x.categoryid=a.categoryid",
			$firstcategoryid, $lastcategoryid // requires QA_CATEGORY_DEPTH=4
		);
	}


	function qa_db_category_rename($categoryid, $title, $tags)
/*
	Set the name of $categoryid to $title and its slug to $tags in the database
*/
	{
		qa_db_query_sub(
			'UPDATE ^categories SET title=$, tags=$ WHERE categoryid=#',
			$title, $tags, $categoryid
		);
Scott Vivian committed
314

Gideon Greenspan committed
315 316
		qa_db_categories_recalc_backpaths($categoryid); // may also require recalculation of its offspring's backpaths
	}
Scott Vivian committed
317 318


Gideon Greenspan committed
319 320 321 322 323 324 325 326 327 328
	function qa_db_category_set_content($categoryid, $content)
/*
	Set the content (=description) of $categoryid to $content
*/
	{
		qa_db_query_sub(
			'UPDATE ^categories SET content=$ WHERE categoryid=#',
			$content, $categoryid
		);
	}
Scott Vivian committed
329 330


Gideon Greenspan committed
331 332 333 334 335 336 337 338 339 340
	function qa_db_category_get_parent($categoryid)
/*
	Return the parentid of $categoryid
*/
	{
		return qa_db_read_one_value(qa_db_query_sub(
			'SELECT parentid FROM ^categories WHERE categoryid=#',
			$categoryid
		));
	}
Scott Vivian committed
341 342


Gideon Greenspan committed
343 344 345 346 347 348 349 350
	function qa_db_category_set_position($categoryid, $newposition)
/*
	Move the category $categoryid into position $newposition under its parent
*/
	{
		qa_db_ordered_move('categories', 'categoryid', $categoryid, $newposition,
			qa_db_apply_sub('parentid<=>#', array(qa_db_category_get_parent($categoryid))));
	}
Scott Vivian committed
351 352


Gideon Greenspan committed
353 354 355 356 357 358
	function qa_db_category_set_parent($categoryid, $newparentid)
/*
	Set the parent of $categoryid to $newparentid, placing it in last position (doesn't do necessary recalculations)
*/
	{
		$oldparentid=qa_db_category_get_parent($categoryid);
Scott Vivian committed
359

Gideon Greenspan committed
360 361
		if (strcmp($oldparentid, $newparentid)) { // if we're changing parent, move to end of old parent, then end of new parent
			$lastpos=qa_db_category_last_pos($oldparentid);
Scott Vivian committed
362

Gideon Greenspan committed
363
			qa_db_ordered_move('categories', 'categoryid', $categoryid, $lastpos, qa_db_apply_sub('parentid<=>#', array($oldparentid)));
Scott Vivian committed
364

Gideon Greenspan committed
365
			$lastpos=qa_db_category_last_pos($newparentid);
Scott Vivian committed
366

Gideon Greenspan committed
367 368 369 370 371 372
			qa_db_query_sub(
				'UPDATE ^categories SET parentid=#, position=# WHERE categoryid=#',
				$newparentid, 1+$lastpos, $categoryid
			);
		}
	}
Scott Vivian committed
373 374


Gideon Greenspan committed
375 376 377 378 379 380 381
	function qa_db_category_reassign($categoryid, $reassignid)
/*
	Change the categoryid of any posts with (exact) $categoryid to $reassignid
*/
	{
		qa_db_query_sub('UPDATE ^posts SET categoryid=# WHERE categoryid<=>#', $reassignid, $categoryid);
	}
Scott Vivian committed
382 383


Gideon Greenspan committed
384 385 386 387 388 389 390 391
	function qa_db_category_delete($categoryid)
/*
	Delete the category $categoryid in the database
*/
	{
		qa_db_ordered_delete('categories', 'categoryid', $categoryid,
			qa_db_apply_sub('parentid<=>#', array(qa_db_category_get_parent($categoryid))));
	}
Scott Vivian committed
392 393


Gideon Greenspan committed
394 395 396 397 398 399 400 401 402 403
	function qa_db_category_slug_to_id($parentid, $slug)
/*
	Return the categoryid for the category with parent $parentid and $slug
*/
	{
		return qa_db_read_one_value(qa_db_query_sub(
			'SELECT categoryid FROM ^categories WHERE parentid<=># AND tags=$',
			$parentid, $slug
		), true);
	}
Scott Vivian committed
404 405


Gideon Greenspan committed
406 407 408 409 410 411
	function qa_db_page_create($title, $flags, $tags, $heading, $content, $permit=null)
/*
	Create a new custom page (or link) in the database
*/
	{
		$position=qa_db_read_one_value(qa_db_query_sub('SELECT 1+COALESCE(MAX(position), 0) FROM ^pages'));
Scott Vivian committed
412

Gideon Greenspan committed
413 414 415 416
		qa_db_query_sub(
			'INSERT INTO ^pages (title, nav, flags, permit, tags, heading, content, position) VALUES ($, \'\', #, #, $, $, $, #)',
			$title, $flags, $permit, $tags, $heading, $content, $position
		);
Scott Vivian committed
417

Gideon Greenspan committed
418 419
		return qa_db_last_insert_id();
	}
Scott Vivian committed
420 421


Gideon Greenspan committed
422 423 424 425 426 427 428 429 430 431
	function qa_db_page_set_fields($pageid, $title, $flags, $tags, $heading, $content, $permit=null)
/*
	Set the fields of $pageid to the values provided in the database
*/
	{
		qa_db_query_sub(
			'UPDATE ^pages SET title=$, flags=#, permit=#, tags=$, heading=$, content=$ WHERE pageid=#',
			$title, $flags, $permit, $tags, $heading, $content, $pageid
		);
	}
Scott Vivian committed
432 433


Gideon Greenspan committed
434 435 436 437 438 439 440 441 442 443 444 445
	function qa_db_page_move($pageid, $nav, $newposition)
/*
	Move the page $pageid into navigation menu $nav and position $newposition in the database
*/
	{
		qa_db_query_sub(
			'UPDATE ^pages SET nav=$ WHERE pageid=#',
			$nav, $pageid
		);

		qa_db_ordered_move('pages', 'pageid', $pageid, $newposition);
	}
Scott Vivian committed
446 447


Gideon Greenspan committed
448 449 450 451 452 453 454
	function qa_db_page_delete($pageid)
/*
	Delete the page $pageid in the database
*/
	{
		qa_db_ordered_delete('pages', 'pageid', $pageid);
	}
Scott Vivian committed
455 456


Gideon Greenspan committed
457 458 459 460 461 462
	function qa_db_ordered_move($table, $idcolumn, $id, $newposition, $conditionsql=null)
/*
	Move the entity identified by $idcolumn=$id into position $newposition (within optional $conditionsql) in $table in the database
*/
	{
		$andsql=isset($conditionsql) ? (' AND '.$conditionsql) : '';
Scott Vivian committed
463

Gideon Greenspan committed
464
		qa_db_query_sub('LOCK TABLES ^'.$table.' WRITE');
Scott Vivian committed
465

Gideon Greenspan committed
466
		$oldposition=qa_db_read_one_value(qa_db_query_sub('SELECT position FROM ^'.$table.' WHERE '.$idcolumn.'=#'.$andsql, $id));
Scott Vivian committed
467

Gideon Greenspan committed
468 469
		if ($newposition!=$oldposition) {
			$lastposition=qa_db_read_one_value(qa_db_query_sub('SELECT MAX(position) FROM ^'.$table.' WHERE TRUE'.$andsql));
Scott Vivian committed
470

Gideon Greenspan committed
471
			$newposition=max(1, min($newposition, $lastposition)); // constrain it to within range
Scott Vivian committed
472

Gideon Greenspan committed
473 474
			qa_db_query_sub('UPDATE ^'.$table.' SET position=# WHERE '.$idcolumn.'=#'.$andsql, 1+$lastposition, $id);
				// move it temporarily off the top because we have a unique key on the position column
Scott Vivian committed
475

Gideon Greenspan committed
476 477 478 479
			if ($newposition<$oldposition)
				qa_db_query_sub('UPDATE ^'.$table.' SET position=position+1 WHERE position BETWEEN # AND #'.$andsql.' ORDER BY position DESC', $newposition, $oldposition);
			else
				qa_db_query_sub('UPDATE ^'.$table.' SET position=position-1 WHERE position BETWEEN # AND #'.$andsql.' ORDER BY position', $oldposition, $newposition);
Scott Vivian committed
480

Gideon Greenspan committed
481 482
			qa_db_query_sub('UPDATE ^'.$table.' SET position=# WHERE '.$idcolumn.'=#'.$andsql, $newposition, $id);
		}
Scott Vivian committed
483

Gideon Greenspan committed
484 485
		qa_db_query_sub('UNLOCK TABLES');
	}
Scott Vivian committed
486 487


Gideon Greenspan committed
488 489 490 491 492 493 494 495
	function qa_db_ordered_delete($table, $idcolumn, $id, $conditionsql=null)
/*
	Delete the entity identified by $idcolumn=$id (and optional $conditionsql) in $table in the database
*/
	{
		$andsql=isset($conditionsql) ? (' AND '.$conditionsql) : '';

		qa_db_query_sub('LOCK TABLES ^'.$table.' WRITE');
Scott Vivian committed
496

Gideon Greenspan committed
497
		$oldposition=qa_db_read_one_value(qa_db_query_sub('SELECT position FROM ^'.$table.' WHERE '.$idcolumn.'=#'.$andsql, $id));
Scott Vivian committed
498

Gideon Greenspan committed
499
		qa_db_query_sub('DELETE FROM ^'.$table.' WHERE '.$idcolumn.'=#'.$andsql, $id);
Scott Vivian committed
500

Gideon Greenspan committed
501
		qa_db_query_sub('UPDATE ^'.$table.' SET position=position-1 WHERE position>#'.$andsql.' ORDER BY position', $oldposition);
Scott Vivian committed
502

Gideon Greenspan committed
503 504
		qa_db_query_sub('UNLOCK TABLES');
	}
Scott Vivian committed
505 506


Gideon Greenspan committed
507
	function qa_db_userfield_create($title, $content, $flags, $permit=null)
Gideon Greenspan committed
508
/*
Gideon Greenspan committed
509
	Create a new user field with (internal) tag $title, label $content, $flags and $permit in the database.
Gideon Greenspan committed
510 511 512
*/
	{
		$position=qa_db_read_one_value(qa_db_query_sub('SELECT 1+COALESCE(MAX(position), 0) FROM ^userfields'));
Scott Vivian committed
513

Gideon Greenspan committed
514
		qa_db_query_sub(
Gideon Greenspan committed
515 516
			'INSERT INTO ^userfields (title, content, position, flags, permit) VALUES ($, $, #, #, #)',
			$title, $content, $position, $flags, $permit
Gideon Greenspan committed
517 518 519 520
		);

		return qa_db_last_insert_id();
	}
Scott Vivian committed
521 522


Gideon Greenspan committed
523
	function qa_db_userfield_set_fields($fieldid, $content, $flags, $permit=null)
Gideon Greenspan committed
524
/*
Gideon Greenspan committed
525
	Change the user field $fieldid to have label $content, $flags and $permit in the database (the title column cannot be changed once set)
Gideon Greenspan committed
526 527 528
*/
	{
		qa_db_query_sub(
Gideon Greenspan committed
529 530
			'UPDATE ^userfields SET content=$, flags=#, permit=# WHERE fieldid=#',
			$content, $flags, $permit, $fieldid
Gideon Greenspan committed
531 532
		);
	}
Scott Vivian committed
533 534


Gideon Greenspan committed
535 536 537 538 539 540 541 542
	function qa_db_userfield_move($fieldid, $newposition)
/*
	Move the user field $fieldid into position $newposition in the database
*/
	{
		qa_db_ordered_move('userfields', 'fieldid', $fieldid, $newposition);
	}

Scott Vivian committed
543

Gideon Greenspan committed
544 545 546 547 548 549 550
	function qa_db_userfield_delete($fieldid)
/*
	Delete the user field $fieldid in the database
*/
	{
		qa_db_ordered_delete('userfields', 'fieldid', $fieldid);
	}
Scott Vivian committed
551 552


Gideon Greenspan committed
553 554 555 556 557 558
	function qa_db_widget_create($title, $tags)
/*
	Return the ID of a new widget, to be displayed by the widget module named $title on templates within $tags (comma-separated list)
*/
	{
		$position=qa_db_read_one_value(qa_db_query_sub('SELECT 1+COALESCE(MAX(position), 0) FROM ^widgets'));
Scott Vivian committed
559

Gideon Greenspan committed
560 561 562 563
		qa_db_query_sub(
			'INSERT INTO ^widgets (place, position, tags, title) VALUES (\'\', #, $, $)',
			$position, $tags, $title
		);
Scott Vivian committed
564

Gideon Greenspan committed
565 566
		return qa_db_last_insert_id();
	}
Scott Vivian committed
567 568


Gideon Greenspan committed
569 570 571 572 573 574 575 576 577 578
	function qa_db_widget_set_fields($widgetid, $tags)
/*
	Set the comma-separated list of templates for $widgetid to $tags
*/
	{
		qa_db_query_sub(
			'UPDATE ^widgets SET tags=$ WHERE widgetid=#',
			$tags, $widgetid
		);
	}
Scott Vivian committed
579 580


Gideon Greenspan committed
581 582 583 584 585 586 587 588 589 590 591 592
	function qa_db_widget_move($widgetid, $place, $newposition)
/*
	Move the widget $widgetit into position $position in the database's order, and show it in $place on the page
*/
	{
		qa_db_query_sub(
			'UPDATE ^widgets SET place=$ WHERE widgetid=#',
			$place, $widgetid
		);

		qa_db_ordered_move('widgets', 'widgetid', $widgetid, $newposition);
	}
Scott Vivian committed
593 594


Gideon Greenspan committed
595 596 597 598 599 600 601 602 603 604 605 606
	function qa_db_widget_delete($widgetid)
/*
	Delete the widget $widgetid in the database
*/
	{
		qa_db_ordered_delete('widgets', 'widgetid', $widgetid);
	}


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