Subversion Repositories Applications.wikini

Rev

Rev 62 | Rev 67 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
50 aurelien 1
<?php
2
// declare(encoding='UTF-8');
3
/**
4
 *
5
 * @category   wiki/smart'Flore
6
 * @package    Scripts
7
 * @author     Aurelien PERONNET <aurelien@tela-botanica.org>
8
 * @license    GPL v3 <http://www.gnu.org/licenses/gpl.txt>
9
 * @license    CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
10
 * @copyright  1999-2015 Tela Botanica <accueil@tela-botanica.org>
11
 */
12
 
13
class MigrationSmartFlore extends Script {
14
 
15
	protected $mode_verbeux = false;
16
 
17
	public function executer() {
18
		$cmd = $this->getParametre('a');
19
		$this->mode_verbeux = $this->getParametre('v');
20
 
21
		switch($cmd) {
64 aurelien 22
			case 'migrerFormatSmartFlore' :
50 aurelien 23
				$this->migrerFormatSmartFlore();
24
			break;
25
 
64 aurelien 26
			case 'migrerSentiersSmartFlore' :
27
				$this->migrerSentiersSmartFlore();
28
			break;
29
 
50 aurelien 30
			default:
31
		}
32
	}
33
 
64 aurelien 34
	protected function migrerSentiersSmartFlore() {
35
		$this->wiki = Registre::get('wikiApi');
36
		$requete = 'SELECT * FROM '.$this->wiki->GetConfigValue('table_prefix').'pages WHERE latest = "Y" '.
37
				'AND tag = "AccesProjet" ';
38
 
39
		$page_sentiers = $this->wiki->LoadSingle($requete);
40
 
41
		preg_match_all("|\[\[([^\]\]]*)\]\]|", $page_sentiers['body'], $sentiers, PREG_PATTERN_ORDER);
42
		$sentiers = $sentiers[1];
43
 
44
		echo "Nombre de sentiers à migrer : ".count($sentiers)."\n";
45
 
46
		$sentiers_a_inserer = array();
47
		$valeurs_sentiers_a_inserer = array();
48
		$fiches_a_associer = array();
49
 
50
		$proprietaires_sentiers = array();
51
		// Chargement du fichier contenant les propriétaires à associer aux sentiers
52
		$fichier = file(realpath(dirname(__FILE__)).'proprietaires_sentiers.csv');
53
 
54
		foreach ($fichier as $ligne) {
55
			$data = str_getcsv($ligne);
56
			if($data[2] != "") {
57
				$proprietaires_sentiers[trim($data[0])] = trim($data[2]);
58
			}
59
		}
60
 
61
		$courriel_proprietaires = array_values(array_unique($proprietaires_sentiers));
62
		$url_infos_courriels = 	Config::get('annuaire_infos_courriels_url').implode(',', $courriel_proprietaires);
63
 
64
		$infos_proprietaires = json_decode(file_get_contents($url_infos_courriels), true);
65
 
66
		$infos_proprietaires_a_sentier = array();
67
 
68
		foreach($proprietaires_sentiers as $nom_sentier => $proprietaire_sentier) {
69
			if(isset($infos_proprietaires[$proprietaire_sentier])) {
70
				$infos_proprietaires_a_sentier[$nom_sentier] = $infos_proprietaires[$proprietaire_sentier]['nomWiki'];
71
			} else {
72
				// les sentiers sans propriétaires sont affectés au compte accueil
73
				$infos_proprietaires_a_sentier[$nom_sentier] = "AssociationTelaBotanica";
74
			}
75
		}
76
 
77
		$requete_insertion = 'INSERT INTO '.$this->wiki->GetConfigValue('table_prefix').'triples '.
78
					'(resource, property, value) VALUES ';
79
 
80
		foreach($sentiers as $sentier) {
81
 
82
			list($tag, $titre) = explode(' ', $sentier, 2);
83
 
84
			$requete = 'SELECT * FROM '.$this->wiki->GetConfigValue('table_prefix').'pages WHERE latest = "Y" '.
85
					'AND tag = "'.$tag.'" ';
86
 
87
			$infos_sentier = $this->wiki->LoadSingle($requete);
88
 
89
			$titre = trim($titre);
90
			$proprietaire = !empty($infos_proprietaires_a_sentier[$titre]) ? $infos_proprietaires_a_sentier[$titre] : "AssociationTelaBotanica";
91
 
92
			$sentiers_a_inserer[] = array(
93
										'resource' => $titre,
94
										'property' => 'smartFlore.sentiers',
95
										'value' => $proprietaire
96
									);
97
 
98
			$valeurs_sentiers_a_inserer[] = "('".addslashes(trim($titre))."', 'smartFlore.sentiers', '".$proprietaire."')";
99
 
100
			preg_match_all("|\[\[(SmartFlore[^(?:nt)]*nt[0-9]*)|", $infos_sentier['body'], $fiches_du_sentier, PREG_PATTERN_ORDER);
101
 
102
			if(!empty($fiches_du_sentier[0])) {
103
				foreach($fiches_du_sentier[1] as $fiche_du_sentier) {
104
					$fiches_a_associer[] = array(
105
							'resource' => $fiche_du_sentier,
106
							'property' => 'smartFlore.sentiers.fiche',
107
							'value' => $titre
108
					);
109
 
110
					$valeurs_fiches_a_associer[] = "('".$fiche_du_sentier."', 'smartFlore.sentiers.fiche', '".addslashes(trim($titre))."')";
111
				}
112
			}
113
		}
114
 
115
		$valeurs_a_inserer = $valeurs_sentiers_a_inserer + $valeurs_fiches_a_associer;
116
		$requete_insertion .= implode(', '."\n", $valeurs_a_inserer);
117
		// Tout est contenu dans la table triple du wiki, donc une seule requête suffit pour tout insérer
118
		$this->wiki->Query($requete_insertion);
119
 
120
		echo 'Migration des sentiers effectuée'."\n";
121
		exit;
122
	}
123
 
50 aurelien 124
	protected function migrerFormatSmartFlore() {
125
 
62 mathias 126
		// sections "souples" - attention, ne seront pas "quotées" mais interprétées comme morceaux de regexp directement !
127
		$sections = array("Fiche simplifi.+e Smart.+flore", "Introduction","Comment la reconna.+tre.+","Son histoire","Ses usages",".+(?:cologie|habitat).+","Ce qu.+il faut savoir.+","Sources");
50 aurelien 128
		$nouvelles_sections = array(
64 aurelien 129
			"Description" => array("Introduction","Comment la reconna.+tre.+","Son histoire"),
130
			"Usages" => array("Ses usages", "Ce qu.+il faut savoir.+"),
62 mathias 131
			"Écologie & habitat" => array(".+(?:cologie|habitat).+"), // groupe non-capturant avec (?:a|b)
64 aurelien 132
			"Sources" => array("Sources")
50 aurelien 133
		);
134
 
135
		$where_section = 'body NOT LIKE "';
136
		$nouvelles_sections_k = array_keys($nouvelles_sections);
137
		foreach($nouvelles_sections_k as $nouvelle_section_k) {
138
			// Encore et toujours de l'iso (d'ailleurs si on ne fait pas de conversion la requete se comporte
139
			// très bizarrement et renvoie des résultats en trop une fois le script déjà exécuté)
140
			$where_section .= '%'.addslashes(ManipulationPage::convertirTexteAppliVersEncodageWiki($nouvelle_section_k)).'%';
141
		}
142
		$where_section = $where_section.'"';
143
 
144
		$this->wiki = Registre::get('wikiApi');
145
		$requete = 'SELECT * FROM '.$this->wiki->GetConfigValue('table_prefix').'pages WHERE latest = "Y" '.
146
						'AND tag LIKE "SmartFlore%nt%" '.
147
						'AND '.$where_section;
148
 
149
		$pages = $this->wiki->LoadAll($requete);
150
		$pages_fmt = array();
151
		echo "Nombre de pages à migrer : ".count($pages)."\n";
152
 
153
		if(!empty($pages)) {
154
			$manipulation = new ManipulationPage($this->wiki, $pages[0]);
155
			echo "Migration en cours... \n";
156
 
157
			foreach($pages as &$page) {
158
 
159
				$page_fmt = array();
160
 
161
				// On capte l'entête de la page situé avant la première section pour le recopier
162
				// dans les nouvelles pages (il contient les backlinks et les noms)
163
				$delim_entete = strpos($page["body"], "==== Introduction ====");
164
				if($delim_entete === false) {
165
					$delim_entete = strpos($page["body"], "====Introduction====");
166
				}
167
				// Attention l'entete est en iso, il faut le convertir manuellement
168
				$entete = $manipulation->convertirTexteWikiVersEncodageAppli(substr($page["body"], 0, $delim_entete));
169
 
170
				// Par contre ici consulterPageSectionsFormatees est gentil et fait la conversion vers l'encodage de l'appli pour nous
171
				$manipulation->consulterPageSectionsFormatees($page, implode(',', $sections));
172
 
173
				// Fusion des anciennes sections dans les nouvelles
174
				foreach($nouvelles_sections as $nom_nouvelle_section => $sections_a_fusionner) {
175
					$page_fmt[$nom_nouvelle_section] = '===='.$nom_nouvelle_section.'====';
176
					foreach($sections_a_fusionner as $section_a_fusionner) {
177
						if(isset($page['sections'][$section_a_fusionner])) {
178
							$page_fmt[$nom_nouvelle_section] .= $page['sections'][$section_a_fusionner];
179
						}
180
					}
181
				}
182
 
183
				$corps = $entete."\n".implode("\n", $page_fmt);
184
				$manipulation->ecrirePage($page["tag"], $corps);
185
			}
186
		}
187
		echo "Migration effectuée \n";
188
		// Le exit est là pour empecher l'affichage d'être pollué par les erreurs
189
		// dûes à certaines antédiluviennités de wikini
190
		exit;
191
	}
192
 
193
	// http://stackoverflow.com/questions/834303/startswith-and-endswith-functions-in-php
194
	protected function endsWith($haystack, $needle) {
195
		// search forward starting from end minus needle length characters
196
		return $needle === "" || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== FALSE);
197
	}
198
}