diff --git a/search/Result.php b/search/Result.php new file mode 100644 index 0000000000000000000000000000000000000000..d2a7cefa63991187a14a4f3552e1addacf437f96 --- /dev/null +++ b/search/Result.php @@ -0,0 +1,147 @@ +<?php + +namespace uga\idoine\search; + +use stdClass; + +require_once dirname(__FILE__, 2).DIRECTORY_SEPARATOR.'vendor/autoload.php'; + +/** + * + * Resultat d'une recherche sur une des divers API de recherche. + * + * @author Gaël PICOT + * + * iDOIne : + * Copyright (C) 2022 UGA + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + * + */ + +/** + * Décrit un élément trouver + */ +class Result { + /** + * URL vers le résultat + * + * @var string + */ + private string $URL; + /** + * Le DOI trouvé + * + * @var string + */ + private string $DOI; + /** + * Liste des noms des auteurs + * + * @var array + */ + private array $authors; + /** + * Titre principal trouver + * + * @var string + */ + private string $title; + /** + * Journal de publication + * + * @var string + */ + private string $journal; + /** + * Date de publication + * + * @var string + */ + private string $publicationDate; + /** + * Avertissement + * + * @var string + */ + private string $warning; + /** + * Ressemblance avec le titre de la publication Hal en pourcentage. + * + * @var float + */ + private float $scorePercent = 0; + + /** + * Constructeur + * + * @param stdClass $data + */ + public function __construct(stdClass $data) { + $this->URL = (isset($data->URL))?$data->URL:''; + $this->DOI = (isset($data->DOI))?$data->DOI:''; + $this->authors = (isset($data->authors))?$data->authors:[]; + $this->title = (isset($data->title))?$data->title:''; + $this->publicationDate = (isset($data->publicationDate))?$data->publicationDate:''; + $this->journal = (isset($data->journal))?$data->journal:''; + $this->warning = (isset($data->warning))?$data->warning:''; + } + + /** + * Accesseur pour le titre + * + * @return string + */ + public function getTitle():string { + return $this->title; + } + + /** + * Revoie le résultat sous forme d'une string JSON. + * + * @return string + */ + public function toJSON(): string{ + return json_encode($this->toArray()); + } + + /** + * Construi un tableau représentant le resultat. + * + * @return array + */ + public function toArray(): array{ + $data = [ + 'URL' => $this->URL, + 'DOI' => $this->DOI, + 'authors' => $this->authors, + 'title' => $this->title, + 'journal' => $this->journal, + 'publicationDate' => $this->publicationDate, + 'scorePercent' => $this->scorePercent, + 'warning' => $this->warning + ]; + return $data; + } + + /** + * Modiffieur de $scorePercent + * + * @param float $scorePercent + * @return void + */ + public function setScorePercent(float $scorePercent) { + $this->scorePercent = $scorePercent; + } +} diff --git a/search/Results.php b/search/Results.php new file mode 100644 index 0000000000000000000000000000000000000000..1e9b470eb5ce814b5e1a7f97168093d19934036f --- /dev/null +++ b/search/Results.php @@ -0,0 +1,152 @@ +<?php + +namespace uga\idoine\search; + +use stdClass; + +require_once dirname(__FILE__, 2).DIRECTORY_SEPARATOR.'vendor/autoload.php'; + +/** + * + * Ensemble de Resultats d'une recherche sur une des divers API de recherche. + * + * @author Gaël PICOT + * + * iDOIne : + * Copyright (C) 2022 UGA + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + * + */ + +/** + * Décrit les résultats d'une recherche + */ +class Results { + /** + * Document Hal objet de la recherche + * + * @var string + */ + private string $halid; + /** + * Donnée extraite de Hal au sujet du document chercher + * + * @var stdClass|null + */ + private ?stdClass $halData = null; + /** + * Liste des résultats trouver + * + * @var array + */ + private array $resultList = []; + /** + * Meilleur score + * + * @var float + */ + private float $bestScore = 0; + /** + * Meilleur résultat trouver + * + * @var Result|null + */ + private ?Result $bestResult = null; + + /** + * Meilleur score résultat en poursentage + * + * @var integer + */ + private int $bestScorePersent = 0; + + /** + * Charge les donnée à partir de l'API Hal + * + * @param string $fl liste des champs cherché dans l'API HAl. + * @return stdClass + */ + public function getHalData(string $fl='title_s,journalTitle_s'): stdClass { + if($this->halData == null) { + $halQuery="https://api.archives-ouvertes.fr/search/?q=halId_s%3A".$this->halid."&fl=".$fl; + $this->halData = json_decode(file_get_contents($halQuery))->response->docs[0]; + } + return $this->halData; + } + + /** + * Accés au titres Hal + * + * @return array + */ + public function getHalTitles():array { + return $this->getHalData()->title_s; + } + + /** + * Constructeur + * + * @param string $halid + */ + public function __construct(string $halid) + { + $this->halid = $halid; + } + + /** + * Ajouté un résultat + * + * @param Result $newResult + * @return void + */ + public function addResult(Result $newResult) { + array_push($this->resultList, $newResult); + $percent = 0; + $score = similar_text(strtolower($this->getHalTitles()[0]), strtolower($newResult->getTitle()), $percent); + $newResult->setScorePercent($percent); + if($score > $this->bestScore||$this->bestScore==0) { + $this->bestResult = $newResult; + $this->bestScore = $score; + } + } + + /** + * Construit une représentaion sous forme de tableau. + * + * @return array + */ + public function toArray(): array{ + $data = [ + 'bestScorePercent' => (isset($this->bestScorePersent))?$this->bestScorePersent:0, + 'resultList' => [], + ]; + if($this->bestResult != null) { + $data['bestResult'] = $this->bestResult->toArray(); + } + foreach($this->resultList as $result){ + array_push($data['resultList'], $result->toArray()); + } + return $data; + } + + /** + * Construit une représentaion au format JSON + * + * @return string + */ + public function toJSON(): string { + return json_encode($this->toArray()); + } +} \ No newline at end of file diff --git a/search/Search.php b/search/Search.php index d340ec17f48e1e2096bc68d820493232bf94fdad..49c2173db184e545559f27feaf98407c90f4d6ab 100644 --- a/search/Search.php +++ b/search/Search.php @@ -2,9 +2,16 @@ namespace uga\idoine\search; +use uga\hallib\OneDocQuery; +use uga\hallib\queryDefinition\LiteralElement; +use uga\hallib\search\SearchField; +use uga\hallib\search\SearchQuery; + +require_once dirname(__FILE__, 2).DIRECTORY_SEPARATOR.'vendor/autoload.php'; + /** * - * Commun au divers API de recherche + * Recherche d'un document Hal dans d'autre service * * @author Gaël PICOT * @@ -26,14 +33,6 @@ namespace uga\idoine\search; * */ -use stdClass; -use uga\hallib\OneDocQuery; -use uga\hallib\queryDefinition\LiteralElement; -use uga\hallib\search\SearchField; -use uga\hallib\search\SearchQuery; - -require_once dirname(__FILE__, 2).DIRECTORY_SEPARATOR.'vendor/autoload.php'; - /** * Recherche d'un document Hal dans d'autre service (CrossRef...) */ @@ -201,240 +200,3 @@ class Search { } } - -/** - * Décrit un élément trouver - */ -class Result { - /** - * URL vers le résultat - * - * @var string - */ - private string $URL; - /** - * Le DOI trouvé - * - * @var string - */ - private string $DOI; - /** - * Liste des noms des auteurs - * - * @var array - */ - private array $authors; - /** - * Titre principal trouver - * - * @var string - */ - private string $title; - /** - * Journal de publication - * - * @var string - */ - private string $journal; - /** - * Date de publication - * - * @var string - */ - private string $publicationDate; - /** - * Avertissement - * - * @var string - */ - private string $warning; - /** - * Ressemblance avec le titre de la publication Hal en pourcentage. - * - * @var float - */ - private float $scorePercent = 0; - - /** - * Constructeur - * - * @param stdClass $data - */ - public function __construct(stdClass $data) { - $this->URL = (isset($data->URL))?$data->URL:''; - $this->DOI = (isset($data->DOI))?$data->DOI:''; - $this->authors = (isset($data->authors))?$data->authors:[]; - $this->title = (isset($data->title))?$data->title:''; - $this->publicationDate = (isset($data->publicationDate))?$data->publicationDate:''; - $this->journal = (isset($data->journal))?$data->journal:''; - $this->warning = (isset($data->warning))?$data->warning:''; - } - - /** - * Accesseur pour le titre - * - * @return string - */ - public function getTitle():string { - return $this->title; - } - - /** - * Revoie le résultat sous forme d'une string JSON. - * - * @return string - */ - public function toJSON(): string{ - return json_encode($this->toArray()); - } - - /** - * Construi un tableau représentant le resultat. - * - * @return array - */ - public function toArray(): array{ - $data = [ - 'URL' => $this->URL, - 'DOI' => $this->DOI, - 'authors' => $this->authors, - 'title' => $this->title, - 'journal' => $this->journal, - 'publicationDate' => $this->publicationDate, - 'scorePercent' => $this->scorePercent, - 'warning' => $this->warning - ]; - return $data; - } - - /** - * Modiffieur de $scorePercent - * - * @param float $scorePercent - * @return void - */ - public function setScorePercent(float $scorePercent) { - $this->scorePercent = $scorePercent; - } -} - -/** - * Décrit les résultats d'une recherche - */ -class Results { - /** - * Document Hal objet de la recherche - * - * @var string - */ - private string $halid; - /** - * Donnée extraite de Hal au sujet du document chercher - * - * @var stdClass|null - */ - private ?stdClass $halData = null; - /** - * Liste des résultats trouver - * - * @var array - */ - private array $resultList = []; - /** - * Meilleur score - * - * @var float - */ - private float $bestScore = 0; - /** - * Meilleur résultat trouver - * - * @var Result|null - */ - private ?Result $bestResult = null; - - /** - * Meilleur score résultat en poursentage - * - * @var integer - */ - private int $bestScorePersent = 0; - - /** - * Charge les donnée à partir de l'API Hal - * - * @param string $fl liste des champs cherché dans l'API HAl. - * @return stdClass - */ - public function getHalData(string $fl='title_s,journalTitle_s'): stdClass { - if($this->halData == null) { - $halQuery="https://api.archives-ouvertes.fr/search/?q=halId_s%3A".$this->halid."&fl=".$fl; - $this->halData = json_decode(file_get_contents($halQuery))->response->docs[0]; - } - return $this->halData; - } - - /** - * Accés au titres Hal - * - * @return array - */ - public function getHalTitles():array { - return $this->getHalData()->title_s; - } - - /** - * Constructeur - * - * @param string $halid - */ - public function __construct(string $halid) - { - $this->halid = $halid; - } - - /** - * Ajouté un résultat - * - * @param Result $newResult - * @return void - */ - public function addResult(Result $newResult) { - array_push($this->resultList, $newResult); - $percent = 0; - $score = similar_text(strtolower($this->getHalTitles()[0]), strtolower($newResult->getTitle()), $percent); - $newResult->setScorePercent($percent); - if($score > $this->bestScore||$this->bestScore==0) { - $this->bestResult = $newResult; - $this->bestScore = $score; - } - } - - /** - * Construit une représentaion sous forme de tableau. - * - * @return array - */ - public function toArray(): array{ - $data = [ - 'bestScorePercent' => (isset($this->bestScorePersent))?$this->bestScorePersent:0, - 'resultList' => [], - ]; - if($this->bestResult != null) { - $data['bestResult'] = $this->bestResult->toArray(); - } - foreach($this->resultList as $result){ - array_push($data['resultList'], $result->toArray()); - } - return $data; - } - - /** - * Construit une représentaion au format JSON - * - * @return string - */ - public function toJSON(): string { - return json_encode($this->toArray()); - } -}