$progressBar = new ezcConsoleProgressbar( $output, $nbImageFiles, $progressBarOptions ); // Loop the image files and check if it is still used by a content object attribute. If not, delete it. foreach( $aImageFiles as $image ) { $filePath = $image->attribute( 'filepath' ); $dirpath = dirname( $filePath ); $contentObjectAttributeID = $image->attribute( 'contentobject_attribute_id' ); $dbResult = eZImageFile::fetchImageAttributesByFilepath( $filePath, $contentObjectAttributeID ); if( count( $dbResult ) == 0 ) { $file = eZClusterFileHandler::instance( $filePath ); if ( $file->exists() ) // Delete the file physically { $file->delete(); eZImageFile::removeFilepath( $contentObjectAttributeID, $filePath ); eZDir::cleanupEmptyDirectories( $dirpath ); } // Delete the obsolete reference in the database $image->remove(); } $progressBar->advance(); } $progressBar->finish(); $output->outputLine(); } else {
/** * Removes all the image aliases and their information. * The stored images will also be removed if the attribute is the owner * of the images. * * After the images are removed the attribute will containe an internal * structure with empty data * * @param eZContentObjectAttribute $contentObjectAttribute * Content object attribute to remove aliases for * * @return void */ function removeAliases($contentObjectAttribute) { $aliasList = $this->aliasList(); $alternativeText = false; $contentObjectAttributeVersion = $this->ContentObjectAttributeData['version']; $contentObjectAttributeID = $this->ContentObjectAttributeData['id']; $isImageOwner = $this->isImageOwner(); // We loop over each image alias, and look up the file in ezcontentobject_attribute // Only images referenced by one version will be removed foreach ($aliasList as $aliasName => $alias) { $dirpath = $alias['dirpath']; $doNotDelete = false; // Do not delete files from storage if ($aliasName == 'original') { $alternativeText = $alias['alternative_text']; } if ($alias['is_valid']) { $filepath = $alias['url']; // Fetch ezimage attributes that use $filepath // Always returns current attribute (array of $contentObjectAttributeID and $contentObjectAttributeVersion) $dbResult = eZImageFile::fetchImageAttributesByFilepath($filepath, $contentObjectAttributeID); $dbResultCount = count($dbResult); // Check if there are the attributes. if ($dbResultCount > 0) { $doNotDelete = true; foreach ($dbResult as $res) { // We only look results where the version matches if ($res['version'] == $contentObjectAttributeVersion) { // If more than one result has been returned, it means // that another version is using the same image, // and we should not delete this file if ($dbResultCount > 1) { continue; } else { $doNotDelete = false; } } eZImageFile::appendFilepath($res['id'], $filepath, true); } } if (!$doNotDelete) { $file = eZClusterFileHandler::instance($filepath); if ($file->exists()) { $file->delete(); eZImageFile::removeFilepath($contentObjectAttributeID, $filepath); eZDir::cleanupEmptyDirectories($dirpath); } else { eZDebug::writeError("Image file {$filepath} for alias {$aliasName} does not exist, could not remove from disk", __METHOD__); } } } } $doc = new DOMDocument('1.0', 'utf-8'); $imageNode = $doc->createElement("ezimage"); $doc->appendChild($imageNode); $imageNode->setAttribute('serial_number', false); $imageNode->setAttribute('is_valid', false); $imageNode->setAttribute('filename', false); $imageNode->setAttribute('suffix', false); $imageNode->setAttribute('basename', false); $imageNode->setAttribute('dirpath', false); $imageNode->setAttribute('url', false); $imageNode->setAttribute('original_filename', false); $imageNode->setAttribute('mime_type', false); $imageNode->setAttribute('width', false); $imageNode->setAttribute('height', false); $imageNode->setAttribute('alternative_text', $alternativeText); $imageNode->setAttribute('alias_key', false); $imageNode->setAttribute('timestamp', false); $this->ContentObjectAttributeData['DataTypeCustom']['dom_tree'] = $doc; unset($this->ContentObjectAttributeData['DataTypeCustom']['alias_list']); $this->storeDOMTree($doc, true, $contentObjectAttribute); }
static function moveFilepath( $contentObjectAttributeID, $oldFilepath, $newFilepath ) { $db = eZDB::instance(); $db->begin(); eZImageFile::removeFilepath( $contentObjectAttributeID, $oldFilepath ); $result = eZImageFile::appendFilepath( $contentObjectAttributeID, $newFilepath ); $db->commit(); return $result; }
/** * Attempt to remove content object 'image' attribute image variations by content object attribute * * @param object $contentClassImageAttribute object of objects of class eZContentObjectAttribute * @param array $class Array of object class identifiers to remove aliases for only these classes. Optional. Defaults to false * @param array $attributes Array of object image attribute identifiers to remove aliases from. Optional. Defaults to false * @param array $aliases Array of object image attribute image aliases to remove. Optional. Defaults to false * * @return bool true if successful, false otherwise * @static */ static function removeByAttribute($contentObjectAttribute = false, $classes = false, $attributes = false, $aliases = false) { if (!is_object($contentObjectAttribute)) { return false; } // Test that content object class attribute identifier matches provided classes if ($classes != false && is_array($classes) && !in_array($contentObjectAttribute->attribute('object')->attribute('class_identifier'), $classes)) { return false; } // Test that content object class attribute identifier matches provided classes if ($attributes != false && is_array($attributes) && !in_array($contentObjectAttribute->attribute('contentclass_attribute_identifier'), $attributes)) { return false; } // Default datatypes to create image alias variations $imageDataTypeStrings = eZINI::instance('bcimagealias.ini')->variable('BCImageAliasSettings', 'ImageDataTypeStringList'); // Check that content object attribute data type string matches allowed datatype settings if (!in_array($contentObjectAttribute->attribute('data_type_string'), $imageDataTypeStrings) || !$contentObjectAttribute->attribute('has_content')) { return false; } $filePaths = array(); $results = array(); $executionOptions = self::executionOptions(); $messageCount = 0; $imageHandler = $contentObjectAttribute->attribute('content'); $aliasList = $imageHandler->aliasList(false); // Do not process the orginal image alias unset($aliasList['original']); if (count($aliasList) == 0) { return false; } // Optional debug output if ($executionOptions['troubleshoot'] && $executionOptions['verboseLevel'] >= 2) { if ($executionOptions['verboseLevel'] >= 3) { self::displayMessage('All attribute image aliases stored in content data text field:', false); self::displayMessage($contentObjectAttribute->attribute('data_text'), "\n"); } if ($executionOptions['verboseLevel'] >= 4) { self::displayMessage('All attribute image aliases stored in content alias list:', false); print_r($aliasList); self::displayMessage('', "\n\n"); } elseif ($executionOptions['verboseLevel'] >= 3) { self::displayMessage('All attribute image aliases stored in content alias list:', false); print_r(array_keys($aliasList)); self::displayMessage('', "\n"); } } $contentObjectID = $contentObjectAttribute->attribute('contentobject_id'); $contentObjectAttributeID = $contentObjectAttribute->attribute('id'); $contentObjectAttributeVersion = $contentObjectAttribute->attribute('version'); if ($contentObjectAttributeVersion === null) { $files = eZImageFile::fetchForContentObjectAttribute($contentObjectAttributeID, true); $dirs = array(); $count = 0; // Iterate over files foreach ($files as $filepath) { // Test $filepath from $files is in contains one of the $aliases items if ($aliases != false && is_array($aliases)) { foreach ($aliases as $alias) { if (!stristr('_' . $alias, $filepath)) { continue 1; } } } $file = eZClusterFileHandler::instance($filepath); if ($file->exists()) { $filePaths[] = $filepath; if (!$executionOptions['dry']) { $file->fileDelete($filepath); $dirs[] = eZDir::dirpath($filepath); } $count++; } } if (!$executionOptions['dry']) { $dirs = array_unique($dirs); foreach ($dirs as $dirpath) { eZDir::cleanupEmptyDirectories($dirpath); } eZImageFile::removeForContentObjectAttribute($contentObjectAttributeID); $message = "Removed datatype " . $contentObjectAttribute->attribute('data_type_string') . "type image alias variation " . $filePaths[$messageCount] . "\n"; } else { $message = "Dry run: Remove datatype " . $contentObjectAttribute->attribute('data_type_string') . "type image alias variation " . $filePaths[$messageCount] . "\n"; } while ($messageCount < $count) { self::scriptIterate($message); $messageCount++; $result = true; } } else { // We loop over each image alias, and look up the file in ezcontentobject_attribute // Only images referenced by one version will be removed foreach ($aliasList as $aliasName => $aliasListAliasItem) { // Test $aliasListAliasItem from $aliasList is in $aliases array if ($aliases != false && is_array($aliases) && !in_array($aliasListAliasItem['name'], $aliases)) { continue; } if ($aliasListAliasItem['is_valid'] && $aliasListAliasItem['name'] != 'original') { $filepath = $aliasListAliasItem['url']; // Calculate appropriate message to Alert user with if (!$executionOptions['dry']) { // Remove the alias variation image file from the attribute dom tree $doc = $imageHandler->ContentObjectAttributeData['DataTypeCustom']['dom_tree']; foreach ($doc->getElementsByTagName('alias') as $aliasNode) { if ($aliasListAliasItem['name'] == $aliasNode->getAttribute('name')) { // Optional debug output if ($executionOptions['troubleshoot']) { self::displayMessage('Removing image alias image variation ' . "'" . $aliasNode->getAttribute('name') . "'" . ' from attribute dom document'); } $aliasNode->parentNode->removeChild($aliasNode); } } $imageHandler->ContentObjectAttributeData['DataTypeCustom']['dom_tree'] = $doc; unset($imageHandler->ContentObjectAttributeData['DataTypeCustom']['alias_list']); $imageHandler->storeDOMTree($doc, true, $contentObjectAttribute); } // Calculate appropriate message to Alert user with if ($executionOptions['dry']) { $message = "Dry run: Calculating removal of datatype " . $contentObjectAttribute->attribute('data_type_string') . "type image alias variation file " . $filepath; } else { $message = "Removed standard datatype " . $contentObjectAttribute->attribute('data_type_string') . "type image alias variation file " . $filepath; } if (!$executionOptions['dry']) { $dirpath = $aliasListAliasItem['dirpath']; $file = eZClusterFileHandler::instance($filepath); if ($file->exists()) { $file->purge(); eZImageFile::removeFilepath($contentObjectAttributeID, $filepath); eZDir::cleanupEmptyDirectories($dirpath); self::scriptIterate($message); $results[] = true; } else { eZDebug::writeError("Image file {$filepath} for alias {$aliasName} does not exist, could not remove from disk", __METHOD__); self::displayMessage("Image file {$filepath} for alias {$aliasName} does not exist, could not remove from disk: " . __METHOD__); } eZContentCacheManager::clearContentCacheIfNeeded($contentObjectID); } else { self::scriptIterate($message); $results[] = true; } } } } // Calculate return results based on execution options and results comparison if (in_array(true, $results) && count($aliasList) == count($results) && !$executionOptions['dry']) { return true; } return false; }