/** * @param int $contentObjectId * @param string $file * @return eZImageFile|null */ protected static function fetchImageFile($contentObjectAttributeId, $file) { return eZImageFile::fetchObject(eZImageFile::definition(), null, array('contentobject_attribute_id' => $contentObjectAttributeId, 'filepath' => $file)); }
public function restoreTrashedObjectAttribute($contentObjectAttribute) { $imageHandler = $contentObjectAttribute->attribute("content"); $originalAlias = $imageHandler->imageAlias("original"); $originalPath = str_replace("/trashed", "", $originalAlias["dirpath"]); $originalName = $imageHandler->imageName($contentObjectAttribute, $contentObjectAttribute->objectVersion()); $imageHandler->updateAliasPath($originalPath, $originalName); if ($imageHandler->isStorageRequired()) { $imageHandler->store($contentObjectAttribute); $contentObjectAttribute->store(); } // Now clean all other aliases, not cleanly registered within the attribute content // First get all remaining aliases full path to then safely remove them $aliasNames = array_keys($imageHandler->aliasList()); $aliasesPath = array(); foreach ($aliasNames as $aliasName) { if ($aliasName === "original") { continue; } $aliasesPath[] = "{$originalAlias["dirpath"]}/{$originalAlias["basename"]}_{$aliasName}.{$originalAlias["suffix"]}"; } if (empty($aliasesPath)) { return; } $conds = array("contentobject_attribute_id" => $contentObjectAttribute->attribute("id"), "filepath" => array($aliasesPath)); $remainingAliases = eZPersistentObject::fetchObjectList(eZImageFile::definition(), null, $conds); unset($conds, $remainingAliasesPath); if (!empty($remainingAliases)) { foreach ($remainingAliases as $remainingAlias) { $filename = basename($remainingAlias->attribute("filepath")); $newFilePath = $originalPath . "/" . $originalName . substr($filename, strrpos($filename, '_')); eZClusterFileHandler::instance($remainingAlias->attribute("filepath"))->move($newFilePath); // $newFilePath might have already been processed in eZImageFile // If so, $remainingAlias is a duplicate. We can then remove it safely $imageFile = eZImageFile::fetchByFilepath(false, $newFilePath, false); if (empty($imageFile)) { $remainingAlias->setAttribute("filepath", $newFilePath); $remainingAlias->store(); } else { $remainingAlias->remove(); } } } }
$output->formats->error->style = array( 'bold' ); $output->formats->error->color = 'red'; if ( !isset( $options['n'] ) ) { $output->outputLine( 'This script will delete images that look orphan' ); $output->outputLine( 'Press ctrl-c in the next 10 seconds to prevent the script from starting...' ); sleep( 10 ); } try { $output->outputLine( 'Looking for obsolete image files...' ); // Fetch all image files in ezimagefile table $aImageFiles = eZPersistentObject::fetchObjectList( eZImageFile::definition() ); $nbImageFiles = count( $aImageFiles ); if( $nbImageFiles > 0 ) { // Progress bar initialization $progressBarOptions = array( 'emptyChar' => ' ', 'barChar' => '=' ); $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' );
static function removeForContentObjectAttribute( $contentObjectAttributeID ) { eZPersistentObject::removeObject( eZImageFile::definition(), array( 'contentobject_attribute_id' => $contentObjectAttributeID ) ); }
/** * Removes $aliasFile and references to it for the attribute. * * The eZImageFile entry as well as the file are only removed if this attribute was the last reference. * * @param string $aliasFile * * @return array bool true if something was actually done, false otherwise */ public function removeAliasFile($aliasFile) { if ($aliasFile == '') { throw new InvalidArgumentException("Expecting image file path"); } // We only delete the eZImageFile if it isn't referenced by attributes of the same id but different version/language if (eZImageFile::isReferencedByOtherAttributes($aliasFile, $this->ContentObjectAttributeData['id'], $this->ContentObjectAttributeData['version'], $this->ContentObjectAttributeData['language_code'])) { return; } eZImageFile::removeObject(eZImageFile::definition(), array('contentobject_attribute_id' => $this->ContentObjectAttributeData['id'], 'filepath' => $aliasFile)); // We only remove the image file if other eZImageFile of the same attribute use it (other versions) if (eZImageFile::isReferencedByOtherImageFiles($aliasFile, $this->ContentObjectAttributeData['id'])) { return; } // finally, remove file if applicable $file = eZClusterFileHandler::instance($aliasFile); if (!$file->exists()) { eZDebug::writeError("Image file {$aliasFile} does not exist, could not remove from disk", __METHOD__); return; } $file->delete(); eZDir::cleanupEmptyDirectories(dirname($aliasFile)); }
public function restoreTrashedObjectAttribute($contentObjectAttribute) { $imageHandler = $contentObjectAttribute->attribute("content"); $originalAlias = $imageHandler->imageAlias("original"); $imageHandler->updateAliasPath(str_replace("/trashed", "", $originalAlias["dirpath"]), $imageHandler->imageName($contentObjectAttribute, $contentObjectAttribute->objectVersion())); if ($imageHandler->isStorageRequired()) { $imageHandler->store($contentObjectAttribute); $contentObjectAttribute->store(); } // Now clean all other aliases, not cleanly registered within the attribute content // First get all remaining aliases full path to then safely remove them $aliasNames = array_keys($imageHandler->aliasList()); $aliasesPath = array(); foreach ($aliasNames as $aliasName) { if ($aliasName === "original") { continue; } $aliasesPath[] = "{$originalAlias["dirpath"]}/{$originalAlias["basename"]}_{$aliasName}.{$originalAlias["suffix"]}"; } if (empty($aliasesPath)) { return; } $conds = array("contentobject_attribute_id" => $contentObjectAttribute->attribute("id"), "filepath" => array($aliasesPath)); $remainingAliases = eZPersistentObject::fetchObjectList(eZImageFile::definition(), null, $conds); unset($conds, $remainingAliasesPath); if (!empty($remainingAliases)) { foreach ($remainingAliases as $remainingAlias) { eZClusterFileHandler::instance($remainingAlias->attribute("filepath"))->delete(); $remainingAlias->remove(); } } }
$path = $options['from_path']; } else { $cli->error("Specifica il percorso da sostituire"); $script->shutdown(); eZExecution::cleanExit(); } $path = rtrim($path, '/') . '/'; $varDir = rtrim($varDir, '/') . '/'; $output = new ezcConsoleOutput(); $question = ezcConsoleQuestionDialog::YesNoQuestion($output, "Correggo i percorsi per VarDir: da \"{$path}\" a \"{$varDir}\" ?", "y"); if (ezcConsoleDialogViewer::displayDialog($question) == "n") { $script->shutdown(); eZExecution::cleanExit(); } else { $cli->output("Process ezimagefile table"); $list = eZImageFile::fetchObjectList(eZImageFile::definition()); foreach ($list as $item) { $newPath = str_replace($path, $varDir, $item->attribute('filepath')); if ($newPath != $item->attribute('filepath')) { $cli->output("Fix attribute " . $item->attribute('contentobject_attribute_id') . " " . $item->attribute('filepath')); eZImageFile::moveFilepath($item->attribute('contentobject_attribute_id'), $item->attribute('filepath'), $newPath); } $attributes = eZPersistentObject::fetchObjectList(eZContentObjectAttribute::definition(), null, array('id' => $item->attribute('contentobject_attribute_id'))); foreach ($attributes as $attribute) { $newDataText = str_replace($path, $varDir, $attribute->attribute('data_text')); $attribute->setAttribute('data_text', $newDataText); $attribute->store(); } } $script->shutdown(); }