Subversion Repositories Applications.annuaire

Rev

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

Rev Author Line No. Line
610 delphine 1
<?php
2
// Encodage : UTF-8
3
// +-------------------------------------------------------------------------------------------------------------------+
4
/**
5
* Migration des utilisateurs vers wordpress
6
*
7
* Description : classe permettant de migrer les profils de l'annuaire vers les profils wordpress
8
* Utilisation : php cli.php migrationwp -a tous
614 delphine 9
* /usr/local/bin/php -d memory_limit=4000M cli.php migrationwp -a tous
612 delphine 10
* vérifier le nom de la base et le préfixe des tables définis dans $basewp
610 delphine 11
*
12
//Auteur original :
13
* @author       Aurélien PERONNET <jpm@tela-botanica.org>
14
* @copyright	Tela-Botanica 1999-2014
15
* @licence		GPL v3 & CeCILL v2
16
* @version		$Id$
17
*/
18
 
19
class Migrationwp extends Script {
614 delphine 20
	private $basewp = "wordpress.site_";
21
	private $table = "site_";
610 delphine 22
 
23
	public function executer() {
24
		$this->bdd = new Bdd();
25
		//$this->bdd->setAttribute(MYSQL_ATTR_USE_BUFFERED_QUERY, true);
26
		// évite les erreurs 2006 "MySQL has gone away"
27
		$this->bdd->executer("SET wait_timeout=300");
28
 
29
		$cmd = $this->getParametre('a');
30
		$this->mode_verbeux = $this->getParametre('v');
31
 
32
		$retour = array();
33
 
34
		switch($cmd) {
35
			case "tous":
36
				$retour = $this->migrerUtilisateur();
37
				$retour = $this->migrerUtilisateurMeta();
614 delphine 38
				$retour = $this->migrerUtilisateurProfil();
610 delphine 39
				$retour = $this->migrerUtilisateurActivite();
40
				break;
41
			case "utilisateur": //liste wordpress
42
				$retour = $this->migrerUtilisateur();
43
				break;
44
			case "meta": //role
45
				$retour = $this->migrerUtilisateurMeta();
46
				break;
47
			case "profil":
48
				$retour = $this->migrerUtilisateurProfil();
49
				break;
50
			case "activite": // obligatoire pour affichage
51
				$retour = $this->migrerUtilisateurActivite();
52
				break;
614 delphine 53
				case "actualiteTout" :
54
					$retour = $this->migrerUtilisateurActualites();
55
					$retour .= $this->migrerUtilisateurActualitesRubrique();
56
					$retour .= $this->migrerUtilisateurActualitesCommentaire();
57
					break;
58
			case "actualite" :
59
				$retour = $this->migrerUtilisateurActualites();
60
				break;
61
			case "actualiteRubrique" :
62
				$retour = $this->migrerUtilisateurActualitesRubrique();
63
				break;
64
			case "actualiteComm" :
65
				$retour = $this->migrerUtilisateurActualitesCommentaire();
66
				break;
67
			default: break;
610 delphine 68
		}
69
 
70
		if($this->mode_verbeux) {
71
			// echo pour que bash capte la sortie et stocke dans le log
72
			//echo 'Identifiants des mails traites : '.implode(',', $retour)."--";
73
		}
74
	}
75
 
76
	private function migrerUtilisateur() {
614 delphine 77
		$requete = "INSERT INTO ".$this->basewp."users
610 delphine 78
				(`ID`, `user_login`, `user_pass`, `user_nicename`, `user_email`, `user_url`, `user_registered`, `user_status`, `display_name`)
614 delphine 79
				SELECT `U_ID`, `U_MAIL`, `U_PASSWD`,concat(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(lower(concat(`U_SURNAME`,'-',`U_NAME`,'-')),' ',''),'\'',''),'é','e'),'è','e'),'ï','i'),'ü','u'),'ø',''),'œ','oe'),'ë','e'),'ç','c'),cast(`U_ID` as char)),
80
				`U_MAIL` as mail,  '' as user_url, `U_DATE`, '0', concat(`U_SURNAME`,' ',`U_NAME`)  FROM tela_prod_v4.`annuaire_tela`";
610 delphine 81
		$retour = $this->bdd->executer($requete);
82
		echo 'Il y a '.count($retour).' utilisateurs migrés '."--";
83
		return $retour;
84
	}
85
 
86
 
614 delphine 87
	//TODO encoder nick/first/last name pour '
610 delphine 88
	private function migrerUtilisateurMeta() {
89
		$retour = array();
614 delphine 90
		$requete = "SELECT `U_ID`, `U_NAME`, `U_SURNAME` FROM `annuaire_tela`;";
610 delphine 91
		$utilisateurs = $this->bdd->recupererTous($requete);
92
		foreach ($utilisateurs as $utilisateur) {
614 delphine 93
			// _access est pour définir les catégories d'articles que l'utilisateur pourra écrire
94
			$requete_insert = "INSERT INTO ".$this->basewp."usermeta (`user_id`, `meta_key`, `meta_value`) VALUES
610 delphine 95
					({$utilisateur['U_ID']}, 'last_activity', '2016-05-18 15:38:18'),
614 delphine 96
					({$utilisateur['U_ID']}, 'first_name', {$this->bdd->proteger($utilisateur['U_SURNAME'])}),
97
					({$utilisateur['U_ID']}, 'last_name', {$this->bdd->proteger($utilisateur['U_NAME'])}),
610 delphine 98
					({$utilisateur['U_ID']}, 'description', ''),
99
					({$utilisateur['U_ID']}, 'rich_editing', 'true'),
100
					({$utilisateur['U_ID']}, 'comment_shortcuts', 'false'),
101
					({$utilisateur['U_ID']}, 'admin_color', 'fresh'),
102
					({$utilisateur['U_ID']}, 'use_ssl', '0'),
103
					({$utilisateur['U_ID']}, 'show_admin_bar_front', 'true'),
614 delphine 104
					({$utilisateur['U_ID']}, '".$this->basewp."capabilities', 'a:1:{s:11:\"contributor\";b:1;}'),
105
					({$utilisateur['U_ID']}, '".$this->basewp."user_level', '1'),
610 delphine 106
					({$utilisateur['U_ID']}, 'dismissed_wp_pointers', ''),
107
					({$utilisateur['U_ID']}, 'wp_dashboard_quick_press_last_post_id', '63'),
108
					({$utilisateur['U_ID']}, '_restrict_media', '1'),
614 delphine 109
					({$utilisateur['U_ID']}, '_access', 'a:4:{i:0;s:1:\"2\";i:1;s:1:\"5\";i:2;s:1:\"6\";i:3;s:1:\"7\";}'),
110
					({$utilisateur['U_ID']}, 'bp_xprofile_visibility_levels', 'a:12:{i:1;s:6:\"public\";i:60;s:6:\'public\';i:61;s:6:\'public\';i:49;s:6:\'public\';i:55;s:6:\'public\';i:48;s:6:\'public\';i:62;s:6:\'public\';i:63;s:6:\'public\';i:68;s:6:\'public\';i:76;s:6:\'public\';i:120;s:6:\'public\';i:81;s:6:\'public\';}');";
610 delphine 111
			$retour[] = $this->bdd->executer($requete_insert);
112
		}
113
		// echo pour que bash capte la sortie et stocke dans le log
114
		//echo 'Il y a '.count($utilisateurs).' utilisateurs '."--";
115
		//print_r($utilisateurs);
116
		return $retour;
117
	}
118
 
119
	private function migrerUtilisateurActivite() {
120
		$retour = array();
121
		$requete = "SELECT `U_ID`, `U_NAME`, `U_SURNAME` FROM `annuaire_tela`;";
122
		$utilisateurs = $this->bdd->recupererTous($requete);
123
		foreach ($utilisateurs as $utilisateur) {
614 delphine 124
			$requete_insert = "INSERT INTO ".$this->basewp."bp_activity
610 delphine 125
					(`id`, `user_id`, `component`, `type`, `action`, `content`, `primary_link`, `item_id`, `secondary_item_id`, `date_recorded`, `hide_sitewide`, `mptt_left`, `mptt_right`, `is_spam`)
126
					VALUES (NULL, {$utilisateur['U_ID']}, 'members', 'last_activity', '', '', '', '0', NULL, '2016-05-19 15:06:16', '0', '0', '0', '0');";
127
			$retour[] = $this->bdd->executer($requete_insert);
128
		}
129
		// echo pour que bash capte la sortie et stocke dans le log
130
		//echo 'Il y a '.count($utilisateurs).' utilisateurs '."--";
131
		//print_r($utilisateurs);
132
		return $retour;
133
	}
134
 
135
	private function migrerUtilisateurProfil() {
614 delphine 136
		$retour = array();
137
		$requete = "SELECT `U_ID`, `U_NAME`, `U_SURNAME`, U_WEB, `U_CITY`, `U_COUNTRY`, pays, `U_NIV`,  `LABEL_NIV` FROM `annuaire_tela`
138
				left join (select  `amo_nom` as pays,  `amo_abreviation` FROM `annu_meta_ontologie` WHERE  `amo_ce_parent` = 1074) liste_pays  on `amo_abreviation` = `U_COUNTRY`
139
				LEFT JOIN `annuaire_LABEL_NIV` ON `ID_LABEL_NIV` = `U_NIV`;";
140
		$utilisateurs = $this->bdd->recupererTous($requete);
141
		$requete_supp = "SELECT *  FROM `annu_meta_valeurs` WHERE `amv_ce_colonne` in (2,137, 99, 125) and (amv_valeur != ''  and amv_valeur != 0)";
142
		$infos_supp = $this->bdd->recupererTous($requete_supp);
143
		$codes_langues = array("30842"=>"Anglais",
144
				"30843"=>"Allemand",
145
				"30844"=>"Italien",
146
				"30845"=>"Espagnol",
147
				"30846"=>"Arabe",
148
				"30847"=>"Chinois",
149
				"30848"=>"Russe");
150
		foreach ($infos_supp as $infos) {
151
			if ($infos['amv_ce_colonne'] == 2) {
152
				//exemple a:3:{i:0;s:7:"Anglais";i:1;s:8:"Espagnol";i:2;s:7:"Italien";}
153
				$langues = explode(";;", $infos['amv_valeur']);
154
				$valeur = "a:".count($langues).':{';
155
				foreach ($langues as $n=>$langue) {
156
					$valeur .= 'i:'.$n.';s:'.strlen($codes_langues[$langue]).':"'.$codes_langues[$langue].'";';
157
				}
158
				$valeur .='}';
159
				$supp[$infos['amv_cle_ligne']][$infos['amv_ce_colonne']] = $valeur;
160
			} else {
161
				$supp[$infos['amv_cle_ligne']][$infos['amv_ce_colonne']] = $infos['amv_valeur'];
162
			}
163
		}
164
 
165
		$correspondance_categories = array("99"=>"1",
166
				"137"=>"2",
167
				"125"=>"11",
168
				"2"=>"13");
169
		foreach ($utilisateurs as $utilisateur) {
170
			$requete_insert = "INSERT INTO ".$this->basewp."bp_xprofile_data (`field_id`, `user_id`, `value`, `last_updated`) VALUES
171
				('3', {$utilisateur['U_ID']}, {$this->bdd->proteger($utilisateur['pays'])}, '2016-05-19 15:06:16'),
172
				('4', {$utilisateur['U_ID']}, {$this->bdd->proteger($utilisateur['U_CITY'])}, '2016-05-19 15:06:16'),
173
				('9', {$utilisateur['U_ID']}, {$this->bdd->proteger($utilisateur['U_NAME'])}, '2016-05-19 15:06:16'),
174
				('10', {$utilisateur['U_ID']}, {$this->bdd->proteger($utilisateur['U_SURNAME'])}, '2016-05-19 15:06:16'),
175
				('12', {$utilisateur['U_ID']}, {$this->bdd->proteger($utilisateur['LABEL_NIV'])}, '2016-05-19 15:06:16'),
176
				('21', {$utilisateur['U_ID']}, {$this->bdd->proteger($utilisateur['U_WEB'])}, '2016-05-19 15:06:16')";
177
			if (isset($supp[$utilisateur['U_ID']])) {
178
				foreach ($supp[$utilisateur['U_ID']] as $num=>$val){
179
					$requete_insert .= ",({$correspondance_categories[$num]}, {$utilisateur['U_ID']}, {$this->bdd->proteger($val)}, '2016-05-19 15:06:16')";
180
				}
181
			}
182
			$requete_insert .= ";";
183
			$retour[] = $this->bdd->executer($requete_insert);
184
		}
185
		return $retour;
610 delphine 186
	}
187
 
614 delphine 188
	private function migrerActualites() {
189
		$retour = array();
190
		/*INSERT INTO `wp4_posts`
191
		 SELECT spip_articles.`id_article` as ID, `id_auteur` as post_author, `date` as post_date, `date` as post_date_gmt,
192
		 replace(replace(replace(replace(replace(replace(replace(replace(replace(convert( convert( texte USING latin1 ) USING utf8 ),'{{{{',''), '}}}}', '<!--more-->'), '{{{','<h2>'), '}}}', '</h2>'), '{{', '<strong>'), '}}', '</strong>'), '{', '<em>'), '}', '</em>'), '_ ', '') as post_content,
193
		 `titre` as post_title,  "" as post_excerpt, replace(replace(replace(replace(replace(`statut`,'poubelle', 'trash'),'publie', 'publish'), 'prepa', 'private'), 'prop', 'pending'), 'refuse', 'trash') as post_status,  "open" as comment_status, "open" as ping_status, "" as post_password, spip_articles.`id_article` as post_name, "" as to_ping, "" as pinged, `date_modif` as post_modified,`date_modif` as post_modified_gmt, "" as post_content_filtered, "" as post_parent,
194
		 concat("http://tela-botanica.net/wpsite/actu",spip_articles.`id_article`) as guid, "0" as menu_order, "post" as post_type, "" as post_mime_type, "" as comment_count FROM tela_prod_spip_actu.`spip_articles` left join tela_prod_spip_actu.spip_auteurs_articles on spip_auteurs_articles.`id_article` =  spip_articles.`id_article` WHERE id_rubrique in (22,54,70,30,19,51)
195
		*/
196
		$requete = "SELECT spip_articles.`id_article` as ID, `id_auteur` as post_author, `date` as post_date, `date` as post_date_gmt,
197
			replace(replace(replace(replace(replace(replace(replace(replace(replace(convert( convert( texte USING latin1 ) USING utf8 ),'{{{{',''), '}}}}', '<!--more-->'), '{{{','<h2>'), '}}}', '</h2>'), '{{', '<strong>'), '}}', '</strong>'), '{', '<em>'), '}', '</em>'), '_ ', '') as post_content,
198
			`titre` as post_title,  \"\" as post_excerpt, replace(replace(replace(replace(replace(`statut`,'poubelle', 'trash'),'publie', 'publish'), 'prepa', 'private'), 'prop', 'pending'), 'refuse', 'trash') as post_status,  \"open\" as comment_status, \"open\" as ping_status, \"\" as post_password, spip_articles.`id_article` as post_name, \"\" as to_ping, \"\" as pinged, `date_modif` as post_modified,`date_modif` as post_modified_gmt, \"\" as post_content_filtered, \"\" as post_parent,
199
			concat(\"http://tela-botanica.net/wpsite/actu\",spip_articles.`id_article`) as guid, \"0\" as menu_order, \"post\" as post_type, \"\" as post_mime_type, \"\" as comment_count FROM tela_prod_spip_actu.`spip_articles` left join tela_prod_spip_actu.spip_auteurs_articles on spip_auteurs_articles.`id_article` =  spip_articles.`id_article` WHERE id_rubrique in (22,54,70,30,19,51)";
200
		$articles = $this->bdd->recupererTous($requete);
201
		$requete_doc = "SELECT d.`id_document`, `fichier`, `id_article` FROM `spip_documents` d  left join spip_documents_articles da on da.`id_document` = d.`id_document`";
202
		$documents = $this->bdd->recupererTous($requete_doc);
203
		foreach ($documents as $doc) {
204
			$doc_loc[$doc['id_document']] = $doc['fichier'];
205
		}
206
		foreach ($articles as $article) {
207
			$article['post_content'] = preg_replace("\[(.*)->(.*)\]", '<a href="\2" target="_blank">\1</a>', $article['post_content']);
208
			//$images = preg_grep("\<img([0-9]*)\|[a-z]*\>", $article['post_content']);
209
			$article['post_content'] = preg_replace("\<img([0-9]*)\|[a-z]*\>", '<img src="'.$doc_loc['${1}'].'" />', $article['post_content']);
210
			$insert[] = "(".implode($article, ', ').")";
211
		}
212
		$requete_insert = "INSERT INTO `wp4_posts` VALUES ".implode($insert, ', ').";";
213
		$retour[] = $this->bdd->executer($requete_insert);
214
		echo 'Il y a '.count($retour).' actualités migrées '."--";
215
		return $retour;
216
	}
217
 
218
	private function migrerActualitesRubrique() {
219
		$requete = "INSERT INTO ".$this->basewp."term_relationships`(`object_id`, `term_taxonomy_id`)
220
			SELECT `id_article`, replace(replace(replace(replace(replace(replace(`id_rubrique`, 22, 20), 54, 21), 30, 23), 19, 24), 51, 25), 70, 22)
221
			FROM `spip_articles` WHERE id_rubrique in (22,54,70,30,19,51)";
222
 
223
		$retour = $this->bdd->executer($requete);
224
		echo 'Il y a '.count($retour).' actualités migrées '."--";
225
		return $retour;
226
	}
227
 
228
	private function migrerActualitesCommentaires() {
229
		$requete = "INSERT INTO ".$this->basewp.'comments`(`comment_ID`, `comment_post_ID`, `comment_author`, `comment_author_email`, `comment_author_url`, `comment_author_IP`, `comment_date`, `comment_date_gmt`, `comment_content`, `comment_karma`, `comment_approved`, `comment_agent`, `comment_type`, `comment_parent`, `user_id`)
230
			SELECT `id_forum` , `id_article` , `auteur` , `email_auteur` , "" AS url, `ip` , `date_heure` , `date_heure` AS gmt, `texte` , "0" AS karma, replace(`statut`, "publie", "1") , "" AS agent, "" AS
231
			TYPE , `id_parent` , `id_auteur`
232
			FROM tela_prod_spip_actu.`spip_forum`
233
			WHERE id_article in (SELECT `id_article` FROM tela_prod_spip_actu.`spip_articles` WHERE id_rubrique in (22,54,70,30,19,51))';
234
 
235
		$retour = $this->bdd->executer($requete);
236
		echo 'Il y a '.count($retour).' actualités migrées '."--";
237
		return $retour;
238
	}
239
 
240
	private function migrerActualitesLogo() {
241
		$requete = "INSERT INTO ".$this->basewp."term_relationships`(`object_id`, `term_taxonomy_id`)
242
			SELECT `id_article`, replace(replace(replace(replace(replace(replace(`id_rubrique`, 22, 20), 54, 21), 30, 23), 19, 24), 51, 25), 70, 22)
243
			FROM `spip_articles` WHERE id_rubrique in (22,54,70,30,19,51)";
244
 
245
		$retour = $this->bdd->executer($requete);
246
		echo 'Il y a '.count($retour).' actualités migrées '."--";
247
		return $retour;
248
	}
249
 
610 delphine 250
}
614 delphine 251
?>