Exemple #1
0
 function maj_entrepot($source_id, $callback_progress = "", $recover = false, $recover_env = "")
 {
     global $charset;
     $this->callback_progress = $callback_progress;
     $params = $this->unserialize_source_params($source_id);
     $p = $params["PARAMETERS"];
     $this->metadata_prefix = $p["formats"];
     $this->source_id = $source_id;
     $this->n_recu = 0;
     $this->xslt_transform = $p["xsl_transform"]["code"];
     //Connexion
     $oai20 = new oai20($p["url"], $charset, $p["TIMEOUT"], $p["clean_base_url"]);
     if (!$oai20->error) {
         if ($recover) {
             $envt = unserialize($recover_env);
             $sets = $envt["sets"];
             $date_start = $envt["date_start"];
             $this->del_old = false;
         } else {
             //Recherche de la dernière date...
             $requete = "select unix_timestamp(max(date_import)) from entrepot_source_" . $this->source_id . " where connector_id='" . addslashes($this->get_id()) . ";";
             $resultat = pmb_mysql_query($requete);
             if (pmb_mysql_num_rows($resultat)) {
                 $last_date = pmb_mysql_result($resultat, 0, 0);
                 if ($last_date) {
                     //En fonction de la granularité, on ajoute une seconde ou un jour !
                     if ($oai20->granularity == "YYYY-MM-DD") {
                         $last_date += 3600 * 24;
                     } else {
                         $last_date += 1;
                     }
                 } else {
                     $earliest_date = new iso8601($oai20->granularity);
                     $last_date = $earliest_date->iso8601_to_unixtime($oai20->earliestDatestamp);
                 }
             } else {
                 $earliest_date = new iso8601($oai20->granularity);
                 $last_date = $earliest_date->iso8601_to_unixtime($oai20->earliestDatestamp);
             }
             //Affectation de la date de départ
             $date_start = $last_date;
             //Recherche des sets sélectionnés
             $this->sets_names = $oai20->sets;
             for ($i = 0; $i < count($p["sets"]); $i++) {
                 if ($oai20->sets[$p["sets"][$i]]) {
                     $sets[] = $p["sets"][$i];
                 }
             }
             $this->del_old = true;
         }
         //Mise à jour de source_sync pour reprise en cas d'erreur
         $envt["sets"] = $sets;
         $envt["date_start"] = $date_start;
         $requete = "update source_sync set env='" . addslashes(serialize($envt)) . "' where source_id=" . $source_id;
         pmb_mysql_query($requete);
         //Lancement de la requête
         $this->current_set = 0;
         $this->total_sets = count($sets);
         if (count($sets)) {
             for ($i = 0; $i < count($sets); $i++) {
                 $this->current_set = $i;
                 $oai20->list_records($date_start, "", $sets[$i], $p["formats"], array(&$this, "rec_record"), array(&$this, "progress"));
                 if ($oai20->error && $oai20->error_oai_code != "noRecordsMatch") {
                     $this->error = true;
                     $this->error_message .= $oai20->error_message . "<br />";
                 }
             }
         } else {
             $this->current_set = 0;
             $this->total_sets = 1;
             $oai20->list_records($date_start, "", "", $p["formats"], array(&$this, "rec_record"), array(&$this, "progress"));
             if ($oai20->error && $oai20->error_oai_code != "noRecordsMatch") {
                 $this->error = true;
                 $this->error_message .= $oai20->error_message . "<br />";
             }
         }
     } else {
         $this->error = true;
         $this->error_message = $oai20->error_message;
     }
     return $this->n_recu;
 }
 /**
  * Récupère les notices marquées comme supprimées de tout les sets
  * @param array $notice_sets Tableau des identifiants de sets
  * @param array $records_not_deleted Tableau des identifiants de notices présentes dans au moins un set
  * @param iso8601 $iso8601
  *
  * @return array Renvoie un tableau array({id_notice} => array('datestamp', 'sets'))
  */
 private function get_deleted_records($record_sets, $records_not_deleted, $iso8601)
 {
     global $dbh;
     $deleted_records = array();
     $query = "select num_notice, num_set, unix_timestamp(deletion_date) as datestamp from connectors_out_oai_deleted_records where num_set in (" . implode(",", $record_sets) . ") and num_notice not in (" . implode(",", $records_not_deleted) . ")";
     if ($this->deletion_management == 1) {
         $query .= " and timestampdiff(second, deletion_date, now()) < " . $this->deletion_management_transient_duration;
     }
     $result = pmb_mysql_query($query, $dbh);
     if ($result && pmb_mysql_num_rows($result)) {
         while ($row = mysql_fetch_object($result)) {
             $deleted_records[$row->num_notice]['sets'][] = $row->num_set;
             $timestamp = $iso8601->unixtime_to_iso8601($row->datestamp);
             if (!isset($deleted_records[$row->num_notice]['datestamp']) || $timestamp > $deleted_records[$row->num_notice]['datestamp']) {
                 $deleted_records[$row->num_notice]['datestamp'] = $timestamp;
             }
         }
     }
     foreach ($deleted_records as $record_id => $record) {
         // Si la notice n'est pas supprimée de tous les sets
         if (count($not_deleted_sets = connector_out_set_noticecaddie::get_notice_setlist($record_id))) {
             // Si la notice n'est pas supprimée de tous les sets de la source, on l'enlève des notices supprimées
             if (count(array_intersect($not_deleted_sets, $record_sets))) {
                 unset($deleted_records[$record_id]);
             }
         }
     }
     return $deleted_records;
 }
 function list_identifiers($from, $until, $set, $metadata_prefix, $callback = "", $callback_progress = "")
 {
     $this->clear_error();
     $records = array();
     //Conversion des from et until en fonction de la granularité
     $iso8601 = new iso8601($this->granularity);
     if ($from) {
         $from = $iso8601->unixtime_to_iso8601($from);
     }
     if ($until) {
         $until = $iso8601->unixtime_to_iso8601($until);
     }
     //Vérification du metadata
     if ($this->check_metadata($metadata_prefix)) {
         $url = $this->url_base . "?verb=ListIdentifiers&metadataPrefix=" . rawurlencode($metadata_prefix);
         if ($from) {
             $url .= "&from=" . $from;
         }
         if ($until) {
             $url .= "&until=" . $until;
         }
         if ($set) {
             $url .= "&set=" . rawurlencode($set);
         }
         $this->send_request($url, $callback, $callback_progress);
         if (!$this->error) {
             if (!$callback) {
                 for ($i = 0; $i < count($this->prt->records); $i++) {
                     $records[] = $this->prt->records[$i];
                 }
             }
         }
     } else {
         $this->error = true;
         $this->error_message = "Unknow metadata prefix : " . $metadata_prefix;
     }
     if (!$callback) {
         return $records;
     }
 }
    function convert_batch_to_oairecords($notices_to_convert, $format, $target_charset)
    {
        global $dbh;
        if (!$notices_to_convert) {
            //Rien à faire? On fait rien
            return;
        }
        //Allons chercher les dates et les statuts des notices
        $notice_datestamps = array();
        $notice_statuts = array();
        $notice_ids = $notices_to_convert;
        //Par paquets de 100 pour ne pas brusquer mysql
        $notice_idsz = array_chunk($notice_ids, 100);
        $iso8601 = new iso8601("YYYY-MM-DDThh:mm:ssZ");
        foreach ($notice_idsz as $anotice_ids) {
            $sql = "SELECT notice_id, UNIX_TIMESTAMP(update_date) AS datestamp, statut FROM notices WHERE notice_id IN (" . implode(",", $anotice_ids) . ")";
            $res = mysql_query($sql, $dbh);
            while ($row = mysql_fetch_assoc($res)) {
                $notice_datestamps[$row["notice_id"]] = $iso8601->unixtime_to_iso8601($row["datestamp"]);
                $notice_statuts[$row["notice_id"]] = $row["statut"];
            }
        }
        //Si il existe un status correspondant à la suppression, on génère ces enregistrements et on les supprime de la liste à générer.
        if ($this->deleted_record_statut) {
            foreach ($notice_statuts as $notice_id => $anotice_statut) {
                if ($anotice_statut == $this->deleted_record_statut) {
                    $notice_sets = connector_out_set_noticecaddie::get_notice_setlist($notice_id);
                    $notice_sets = array_intersect($notice_sets, $this->source_set_ids);
                    $oai_record = "";
                    $oai_record .= "<record>";
                    $oai_record .= '<header status="deleted">
									<identifier>oai:' . XMLEntities($this->repository_identifier) . ':' . $notice_id . '</identifier>
									<datestamp>' . $notice_datestamps[$notice_id] . '</datestamp>';
                    foreach ($notice_sets as $aset_id) {
                        $oai_record .= "<setSpec>set_" . $aset_id . "</setSpec>";
                    }
                    $oai_record .= '</header>';
                    $oai_record .= "</record>";
                    $this->results[$notice_id] = $oai_record;
                    unset($notices_to_convert[array_search($notice_id, $notices_to_convert)]);
                }
            }
        }
        //Convertissons les notices au format demandé si on ne souhaite pas uniquement les entêtes
        $only_identifier = $format == "__oai_identifier";
        if (!$only_identifier) {
            $converter = new external_services_converter_notices(1, $this->set_life_duration);
            $converter->params["include_items"] = $this->include_items;
            $converter->params["include_links"] = $this->include_links;
            $metadatas = $converter->convert_batch($notices_to_convert, $format, $target_charset, $this->xslt);
        }
        //Fabriquons les enregistrements
        foreach ($notices_to_convert as $notice_id) {
            $notice_sets = connector_out_set_noticecaddie::get_notice_setlist($notice_id);
            $notice_sets = array_intersect($notice_sets, $this->source_set_ids);
            $oai_record = "";
            if (!$only_identifier) {
                $oai_record .= "<record>";
            }
            $oai_record .= '<header>
							<identifier>oai:' . XMLEntities($this->repository_identifier) . ':' . $notice_id . '</identifier>
							<datestamp>' . $notice_datestamps[$notice_id] . '</datestamp>';
            foreach ($notice_sets as $aset_id) {
                $oai_record .= "<setSpec>set_" . $aset_id . "</setSpec>";
            }
            $oai_record .= '</header>';
            if (!$only_identifier) {
                $oai_record .= "<metadata>";
                $oai_record .= $metadatas[$notice_id];
                $oai_record .= "</metadata>";
            }
            if (!$only_identifier) {
                $oai_record .= "</record>";
            }
            $this->results[$notice_id] = $oai_record;
        }
    }