Skip to content

Commit b233d14

Browse files
committed
Add function to run uninstall.sql file of the extension when the extension gets removed.
1 parent 2065999 commit b233d14

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

server/lib/classes/extension_installer.inc.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,53 @@ public function load_install_sql($name) {
589589
return true;
590590
}
591591

592+
/**
593+
* Run uninstall.sql file in the database
594+
*/
595+
public function run_uninstall_sql($name) {
596+
global $app, $conf;
597+
598+
// check name validity
599+
if(!preg_match('/^[a-zA-Z0-9_]{1,64}$/',$name)) {
600+
$app->log('Invalid extension name: '.$name, LOGLEVEL_WARN);
601+
$this->addError('Invalid extension name: '.$name);
602+
return false;
603+
}
604+
605+
$ext_dir = $this->extension_basedir.'/'.$name;
606+
607+
// Check if the extension has already been downloaded
608+
if(!is_dir($ext_dir)) {
609+
$app->log('Loading install.sql for extension'.$name.'failed. No such directory.',LOGLEVEL_WARN);
610+
$this->addError('No such directory: '.$ext_dir);
611+
return false;
612+
}
613+
614+
// Check if we have a uninstall.sql
615+
$uninstall_sql_path = $ext_dir.'/install/uninstall.sql';
616+
if(!file_exists($uninstall_sql_path)) {
617+
$app->log('The extension '.$name.' has no uninstall.sql.',LOGLEVEL_DEBUG);
618+
//$this->errors[] = 'No uninstall.sql: '.$uninstall_sql_path;
619+
return false;
620+
}
621+
622+
// Run uninstall.sql using mysql command with login details from $conf
623+
exec('mysql -u '.escapeshellarg($conf['mysql']['user']).' -p'.escapeshellarg($conf['mysql']['password']).' -D '.escapeshellarg($conf['mysql']['db_name']).' < '.escapeshellarg($uninstall_sql_path), $output, $return_var);
624+
625+
// check if execcommand was successful
626+
if($return_var != 0) {
627+
$app->log('Failed to run uninstall.sql for extension '.$name, LOGLEVEL_WARN);
628+
$this->addError('Failed to run uninstall.sql: '.$uninstall_sql_path);
629+
return false;
630+
}
631+
632+
633+
// log success
634+
$app->log('Run uninstall.sql for extension '.$name, LOGLEVEL_INFO);
635+
636+
return true;
637+
}
638+
592639
public function scan_extensions() {
593640
global $app, $conf;
594641

@@ -810,6 +857,9 @@ public function uninstall_extension($name) {
810857
// Uninstall extension
811858
$installer->uninstall($name);
812859

860+
// Run uninstall.sql
861+
$this->run_uninstall_sql($name);
862+
813863
// Remove extension directory
814864
if(!empty($this->extension_basedir.'/'.$name) && is_dir($this->extension_basedir.'/'.$name)) {
815865
exec('rm -rf '.escapeshellarg($this->extension_basedir.'/'.$name));

0 commit comments

Comments
 (0)