qa-db-metas.php 5.19 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-metas.php
	Version: See define()s at top of qa-include/qa-base.php
	Description: Database-level access to metas tables


	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
	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;
	}


Gideon Greenspan committed
33
	function qa_db_usermeta_set($userid, $key, $value)
Gideon Greenspan committed
34
/*
Gideon Greenspan committed
35
	Set the metadata for user $userid with $key to $value. Keys beginning qa_ are reserved for the Q2A core.
Gideon Greenspan committed
36 37
*/
	{
Gideon Greenspan committed
38
		qa_db_meta_set('usermetas', 'userid', $userid, $key, $value);
Gideon Greenspan committed
39 40
	}

Scott Vivian committed
41

Gideon Greenspan committed
42
	function qa_db_usermeta_clear($userid, $key)
Gideon Greenspan committed
43
/*
Gideon Greenspan committed
44
	Clear the metadata for user $userid with $key ($key can also be an array of keys)
Gideon Greenspan committed
45 46
*/
	{
Gideon Greenspan committed
47
		qa_db_meta_clear('usermetas', 'userid', $userid, $key);
Gideon Greenspan committed
48 49
	}

Scott Vivian committed
50

Gideon Greenspan committed
51
	function qa_db_usermeta_get($userid, $key)
Gideon Greenspan committed
52
/*
Gideon Greenspan committed
53
	Return the metadata value for user $userid with $key ($key can also be an array of keys in which case this
Gideon Greenspan committed
54 55 56
	returns an array of metadata key => value).
*/
	{
Gideon Greenspan committed
57
		return qa_db_meta_get('usermetas', 'userid', $userid, $key);
Gideon Greenspan committed
58
	}
Scott Vivian committed
59

Gideon Greenspan committed
60

Gideon Greenspan committed
61
	function qa_db_postmeta_set($postid, $key, $value)
Gideon Greenspan committed
62
/*
Gideon Greenspan committed
63
	Set the metadata for post $postid with $key to $value. Keys beginning qa_ are reserved for the Q2A core.
Gideon Greenspan committed
64 65
*/
	{
Gideon Greenspan committed
66
		qa_db_meta_set('postmetas', 'postid', $postid, $key, $value);
Gideon Greenspan committed
67 68
	}

Scott Vivian committed
69

Gideon Greenspan committed
70
	function qa_db_postmeta_clear($postid, $key)
Gideon Greenspan committed
71
/*
Gideon Greenspan committed
72
	Clear the metadata for post $postid with $key ($key can also be an array of keys)
Gideon Greenspan committed
73 74
*/
	{
Gideon Greenspan committed
75
		qa_db_meta_clear('postmetas', 'postid', $postid, $key);
Gideon Greenspan committed
76 77
	}

Scott Vivian committed
78

Gideon Greenspan committed
79
	function qa_db_postmeta_get($postid, $key)
Gideon Greenspan committed
80
/*
Gideon Greenspan committed
81
	Return the metadata value for post $postid with $key ($key can also be an array of keys in which case this
Gideon Greenspan committed
82 83 84
	returns an array of metadata key => value).
*/
	{
Gideon Greenspan committed
85
		return qa_db_meta_get('postmetas', 'postid', $postid, $key);
Gideon Greenspan committed
86 87 88
	}


Gideon Greenspan committed
89
	function qa_db_categorymeta_set($categoryid, $key, $value)
Gideon Greenspan committed
90
/*
Gideon Greenspan committed
91
	Set the metadata for category $categoryid with $key to $value. Keys beginning qa_ are reserved for the Q2A core.
Gideon Greenspan committed
92 93
*/
	{
Gideon Greenspan committed
94
		qa_db_meta_set('categorymetas', 'categoryid', $categoryid, $key, $value);
Gideon Greenspan committed
95 96
	}

Scott Vivian committed
97

Gideon Greenspan committed
98
	function qa_db_categorymeta_clear($categoryid, $key)
Gideon Greenspan committed
99
/*
Gideon Greenspan committed
100
	Clear the metadata for category $categoryid with $key ($key can also be an array of keys)
Gideon Greenspan committed
101 102
*/
	{
Gideon Greenspan committed
103
		qa_db_meta_clear('categorymetas', 'categoryid', $categoryid, $key);
Gideon Greenspan committed
104 105
	}

Scott Vivian committed
106

Gideon Greenspan committed
107
	function qa_db_categorymeta_get($categoryid, $key)
Gideon Greenspan committed
108
/*
Gideon Greenspan committed
109
	Return the metadata value for category $categoryid with $key ($key can also be an array of keys in which
Gideon Greenspan committed
110 111 112
	case this returns an array of metadata key => value).
*/
	{
Gideon Greenspan committed
113
		return qa_db_meta_get('categorymetas', 'categoryid', $categoryid, $key);
Gideon Greenspan committed
114
	}
Scott Vivian committed
115

Gideon Greenspan committed
116

Gideon Greenspan committed
117
	function qa_db_tagmeta_set($tag, $key, $value)
Gideon Greenspan committed
118
/*
Gideon Greenspan committed
119
	Set the metadata for tag $tag with $key to $value. Keys beginning qa_ are reserved for the Q2A core.
Gideon Greenspan committed
120 121
*/
	{
Gideon Greenspan committed
122
		qa_db_meta_set('tagmetas', 'tag', $tag, $key, $value);
Gideon Greenspan committed
123 124
	}

Scott Vivian committed
125

Gideon Greenspan committed
126
	function qa_db_tagmeta_clear($tag, $key)
Gideon Greenspan committed
127
/*
Gideon Greenspan committed
128
	Clear the metadata for tag $tag with $key ($key can also be an array of keys)
Gideon Greenspan committed
129 130
*/
	{
Gideon Greenspan committed
131
		qa_db_meta_clear('tagmetas', 'tag', $tag, $key);
Gideon Greenspan committed
132 133
	}

Scott Vivian committed
134

Gideon Greenspan committed
135
	function qa_db_tagmeta_get($tag, $key)
Gideon Greenspan committed
136
/*
Gideon Greenspan committed
137
	Return the metadata value for tag $tag with $key ($key can also be an array of keys in which case this
Gideon Greenspan committed
138 139 140
	returns an array of metadata key => value).
*/
	{
Gideon Greenspan committed
141
		return qa_db_meta_get('tagmetas', 'tag', $tag, $key);
Gideon Greenspan committed
142 143 144
	}


Scott Vivian committed
145
	function qa_db_meta_set($metatable, $idcolumn, $idvalue, $title, $content)
Gideon Greenspan committed
146 147 148 149 150 151 152 153 154 155
/*
	Internal general function to set metadata
*/
	{
		qa_db_query_sub(
			'REPLACE ^'.$metatable.' ('.$idcolumn.', title, content) VALUES ($, $, $)',
			$idvalue, $title, $content
		);
	}

Scott Vivian committed
156

Gideon Greenspan committed
157 158 159 160 161 162 163 164 165 166 167
	function qa_db_meta_clear($metatable, $idcolumn, $idvalue, $title)
/*
	Internal general function to clear metadata
*/
	{
		if (is_array($title)) {
			if (count($title))
				qa_db_query_sub(
					'DELETE FROM ^'.$metatable.' WHERE '.$idcolumn.'=$ AND title IN ($)',
					$idvalue, $title
				);
Scott Vivian committed
168

Gideon Greenspan committed
169 170 171 172 173 174 175
		} else
			qa_db_query_sub(
				'DELETE FROM ^'.$metatable.' WHERE '.$idcolumn.'=$ AND title=$',
				$idvalue, $title
			);
	}

Scott Vivian committed
176

Gideon Greenspan committed
177 178 179 180 181 182 183 184 185 186 187 188 189
	function qa_db_meta_get($metatable, $idcolumn, $idvalue, $title)
/*
	Internal general function to return metadata
*/
	{
		if (is_array($title)) {
			if (count($title))
				return qa_db_read_all_assoc(qa_db_query_sub(
					'SELECT title, content FROM ^'.$metatable.' WHERE '.$idcolumn.'=$ AND title IN($)',
					$idvalue, $title
				), 'title', 'content');
			else
				return array();
Scott Vivian committed
190

Gideon Greenspan committed
191 192 193 194 195 196 197
		} else
			return qa_db_read_one_value(qa_db_query_sub(
				'SELECT content FROM ^'.$metatable.' WHERE '.$idcolumn.'=$ AND title=$',
				$idvalue, $title
			), true);
	}

Scott Vivian committed
198

Gideon Greenspan committed
199 200 201
/*
	Omit PHP closing tag to help avoid accidental output
*/