Fix enabling disabled extensions with dependencies

Improve enabling extensions with no dependencies
This commit is contained in:
Shivam Mathur 2022-01-17 08:58:30 +05:30
parent 51476af47d
commit 449da348c0
No known key found for this signature in database
GPG Key ID: 3E13E4C8591ACC2A
5 changed files with 60 additions and 18 deletions

View File

@ -23,11 +23,14 @@ disable_dependency_extensions() {
disable_extension_helper() { disable_extension_helper() {
local extension=$1 local extension=$1
local disable_dependents=${2:-false} local disable_dependents=${2:-false}
get_extension_map
if [ "$disable_dependents" = "true" ]; then if [ "$disable_dependents" = "true" ]; then
disable_extension_dependents "$extension" disable_extension_dependents "$extension"
fi fi
sudo sed -Ei '' "/=(.*\/)?\"?$extension(.so)?$/d" "${ini_file:?}" sudo sed -Ei '' "/=(.*\/)?\"?$extension(.so)?$/d" "${ini_file:?}"
sudo rm -rf "$scan_dir"/*"$extension"* sudo rm -rf "$scan_dir"/*"$extension"*
mkdir -p /tmp/extdisabled/"$version"
echo '' | sudo tee /tmp/extdisabled/"$version"/"$extension" >/dev/null 2>&1
} }
# Function to fetch a brew tap. # Function to fetch a brew tap.

View File

@ -82,7 +82,7 @@ Function Add-Extension {
# Function to get a map of extensions and their dependent shared extensions. # Function to get a map of extensions and their dependent shared extensions.
Function Get-ExtensionMap { Function Get-ExtensionMap {
php -d'error_reporting=0' $dist\..\src\scripts\ext\extension_map.php php -d'error_reporting=0' $dist\..\src\scripts\extensions\extension_map.php $env:TEMP\map.orig
} }
# Function to enable extension dependencies which are also extensions. # Function to enable extension dependencies which are also extensions.
@ -94,9 +94,10 @@ Function Enable-ExtensionDependencies {
[string] [string]
$extension $extension
) )
if (-not(Test-Path $env:TEMP\map.orig)) { if (-not(Test-Path $env:TEMP\extdisabled\$extension)) {
Get-ExtensionMap | Set-Content -Path $env:TEMP\map.orig return
} }
Get-ExtensionMap
$entry = findstr /r "$extension`:.*" $env:TEMP\map.orig $entry = findstr /r "$extension`:.*" $env:TEMP\map.orig
if($entry) { if($entry) {
$entry.split(':')[1].trim().split(' ') | ForEach-Object { $entry.split(':')[1].trim().split(' ') | ForEach-Object {
@ -105,6 +106,7 @@ Function Enable-ExtensionDependencies {
} }
} }
} }
Remove-Item $env:TEMP\extdisabled\$extension -Force
} }
# Function to disable dependent extensions. # Function to disable dependent extensions.
@ -116,7 +118,7 @@ Function Disable-DependentExtensions() {
[string] [string]
$extension $extension
) )
Get-ExtensionMap | Select-String -Pattern ".*:.*\s$extension(\s|$)" | ForEach-Object { Select-String -Pattern ".*:.*\s$extension(\s|$)" $env:TEMP\map.orig | ForEach-Object {
$dependent = $_.Matches[0].Value.split(':')[0]; $dependent = $_.Matches[0].Value.split(':')[0];
Disable-ExtensionHelper -Extension $dependent -DisableDependents Disable-ExtensionHelper -Extension $dependent -DisableDependents
Add-Log $tick ":$extension" "Disabled $dependent as it depends on $extension" Add-Log $tick ":$extension" "Disabled $dependent as it depends on $extension"
@ -133,10 +135,13 @@ Function Disable-ExtensionHelper() {
$extension, $extension,
[switch] $DisableDependents [switch] $DisableDependents
) )
Get-ExtensionMap
if($DisableDependents) { if($DisableDependents) {
Disable-DependentExtensions $extension Disable-DependentExtensions $extension
} }
Disable-PhpExtension -Extension $extension -Path $php_dir Disable-PhpExtension -Extension $extension -Path $php_dir
New-Item $env:TEMP\extdisabled -Type Directory -Force > $null 2>&1
New-Item $env:TEMP\extdisabled\$extension -Type File -Force > $null 2>&1
} }
# Function to disable an extension. # Function to disable an extension.
@ -174,7 +179,12 @@ Function Disable-Extension() {
# Function to disable shared extensions. # Function to disable shared extensions.
Function Disable-AllShared() { Function Disable-AllShared() {
Get-ExtensionMap
(Get-Content $php_dir\php.ini) | Where-Object {$_ -notmatch '^(zend_)?extension\s*='} | Set-Content $php_dir\php.ini (Get-Content $php_dir\php.ini) | Where-Object {$_ -notmatch '^(zend_)?extension\s*='} | Set-Content $php_dir\php.ini
New-Item $env:TEMP\extdisabled\$version -Type Directory -Force > $null 2>&1
Get-Childitem $ext_dir\*.dll | ForEach-Object {
New-Item ("$env:TEMP\extdisabled\$version\" + ($_.Name.split('.')[0].split('_')[1])) -Type File -Force > $null 2>&1
}
Add-Log $tick "none" "Disabled all shared extensions" Add-Log $tick "none" "Disabled all shared extensions"
} }

View File

@ -49,25 +49,25 @@ enable_extension() {
# Function to get a map of extensions and their dependent shared extensions. # Function to get a map of extensions and their dependent shared extensions.
get_extension_map() { get_extension_map() {
php -d'error_reporting=0' "${dist:?}"/../src/scripts/extensions/extension_map.php php -d'error_reporting=0' "${dist:?}"/../src/scripts/extensions/extension_map.php /tmp/map.orig
} }
# Function to enable extension dependencies which are also extensions. # Function to enable extension dependencies which are also extensions.
enable_extension_dependencies() { enable_extension_dependencies() {
local extension=$1 local extension=$1
prefix=$2 prefix=$2
if ! [ -e /tmp/map.orig ]; then [ -e /tmp/extdisabled/"$version"/"$extension" ] || return;
get_extension_map | sudo tee /tmp/map.orig >/dev/null get_extension_map
fi
for dependency in $(grep "$extension:" /tmp/map.orig | cut -d ':' -f 2 | tr '\n' ' '); do for dependency in $(grep "$extension:" /tmp/map.orig | cut -d ':' -f 2 | tr '\n' ' '); do
enable_extension "$dependency" "$prefix" enable_extension "$dependency" "$prefix"
done done
rm /tmp/extdisabled/"$version"/"$extension"
} }
# Function to disable dependent extensions. # Function to disable dependent extensions.
disable_extension_dependents() { disable_extension_dependents() {
local extension=$1 local extension=$1
for dependent in $(get_extension_map | grep -E ".*:.*\s$extension(\s|$)" | cut -d ':' -f 1 | tr '\n' ' '); do for dependent in $(grep -E ".*:.*\s$extension(\s|$)" /tmp/map.orig | cut -d ':' -f 1 | tr '\n' ' '); do
disable_extension_helper "$dependent" true disable_extension_helper "$dependent" true
add_log "${tick:?}" ":$extension" "Disabled $dependent as it depends on $extension" add_log "${tick:?}" ":$extension" "Disabled $dependent as it depends on $extension"
done done
@ -93,8 +93,11 @@ disable_extension() {
# Function to disable shared extensions. # Function to disable shared extensions.
disable_all_shared() { disable_all_shared() {
get_extension_map
sudo sed -i.orig -E -e "/^(zend_)?extension\s*=/d" "${ini_file[@]}" "$pecl_file" 2>/dev/null || true sudo sed -i.orig -E -e "/^(zend_)?extension\s*=/d" "${ini_file[@]}" "$pecl_file" 2>/dev/null || true
sudo find "${ini_dir:-$scan_dir}"/.. -name "*.ini" -not -path "*php.ini" -not -path "*phar.ini" -not -path "*pecl.ini" -not -path "*mods-available*" -delete >/dev/null 2>&1 || true sudo find "${ini_dir:-$scan_dir}"/.. -name "*.ini" -not -path "*php.ini" -not -path "*phar.ini" -not -path "*pecl.ini" -not -path "*mods-available*" -delete >/dev/null 2>&1 || true
mkdir -p /tmp/extdisabled/"$version"
sudo find "$ext_dir" -name '*.so' -print0 | xargs -0 -n 1 basename -s .so | xargs -n 1 -I{} touch /tmp/extdisabled/"$version"/{}
add_log "${tick:?}" "none" "Disabled all shared extensions" add_log "${tick:?}" "none" "Disabled all shared extensions"
} }

View File

@ -15,7 +15,7 @@ class ExtensionMap {
/** @var string Prefix in PHP extension file. */ /** @var string Prefix in PHP extension file. */
private $file_prefix; private $file_prefix;
/** @var string String to store the map */ /** @var array Array to store the map */
private $map; private $map;
/** /**
@ -25,7 +25,23 @@ class ExtensionMap {
$this->extension_dir = ini_get('extension_dir'); $this->extension_dir = ini_get('extension_dir');
$this->file_extension = (PHP_OS == 'WINNT' ? '.dll' : '.so'); $this->file_extension = (PHP_OS == 'WINNT' ? '.dll' : '.so');
$this->file_prefix = (PHP_OS == 'WINNT' ? 'php_' : ''); $this->file_prefix = (PHP_OS == 'WINNT' ? 'php_' : '');
$this->map = ''; $this->map = array();
}
/**
* Function to read the extension map.
*/
public function parseMap($path) {
if(file_exists($path)) {
$handle = fopen($path, "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
$line_parts = explode(':', $line);
$this->map[$line_parts[0]] = explode(' ', trim($line_parts[1]));
}
fclose($handle);
}
}
} }
/** /**
@ -75,6 +91,9 @@ class ExtensionMap {
* @throws ReflectionException * @throws ReflectionException
*/ */
public function addExtensionToMap($extension) { public function addExtensionToMap($extension) {
if($this->map && array_key_exists($extension, $this->map) && !empty($this->map[$extension])) {
return;
}
// PHP 5.3 does not allow using $this. // PHP 5.3 does not allow using $this.
$self = $this; $self = $this;
@ -84,15 +103,15 @@ class ExtensionMap {
$dependencies = array_filter($dependencies, function ($dependency) use ($self) { $dependencies = array_filter($dependencies, function ($dependency) use ($self) {
return $self->checkSharedExtension($dependency); return $self->checkSharedExtension($dependency);
}); });
$self->map .= $extension . ': ' . implode(' ', $dependencies) . PHP_EOL; $self->map[$extension] = $dependencies;
} }
/** /**
* Function to print the map of shared extensions and their dependent extensions. * Function to write the map of shared extensions and their dependent extensions.
*
* @return string
*/ */
public function __toString() { public function write() {
$path = $_SERVER['argv'][1];
$this->parseMap($path);
$extensions = array_map('strtolower', $this->getSharedExtensions()); $extensions = array_map('strtolower', $this->getSharedExtensions());
foreach ($extensions as $extension) { foreach ($extensions as $extension) {
try { try {
@ -101,9 +120,13 @@ class ExtensionMap {
} }
} }
return $this->map; $map_string = '';
foreach($this->map as $extension => $dependencies) {
$map_string .= $extension . ': ' . implode(' ', $dependencies) . PHP_EOL;
}
file_put_contents($path, $map_string);
} }
} }
$extension_map = new ExtensionMap(); $extension_map = new ExtensionMap();
echo $extension_map; $extension_map->write();

View File

@ -25,11 +25,14 @@ install_packages() {
disable_extension_helper() { disable_extension_helper() {
local extension=$1 local extension=$1
local disable_dependents=${2:-false} local disable_dependents=${2:-false}
get_extension_map
if [ "$disable_dependents" = "true" ]; then if [ "$disable_dependents" = "true" ]; then
disable_extension_dependents "$extension" disable_extension_dependents "$extension"
fi fi
sudo sed -Ei "/=(.*\/)?\"?$extension(.so)?$/d" "${ini_file[@]}" "$pecl_file" sudo sed -Ei "/=(.*\/)?\"?$extension(.so)?$/d" "${ini_file[@]}" "$pecl_file"
sudo find "$ini_dir"/.. -name "*$extension.ini" -not -path "*phar.ini" -not -path "*pecl.ini" -not -path "*mods-available*" -delete >/dev/null 2>&1 || true sudo find "$ini_dir"/.. -name "*$extension.ini" -not -path "*phar.ini" -not -path "*pecl.ini" -not -path "*mods-available*" -delete >/dev/null 2>&1 || true
mkdir -p /tmp/extdisabled/"$version"
echo '' | sudo tee /tmp/extdisabled/"$version"/"$extension" >/dev/null 2>&1
} }
# Function to add PDO extension. # Function to add PDO extension.