aboutsummaryrefslogtreecommitdiff
path: root/engine/lib/metastrings.php
blob: 3b0fba2e711858b356db1a74af2131f636dd3b02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
	/**
	 * Elgg metastrngs
	 * Functions to manage object metastrings.
	 * 
	 * @package Elgg
	 * @subpackage Core
	 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
	 * @author Marcus Povey <marcus@dushka.co.uk>
	 * @copyright Curverider Ltd 2008
	 * @link http://elgg.org/
	 */

	/** Cache metastrings for a page */
	$METASTRINGS_CACHE = array();

	/**
	 * Return the meta string id for a given tag, or false.
	 * 
	 * @param string $string The value (whatever that is) to be stored
	 * @return mixed Integer tag or false.
	 */
	function get_metastring_id($string)
	{
		global $CONFIG, $METASTRINGS_CACHE;
		
		$string = sanitise_string($string);
		$result = array_search($string, $METASTRINGS_CACHE);
		if ($result!==false) {
			
			if ($CONFIG->debug)
				error_log("Returning id for string:$string from cache.");
			
			return $result;
		}
			
		$row = get_data_row("SELECT * from {$CONFIG->dbprefix}metastrings where string='$string' limit 1");
		if ($row) { 
			$METASTRINGS_CACHE[$row->id] = $row->string; // Cache it
			
			if ($CONFIG->debug)
				error_log("Cacheing {$row->string}");
				
			return $row->id;
		}
			
		return false;
	}
	
	/**
	 * When given an ID, returns the corresponding metastring
	 *
	 * @param int $id Metastring ID
	 * @return string Metastring
	 */
	function get_metastring($id) {
		
		global $CONFIG, $METASTRINGS_CACHE;
		
		$id = (int) $id;
		
		if (isset($METASTRINGS_CACHE[$id])) {
			
			if ($CONFIG->debug)
				error_log("Returning string for id:$id from cache.");
			
			return $METASTRINGS_CACHE[$id];
		}
		
		$row = get_data_row("SELECT * from {$CONFIG->dbprefix}metastrings where id='$id' limit 1");
		if ($row) {
			$METASTRINGS_CACHE[$id] = $row->string; // Cache it
			
			if ($CONFIG->debug)
				error_log("Cacheing {$row->string}");
			
			return $row->string;
		}
			
		return false;
		
	}

	/**
	 * Add a metastring.
	 * It returns the id of the tag, whether by creating it or updating it.
	 * 
	 * @param string $string The value (whatever that is) to be stored
	 * @return mixed Integer tag or false.
	 */
	function add_metastring($string)
	{
		global $CONFIG, $METASTRINGS_CACHE;
		
		$sanstring = sanitise_string($string);
		
		$id = get_metastring_id($string);
		if ($id) return $id;
		
		$result = insert_data("INSERT into {$CONFIG->dbprefix}metastrings (string) values ('$sanstring')");
		if ($result)
			$METASTRINGS_CACHE[$result] = $string;
			
		return $result;
	}
	
?>