| /trunk/api/scripts/modules/migration_smart_flore/MigrationSmartFlore.php |
|---|
| New file |
| 0,0 → 1,109 |
| <?php |
| // declare(encoding='UTF-8'); |
| /** |
| * |
| * @category wiki/smart'Flore |
| * @package Scripts |
| * @author Aurelien PERONNET <aurelien@tela-botanica.org> |
| * @license GPL v3 <http://www.gnu.org/licenses/gpl.txt> |
| * @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt> |
| * @copyright 1999-2015 Tela Botanica <accueil@tela-botanica.org> |
| */ |
| class MigrationSmartFlore extends Script { |
| protected $mode_verbeux = false; |
| // Paramêtres autorisées lors de l'appel au script en ligne de commande |
| protected $parametres_autorises = array( |
| '-n' => array(true, true, 'Nom du fichier ou du dossier à traiter')); |
| public function executer() { |
| // L'obligation de mettre un paramètre -a donnée par le framework |
| // n'a pas de sens, ça ne doit pas être obligatoire !!! |
| $cmd = $this->getParametre('a'); |
| $this->mode_verbeux = $this->getParametre('v'); |
| switch($cmd) { |
| case 'tous' : |
| $this->migrerFormatSmartFlore(); |
| break; |
| default: |
| } |
| } |
| protected function migrerFormatSmartFlore() { |
| $sections = array("Fiche simplifiée Smart'flore", "Introduction","Comment la reconnaître ?","Son histoire","Ses usages","Écologie & habitat","Ce qu'il faut savoir...","Sources"); |
| $nouvelles_sections = array( |
| "Description" => array("Introduction","Comment la reconnaître ?","Son histoire"), |
| "Usages" => array("Ses usages", "Ce qu'il faut savoir..."), |
| "Écologie % habitat" => array("Écologie & habitat"), |
| "Sources" => array("Sources") |
| ); |
| $where_section = 'body NOT LIKE "'; |
| $nouvelles_sections_k = array_keys($nouvelles_sections); |
| foreach($nouvelles_sections_k as $nouvelle_section_k) { |
| // Encore et toujours de l'iso (d'ailleurs si on ne fait pas de conversion la requete se comporte |
| // très bizarrement et renvoie des résultats en trop une fois le script déjà exécuté) |
| $where_section .= '%'.addslashes(ManipulationPage::convertirTexteAppliVersEncodageWiki($nouvelle_section_k)).'%'; |
| } |
| $where_section = $where_section.'"'; |
| $this->wiki = Registre::get('wikiApi'); |
| $requete = 'SELECT * FROM '.$this->wiki->GetConfigValue('table_prefix').'pages WHERE latest = "Y" '. |
| 'AND tag LIKE "SmartFlore%nt%" '. |
| 'AND '.$where_section; |
| $pages = $this->wiki->LoadAll($requete); |
| $pages_fmt = array(); |
| echo "Nombre de pages à migrer : ".count($pages)."\n"; |
| if(!empty($pages)) { |
| $manipulation = new ManipulationPage($this->wiki, $pages[0]); |
| echo "Migration en cours... \n"; |
| foreach($pages as &$page) { |
| $page_fmt = array(); |
| // On capte l'entête de la page situé avant la première section pour le recopier |
| // dans les nouvelles pages (il contient les backlinks et les noms) |
| $delim_entete = strpos($page["body"], "==== Introduction ===="); |
| if($delim_entete === false) { |
| $delim_entete = strpos($page["body"], "====Introduction===="); |
| } |
| // Attention l'entete est en iso, il faut le convertir manuellement |
| $entete = $manipulation->convertirTexteWikiVersEncodageAppli(substr($page["body"], 0, $delim_entete)); |
| // Par contre ici consulterPageSectionsFormatees est gentil et fait la conversion vers l'encodage de l'appli pour nous |
| $manipulation->consulterPageSectionsFormatees($page, implode(',', $sections)); |
| // Fusion des anciennes sections dans les nouvelles |
| foreach($nouvelles_sections as $nom_nouvelle_section => $sections_a_fusionner) { |
| $page_fmt[$nom_nouvelle_section] = '===='.$nom_nouvelle_section.'===='; |
| foreach($sections_a_fusionner as $section_a_fusionner) { |
| if(isset($page['sections'][$section_a_fusionner])) { |
| $page_fmt[$nom_nouvelle_section] .= $page['sections'][$section_a_fusionner]; |
| } |
| } |
| } |
| $corps = $entete."\n".implode("\n", $page_fmt); |
| $manipulation->ecrirePage($page["tag"], $corps); |
| } |
| } |
| echo "Migration effectuée \n"; |
| // Le exit est là pour empecher l'affichage d'être pollué par les erreurs |
| // dûes à certaines antédiluviennités de wikini |
| exit; |
| } |
| // http://stackoverflow.com/questions/834303/startswith-and-endswith-functions-in-php |
| protected function endsWith($haystack, $needle) { |
| // search forward starting from end minus needle length characters |
| return $needle === "" || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== FALSE); |
| } |
| } |
| /trunk/api/scripts/configurations/config.defaut.ini |
|---|
| New file |
| 0,0 → 1,2 |
| encodage_appli = "UTF-8" |
| encodage_wiki = "ISO-8859-15" |
| /trunk/api/scripts/framework.defaut.php |
|---|
| New file |
| 0,0 → 1,6 |
| <?php |
| // Inclusion du Framework |
| // Renomer ce fichier en "framework.php" |
| // Indiquer ci-dessous le chemin absolu vers le fichier autoload.inc.php de la bonne version du Framework |
| require_once '/home/www/commun/framework/0.3/Framework.php'; |
| ?> |
| /trunk/api/scripts/bibliotheque/ManipulationPage.php |
|---|
| New file |
| 0,0 → 1,191 |
| <?php |
| // declare(encoding='UTF-8'); |
| /** |
| * Librairie de consultation d'une page wiki |
| * |
| * @category php 5.2 |
| * @package wapi |
| * @author Aurélien Peronnet < aurelien@tela-botanica.org> |
| * @copyright Copyright (c) 2015, Tela Botanica (accueil@tela-botanica.org) |
| * @license http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL |
| * @license http://www.gnu.org/licenses/gpl.html Licence GNU-GPL |
| * @version $Id$ |
| */ |
| class ManipulationPage { |
| // C'est dommage cette classe fait doublon avec la classe contenue dans le dossier rest |
| // il faudrait faire une factorisation de tout ça |
| private $wiki = null; |
| private $pageNom = null; |
| private $section = null; |
| private $creerPage = false; |
| private $templateDefaut = null; |
| public function __construct($wiki, $pageNom) { |
| // Cette construction bizarre sert à éviter des bugs issus du comportement de wikini |
| $this->wiki = $wiki; |
| global $wiki; |
| $wiki = $this->wiki; |
| $this->pageNom = $pageNom; |
| $this->wiki->setPageCourante($this->pageNom); |
| } |
| public function consulterPage($page, $section = null) { |
| $page = $this->wiki->LoadPage($page); |
| if ($page != null) { |
| $this->consulterPageSectionsFormatees($page, $section); |
| } |
| return $page; |
| } |
| public function consulterPageSectionsFormatees(&$page, $section) { |
| // attention les wikis sont souvent en ISO ! |
| $page["body"] = $this->convertirTexteWikiVersEncodageAppli($page['body']); |
| if($section != null) { |
| $sections_tab = explode(',', $section); |
| if(count($sections_tab) > 1) { |
| foreach($sections_tab as $section_t) { |
| $page["sections"][$section_t] = $this->decouperPageSection($page["body"], $section_t); |
| } |
| } else { |
| $page["body"] = $this->decouperPageSection($page["body"], $section); |
| } |
| } |
| } |
| public function decouperPageSection($contenu_page, $section) { |
| $section_retour = ''; |
| if (is_numeric($section)) { |
| $section_retour = $this->getSectionParNumero($contenu_page, $section); |
| } else { |
| $section_retour = $this->getSectionParTitre($contenu_page, $section, false); |
| } |
| return $section_retour; |
| } |
| public function getSectionParNumero($page, $num) { |
| preg_match_all('/(=[=]+[ ]*)(.[.^=]*)+[ ]*=[=]+[.]*/i', $page, $sections, PREG_OFFSET_CAPTURE); |
| $sectionTxt = ''; |
| $debut_section = 0; |
| $lg_page = strlen($page); |
| $fin_section = $lg_page; |
| if ($num <= count($sections[1]) && $num > 0) { |
| $debut_section = $sections[1][$num - 1][1]; |
| $separateur = trim($sections[1][$num - 1][0]); |
| $separateur_trouve = false; |
| for ($i = $num; $i < count($sections[1]); $i++) { |
| $fin_section = $sections[1][$i][1]; |
| if($separateur == trim($sections[1][$i][0])) { |
| $separateur_trouve = true; |
| break; |
| } |
| } |
| $fin_section = $separateur_trouve ? $fin_section : $lg_page; |
| $sectionTxt = substr($page, $debut_section, $fin_section - $debut_section); |
| } else { |
| $sectionTxt = ''; |
| } |
| return $sectionTxt; |
| } |
| public function getSectionParTitre($page, $titre, $inclure_titre = false) { |
| $section = ''; |
| $reg_exp = '/((=[=]+)[ ]*'.preg_quote(trim($titre), '/').'[ ]*=[=]+)[.]*/i'; |
| $match = preg_split($reg_exp, $page, 2, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); |
| if (count($match) > 3) { |
| $section = explode(trim($match[2]), $match[3], 2); |
| $section = $section[0]; |
| $section = ($inclure_titre) ? $match[1].$section : $section; |
| } elseif (count($match) == 3) { |
| $section = explode(trim($match[1]), $match[2], 2); |
| $section = $section[0]; |
| $section = ($inclure_titre) ? $match[0].$section : $section; |
| } else { |
| $section = ""; |
| } |
| return $section; |
| } |
| private function creerPageAPartirTemplate($tag_page_a_creer, $tag_template) { |
| $page_template = $this->consulterPage($tag_template); |
| $corps_nouvelle_page = ($page_template != null) ? $page_template['body'] : ''; |
| // si le template n'existe pas, la page créée sera vide |
| $ecriture = $this->ecrirePage($tag_page_a_creer, $corps_nouvelle_page); |
| return $ecriture; |
| } |
| /** |
| * |
| * Si la section demandée existe dans la page, renvoie un tableau contenant le numéro de caractère de |
| * début de la section, après son titre, ainsi que la longeur du titre |
| * @param string $titre de la section |
| * @param string $page contenu de la page wiki |
| * @return tableau associatif tel que décrit ici |
| */ |
| private function getInformationsPositionSection($titre, $page) { |
| preg_match_all('/(=[=]+[ ]*'.preg_quote(trim($titre), '/').'[ ]*=[=]+[.]*)/i', $page, $sections, PREG_OFFSET_CAPTURE); |
| $longueur_titre = 0; |
| $debut_section_apres_titre = 0; |
| if (count($sections) > 0 && is_array($sections[0]) && count($sections[0][0]) >= 2) { |
| $longueur_titre = mb_strlen($sections[0][0][0]); |
| $debut_section_apres_titre = $sections[0][0][1] + $longueur_titre; |
| } |
| // ATTENTION : début contient le numéro du caractere de début de la section, après le titre |
| $infos = array('debut' => $debut_section_apres_titre, |
| 'longueur_titre' => $longueur_titre |
| ); |
| return $infos; |
| } |
| private function remplacerSection($titre_section, $section_remplacement, $corps) { |
| // insertion d'un saut de ligne pour empêcher de casser le titre, lorsque le titre |
| // suivant vient directement après la section, sans saut de ligne ni espace |
| $section_remplacement = "\n".$section_remplacement."\n"; |
| $section_page_originale = $this->getSectionParTitre($corps, $titre_section, true); |
| $infos_section = $this->getInformationsPositionSection($titre_section, $corps); |
| $nb_caracteres_a_remplacer = mb_strlen($section_page_originale) - $infos_section['longueur_titre']; |
| $nouveau_contenu = substr_replace($corps, $section_remplacement, $infos_section['debut'], $nb_caracteres_a_remplacer); |
| return $nouveau_contenu; |
| } |
| public function ecrirePage($nom_page, $contenu) { |
| $texte_encode = $this->convertirTexteAppliVersEncodageWiki($contenu); |
| $ecriture = $this->wiki->SavePage($nom_page, $texte_encode, "", true); |
| return $ecriture; |
| } |
| public static function convertirTexteWikiVersEncodageAppli($texte) { |
| if (Config::get('encodage_appli') != Config::get('encodage_wiki')) { |
| $texte = mb_convert_encoding($texte,Config::get('encodage_appli'),Config::get('encodage_wiki')); |
| } |
| return $texte; |
| } |
| public static function convertirTexteAppliVersEncodageWiki($texte) { |
| if (Config::get('encodage_appli') != Config::get('encodage_wiki')) { |
| $texte = mb_convert_encoding($texte,Config::get('encodage_wiki'),Config::get('encodage_appli')); |
| } |
| return $texte; |
| } |
| } |
| ?> |
| /trunk/api/scripts/bibliotheque/WikiApi.php |
|---|
| New file |
| 0,0 → 1,54 |
| <?php |
| class WikiApi { |
| private $page = null; |
| private $wikiObjet = null; |
| private $cheminWiki = null; |
| private $cheminApi = null; |
| public function __construct($cheminWiki, $cheminApi) { |
| $this->cheminWiki = $cheminWiki; |
| $this->cheminApi = $cheminApi; |
| $this->initialiser(); |
| } |
| private function initialiser() { |
| if ($this->page != null) { |
| $_REQUEST['wiki'] = $this->page; |
| } |
| ini_set('include_path',ini_get('include_path').':'.$this->cheminWiki.':'); |
| chdir($this->cheminWiki); |
| include 'api.php'; |
| $this->wikiObjet = $wiki; |
| chdir($this->cheminApi); |
| } |
| public function setPageCourante($page) { |
| $this->page = $page; |
| } |
| public function chargerClasseWiki($classe) { |
| $chemins = array($this->cheminWiki.'/tools/', $this->cheminWiki.'/formatters/'); |
| foreach ($chemins as $chemin) { |
| $chemin = $chemin.$classe.'.php'; |
| if (file_exists($chemin)) { |
| require_once $chemin; |
| $classeTrouvee = true; |
| } |
| } |
| } |
| public function __call($methodeNom, $arguments) { |
| if ($this->wikiObjet == null) { |
| $this->initialiser(); |
| } |
| chdir($this->cheminWiki); |
| $retour = call_user_func_array(array($this->wikiObjet, $methodeNom), $arguments); |
| chdir($this->cheminApi); |
| return $retour; |
| } |
| } |
| ?> |
| /trunk/api/scripts/cli.php |
|---|
| New file |
| 0,0 → 1,44 |
| <?php |
| // Encodage : UTF-8 |
| // +-------------------------------------------------------------------------------------------------------------------+ |
| /** |
| * Initialise le chargement et l'exécution des scripts |
| * |
| * Lancer ce fichier en ligne de commande avec : |
| * <code>/opt/lampp/bin/php cli.php mon_script -a test</code> |
| * |
| * @category CEL |
| * @package Scripts |
| * @author Mathias CHOUET <mathias@tela-botanica.org> |
| * @author Jean-Pascal MILCENT <jpm@tela-botanica.org> |
| * @author Aurelien PERONNET <aurelien@tela-botanica.org> |
| * @license GPL v3 <http://www.gnu.org/licenses/gpl.txt> |
| * @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt> |
| * @copyright 1999-2014 Tela Botanica <accueil@tela-botanica.org> |
| */ |
| // Le fichier Framework.php du Framework de Tela Botanica doit être appelée avant tout autre chose dans l'application. |
| // Sinon, rien ne sera chargé. |
| // Chemin du fichier chargeant le framework requis |
| $framework = dirname(__FILE__).DIRECTORY_SEPARATOR.'framework.php'; |
| if (!file_exists($framework)) { |
| $e = "Veuillez paramétrer l'emplacement et la version du Framework dans le fichier $framework"; |
| trigger_error($e, E_USER_ERROR); |
| } else { |
| // Inclusion du Framework |
| require_once $framework; |
| // Ajout d'information concernant cette application |
| Framework::setCheminAppli(__FILE__);// Obligatoire |
| Framework::setInfoAppli(Config::get('info')); |
| // Création de l'objet Wiki qui sera transmis au service via le Registre |
| Registre::set('cheminApi', getcwd()); |
| Registre::set('cheminWiki', realpath(dirname(__FILE__).DS.'..'.DS.'..'.DS).DS); |
| //require_once(getcwd().DS.'bibliotheque'.DS.'WikiApi.php'); |
| $wikiApi = new WikiApi(Registre::get('cheminWiki'), Registre::get('cheminApi')); |
| Registre::set('wikiApi', $wikiApi); |
| // Initialisation et lancement du script appelé en ligne de commande |
| Cli::executer(); |
| } |