forked from vfremaux/moodle-local_vmoodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupgradelib.php
491 lines (414 loc) · 21.4 KB
/
upgradelib.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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle 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 3 of the License, or
// (at your option) any later version.
//
// Moodle 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 Moodle. If not, see <http://www.gnu.org/licenses/>.
// DEPRECATED / Local plugin is naturally handled for subplugins.
/**
* Find and check all submodules and load them up or upgrade them if necessary
*
* @global object
* @global object
*/
function vmoodle_upgrade_subplugins_modules($startcallback, $endcallback, $verbose = true) {
global $CFG, $DB;
include($CFG->dirroot.'/local/vmoodle/db/subplugins.php');
foreach ($subplugins as $type => $subpluginpath) {
$plugindirs = glob($CFG->dirroot.'/'.$subpluginpath.'/*');
foreach ($plugindirs as $dir) {
$plug = basename($dir);
$fullplug = $dir;
if ($plug === 'CVS') { // Someone has unzipped the template, ignore it.
continue;
}
if ($plug === 'NEWMODULE') { // Someone has unzipped the template, ignore it.
continue;
}
// Reset time so that it works when installing a large number of plugins.
set_time_limit(600);
$component = clean_param($type.'_'.$plug, PARAM_COMPONENT); // standardised plugin name
// Check plugin dir is valid name.
if (empty($component)) {
throw new plugin_defective_exception($type.'_'.$plug, 'Invalid plugin directory name.');
}
if (!is_readable($fullplug.'/version.php')) {
continue;
}
$plugin = new stdClass();
require($fullplug.'/version.php'); // Defines $plugin with version etc.
// If plugin tells us it's full name we may check the location.
if (isset($plugin->component)) {
if ($plugin->component !== $component) {
throw new plugin_defective_exception($component, 'Plugin installed in wrong folder.');
}
}
if (empty($plugin->version)) {
throw new plugin_defective_exception($component, 'Missing version value in version.php');
}
$plugin->name = $plug;
$plugin->fullname = $component;
if (!empty($plugin->requires)) {
if ($plugin->requires > $CFG->version) {
throw new upgrade_requires_exception($component, $plugin->version, $CFG->version, $plugin->requires);
} else if ($plugin->requires < 2010000000) {
throw new plugin_defective_exception($component, 'Plugin is not compatible with Moodle 2.x or later.');
}
}
// Try to recover from interrupted install.php if needed.
if (file_exists($fullplug.'/db/install.php')) {
if (get_config($plugin->fullname, 'installrunning')) {
require_once($fullplug.'/db/install.php');
$recover_install_function = 'xmldb_'.$plugin->fullname.'_install_recovery';
if (function_exists($recover_install_function)) {
$startcallback($component, true, $verbose);
$recover_install_function();
unset_config('installrunning', $plugin->fullname);
update_capabilities($component);
log_update_descriptions($component);
external_update_descriptions($component);
events_update_definition($component);
message_update_providers($component);
if ($type === 'message') {
message_update_processors($plug);
}
vmoodle_upgrade_plugin_mnet_functions($component, $fullplug);
// Fix wrongly twicked paths.
if ($rpc_shifted_defines = $DB->get_records_select('mnet_rpc', " xmlrpcpath LIKE 'vmoodleadminset' ", array())) {
foreach($rpc_shifted_defines as $rpc) {
$rpc->xmlrpcpath = str_replace('vmoocleadminset', 'local/vmoodle/plugins');
$DB->update_record('mnet_rpc', $rpc);
}
}
$endcallback($component, true, $verbose);
}
}
}
$installedversion = get_config($plugin->fullname, 'version');
if (empty($installedversion)) { // New installation.
$startcallback($component, true, $verbose);
// Install tables if defined.
if (file_exists($fullplug.'/db/install.xml')) {
$DB->get_manager()->install_from_xmldb_file($fullplug.'/db/install.xml');
}
// Store version.
upgrade_plugin_savepoint(true, $plugin->version, $type, $plug, false);
// Execute post install file.
if (file_exists($fullplug.'/db/install.php')) {
require_once($fullplug.'/db/install.php');
set_config('installrunning', 1, $plugin->fullname);
$post_install_function = 'xmldb_'.$plugin->fullname.'_install';
$post_install_function();
unset_config('installrunning', $plugin->fullname);
}
// Install various components.
update_capabilities($component);
log_update_descriptions($component);
external_update_descriptions($component);
events_update_definition($component);
message_update_providers($component);
if ($type === 'message') {
message_update_processors($plug);
}
vmoodle_upgrade_plugin_mnet_functions($component, $fullplug);
// Fix wrongly twicked paths.
if ($rpc_shifted_defines = $DB->get_records_select('mnet_rpc', " xmlrpcpath LIKE 'vmoodleadminset' ", array())) {
foreach($rpc_shifted_defines as $rpc) {
$rpc->xmlrpcpath = str_replace('vmoocleadminset', 'local/vmoodle/plugins');
$DB->update_record('mnet_rpc', $rpc);
}
}
purge_all_caches();
$endcallback($component, true, $verbose);
} else if ($installedversion < $plugin->version) { // Upgrade
// Run the upgrade function for the plugin.
$startcallback($component, false, $verbose);
if (is_readable($fullplug.'/db/upgrade.php')) {
require_once($fullplug.'/db/upgrade.php'); // Defines upgrading function
$newupgrade_function = 'xmldb_'.$plugin->fullname.'_upgrade';
$result = $newupgrade_function($installedversion);
} else {
$result = true;
}
$installedversion = get_config($plugin->fullname, 'version');
if ($installedversion < $plugin->version) {
// store version if not already there.
upgrade_plugin_savepoint($result, $plugin->version, $type, $plug, false);
}
// Upgrade various components.
update_capabilities($component);
log_update_descriptions($component);
external_update_descriptions($component);
events_update_definition($component);
message_update_providers($component);
if ($type === 'message') {
message_update_processors($plug);
}
vmoodle_upgrade_plugin_mnet_functions($component, $fullplug);
purge_all_caches();
$endcallback($component, false, $verbose);
} else if ($installedversion > $plugin->version) {
throw new downgrade_exception($component, $installedversion, $plugin->version);
}
}
}
}
function vmoodle_uninstall_plugins() {
global $CFG;
include($CFG->dirroot.'/local/vmoodle/db/subplugins.php');
foreach($subplugins as $type => $subpluginpath) {
$plugindirs = glob($CFG->dirroot.'/'.$subpluginpath.'/*');
foreach ($plugindirs as $dir) {
$plug = basename($dir);
$fullplug = $dir;
if ($plug === 'CVS') { // Someone has unzipped the template, ignore it.
continue;
}
if ($plug === 'NEWMODULE') { // Someone has unzipped the template, ignore it.
continue;
}
vmoodle_uninstall_plugin($type, $plug, $fullplug);
}
}
}
/**
* Automatically clean-up all plugin data and remove the plugin DB tables
*
* @param string $type The subplugin type, eg. 'vmoodleadminset' etc.
* @param string $name The subplugin name, eg. 'example', 'generic', etc.
* @uses global $OUTPUT to produce notices and other messages
* @return void
*/
function vmoodle_uninstall_plugin($type, $name, $plugindirectory) {
global $CFG, $DB, $OUTPUT;
// This may take a long time.
@set_time_limit(0);
$component = $type . '_' . $name; // eg. 'qtype_multichoice' or 'workshopgrading_accumulative' or 'mod_forum'
$pluginname = $component;
if (get_string_manager()->string_exists('pluginname', $component)) {
$strpluginname = get_string('pluginname', $component);
} else {
$strpluginname = $component;
}
echo $OUTPUT->heading($pluginname);
$uninstalllib = $plugindirectory . '/db/uninstall.php';
if (file_exists($uninstalllib)) {
require_once($uninstalllib);
$uninstallfunction = 'xmldb_' . $pluginname . '_uninstall'; // eg. 'xmldb_workshop_uninstall()'
if (function_exists($uninstallfunction)) {
if (!$uninstallfunction()) {
echo $OUTPUT->notification('Encountered a problem running uninstall function for '. $pluginname);
}
}
}
// Perform clean-up task common for all the plugin/subplugin types.
// Delete the web service functions and pre-built services.
require_once($CFG->dirroot.'/lib/externallib.php');
external_delete_descriptions($component);
// Delete calendar events.
$DB->delete_records('event', array('modulename' => $pluginname));
// Delete all the logs.
$DB->delete_records('log', array('module' => $pluginname));
// Delete log_display information.
$DB->delete_records('log_display', array('component' => $component));
// Delete the module configuration records.
unset_all_config_for_plugin($pluginname);
// delete message provider
message_provider_uninstall($component);
// Delete message processor.
if ($type === 'message') {
message_processor_uninstall($name);
}
// Delete the plugin tables.
$xmldbfilepath = $plugindirectory . '/db/install.xml';
drop_plugin_tables($component, $xmldbfilepath, false);
// Delete the capabilities that were defined by this module.
capabilities_cleanup($component);
// Remove event handlers and dequeue pending events.
events_uninstall($component);
echo $OUTPUT->notification(get_string('success'), 'notifysuccess');
}
/**
* upgrades the mnet rpc definitions for the given component.
* this method doesn't return status, an exception will be thrown in the case of an error
*
* @param string $component the plugin to upgrade, eg auth_mnet
*/
function vmoodle_upgrade_plugin_mnet_functions($component, $path) {
global $DB, $CFG;
list($type, $plugin) = explode('_', $component);
$publishes = array();
$subscribes = array();
if (file_exists($path . '/db/mnet.php')) {
require_once($path . '/db/mnet.php'); // $publishes comes from this file.
}
if (empty($publishes)) {
$publishes = array(); // Still need this to be able to disable stuff later.
}
if (empty($subscribes)) {
$subscribes = array(); // Still need this to be able to disable stuff later.
}
static $servicecache = array();
// Rekey an array based on the rpc method for easy lookups later.
$publishmethodservices = array();
$subscribemethodservices = array();
foreach ($publishes as $servicename => $service) {
if (is_array($service['methods'])) {
foreach ($service['methods'] as $methodname) {
$service['servicename'] = $servicename;
$publishmethodservices[$methodname][] = $service;
}
}
}
// Disable functions that don't exist (any more) in the source.
// Should these be deleted? What about their permissions records?
foreach ($DB->get_records('mnet_rpc', array('pluginname' => $plugin, 'plugintype' => $type), 'functionname ASC ') as $rpc) {
if (!array_key_exists($rpc->functionname, $publishmethodservices) && $rpc->enabled) {
$DB->set_field('mnet_rpc', 'enabled', 0, array('id' => $rpc->id));
} else if (array_key_exists($rpc->functionname, $publishmethodservices) && !$rpc->enabled) {
$DB->set_field('mnet_rpc', 'enabled', 1, array('id' => $rpc->id));
}
}
// Reflect all the services we're publishing and save them.
require_once($CFG->dirroot . '/lib/zend/Zend/Server/Reflection.php');
static $cachedclasses = array(); // To store reflection information in.
foreach ($publishes as $service => $data) {
$f = $data['filename'];
$c = $data['classname'];
foreach ($data['methods'] as $method) {
$dataobject = new stdClass();
$dataobject->plugintype = $type;
$dataobject->pluginname = $plugin;
$dataobject->enabled = 1;
$dataobject->classname = $c;
$dataobject->filename = $f;
if (is_string($method)) {
$dataobject->functionname = $method;
} elseif (is_array($method)) { // Wants to override file or class.
$dataobject->functionname = $method['method'];
$dataobject->classname = $method['classname'];
$dataobject->filename = $method['filename'];
}
$dataobject->xmlrpcpath = $type.'/'.$plugin.'/'.$dataobject->filename.'/'.$method;
$dataobject->static = false;
require_once($path . '/' . $dataobject->filename);
$functionreflect = null; // Slightly different ways to get this depending on whether it's a class method or a function.
if (!empty($dataobject->classname)) {
if (!class_exists($dataobject->classname)) {
throw new moodle_exception('installnosuchmethod', 'mnet', '', (object)array('method' => $dataobject->functionname, 'class' => $dataobject->classname));
}
$key = $dataobject->filename . '|' . $dataobject->classname;
if (!array_key_exists($key, $cachedclasses)) { // Look to see if we've already got a reflection object.
try {
$cachedclasses[$key] = Zend_Server_Reflection::reflectClass($dataobject->classname);
} catch (Zend_Server_Reflection_Exception $e) { // catch these and rethrow them to something more helpful
throw new moodle_exception('installreflectionclasserror', 'mnet', '', (object)array('method' => $dataobject->functionname, 'class' => $dataobject->classname, 'error' => $e->getMessage()));
}
}
$r =& $cachedclasses[$key];
if (!$r->hasMethod($dataobject->functionname)) {
throw new moodle_exception('installnosuchmethod', 'mnet', '', (object)array('method' => $dataobject->functionname, 'class' => $dataobject->classname));
}
// stupid workaround for zend not having a getMethod($name) function
$ms = $r->getMethods();
foreach ($ms as $m) {
if ($m->getName() == $dataobject->functionname) {
$functionreflect = $m;
break;
}
}
$dataobject->static = (int)$functionreflect->isStatic();
} else {
if (!function_exists($dataobject->functionname)) {
throw new moodle_exception('installnosuchfunction', 'mnet', '', (object)array('method' => $dataobject->functionname, 'file' => $dataobject->filename));
}
try {
$functionreflect = Zend_Server_Reflection::reflectFunction($dataobject->functionname);
} catch (Zend_Server_Reflection_Exception $e) { // catch these and rethrow them to something more helpful
throw new moodle_exception('installreflectionfunctionerror', 'mnet', '', (object)array('method' => $dataobject->functionname, '' => $dataobject->filename, 'error' => $e->getMessage()));
}
}
$dataobject->profile = serialize(admin_mnet_method_profile($functionreflect));
$dataobject->help = $functionreflect->getDescription();
if ($record_exists = $DB->get_record('mnet_rpc', array('xmlrpcpath'=>$dataobject->xmlrpcpath))) {
$dataobject->id = $record_exists->id;
$dataobject->enabled = $record_exists->enabled;
$DB->update_record('mnet_rpc', $dataobject);
} else {
$dataobject->id = $DB->insert_record('mnet_rpc', $dataobject, true);
}
/*
* TODO this API versioning must be reworked, here the recently processed method
* sets the service API which may not be correct
*/
foreach ($publishmethodservices[$dataobject->functionname] as $service) {
if ($serviceobj = $DB->get_record('mnet_service', array('name'=>$service['servicename']))) {
$serviceobj->apiversion = $service['apiversion'];
$DB->update_record('mnet_service', $serviceobj);
} else {
$serviceobj = new stdClass();
$serviceobj->name = $service['servicename'];
$serviceobj->description = empty($service['description']) ? '' : $service['description'];
$serviceobj->apiversion = $service['apiversion'];
$serviceobj->offer = 1;
$serviceobj->id = $DB->insert_record('mnet_service', $serviceobj);
}
$servicecache[$service['servicename']] = $serviceobj;
if (!$DB->record_exists('mnet_service2rpc', array('rpcid'=>$dataobject->id, 'serviceid'=>$serviceobj->id))) {
$obj = new stdClass();
$obj->rpcid = $dataobject->id;
$obj->serviceid = $serviceobj->id;
$DB->insert_record('mnet_service2rpc', $obj, true);
}
}
}
}
// Finished with methods we publish, now do subscribable methods.
foreach($subscribes as $service => $methods) {
if (!array_key_exists($service, $servicecache)) {
if (!$serviceobj = $DB->get_record('mnet_service', array('name' => $service))) {
debugging("TODO: skipping unknown service $service - somebody needs to fix MDL-21993");
continue;
}
$servicecache[$service] = $serviceobj;
} else {
$serviceobj = $servicecache[$service];
}
foreach ($methods as $method => $xmlrpcpath) {
if (!$rpcid = $DB->get_field('mnet_remote_rpc', 'id', array('xmlrpcpath'=>$xmlrpcpath))) {
$remoterpc = (object)array(
'functionname' => $method,
'xmlrpcpath' => $xmlrpcpath,
'plugintype' => $type,
'pluginname' => $plugin,
'enabled' => 1,
);
$rpcid = $remoterpc->id = $DB->insert_record('mnet_remote_rpc', $remoterpc, true);
}
if (!$DB->record_exists('mnet_remote_service2rpc', array('rpcid'=>$rpcid, 'serviceid'=>$serviceobj->id))) {
$obj = new stdClass();
$obj->rpcid = $rpcid;
$obj->serviceid = $serviceobj->id;
$DB->insert_record('mnet_remote_service2rpc', $obj, true);
}
$subscribemethodservices[$method][] = $service;
}
}
foreach ($DB->get_records('mnet_remote_rpc', array('pluginname'=>$plugin, 'plugintype'=>$type), 'functionname ASC ') as $rpc) {
if (!array_key_exists($rpc->functionname, $subscribemethodservices) && $rpc->enabled) {
$DB->set_field('mnet_remote_rpc', 'enabled', 0, array('id' => $rpc->id));
} else if (array_key_exists($rpc->functionname, $subscribemethodservices) && !$rpc->enabled) {
$DB->set_field('mnet_remote_rpc', 'enabled', 1, array('id' => $rpc->id));
}
}
return true;
}