-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathphp_midgard_replicator.c
274 lines (204 loc) · 8.06 KB
/
php_midgard_replicator.c
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/* Copyright (C) 2006 Piotr Pokora <[email protected]>
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "php_midgard.h"
#include "php_midgard_gobject.h"
#include "php_midgard__helpers.h"
static zend_class_entry *php_midgard_replicator_class;
static PHP_METHOD(midgard_replicator, export)
{
RETVAL_FALSE;
MidgardConnection *mgd = mgd_handle(TSRMLS_C);
CHECK_MGD(mgd);
zval *zobject;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "o", &zobject) == FAILURE)
return;
gboolean exported = midgard_replicator_export(__midgard_dbobject_get_ptr(zobject));
RETURN_BOOL(exported);
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_midgard_replicator_export, 0, 0, 1)
ZEND_ARG_OBJ_INFO(0, object, midgard_dbobject, 0)
ZEND_END_ARG_INFO()
static PHP_METHOD(midgard_replicator, export_by_guid)
{
RETVAL_FALSE;
MidgardConnection *mgd = mgd_handle(TSRMLS_C);
CHECK_MGD(mgd);
char *guid;
int guid_length;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &guid, &guid_length) == FAILURE)
return;
gboolean rv = midgard_replicator_export_by_guid(mgd, (const gchar *) guid);
RETURN_BOOL(rv);
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_midgard_replicator_export_by_guid, 0, 0, 1)
ZEND_ARG_INFO(0, guid)
ZEND_END_ARG_INFO()
static PHP_METHOD(midgard_replicator, export_purged)
{
RETVAL_FALSE;
MidgardConnection *mgd = mgd_handle(TSRMLS_C);
CHECK_MGD(mgd);
char *startdate = NULL, *enddate = NULL;
int start_length, end_length;
zval *ook; /* Object Or Klass name zval */
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|ss", &ook, &startdate, &start_length, &enddate, &end_length) == FAILURE) {
return;
}
const gchar *classname;
if (Z_TYPE_P(ook) == IS_STRING) {
classname = (const gchar *)Z_STRVAL_P(ook);
} else if (Z_TYPE_P(ook) == IS_OBJECT) {
classname = (const gchar *)Z_OBJCE_P(ook)->name;
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "first parameter should be object or string");
return;
}
MidgardObjectClass *klass = MIDGARD_OBJECT_GET_CLASS_BY_NAME(classname);
if (!klass) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s class is not found", classname);
return;
}
char *xml = midgard_replicator_export_purged(mgd, classname, startdate, enddate);
if (xml == NULL)
RETURN_NULL();
RETVAL_STRING(xml, 1);
g_free(xml);
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_midgard_replicator_export_purged, 0, 0, 1)
ZEND_ARG_INFO(0, class)
ZEND_ARG_INFO(0, startdate)
ZEND_ARG_INFO(0, enddate)
ZEND_END_ARG_INFO()
static PHP_METHOD(midgard_replicator, serialize)
{
RETVAL_FALSE;
MidgardConnection *mgd = mgd_handle(TSRMLS_C);
CHECK_MGD(mgd);
gchar *xml;
zval *zobject;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "o", &zobject) == FAILURE)
return;
GObject *object = __php_gobject_ptr(zobject);
xml = midgard_replicator_serialize(object);
if (xml == NULL)
RETURN_NULL();
RETVAL_STRING(xml, 1);
g_free(xml);
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_midgard_replicator_serialize, 0, 0, 1)
ZEND_ARG_OBJ_INFO(0, object, midgard_dbobject, 0)
ZEND_END_ARG_INFO()
static PHP_METHOD(midgard_replicator, serialize_blob)
{
RETVAL_FALSE;
MidgardConnection *mgd = mgd_handle(TSRMLS_C);
CHECK_MGD(mgd);
zval *zobject;
zend_class_entry *midgard_attachment_ce = zend_fetch_class("midgard_attachment", strlen("midgard_attachment"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC);
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &zobject, midgard_attachment_ce) == FAILURE)
return;
gchar *xml = midgard_replicator_serialize_blob(__midgard_object_get_ptr(zobject));
if (xml == NULL)
RETURN_NULL();
RETVAL_STRING(xml, 1);
g_free(xml);
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_midgard_replicator_serialize_blob, 0, 0, 1)
ZEND_ARG_OBJ_INFO(0, object, midgard_attachment, 0)
ZEND_END_ARG_INFO()
static PHP_METHOD(midgard_replicator, unserialize)
{
RETVAL_FALSE;
MidgardConnection *mgd = mgd_handle(TSRMLS_C);
CHECK_MGD(mgd);
char *xml = NULL;
int xml_length;
zend_bool force = FALSE;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &xml, &xml_length, &force) == FAILURE)
return;
array_init(return_value);
GObject **objects = midgard_replicator_unserialize(mgd, (const gchar *)xml, (gboolean)force);
if (!objects)
return;
size_t i = 0;
zend_class_entry *ce;
while (objects[i] != NULL) {
zval *zobject;
MAKE_STD_ZVAL(zobject);
char *class_name = (char *)G_OBJECT_TYPE_NAME(G_OBJECT(objects[i]));
ce = zend_fetch_class(class_name, strlen(class_name), ZEND_FETCH_CLASS_AUTO TSRMLS_CC);
php_midgard_gobject_new_with_gobject(zobject, ce, objects[i], TRUE TSRMLS_CC);
zend_hash_next_index_insert(HASH_OF(return_value), &zobject, sizeof(zval *), NULL);
i++;
};
g_free(objects);
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_midgard_replicator_unserialize, 0, 0, 1)
ZEND_ARG_INFO(0, xml)
ZEND_ARG_INFO(0, force)
ZEND_END_ARG_INFO()
static PHP_METHOD(midgard_replicator, import_object)
{
RETVAL_FALSE;
MidgardConnection *mgd = mgd_handle(TSRMLS_C);
CHECK_MGD(mgd);
zval *zobject;
zend_bool force = FALSE;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "o|b", &zobject, &force) == FAILURE)
return;
gboolean imported = midgard_replicator_import_object(__midgard_dbobject_get_ptr(zobject), force);
RETURN_BOOL(imported);
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_midgard_replicator_import_object, 0, 0, 1)
ZEND_ARG_INFO(0, object)
ZEND_ARG_INFO(0, force)
ZEND_END_ARG_INFO()
static PHP_METHOD(midgard_replicator, import_from_xml)
{
RETVAL_FALSE;
MidgardConnection *mgd = mgd_handle(TSRMLS_C);
CHECK_MGD(mgd);
char *xml = NULL;
int xml_length;
zend_bool force = FALSE;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|z", &xml, &xml_length, &force) == FAILURE)
return;
midgard_replicator_import_from_xml(mgd, (const gchar *)xml, force);
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_midgard_replicator_import_from_xml, 0, 0, 1)
ZEND_ARG_INFO(0, xml)
ZEND_ARG_INFO(0, force)
ZEND_END_ARG_INFO()
PHP_MINIT_FUNCTION(midgard2_replicator)
{
static zend_function_entry replicator_methods[] = {
PHP_ME(midgard_replicator, export, arginfo_midgard_replicator_export, ZEND_ACC_STATIC|ZEND_ACC_PUBLIC)
PHP_ME(midgard_replicator, export_by_guid, arginfo_midgard_replicator_export_by_guid, ZEND_ACC_STATIC|ZEND_ACC_PUBLIC)
PHP_ME(midgard_replicator, export_purged, arginfo_midgard_replicator_export_purged, ZEND_ACC_STATIC|ZEND_ACC_PUBLIC)
PHP_ME(midgard_replicator, serialize, arginfo_midgard_replicator_serialize, ZEND_ACC_STATIC|ZEND_ACC_PUBLIC)
PHP_ME(midgard_replicator, serialize_blob, arginfo_midgard_replicator_serialize_blob, ZEND_ACC_STATIC|ZEND_ACC_PUBLIC)
PHP_ME(midgard_replicator, unserialize, arginfo_midgard_replicator_unserialize, ZEND_ACC_STATIC|ZEND_ACC_PUBLIC)
PHP_ME(midgard_replicator, import_object, arginfo_midgard_replicator_import_object, ZEND_ACC_STATIC|ZEND_ACC_PUBLIC)
PHP_ME(midgard_replicator, import_from_xml, arginfo_midgard_replicator_import_from_xml, ZEND_ACC_STATIC|ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
};
static zend_class_entry php_midgard_replicator_class_entry;
INIT_CLASS_ENTRY(php_midgard_replicator_class_entry, "MidgardReplicator", replicator_methods);
php_midgard_replicator_class = zend_register_internal_class(&php_midgard_replicator_class_entry TSRMLS_CC);
CLASS_SET_DOC_COMMENT(php_midgard_replicator_class, strdup("Collection of static methods for serializing, unserializing data to XML; importing and exporting it"));
_FIXME_zend_register_class_alias("midgard_replicator", php_midgard_replicator_class);
return SUCCESS;
}