From bb67a1a4bcc27a4a9f6aae39ed0dc217e42d4139 Mon Sep 17 00:00:00 2001 From: Jean-Yves Gaulier Date: Tue, 13 Oct 2015 17:23:52 +0200 Subject: [PATCH] PHRAS-752 #time 4h fix : tf-* (phraseanet) admin / field / source does not says "invalid" add : better completion for field source : now can type things like "phr:tim" to match namespace(s) and tagname(s) --- .../Controller/Admin/FieldsController.php | 38 +++++++---- lib/Alchemy/Phrasea/Metadata/TagFactory.php | 63 +++++++++++-------- lib/classes/databox/field.php | 2 +- 3 files changed, 64 insertions(+), 39 deletions(-) diff --git a/lib/Alchemy/Phrasea/Controller/Admin/FieldsController.php b/lib/Alchemy/Phrasea/Controller/Admin/FieldsController.php index 8880c33108..65859970a4 100644 --- a/lib/Alchemy/Phrasea/Controller/Admin/FieldsController.php +++ b/lib/Alchemy/Phrasea/Controller/Admin/FieldsController.php @@ -119,26 +119,40 @@ class FieldsController extends Controller public function searchTag(Request $request) { - $term = trim(strtolower($request->query->get('term'))); + $term = str_replace(['/', ':', '.'], ' ', strtolower($request->query->get('term'))); $res = []; - if ($term) { + $term = explode(' ', $term, 2); + if(($term[0] = trim($term[0])) != '') { + if( ($nparts = count($term)) == 2) { + $term[1] = trim($term[1]); + } $provider = new TagProvider(); foreach ($provider->getLookupTable() as $namespace => $tags) { - $ns = strpos($namespace, $term); - + $match_ns = (strpos($namespace, $term[0]) !== false); + if($nparts == 2 && !$match_ns) { + // with "abc:xyz", "abc" MUST match the namespace + continue; + } foreach ($tags as $tagname => $datas) { - if ($ns === false && strpos($tagname, $term) === false) { - continue; + if($nparts == 1) { + // "abc" can match the namespace OR the tagname + $match = $match_ns || (strpos($tagname, $term[0]) !== false); + } + else { + // match "abc:xyz" against namespace (already true) AND tagname + $match = ($term[1] == '' || strpos($tagname, $term[1]) !== false); } - $res[] = [ - 'id' => $namespace . '/' . $tagname, - /** @Ignore */ - 'label' => $datas['namespace'] . ' / ' . $datas['tagname'], - 'value' => $datas['namespace'] . ':' . $datas['tagname'], - ]; + if($match) { + $res[] = [ + 'id' => $namespace . '/' . $tagname, + /** @Ignore */ + 'label' => $datas['namespace'] . ' / ' . $datas['tagname'], + 'value' => $datas['namespace'] . ':' . $datas['tagname'], + ]; + } } } } diff --git a/lib/Alchemy/Phrasea/Metadata/TagFactory.php b/lib/Alchemy/Phrasea/Metadata/TagFactory.php index ef98061e9e..c5e0d1b1ca 100644 --- a/lib/Alchemy/Phrasea/Metadata/TagFactory.php +++ b/lib/Alchemy/Phrasea/Metadata/TagFactory.php @@ -10,43 +10,54 @@ namespace Alchemy\Phrasea\Metadata; use PHPExiftool\Driver\TagFactory as BaseTagFactory; +use PHPExiftool\Exception\TagUnknown; class TagFactory extends BaseTagFactory { protected static $knownClasses = [ - 'pdf-text' => 'Alchemy\Phrasea\Metadata\Tag\PdfText', - 'tf-archivedate' => 'Alchemy\Phrasea\Metadata\Tag\TfArchivedate', - 'tf-atime' => 'Alchemy\Phrasea\Metadata\Tag\TfAtime', - 'tf-basename' => 'Alchemy\Phrasea\Metadata\Tag\TfBasename', - 'tf-bits' => 'Alchemy\Phrasea\Metadata\Tag\TfBits', - 'tf-channels' => 'Alchemy\Phrasea\Metadata\Tag\TfChannels', - 'tf-ctime' => 'Alchemy\Phrasea\Metadata\Tag\TfCtime', - 'tf-dirname' => 'Alchemy\Phrasea\Metadata\Tag\TfDirname', - 'tf-duration' => 'Alchemy\Phrasea\Metadata\Tag\TfDuration', - 'tf-editdate' => 'Alchemy\Phrasea\Metadata\Tag\TfEditdate', - 'tf-extension' => 'Alchemy\Phrasea\Metadata\Tag\TfExtension', - 'tf-filename' => 'Alchemy\Phrasea\Metadata\Tag\TfFilename', - 'tf-filepath' => 'Alchemy\Phrasea\Metadata\Tag\TfFilepath', - 'tf-height' => 'Alchemy\Phrasea\Metadata\Tag\TfHeight', - 'tf-mimetype' => 'Alchemy\Phrasea\Metadata\Tag\TfMimetype', - 'tf-mtime' => 'Alchemy\Phrasea\Metadata\Tag\TfMtime', - 'tf-quarantine' => 'Alchemy\Phrasea\Metadata\Tag\TfQuarantine', - 'tf-recordid' => 'Alchemy\Phrasea\Metadata\Tag\TfRecordid', - 'tf-size' => 'Alchemy\Phrasea\Metadata\Tag\TfSize', - 'tf-width' => 'Alchemy\Phrasea\Metadata\Tag\TfWidth', + 'pdftext' => 'Alchemy\Phrasea\Metadata\Tag\PdfText', + 'tfarchivedate' => 'Alchemy\Phrasea\Metadata\Tag\TfArchivedate', + 'tfatime' => 'Alchemy\Phrasea\Metadata\Tag\TfAtime', + 'tfbasename' => 'Alchemy\Phrasea\Metadata\Tag\TfBasename', + 'tfbits' => 'Alchemy\Phrasea\Metadata\Tag\TfBits', + 'tfchannels' => 'Alchemy\Phrasea\Metadata\Tag\TfChannels', + 'tfctime' => 'Alchemy\Phrasea\Metadata\Tag\TfCtime', + 'tfdirname' => 'Alchemy\Phrasea\Metadata\Tag\TfDirname', + 'tfduration' => 'Alchemy\Phrasea\Metadata\Tag\TfDuration', + 'tfeditdate' => 'Alchemy\Phrasea\Metadata\Tag\TfEditdate', + 'tfextension' => 'Alchemy\Phrasea\Metadata\Tag\TfExtension', + 'tffilename' => 'Alchemy\Phrasea\Metadata\Tag\TfFilename', + 'tffilepath' => 'Alchemy\Phrasea\Metadata\Tag\TfFilepath', + 'tfheight' => 'Alchemy\Phrasea\Metadata\Tag\TfHeight', + 'tfmimetype' => 'Alchemy\Phrasea\Metadata\Tag\TfMimetype', + 'tfmtime' => 'Alchemy\Phrasea\Metadata\Tag\TfMtime', + 'tfquarantine' => 'Alchemy\Phrasea\Metadata\Tag\TfQuarantine', + 'tfrecordid' => 'Alchemy\Phrasea\Metadata\Tag\TfRecordid', + 'tfsize' => 'Alchemy\Phrasea\Metadata\Tag\TfSize', + 'tfwidth' => 'Alchemy\Phrasea\Metadata\Tag\TfWidth', ]; + public static function getFromTagname($tagname) + { + $classname = static::classnameFromTagname($tagname); + + if ( ! class_exists($classname)) { + throw new TagUnknown(sprintf('Unknown tag %s', $tagname)); + } + + return new $classname; + } + protected static function classnameFromTagname($tagname) { $tagname = str_replace('rdf:RDF/rdf:Description/', '', $tagname); - if ('Phraseanet:' === substr($tagname, 0, 11)) { - $parts = explode(':', $tagname, 2); - if (isset(self::$knownClasses[$parts[1]])) { - return self::$knownClasses[$parts[1]]; - } + $parts = explode(':', strtolower($tagname), 2); + if (count($parts) == 2 && $parts[0] == 'phraseanet' && isset(self::$knownClasses[$parts[1]])) { + // a specific phraseanet fieldname + return self::$knownClasses[$parts[1]]; } - + // another (exiftool) fieldname ? return parent::classnameFromTagname($tagname); } } diff --git a/lib/classes/databox/field.php b/lib/classes/databox/field.php index 1b75e757c9..470b9255ca 100644 --- a/lib/classes/databox/field.php +++ b/lib/classes/databox/field.php @@ -468,7 +468,7 @@ class databox_field implements cache_cacheableInterface } try { - return TagFactory::getFromRDFTagname($tagName); + return TagFactory::getFromTagname($tagName); } catch (TagUnknown $exception) { if ($throwException) { throw new NotFoundHttpException(sprintf("Tag %s not found", $tagName), $exception);