Subversion Repositories Applications.wikini

Rev

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

Rev 64 Rev 67
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
	
16
	
17
	public function executer() {
17
	public function executer() {
18
		$cmd = $this->getParametre('a');
18
		$cmd = $this->getParametre('a');
19
		$this->mode_verbeux = $this->getParametre('v');
19
		$this->mode_verbeux = $this->getParametre('v');
20
		
20
		
21
		switch($cmd) {
21
		switch($cmd) {
22
			case 'migrerFormatSmartFlore' :
22
			case 'migrerFormatSmartFlore' :
23
				$this->migrerFormatSmartFlore();
23
				$this->migrerFormatSmartFlore();
24
			break;
24
			break;
25
			
25
			
26
			case 'migrerSentiersSmartFlore' :
26
			case 'migrerSentiersSmartFlore' :
27
				$this->migrerSentiersSmartFlore();
27
				$this->migrerSentiersSmartFlore();
28
			break;
28
			break;
29
			
29
			
30
			default:
30
			default:
31
		}
31
		}
32
	}
32
	}
33
	
33
	
34
	protected function migrerSentiersSmartFlore() {
34
	protected function migrerSentiersSmartFlore() {
35
		$this->wiki = Registre::get('wikiApi');
35
		$this->wiki = Registre::get('wikiApi');
36
		$requete = 'SELECT * FROM '.$this->wiki->GetConfigValue('table_prefix').'pages WHERE latest = "Y" '.
36
		$requete = 'SELECT * FROM '.$this->wiki->GetConfigValue('table_prefix').'pages WHERE latest = "Y" '.
37
				'AND tag = "AccesProjet" ';
37
				'AND tag = "AccesProjet" ';
38
				
38
				
39
		$page_sentiers = $this->wiki->LoadSingle($requete);
39
		$page_sentiers = $this->wiki->LoadSingle($requete);
40
		
-
 
-
 
40
		// Attention les wiki sont en iso Argh%#[{!?
-
 
41
		// Il faut convertir car sinon certains sentiers ne matcheront pas à leur propriétaire
-
 
42
		$page_sentiers['body'] = mb_convert_encoding($page_sentiers['body'], Config::get('encodage_appli'), Config::get('encodage_wiki'));
41
		preg_match_all("|\[\[([^\]\]]*)\]\]|", $page_sentiers['body'], $sentiers, PREG_PATTERN_ORDER);
43
		preg_match_all("|\[\[([^\]\]]*)\]\]|", $page_sentiers['body'], $sentiers, PREG_PATTERN_ORDER);
-
 
44
		
-
 
45
		// Seule une partie du tableau contenant les noms des sentiers nous interesse
42
		$sentiers = $sentiers[1];
46
		$sentiers = $sentiers[1];
43
		
47
		
44
		echo "Nombre de sentiers à migrer : ".count($sentiers)."\n";
48
		echo "Nombre de sentiers à migrer : ".count($sentiers)."\n";
45
		
-
 
46
		$sentiers_a_inserer = array();
49
		
47
		$valeurs_sentiers_a_inserer = array();
-
 
48
		$fiches_a_associer = array();
50
		$valeurs_sentiers_a_inserer = array();
49
		
51
		
50
		$proprietaires_sentiers = array();
52
		$proprietaires_sentiers = array();
51
		// Chargement du fichier contenant les propriétaires à associer aux sentiers
53
		// Chargement du fichier contenant les propriétaires à associer aux sentiers
52
		$fichier = file(realpath(dirname(__FILE__)).'proprietaires_sentiers.csv');
54
		$fichier = file(realpath(dirname(__FILE__)).'/proprietaires_sentiers.csv');
53
		
55
		
54
		foreach ($fichier as $ligne) {
56
		foreach ($fichier as $ligne) {
55
			$data = str_getcsv($ligne);
57
			$data = str_getcsv($ligne);
56
			if($data[2] != "") {
58
			if($data[2] != "") {
-
 
59
				// on associe un titre de sentier à un email (la clé est le titre de sentier)
57
				$proprietaires_sentiers[trim($data[0])] = trim($data[2]);
60
				$proprietaires_sentiers[trim($data[0])] = trim($data[2]);
58
			}		
61
			}		
59
		}
62
		}
60
		
63
		
61
		$courriel_proprietaires = array_values(array_unique($proprietaires_sentiers));	
64
		$courriel_proprietaires = array_values(array_unique($proprietaires_sentiers));	
-
 
65
		// Chargement des infos des utilisateurs pour obtenirs leurs nomWikis associés
62
		$url_infos_courriels = 	Config::get('annuaire_infos_courriels_url').implode(',', $courriel_proprietaires);
66
		$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);
67
		$infos_proprietaires = json_decode(file_get_contents($url_infos_courriels), true);
65
		
68
		
66
		$infos_proprietaires_a_sentier = array();
69
		$infos_proprietaires_a_sentier = array();
67
		
70
		
68
		foreach($proprietaires_sentiers as $nom_sentier => $proprietaire_sentier) {
71
		foreach($proprietaires_sentiers as $nom_sentier => $proprietaire_sentier) {
69
			if(isset($infos_proprietaires[$proprietaire_sentier])) {
72
			if(isset($infos_proprietaires[$proprietaire_sentier])) {
70
				$infos_proprietaires_a_sentier[$nom_sentier] = $infos_proprietaires[$proprietaire_sentier]['nomWiki'];
73
				$infos_proprietaires_a_sentier[$nom_sentier] = $infos_proprietaires[$proprietaire_sentier]['nomWiki'];
71
			} else {
74
			} else {
72
				// les sentiers sans propriétaires sont affectés au compte accueil
75
				// les sentiers sans propriétaires sont affectés au compte accueil
73
				$infos_proprietaires_a_sentier[$nom_sentier] = "AssociationTelaBotanica";
76
				$infos_proprietaires_a_sentier[$nom_sentier] = "AssociationTelaBotanica";
74
			}
77
			}
75
		}
78
		}
76
		
79
		
77
		$requete_insertion = 'INSERT INTO '.$this->wiki->GetConfigValue('table_prefix').'triples '.
80
		$requete_insertion = 'INSERT INTO '.$this->wiki->GetConfigValue('table_prefix').'triples '.
78
					'(resource, property, value) VALUES ';
81
					'(resource, property, value) VALUES ';
79
		
82
		
80
		foreach($sentiers as $sentier) {
83
		foreach($sentiers as $sentier) {
81
			
84
			
-
 
85
			list($tag, $titre) = explode(' ', $sentier, 2);
-
 
86
			// Reconversion dans l'encodage du wiki pour l'insertion
82
			list($tag, $titre) = explode(' ', $sentier, 2);
87
			$titre_encode_wiki = mb_convert_encoding($titre, Config::get('encodage_wiki'), Config::get('encodage_appli'));
83
			
88
			
84
			$requete = 'SELECT * FROM '.$this->wiki->GetConfigValue('table_prefix').'pages WHERE latest = "Y" '.
89
			$requete = 'SELECT * FROM '.$this->wiki->GetConfigValue('table_prefix').'pages WHERE latest = "Y" '.
85
					'AND tag = "'.$tag.'" ';
90
					'AND tag = "'.$tag.'" ';
86
			
91
			
87
			$infos_sentier = $this->wiki->LoadSingle($requete);
92
			$infos_sentier = $this->wiki->LoadSingle($requete);
88
			
93
		
89
			$titre = trim($titre);
-
 
90
			$proprietaire = !empty($infos_proprietaires_a_sentier[$titre]) ? $infos_proprietaires_a_sentier[$titre] : "AssociationTelaBotanica";
94
			$proprietaire = !empty($infos_proprietaires_a_sentier[$titre]) ? $infos_proprietaires_a_sentier[$titre] : "AssociationTelaBotanica";
-
 
95
			$valeurs_sentiers_a_inserer[] = "('".addslashes(trim($titre_encode_wiki))."', 'smartFlore.sentiers', '".$proprietaire."')";
91
			
-
 
92
			$sentiers_a_inserer[] = array(
-
 
93
										'resource' => $titre,
96
			
94
										'property' => 'smartFlore.sentiers',
-
 
95
										'value' => $proprietaire
-
 
96
									);
-
 
97
			
97
			// Recherche de toutes les fiches contenues dans le sentier (normalement sous forme de liens vers des fiches de type 
98
			$valeurs_sentiers_a_inserer[] = "('".addslashes(trim($titre))."', 'smartFlore.sentiers', '".$proprietaire."')";
-
 
99
			
98
			// SmartFloreYYYntZZZ où YYY est un code de référentiel et ZZZ un numéro taxonomique)
100
			preg_match_all("|\[\[(SmartFlore[^(?:nt)]*nt[0-9]*)|", $infos_sentier['body'], $fiches_du_sentier, PREG_PATTERN_ORDER);
99
			preg_match_all("|\[\[(SmartFlore[^(?:nt)]*nt[0-9]*)|", $infos_sentier['body'], $fiches_du_sentier, PREG_PATTERN_ORDER);
101
			
100
			
102
			if(!empty($fiches_du_sentier[0])) {
101
			if(!empty($fiches_du_sentier[0])) {
103
				foreach($fiches_du_sentier[1] as $fiche_du_sentier) {
102
				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))."')";
103
					$valeurs_fiches_a_associer[] = "('".$fiche_du_sentier."', 'smartFlore.sentiers.fiche', '".addslashes(trim($titre_encode_wiki))."')";
111
				}
104
				}
112
			}
105
			}
113
		}
106
		}
114
		
107
		
115
		$valeurs_a_inserer = $valeurs_sentiers_a_inserer + $valeurs_fiches_a_associer;
108
		$valeurs_a_inserer = $valeurs_sentiers_a_inserer + $valeurs_fiches_a_associer;
116
		$requete_insertion .= implode(', '."\n", $valeurs_a_inserer);
109
		$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
110
		// 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);
111
		$this->wiki->Query($requete_insertion);
119
		
112
		
120
		echo 'Migration des sentiers effectuée'."\n";
113
		echo 'Migration des sentiers effectuée'."\n";
121
		exit;
114
		exit;
122
	}
115
	}
123
	
116
	
124
	protected function migrerFormatSmartFlore() {	
117
	protected function migrerFormatSmartFlore() {	
125
 
118
 
126
		// sections "souples" - attention, ne seront pas "quotées" mais interprétées comme morceaux de regexp directement !
119
		// 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");
120
		$sections = array("Fiche simplifi.+e Smart.+flore", "Introduction","Comment la reconna.+tre.+","Son histoire","Ses usages",".+(?:cologie|habitat).+","Ce qu.+il faut savoir.+","Sources");
128
		$nouvelles_sections = array(
121
		$nouvelles_sections = array(
129
			"Description" => array("Introduction","Comment la reconna.+tre.+","Son histoire"),
122
			"Description" => array("Introduction","Comment la reconna.+tre.+","Son histoire"),
130
			"Usages" => array("Ses usages", "Ce qu.+il faut savoir.+"),
123
			"Usages" => array("Ses usages", "Ce qu.+il faut savoir.+"),
131
			"Écologie & habitat" => array(".+(?:cologie|habitat).+"), // groupe non-capturant avec (?:a|b)
124
			"Écologie & habitat" => array(".+(?:cologie|habitat).+"), // groupe non-capturant avec (?:a|b)
132
			"Sources" => array("Sources")
125
			"Sources" => array("Sources")
133
		);
126
		);
134
		
127
		
135
		$where_section = 'body NOT LIKE "';
128
		$where_section = 'body NOT LIKE "';
136
		$nouvelles_sections_k = array_keys($nouvelles_sections);
129
		$nouvelles_sections_k = array_keys($nouvelles_sections);
137
		foreach($nouvelles_sections_k as $nouvelle_section_k) {
130
		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
131
			// 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é)
132
			// 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)).'%';
133
			$where_section .= '%'.addslashes(ManipulationPage::convertirTexteAppliVersEncodageWiki($nouvelle_section_k)).'%';
141
		}
134
		}
142
		$where_section = $where_section.'"';
135
		$where_section = $where_section.'"';
143
			
136
			
144
		$this->wiki = Registre::get('wikiApi');
137
		$this->wiki = Registre::get('wikiApi');
145
		$requete = 'SELECT * FROM '.$this->wiki->GetConfigValue('table_prefix').'pages WHERE latest = "Y" '.
138
		$requete = 'SELECT * FROM '.$this->wiki->GetConfigValue('table_prefix').'pages WHERE latest = "Y" '.
146
						'AND tag LIKE "SmartFlore%nt%" '.
139
						'AND tag LIKE "SmartFlore%nt%" '.
147
						'AND '.$where_section;
140
						'AND '.$where_section;
148
 
141
 
149
		$pages = $this->wiki->LoadAll($requete);
142
		$pages = $this->wiki->LoadAll($requete);
150
		$pages_fmt = array();
143
		$pages_fmt = array();
151
		echo "Nombre de pages à migrer : ".count($pages)."\n";
144
		echo "Nombre de pages à migrer : ".count($pages)."\n";
152
 
145
 
153
		if(!empty($pages)) {
146
		if(!empty($pages)) {
154
			$manipulation = new ManipulationPage($this->wiki, $pages[0]);
147
			$manipulation = new ManipulationPage($this->wiki, $pages[0]);
155
			echo "Migration en cours... \n";
148
			echo "Migration en cours... \n";
156
			
149
			
157
			foreach($pages as &$page) {
150
			foreach($pages as &$page) {
158
				
151
				
159
				$page_fmt = array();
152
				$page_fmt = array();
160
				
153
				
161
				// On capte l'entête de la page situé avant la première section pour le recopier
154
				// 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)
155
				// dans les nouvelles pages (il contient les backlinks et les noms)
163
				$delim_entete = strpos($page["body"], "==== Introduction ====");
156
				$delim_entete = strpos($page["body"], "==== Introduction ====");
164
				if($delim_entete === false) {
157
				if($delim_entete === false) {
165
					$delim_entete = strpos($page["body"], "====Introduction====");
158
					$delim_entete = strpos($page["body"], "====Introduction====");
166
				}
159
				}
167
				// Attention l'entete est en iso, il faut le convertir manuellement
160
				// Attention l'entete est en iso, il faut le convertir manuellement
168
				$entete = $manipulation->convertirTexteWikiVersEncodageAppli(substr($page["body"], 0, $delim_entete));
161
				$entete = $manipulation->convertirTexteWikiVersEncodageAppli(substr($page["body"], 0, $delim_entete));
169
				
162
				
170
				// Par contre ici consulterPageSectionsFormatees est gentil et fait la conversion vers l'encodage de l'appli pour nous
163
				// Par contre ici consulterPageSectionsFormatees est gentil et fait la conversion vers l'encodage de l'appli pour nous
171
				$manipulation->consulterPageSectionsFormatees($page, implode(',', $sections));
164
				$manipulation->consulterPageSectionsFormatees($page, implode(',', $sections));
172
				
165
				
173
				// Fusion des anciennes sections dans les nouvelles
166
				// Fusion des anciennes sections dans les nouvelles
174
				foreach($nouvelles_sections as $nom_nouvelle_section => $sections_a_fusionner) {
167
				foreach($nouvelles_sections as $nom_nouvelle_section => $sections_a_fusionner) {
175
					$page_fmt[$nom_nouvelle_section] = '===='.$nom_nouvelle_section.'====';
168
					$page_fmt[$nom_nouvelle_section] = '===='.$nom_nouvelle_section.'====';
176
					foreach($sections_a_fusionner as $section_a_fusionner) {
169
					foreach($sections_a_fusionner as $section_a_fusionner) {
177
						if(isset($page['sections'][$section_a_fusionner])) {
170
						if(isset($page['sections'][$section_a_fusionner])) {
178
							$page_fmt[$nom_nouvelle_section] .= $page['sections'][$section_a_fusionner];
171
							$page_fmt[$nom_nouvelle_section] .= $page['sections'][$section_a_fusionner];
179
						}
172
						}
180
					}
173
					}
181
				}
174
				}
182
				
175
				
183
				$corps = $entete."\n".implode("\n", $page_fmt);
176
				$corps = $entete."\n".implode("\n", $page_fmt);
184
				$manipulation->ecrirePage($page["tag"], $corps);
177
				$manipulation->ecrirePage($page["tag"], $corps);
185
			}
178
			}
186
		}
179
		}
187
		echo "Migration effectuée \n";
180
		echo "Migration effectuée \n";
188
		// Le exit est là pour empecher l'affichage d'être pollué par les erreurs 
181
		// Le exit est là pour empecher l'affichage d'être pollué par les erreurs 
189
		// dûes à certaines antédiluviennités de wikini
182
		// dûes à certaines antédiluviennités de wikini
190
		exit;
183
		exit;
191
	}
184
	}
192
	
185
	
193
	// http://stackoverflow.com/questions/834303/startswith-and-endswith-functions-in-php
186
	// http://stackoverflow.com/questions/834303/startswith-and-endswith-functions-in-php
194
	protected function endsWith($haystack, $needle) {
187
	protected function endsWith($haystack, $needle) {
195
		// search forward starting from end minus needle length characters
188
		// search forward starting from end minus needle length characters
196
		return $needle === "" || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== FALSE);
189
		return $needle === "" || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== FALSE);
197
	}
190
	}
198
}
191
}