-
Notifications
You must be signed in to change notification settings - Fork 66
/
variables.php
165 lines (140 loc) · 4.52 KB
/
variables.php
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
/*
----------------------------------------------------------------------
LICENSE
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License (GPL)
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.
To read the license please visit http://www.gnu.org/copyleft/gpl.html
----------------------------------------------------------------------
*/
/**
* Persistent variable table
*
* @author Drupal Developpement Team
* @link http://cvs.drupal.org/viewcvs/drupal/drupal/includes/bootstrap.inc?rev=1.38&view=auto
*/
/**
* Load the persistent variable table.
*
* The variable table is composed of values that have been saved in the table
* with variable_set() as well as those explicitly specified in the configuration
* file.
*/
function variable_init($conf = array()) {
global $db;
$result = $db->query('SELECT * FROM `variable`');
$variables=array();
while ($db->next_record($result)) {
/* maybe the data is *not* serialized, in that case, take it verbatim */
$variable = $db->Record;
if (($variables[$variable['name']] = @unserialize($variable['value'])) === FALSE) {
$variables[$variable['name']] = $variable['value'];
}
}
foreach ($conf as $name => $value) {
$variables[$name] = $value;
}
return $variables;
}
/**
* Initialize the global $conf array if necessary
*
* @global $conf the global conf array
* @uses variable_init()
*/
function variable_init_maybe() {
global $conf;
if (!isset($conf)) {
$conf = variable_init();
}
}
/**
* Return a persistent variable.
*
* @param $name
* The name of the variable to return.
* @param $default
* The default value to use if this variable has never been set.
* @param $createit_comment
* If variable doesn't exist, create it with the default value
* and createit_comment value as comment
* @return
* The value of the variable.
* @global $conf
* A cache of the configuration.
*/
function variable_get($name, $default = null, $createit_comment = null) {
global $conf;
variable_init_maybe();
if (isset($conf[$name])) {
return $conf[$name];
} elseif (!is_null($createit_comment)) {
variable_set($name, $default, $createit_comment);
}
return $default;
}
/**
* Set a persistent variable.
*
* @param $name
* The name of the variable to set.
* @param $value
* The value to set. This can be any PHP data type; these functions take care
* of serialization as necessary.
*/
function variable_set($name, $value, $comment = null) {
global $conf, $db, $msg, $hooks;
$msg->log('variable', 'variable_set', '+' . serialize($value) . '+' . $comment . '+');
variable_init_maybe();
if (is_object($value) || is_array($value)) {
$value2 = serialize($value);
} else {
$value2 = $value;
}
if (array_key_exists($name, $conf)) {
$previous = $conf[$name];
} else {
$previous = null;
}
if (!array_key_exists($name, $conf) || $value != $conf[$name]) {
$conf[$name] = $value;
if (empty($comment)) {
$query = "INSERT INTO variable (name, value) values ( ?, ?) on duplicate key update name= ?, value= ? ;";
$query_args = array($name, $value2, $name, $value2);
} else {
$query = "INSERT INTO variable (name, value, comment) values ( ?, ?, ?) on duplicate key update name= ?, value= ?, comment= ? ;";
$query_args = array($name, $value2, $comment, $name, $value2, $comment);
}
$db->query($query, $query_args);
$hooks->invoke("hook_variable_set", array("name" => $name, "old" => $previous, "new" => $value));
}
}
/**
* Unset a persistent variable.
*
* @param $name
* The name of the variable to undefine.
*/
function variable_del($name) {
global $conf, $db;
$db->query("DELETE FROM `variable` WHERE name = ?;", array($name));
unset($conf[$name]);
}
/**
* List all variables
*/
function variables_list() {
global $db;
$t = array();
$db->query("SELECT * FROM `variable` WHERE `comment` IS NOT NULL ORDER BY `name`");
while ($db->next_record()) {
$t[] = $db->Record;
}
return $t;
}