Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
| scrutaridata:exportapi:php [2016/07/07 13:10] – vincent | scrutaridata:exportapi:php [2016/09/21 18:19] (Version actuelle) – vincent | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| ====== Implémentation de l'API en PHP ====== | ====== Implémentation de l'API en PHP ====== | ||
| - | L' | + | L' |
| - | Le répertoire // | + | Toutes les classes et constantes de ce fichier commence par le suffixe **SDE_** afin de minimiser le risque de conflits de noms avec d' |
| - | svn co http://depot.exemole.fr/ | + | Ces scripts sont accessibles avec Git via https://framagit.org/Scrutari/export-php |
| - | ===== Utilisation de l' | + | Un autre accès avec Subversion : |
| - | Pour utiliser cette implémentation, | + | svn co http://depot.exemole.fr/svn/web/ |
| - | Tous les fichiers reprennent les noms de classe de l'API à l' | ||
| - | L' | + | ===== Exemple du site www.comedie.org ===== |
| - | ===== Exemple | + | L' |
| + | |||
| + | Outre le fichier scrutaridataexport.php, | ||
| + | |||
| + | === Fichier comedie.scrutari-data.php | ||
| + | |||
| + | Ce fichier contient les informations de la configuration (accès à la base de données). | ||
| <code php> | <code php> | ||
| <?php | <?php | ||
| - | /* Chargement de la bibliothèque | + | //Indication que les données transmises sont du XML |
| + | header(" | ||
| + | |||
| + | // Nom de la base de données de WordPress. | ||
| + | define(' | ||
| + | |||
| + | // Utilisateur de la base de données MySQL. | ||
| + | define(' | ||
| + | |||
| + | // Mot de passe de la base de données MySQL. | ||
| + | define(' | ||
| + | |||
| + | // Adresse de l' | ||
| + | define(' | ||
| + | |||
| + | // Jeu de caractères à utiliser par la base de données | ||
| + | define(' | ||
| + | |||
| + | // Chaine vide pour SCRUTARIDATA_PATH, | ||
| + | define(' | ||
| + | |||
| + | // Appel de comedie.php | ||
| + | require(" | ||
| + | </ | ||
| + | |||
| + | Si on avait voulu que l' | ||
| + | |||
| + | <code php> | ||
| + | define(' | ||
| + | </ | ||
| + | |||
| + | === Fichier comedie.php === | ||
| + | |||
| + | Ce fichier (http:// | ||
| + | |||
| + | Le fichier commence par l' | ||
| + | |||
| + | <code php> | ||
| + | //Chargement de la bibliothèque | ||
| + | require_once(" | ||
| + | </ | ||
| + | |||
| + | Vient ensuite la déclaration d'une classe utilitaire : | ||
| + | |||
| + | <code php> | ||
| + | /** | ||
| + | * Recensement des posts Wordpress retenus pour l' | ||
| */ | */ | ||
| - | require_once(" | + | class PostCensus { |
| - | /* Ouverture d'un fichier en écriture | + | private |
| - | */ | + | |
| - | $file = fopen(" | + | |
| - | /* Construction d'une instance SDE_XmlWriter | + | function __construct() { |
| - | Pour que le code XML s' | + | $this-> |
| - | */ | + | } |
| - | $xmlWriter | + | |
| + | function put($id, $corpus) { | ||
| + | $this-> | ||
| + | } | ||
| + | |||
| + | function get($id) { | ||
| + | return $this-> | ||
| + | } | ||
| - | /* On inclus préalablement | + | } |
| - | (méthode hors API) | + | </code> |
| + | |||
| + | Puis la définit des fonctions. | ||
| + | |||
| + | <code php> | ||
| + | /** | ||
| + | * Ajoute les posts appartenant à la catégorie indiquée par $termTaxonomyId, | ||
| + | * ces posts étant destiné à être inclus dans le corpus de nom $corpusName | ||
| */ | */ | ||
| - | $xmlWriter->appendDeclaration(); | + | function addPosts($pdo, $scrutariDataExport, |
| + | $statement = $pdo->query(' | ||
| + | $array = array(); | ||
| + | while ($row = $statement-> | ||
| + | $array[] = $row[0]; | ||
| + | } | ||
| + | foreach($array as $postId) { | ||
| + | $postStatement = $pdo-> | ||
| + | $postRow = $postStatement-> | ||
| + | if ($postRow) { | ||
| + | $postCensus-> | ||
| + | $titre = $postRow[' | ||
| + | $soustitre = ""; | ||
| + | $date = ""; | ||
| + | $auteurs = ""; | ||
| + | $publication_annee = ""; | ||
| + | $publication_auteur = ""; | ||
| + | $publication_editeur = ""; | ||
| + | $metaStatement = $pdo-> | ||
| + | $dateDone = false; | ||
| + | while($metaRow = $metaStatement-> | ||
| + | $metaKey = $metaRow[' | ||
| + | $value = $metaRow[' | ||
| + | if ($metaKey == ' | ||
| + | $soustitre = $value; | ||
| + | } else if ($metaKey == ' | ||
| + | if ((!$dateDone) && (strlen($value) > 0)) { | ||
| + | $date = $value; | ||
| + | } | ||
| + | } else if ($metaKey == ' | ||
| + | if (strlen($value) > 0) { | ||
| + | $date = $value; | ||
| + | $dateDone = true; | ||
| + | } | ||
| + | } else if ($metaKey == ' | ||
| + | $auteurs = $value; | ||
| + | } else if ($metaKey == ' | ||
| + | $publication_annee = $value; | ||
| + | } else if ($metaKey == ' | ||
| + | $publication_auteur = $value; | ||
| + | } else if ($metaKey == ' | ||
| + | $publication_editeur = $value; | ||
| + | } | ||
| + | } | ||
| + | if ($corpusName == ' | ||
| + | $titre = "Note de lecture : " | ||
| + | if (strlen($soustitre) > 0) { | ||
| + | if (substr($titre, | ||
| + | $titre .= " | ||
| + | } | ||
| + | $titre .= " " | ||
| + | $soustitre = ""; | ||
| + | } | ||
| + | $soustitre = appendInfo($soustitre, | ||
| + | $soustitre = appendInfo($soustitre, | ||
| + | $soustitre = appendInfo($soustitre, | ||
| + | } | ||
| + | $ficheExport = $scrutariDataExport-> | ||
| + | $ficheExport-> | ||
| + | $ficheExport-> | ||
| + | $ficheExport-> | ||
| + | $ficheExport-> | ||
| + | if (strlen($date) > 0) { | ||
| + | $ficheExport-> | ||
| + | } | ||
| + | if (strlen($auteurs) > 0) { | ||
| + | parseAuteurs($auteurs, | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| - | /* Construction d'une instance de SDE_ScrutariDataExport qui suit l'API de ScrutariDataExport | + | /** |
| + | * Ajoute $value à la fin de $text en rajoutant | ||
| */ | */ | ||
| - | $scrutariDataExport | + | function appendInfo($text, $value) { |
| + | $value | ||
| + | $value = str_replace(" | ||
| + | if (substr($value, | ||
| + | $value = substr($value, | ||
| + | } | ||
| + | if (strlen($value) == 0) { | ||
| + | return $text; | ||
| + | } | ||
| + | if (strlen($text) > 0) { | ||
| + | $text .= ", "; | ||
| + | } | ||
| + | $text .= $value; | ||
| + | return $text; | ||
| + | } | ||
| - | /* Début de l'export, $baseMetadataExport | + | /** |
| + | * Récupère le nom des auteurs d'un post. Sur le site comedie.org, les véritables auteurs d'un post sont indiqués | ||
| + | * par meta_key=auteurs_plus dans la table wp_postmeta et sous la forme a: | ||
| + | * l' | ||
| */ | */ | ||
| - | $baseMetadataExport | + | function parseAuteurs($auteurs, $pdo, $ficheExport) { |
| + | $idx1 = strpos($auteurs, " | ||
| + | if ($idx1 < 1) { | ||
| + | return; | ||
| + | } | ||
| + | $idx2 = strpos($auteurs, | ||
| + | if ($idx2 < 1) { | ||
| + | return; | ||
| + | } | ||
| + | $auteurId = substr($auteurs, | ||
| + | if ($auteurId == 572) { // | ||
| + | return; | ||
| + | } | ||
| + | $authorStatement = $pdo->query(' | ||
| + | $auteur = $authorStatement-> | ||
| + | if ($auteur) { | ||
| + | $ficheExport-> | ||
| + | } | ||
| + | } | ||
| - | /* Définition des métadonnées | + | /** |
| - | SDE_INTITULE_SHORT correspond à la constante INTITULE_SHORT | + | * Ajoute les termes |
| + | * la fonction récupère également les posts liés au terme et crée une indexation si le post a bien été recensé | ||
| + | * préalablement dans $postCensus | ||
| */ | */ | ||
| - | $baseMetadataExport->setAuthority("e17a05b0-c45e-11d8-9669-0800200c9a66"); | + | function addTerms($pdo, $scrutariDataExport, |
| - | $baseMetadataExport->setBaseName("test"); | + | $statement = $pdo->query("SELECT wp_term_taxonomy.term_taxonomy_id, |
| - | $baseMetadataExport->setBaseIcon("http://monsite.net/icon-16x16pixels.png"); | + | $array = array(); |
| - | $baseMetadataExport-> | + | while ($row = $statement-> |
| - | $baseMetadataExport-> | + | $id = $row[0]; |
| - | $baseMetadataExport-> | + | $name = $row[1]; |
| - | $baseMetadataExport-> | + | if ((strlen($name) > 0) && (strpos($name, |
| + | $array[] = $id; | ||
| + | $motcleExport = $scrutariDataExport-> | ||
| + | $motcleExport-> | ||
| + | } | ||
| + | } | ||
| + | foreach($array as $termId) { | ||
| + | $postStatement = $pdo->query("SELECT object_id FROM wp_term_relationships WHERE term_taxonomy_id=".$termId); | ||
| + | while ($postId = $postStatement->fetchColumn(0)) { | ||
| + | $corpusName = $postCensus-> | ||
| + | if (strlen($corpusName) > 0) { | ||
| + | $scrutariDataExport-> | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | C'est alors que commmence l' | ||
| + | |||
| + | <code php> | ||
| + | //Test si l' | ||
| + | $file = false; | ||
| + | if (strlen(SCRUTARIDATA_PATH) > 0) { | ||
| + | $file = fopen(SCRUTARIDATA_PATH, | ||
| + | } | ||
| + | |||
| + | //Accès à la base de données | ||
| + | $pdo = new PDO(' | ||
| + | |||
| + | // Instance de PostCensus recensant les posts inclus dans l' | ||
| + | $postCensus = new PostCensus(); | ||
| + | |||
| + | // Instance de SDE_XmlWriter recensant les posts inclus dans l' | ||
| + | $xmlWriter = new SDE_XmlWriter($file, | ||
| + | $scrutariDataExport = new SDE_ScrutariDataExport($xmlWriter); | ||
| + | </ | ||
| + | |||
| + | Vient ensuite l' | ||
| + | |||
| + | <code php> | ||
| + | //Démarrage de l' | ||
| + | $baseMetadataExport = $scrutariDataExport-> | ||
| + | $baseMetadataExport-> | ||
| + | $baseMetadataExport-> | ||
| + | $baseMetadataExport-> | ||
| + | $baseMetadataExport-> | ||
| + | $baseMetadataExport-> | ||
| $baseMetadataExport-> | $baseMetadataExport-> | ||
| - | $baseMetadataExport-> | ||
| - | /* Insertion d' | + | //Création du corpus |
| - | $corpusMetadataExport est une instance | + | $corpusMetadataExport = $scrutariDataExport-> |
| - | */ | + | $corpusMetadataExport-> |
| - | $corpusMetadataExport = $scrutariDataExport-> | + | $corpusMetadataExport-> |
| - | /* Définition | + | //Ajout des posts des expériences (12 est l' |
| - | */ | + | addPosts($pdo, $scrutariDataExport, |
| - | $corpusMetadataExport-> | + | |
| - | $corpusMetadataExport-> | + | |
| - | /* Définition | + | //Création du corpus monographie correspondant aux fiches |
| - | */ | + | $corpusMetadataExport |
| - | $numero | + | $corpusMetadataExport-> |
| - | $corpusMetadataExport-> | + | $corpusMetadataExport-> |
| - | $corpusMetadataExport-> | + | |
| - | $numero = $corpusMetadataExport-> | + | |
| - | $corpusMetadataExport-> | + | |
| - | /* Premier exemple d'une fiche | + | //Ajout des posts des monographies |
| - | $ficheExport est une instance de SDE_FicheExport | + | addPosts($pdo, $scrutariDataExport, 15, $postCensus, "monographie"); |
| - | */ | + | |
| - | $ficheExport = $scrutariDataExport-> | + | |
| - | $ficheExport-> | + | |
| - | $ficheExport-> | + | |
| - | $ficheExport-> | + | |
| - | $ficheExport-> | + | |
| - | /*Second exemple | + | //Création du corpus video correspondant aux fiches |
| - | */ | + | $corpusMetadataExport |
| - | $ficheExport | + | $corpusMetadataExport->setIntitule(SDE_INTITULE_CORPUS, |
| - | $ficheExport->setTitre(" | + | $corpusMetadataExport->setIntitule(SDE_INTITULE_FICHE, "fr", "Vidéo n°"); |
| - | $ficheExport-> | + | |
| - | $ficheExport-> | + | |
| - | $ficheExport-> | + | |
| - | $ficheExport-> | + | |
| - | $ficheExport->addComplement(2,"L'ASSO"); | + | |
| - | $ficheExport-> | + | |
| - | /* Exemple d' | + | //Ajout des posts des vidéos |
| - | même s'il ne s'agit du mot-clé ou de la fiche en cours ou que le thésaurus ou le corpus | + | addPosts($pdo, |
| - | n'ont pas encore été définis. | + | |
| - | */ | + | |
| - | $scrutariDataExport-> | + | |
| - | /* Insertion d'un thésaurus | + | //Création du corpus lecture correspondant aux fiches |
| - | $thesaurusMetadataExport est une instance | + | $corpusMetadataExport |
| - | */ | + | $corpusMetadataExport-> |
| - | $thesaurusMetadataExport | + | $corpusMetadataExport-> |
| - | /* Définition | + | // |
| - | */ | + | addPosts($pdo, |
| + | |||
| + | // | ||
| + | $thesaurusMetadataExport = $scrutariDataExport-> | ||
| $thesaurusMetadataExport-> | $thesaurusMetadataExport-> | ||
| - | $thesaurusMetadataExport-> | ||
| - | /* Exemple d' | + | //Ajout des termes, les mots-clés sont distingués des autres termes par la taxonomie mots-clefs |
| - | */ | + | addTerms($pdo, $scrutariDataExport, "mots-clefs", $postCensus, "motcle"); |
| - | $motcleExport = $scrutariDataExport-> | + | |
| - | $motcleExport-> | + | |
| - | /* Autre exemple d' | + | //Fin de l' |
| - | $scrutariDataExport-> | + | $scrutariDataExport-> |
| + | </ | ||
| + | |||
| + | Ce script peut facilement être adapté à un autre site sous Wordpress. Les grandes différences d'un site à l' | ||
| - | /* L' | ||
| - | toujours sur le premier mot-clé */ | ||
| - | $motcleExport-> | ||
| - | /* Fin de l' | ||
| - | */ | ||
| - | $scrutariDataExport-> | ||
| - | /* Fermeture du fichier */ | ||
| - | fclose($file); | ||
| - | </ | ||
