/**
* Method to parse through a files element of the installation manifest and remove
* the files that were installed
*
* @param object $element The XML node to process
* @param integer $cid Application ID of application to remove from
*
* @return boolean True on success
*
* @since 3.1
*/
public function removeFiles($element, $cid = 0)
{
if (!$element || !\count($element->children())) {
// Either the tag does not exist or has no children therefore we return zero files processed.
return true;
}
$retval = true;
// Get the client info if we're using a specific client
if ($cid > -1) {
$client = ApplicationHelper::getClientInfo($cid);
} else {
$client = null;
}
// Get the array of file nodes to process
$files = $element->children();
if (\count($files) === 0) {
// No files to process
return true;
}
$folder = '';
/*
* Here we set the folder we are going to remove the files from. There are a few
* special cases that need to be considered for certain reserved tags.
*/
switch ($element->getName()) {
case 'media':
if ((string) $element->attributes()->destination) {
$folder = (string) $element->attributes()->destination;
} else {
$folder = '';
}
$source = $client->path . '/media/' . $folder;
break;
case 'languages':
$lang_client = (string) $element->attributes()->client;
if ($lang_client) {
$client = ApplicationHelper::getClientInfo($lang_client, true);
$source = $client->path . '/language';
} else {
if ($client) {
$source = $client->path . '/language';
} else {
$source = '';
}
}
break;
default:
if ($client) {
$pathname = 'extension_' . $client->name;
$source = $this->getPath($pathname);
} else {
$pathname = 'extension_root';
$source = $this->getPath($pathname);
}
break;
}
// Process each file in the $files array (children of $tagName).
foreach ($files as $file) {
/*
* If the file is a language, we must handle it differently. Language files
* go in a subdirectory based on the language code, ie.
* <language tag="en_US">en_US.mycomponent.ini</language>
* would go in the en_US subdirectory of the languages directory.
*/
if ($file->getName() === 'language' && (string) $file->attributes()->tag !== '') {
if ($source) {
$path = $source . '/' . $file->attributes()->tag . '/' . basename((string) $file);
} else {
$target_client = ApplicationHelper::getClientInfo((string) $file->attributes()->client, true);
$path = $target_client->path . '/language/' . $file->attributes()->tag . '/' . basename((string) $file);
}
// If the language folder is not present, then the core pack hasn't been installed... ignore
if (!Folder::exists(\dirname($path))) {
continue;
}
} else {
$path = $source . '/' . $file;
}
// Actually delete the files/folders
if (is_dir($path)) {
$val = Folder::delete($path);
} else {
$val = File::delete($path);
}
if ($val === false) {
Log::add('Failed to delete ' . $path, Log::WARNING, 'jerror');
$retval = false;
}
}
if (!empty($folder)) {
Folder::delete($source);
}
return $retval;
}