Subversion Repositories Applications.wikini

Rev

Rev 62 | Rev 67 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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