<commit_before><?php
namespace search\controller;

class FileController
{

    /**
     * Download a file
     * @param doi of dataset, filename,data of dataset
     * @return true if ok else false
     */
    public function download($doi, $filename, $response)
    {
        $config        = self::ConfigFile();
        $UPLOAD_FOLDER = $config["UPLOAD_FOLDER"];
        $DOI_PREFIX    = $config["DOI_PREFIX"];
        $doi           = str_replace($config["UPLOAD_FOLDER"], "", $doi);
        if (isset($response['_source']['DATA'])) {
            if (strstr($doi, $config['REPOSITORY_NAME']) !== false) {
                $file = $UPLOAD_FOLDER . $DOI_PREFIX . "/" . $doi . "/" . $filename;
            } else {
                $file = $UPLOAD_FOLDER . $doi . "/" . $filename;
            }
            if (file_exists($file)) {
                header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
                header("Content-Disposition: attachment; filename=" . $filename);
                $readfile = file_get_contents($file);
                print $readfile;
            }
            if ($readfile == false) {
                return false;
            } else {
                return true;
            }
            exit;
        }

    }

    /**
     * Export to datacite xml format
     * @param data of dataset
     * @return true if ok else false
     */
    public function export_to_datacite_xml($response)
    {
        if (isset($response['_source']['INTRO'])) {
            $sxe = new \SimpleXMLElement("<resource/>");
            $sxe->addAttribute('xmlns:xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
            $sxe->addAttribute('xmlns', 'http://datacite.org/schema/kernel-4');
            $sxe->addAttribute('xsi:xsi:schemaLocation', 'http://datacite.org/schema/kernel-4 http://schema.datacite.org/meta/kernel-4/metadata.xsd');

            $identifier = $sxe->addChild('identifier', $response['_id']);
            $identifier->addAttribute('identifierType', 'DOI');
            $creators = $sxe->addChild('creators');
            foreach ($response['_source']['INTRO']['FILE_CREATOR'] as $key => $value) {
                $creator = $creators->addChild('creator');
                $creator->addChild('creatorName', $value['DISPLAY_NAME']);
            }
            $titles = $sxe->addChild('titles');

            $title           = $titles->addChild('title', $response['_source']['INTRO']['TITLE']);
            $publisher       = $sxe->addChild('publisher', $response['_source']['INTRO']['PUBLISHER']);
            $publicationYear = $sxe->addChild('publicationYear', $response['_source']['INTRO']['PUBLICATION_DATE']);
            $subjects        = $sxe->addChild('subjects');
            foreach ($response['_source']['INTRO']['SCIENTIFIC_FIELD'] as $key => $value) {
                $subjects->addChild('subject', $value['NAME']);
            }

            $RessourceType = $sxe->addChild('resourceType', 'Dataset');
            $sxe->addChild('language', $response['_source']['INTRO']['LANGUAGE']);
            $RessourceType->addAttribute('resourceTypeGeneral', 'Dataset');
            $Version      = $sxe->addChild('version', '1');
            $descriptions = $sxe->addChild('descriptions');
            $description  = $descriptions->addChild('description', $response['_source']['INTRO']['DATA_DESCRIPTION']);
            $description->addAttribute('descriptionType', 'Abstract');

            return $sxe;
        } else {
            return false;
        }

    }

    /**
     * Export to dublincore xml format
     * @param data of dataset
     * @return true if ok else false
     */
    public function export_to_dublincore_xml($response)
    {
        if (isset($response['_source']['INTRO'])) {
            @$sxe = new \SimpleXMLElement("<oai_dc:dc/>");
            $sxe->addAttribute('xmlns:xmlns:dc', 'http://purl.org/dc/elements/1.1/');
            $sxe->addAttribute('xmlns:xmlns:oai_dc', 'http://www.openarchives.org/OAI/2.0/oai_dc/');
            $sxe->addAttribute('xmlns:xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
            $sxe->addAttribute('xsi:xsi:schemaLocation', 'http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd');

            $identifier = $sxe->addChild('dc:dc:identifier', $response['_id']);
            foreach ($response['_source']['INTRO']['FILE_CREATOR'] as $key => $value) {
                $sxe->addChild('dc:dc:creator', $value['DISPLAY_NAME']);
            }

            $sxe->addChild('dc:dc:title', $response['_source']['INTRO']['TITLE']);
            $publisher       = $sxe->addChild('dc:dc:publisher', $response['_source']['INTRO']['PUBLISHER']);
            $publicationYear = $sxe->addChild('dc:dc:publicationYear', $response['_source']['INTRO']['PUBLICATION_DATE']);
            foreach ($response['_source']['INTRO']['SCIENTIFIC_FIELD'] as $key => $value) {
                $sxe->addChild('dc:dc:subject', $value['NAME']);
            }

            $RessourceType = $sxe->addChild('dc:dc:resourceType', 'Dataset');
            $sxe->addChild('language', $response['_source']['INTRO']['LANGUAGE']);
            $RessourceType->addAttribute('dc:dc:resourceTypeGeneral', 'Dataset');
            $sxe->addChild('dc:dc:description', $response['_source']['INTRO']['DATA_DESCRIPTION']);

            return $sxe;
        } else {
            return false;
        }

    }

    /**
     * Export to bibtex format
     * @param data of dataset
     * @return true if ok else false
     */
    public function export_to_Bibtex($response)
    {
        if (isset($response['_source']['INTRO'])) {
            $authors = null;
            foreach ($response['_source']['INTRO']['FILE_CREATOR'] as $key => $value) {
                $authors .= $value['DISPLAY_NAME'] . ",";
            }
            $title       = $response['_source']['INTRO']['TITLE'];
            $description = $response['_source']['INTRO']['DATA_DESCRIPTION'];
            $year        = $response['_source']['INTRO']['PUBLICATION_DATE'];
            $publisher   = $response['_source']['INTRO']['PUBLISHER'];
            $doi         = $response['_id'];
            $bibtex      = "
             @data{
              author       = {" . $authors . "},<commit_msg>Ajout ORCID format export<commit_after><?php
namespace search\controller;

class FileController
{

    /**
     * Download a file
     * @param doi of dataset, filename,data of dataset
     * @return true if ok else false
     */
    public function download($doi, $filename, $response)
    {
        $config        = self::ConfigFile();
        $UPLOAD_FOLDER = $config["UPLOAD_FOLDER"];
        $DOI_PREFIX    = $config["DOI_PREFIX"];
        $doi           = str_replace($config["UPLOAD_FOLDER"], "", $doi);
        if (isset($response['_source']['DATA'])) {
            if (strstr($doi, $config['REPOSITORY_NAME']) !== false) {
                $file = $UPLOAD_FOLDER . $DOI_PREFIX . "/" . $doi . "/" . $filename;
            } else {
                $file = $UPLOAD_FOLDER . $doi . "/" . $filename;
            }
            if (file_exists($file)) {
                header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
                header("Content-Disposition: attachment; filename=" . $filename);
                $readfile = file_get_contents($file);
                print $readfile;
            }
            if ($readfile == false) {
                return false;
            } else {
                return true;
            }
            exit;
        }

    }

    /**
     * Export to datacite xml format
     * @param data of dataset
     * @return true if ok else false
     */
    public function export_to_datacite_xml($response)
    {
        if (isset($response['_source']['INTRO'])) {
            $sxe = new \SimpleXMLElement("<resource/>");
            $sxe->addAttribute('xmlns:xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
            $sxe->addAttribute('xmlns', 'http://datacite.org/schema/kernel-4');
            $sxe->addAttribute('xsi:xsi:schemaLocation', 'http://datacite.org/schema/kernel-4 http://schema.datacite.org/meta/kernel-4/metadata.xsd');

            $identifier = $sxe->addChild('identifier', $response['_id']);
            $identifier->addAttribute('identifierType', 'DOI');
            $creators = $sxe->addChild('creators');
            foreach ($response['_source']['INTRO']['FILE_CREATOR'] as $key => $value) {
                $creator = $creators->addChild('creator');
                $creator->addChild('creatorName', $value['DISPLAY_NAME']);
                if ($value['ORCID']) {
                $creator->addChild('nameIdentifier', $value['ORCID']);
                }
            }
            $titles = $sxe->addChild('titles');

            $title           = $titles->addChild('title', $response['_source']['INTRO']['TITLE']);
            $publisher       = $sxe->addChild('publisher', $response['_source']['INTRO']['PUBLISHER']);
            $publicationYear = $sxe->addChild('publicationYear', $response['_source']['INTRO']['PUBLICATION_DATE']);
            $subjects        = $sxe->addChild('subjects');
            foreach ($response['_source']['INTRO']['SCIENTIFIC_FIELD'] as $key => $value) {
                $subjects->addChild('subject', $value['NAME']);
            }

            $RessourceType = $sxe->addChild('resourceType', 'Dataset');
            $sxe->addChild('language', $response['_source']['INTRO']['LANGUAGE']);
            $RessourceType->addAttribute('resourceTypeGeneral', 'Dataset');
            $Version      = $sxe->addChild('version', '1');
            $descriptions = $sxe->addChild('descriptions');
            $description  = $descriptions->addChild('description', $response['_source']['INTRO']['DATA_DESCRIPTION']);
            $description->addAttribute('descriptionType', 'Abstract');

            return $sxe;
        } else {
            return false;
        }

    }

    /**
     * Export to dublincore xml format
     * @param data of dataset
     * @return true if ok else false
     */
    public function export_to_dublincore_xml($response)
    {
        if (isset($response['_source']['INTRO'])) {
            @$sxe = new \SimpleXMLElement("<oai_dc:dc/>");
            $sxe->addAttribute('xmlns:xmlns:dc', 'http://purl.org/dc/elements/1.1/');
            $sxe->addAttribute('xmlns:xmlns:oai_dc', 'http://www.openarchives.org/OAI/2.0/oai_dc/');
            $sxe->addAttribute('xmlns:xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
            $sxe->addAttribute('xsi:xsi:schemaLocation', 'http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd');

            $identifier = $sxe->addChild('dc:dc:identifier', $response['_id']);
            foreach ($response['_source']['INTRO']['FILE_CREATOR'] as $key => $value) {
                $sxe->addChild('dc:dc:creator', $value['DISPLAY_NAME']);
                if ($value['ORCID']) {
                $sxe->addChild('dc:dc:nameIdentifier', $value['ORCID']);
                }
            }

            $sxe->addChild('dc:dc:title', $response['_source']['INTRO']['TITLE']);
            $publisher       = $sxe->addChild('dc:dc:publisher', $response['_source']['INTRO']['PUBLISHER']);
            $publicationYear = $sxe->addChild('dc:dc:publicationYear', $response['_source']['INTRO']['PUBLICATION_DATE']);
            foreach ($response['_source']['INTRO']['SCIENTIFIC_FIELD'] as $key => $value) {
                $sxe->addChild('dc:dc:subject', $value['NAME']);
            }

            $RessourceType = $sxe->addChild('dc:dc:resourceType', 'Dataset');
            $sxe->addChild('language', $response['_source']['INTRO']['LANGUAGE']);
            $RessourceType->addAttribute('dc:dc:resourceTypeGeneral', 'Dataset');
            $sxe->addChild('dc:dc:description', $response['_source']['INTRO']['DATA_DESCRIPTION']);

            return $sxe;
        } else {
            return false;
        }

    }

    /**
     * Export to bibtex format
     * @param data of dataset
     * @return true if ok else false
     */
    public function export_to_Bibtex($response)
    {
        if (isset($response['_source']['INTRO'])) {
            $authors = null;
            $orcid= null;
            foreach ($response['_source']['INTRO']['FILE_CREATOR'] as $key => $value) {
                $authors .= $value['DISPLAY_NAME'] . ",";
                if ($value['ORCID']) {
                $orcid  .= $value['ORCID'] . ",";
                }
            }
            $title       = $response['_source']['INTRO']['TITLE'];
            $description = $response['_source']['INTRO']['DATA_DESCRIPTION'];
            $year        = $response['_source']['INTRO']['PUBLICATION_DATE'];
            $publisher   = $response['_source']['INTRO']['PUBLISHER'];
            $doi         = $response['_id'];
            $bibtex      = "
             @data{
              author       = {" . $authors . "},
              nameidentifier = {" . $orcid . "},
{'fraction_non_alphanumeric': 0.1825287356321839, 'fraction_numerical': 0.0036781609195402297, 'mean_word_length': 2.2915510718789407, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 10, 'xml': 26}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2760539', 'n_tokens_mistral': 3973, 'n_tokens_neox': 3851, 'n_words': 569}