Scrutari et MediaWiki

L'exportation des données de MediaWiki vers le format ScrutariData a été fait pour le Wikicoredem. Dans l'état actuel, il est assez simpliste, la base générée ne comprend qu'un seul corpus. Cette exportation se fait à l'aide d'une classe php MediawikiScrutariDataWriter qui utilise une classe utilitaire UtilXmlWriter. Les points à savoir pour comprendre le code sont les suivants :

  • La question de la connexion à la base de données n'est pas traitée, on considère qu'elle est établie préalablement (de là le simple appel $resultat = mysql_query($requete);)
  • Les premières méthodes de MediawikiScrutariDataWriter qui commencent par set sont des méthodes de définition des différents paramètres utilisés dans la méthode principale writeScrutariData()
  • L'argument $file de la méthode writeScrutariData() correspond à un pointeur vers un fichier, s'il est omis, le XML sera écrit en sortie (fonction echo)
  • Seules les pages de l'espace de nom principal sont extraites et les pages de redirection sont ignorées. Pour affiner la requête, il faut modifier la ligne $requete = “SELECT page_id,page_title FROM page WHERE page_namespace=0 AND page_is_redirect != 1 ORDER BY page_id”;.

Le résultat est accessible ici : http://scrutari.coredem.info/infos/php/coredem.scrutari-data.php?lang=fr.

class-MediawikiScrutariDataWriter.php

<?php
require_once("class-UtilXmlWriter.php");
 
class MediawikiScrutariDataWriter {
private $authority;
private $baseName;
private $baseIcon;
private $lang;
private $baseIntituleShort;
private $baseIntituleLong;
private $corpusName;
private $corpusIntituleCorpus;
private $corpusIntituleFiche;
private $hrefParent;
 
public function setAuthority($authority) {
$this->authority = $authority;
}
 
public function setBaseName($baseName) {
$this->baseName = $baseName;
}
 
public function setBaseIcon($baseIcon) {
$this->baseIcon = $baseIcon;
}
 
public function setLang($lang) {
$this->lang = $lang;
}
 
public function setBaseIntituleShort($baseIntituleShort) {
$this->baseIntituleShort = $baseIntituleShort;
}
 
public function setBaseIntituleLong($baseIntituleLong) {
$this->baseIntituleLong = $baseIntituleLong;
}
 
public function setCorpusName($corpusName) {
$this->corpusName = $corpusName;
}
 
public function setCorpusIntituleCorpus($corpusIntituleCorpus) {
$this->corpusIntituleCorpus = $corpusIntituleCorpus;
}
 
public function setCorpusIntituleFiche($corpusIntituleFiche) {
$this->corpusIntituleFiche = $corpusIntituleFiche;
}
 
public function setHrefParent($hrefParent) {
$this->hrefParent = $hrefParent;
}
 
public function writeScrutariData($file) {
    $xmlWriter = new UtilXmlWriter($file);
    $xmlWriter->appendDeclaration();
    $xmlWriter->openTag("base");
    $xmlWriter->openTag("base-metadata");
    $xmlWriter->addElement("authority",$this->authority);
    $xmlWriter->addElement("base-name",$this->baseName);
    $xmlWriter->addElement("base-icon",$this->baseIcon);
    $xmlWriter->openTag("intitule-short");
    $xmlWriter->addLibElement($this->lang,$this->baseIntituleShort);
    $xmlWriter->closeTag("intitule-short");
    $xmlWriter->openTag("intitule-long");
    $xmlWriter->addLibElement($this->lang,$this->baseIntituleLong);
    $xmlWriter->closeTag("intitule-long");
    $xmlWriter->openTag("langs-ui");
    $xmlWriter->addElement("lang",$this->lang);
    $xmlWriter->closeTag("langs-ui");
    $xmlWriter->closeTag("base-metadata");
 
    $xmlWriter->write("<corpus corpus-name=\"");
    $xmlWriter->write($this->corpusName);
    $xmlWriter->write("\">");
    $xmlWriter->openTag("corpus-metadata");
    $xmlWriter->openTag("intitule-corpus");
    $xmlWriter->addLibElement($this->lang,$this->corpusIntituleCorpus);
    $xmlWriter->closeTag("intitule-corpus");
    $xmlWriter->openTag("intitule-fiche");
    $xmlWriter->addLibElement($this->lang,$this->corpusIntituleFiche);
    $xmlWriter->closeTag("intitule-fiche");
    $xmlWriter->addElement("type","CNC");
    $xmlWriter->addElement("href-parent",$this->hrefParent);
    $xmlWriter->closeTag("corpus-metadata");
 
    $requete = "SELECT page_id,page_title FROM page WHERE page_namespace=0 AND page_is_redirect != 1 ORDER BY page_id";
 
    $resultat = mysql_query($requete);
 
    while($row = mysql_fetch_row($resultat)) {
        $page_id = $row[0];
        $page_title = $row[1];
        $xmlWriter->write("<fiche fiche-id=\"");
        $xmlWriter->write($page_id);
        $xmlWriter->write("\">");
        $titre = str_replace("_"," ",$page_title);
        $xmlWriter->addElement("titre",$titre);
        $xmlWriter->addElement("lang",$this->lang);
        $href = rawurlencode($page_title);
        $xmlWriter->addElement(href,$href);
        $xmlWriter->closeTag("fiche");
    }
 
    $xmlWriter->closeTag("corpus");
    $xmlWriter->closeTag("base");
}
 
}

class-UtilXmlWriter.php

<?php
 
class UtilXmlWriter {
	private $file;
 
	function __construct($file) {
		$this->file = $file;
	}
 
	function appendDeclaration() {
		$this->write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
	}
 
	function openTag($tagName) {
		$this->write("<");
		$this->write($tagName);
		$this->write(">");
	}
 
	function closeTag($tagName) {
		$this->write("</");
		$this->write($tagName);
		$this->write(">");
		$this->write("\n");
	}
 
	function addElement($tagName, $value) {
		$this->openTag($tagName);
		$this->write($value);
		$this->closeTag($tagName);
	}
 
	function addLibElement($lang,$value) {
		$this->write("<lib xml:lang=\"");
		$this->write($lang);
		$this->write("\">");
		$this->write($value);
		$this->write("</lib>");
	}
 
 
	function write($text) {
		if ($this->file) {
			fwrite($this->file,$text);
		}
		else {
			print($text);
		}
	}
}
scrutaridata/impl/mediawiki.txt · Dernière modification: 2016/07/06 18:38 par vincent
CC Attribution-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0