1 | <?php |
---|
2 | |
---|
3 | function action_importer_blog() { |
---|
4 | |
---|
5 | header('Content-Type: text/html; charset=utf-8'); |
---|
6 | include_spip('inc/minipres'); |
---|
7 | |
---|
8 | if (!autoriser('webmestre')) { |
---|
9 | echo minipres(); |
---|
10 | exit; |
---|
11 | } |
---|
12 | |
---|
13 | if ($_FILES |
---|
14 | AND $f = array_pop($_FILES) |
---|
15 | AND !$f['error']) { |
---|
16 | $content = file_get_contents($f['tmp_name']); |
---|
17 | } |
---|
18 | else { |
---|
19 | echo minipres('Erreur de fichier'); |
---|
20 | exit; |
---|
21 | |
---|
22 | } |
---|
23 | |
---|
24 | // Mode de stockage des liens mots<->articles |
---|
25 | $trouver_table = charger_fonction('trouver_table', 'base'); |
---|
26 | define('_MODE_MOTS', ($trouver_table('spip_mots_liens') ? 1 : 0)); |
---|
27 | define('_MODE_AUTEURS', ($trouver_table('spip_auteurs_liens') ? 1 : 0)); |
---|
28 | define('_MODE_FORUM', _MODE_MOTS); // je suis flemmard, là... |
---|
29 | |
---|
30 | |
---|
31 | echo install_debut_html('Import de '.$f['name']); |
---|
32 | |
---|
33 | include_spip('iterateur/data'); |
---|
34 | |
---|
35 | $r = importer_blogspot($content); |
---|
36 | |
---|
37 | if (!$r) |
---|
38 | echo "<h1>Import terminé avec succès</h1>"; |
---|
39 | else |
---|
40 | echo "<h1>Erreur : $r</h1>\n"; |
---|
41 | |
---|
42 | |
---|
43 | echo install_fin_html(); |
---|
44 | } |
---|
45 | |
---|
46 | |
---|
47 | function nettoyer_html($texte) { |
---|
48 | /* |
---|
49 | $texte = preg_replace(',<br ?/?><br ?/?>,i', "\n\n", $texte); |
---|
50 | $texte = preg_replace(',<br ?/?>,i', "\n_ ", $texte); |
---|
51 | $texte = preg_replace(',</div>,i', "\0\n\n", $texte); |
---|
52 | */ |
---|
53 | |
---|
54 | return $texte; |
---|
55 | } |
---|
56 | |
---|
57 | |
---|
58 | function importer_post($a, $rub =1) { |
---|
59 | $ref = "*".$a['id']; |
---|
60 | $s = sql_query($q = "SELECT id_article FROM spip_articles WHERE nom_site=".sql_quote((string)$ref)); |
---|
61 | if ($t = sql_fetch($s)) |
---|
62 | $id = $t['id_article']; |
---|
63 | else { |
---|
64 | $id = sql_insertq('spip_articles', array( |
---|
65 | 'nom_site' => $ref, |
---|
66 | 'statut' => 'publie' |
---|
67 | )); |
---|
68 | } |
---|
69 | |
---|
70 | if (!$id) { |
---|
71 | echo "erreur sur $ref"; |
---|
72 | return; |
---|
73 | } |
---|
74 | |
---|
75 | $texte = importer_texte($a['content']); |
---|
76 | |
---|
77 | $p = sql_updateq('spip_articles', |
---|
78 | array( |
---|
79 | 'titre' => $a['title'], |
---|
80 | 'texte' => $texte, |
---|
81 | 'date' => $a['date'], |
---|
82 | 'id_rubrique' => $rub, |
---|
83 | 'id_secteur' => $rub, |
---|
84 | 'lang' => 'fr', |
---|
85 | ), |
---|
86 | 'id_article='.$id |
---|
87 | ); |
---|
88 | |
---|
89 | |
---|
90 | if (_MODE_AUTEURS) { /* SPIP 3 */ |
---|
91 | sql_delete('spip_auteurs_liens', 'id_objet='.$id.' AND objet="article"'); |
---|
92 | if ($id_auteur = get_id_auteur($a['author'], $a['email'])) { |
---|
93 | sql_insertq('spip_auteurs_liens', array('id_objet'=>$id, 'id_auteur' => $id_auteur, 'objet' => 'article')); |
---|
94 | } |
---|
95 | } else { /* spip 2.1 */ |
---|
96 | sql_delete('spip_auteurs_articles', 'id_article='.$id); |
---|
97 | if ($id_auteur = get_id_auteur($a['author'], $a['email'])) { |
---|
98 | sql_insertq('spip_auteurs_articles', array('id_article'=>$id, 'id_auteur' => $id_auteur)); |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | if (_MODE_MOTS) { /* SPIP 3 */ |
---|
103 | sql_delete('spip_mots_liens', 'id_objet='.$id.' AND objet="article"'); |
---|
104 | if (is_array($a['terms'])) |
---|
105 | foreach($a['terms'] as $term) |
---|
106 | if ($id_mot = get_id_mot($term)) { |
---|
107 | sql_insertq('spip_mots_liens', array('id_objet'=>$id, 'id_mot' => $id_mot, 'objet' => 'article')); |
---|
108 | } |
---|
109 | } else { /* spip 2.1 */ |
---|
110 | sql_delete('spip_mots_articles', 'id_article='.$id); |
---|
111 | if (is_array($a['terms'])) |
---|
112 | foreach($a['terms'] as $term) |
---|
113 | if ($id_mot = get_id_mot($term)) { |
---|
114 | sql_insertq('spip_mots_articles', array('id_article'=>$id, 'id_mot' => $id_mot)); |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | echo "<dd><a href='"._DIR_RESTREINT."?exec=articles&id_article=$id'>article $id</a></dd>\n"; |
---|
119 | |
---|
120 | # var_dump($a, $p, $id, $id_auteur); |
---|
121 | # exit; |
---|
122 | } |
---|
123 | |
---|
124 | |
---|
125 | function importer_comment($a) { |
---|
126 | static $vu = array(); |
---|
127 | |
---|
128 | $ref_article = "*".$a['parent']; |
---|
129 | |
---|
130 | $s = sql_query('SELECT id_article AS id FROM spip_articles WHERE nom_site='._q($ref_article)); |
---|
131 | if (!$t = sql_fetch($s)) { |
---|
132 | echo "l'article $ref_article n'existe pas (encore?), on passe.\n"; |
---|
133 | return false; |
---|
134 | } |
---|
135 | $id_objet = $t['id']; |
---|
136 | |
---|
137 | $ref = '*'.$a['id']; |
---|
138 | |
---|
139 | $s = sql_query($q = "SELECT id_forum FROM spip_forum WHERE nom_site=".sql_quote((string)$ref)); |
---|
140 | if ($t = sql_fetch($s)) |
---|
141 | $id = $t['id_forum']; |
---|
142 | else { |
---|
143 | $id = sql_insertq('spip_forum', array( |
---|
144 | 'nom_site' => $ref, |
---|
145 | 'statut' => 'publie' |
---|
146 | )); |
---|
147 | } |
---|
148 | |
---|
149 | if (!$id) { |
---|
150 | echo "erreur sur $ref"; |
---|
151 | return; |
---|
152 | } |
---|
153 | |
---|
154 | $texte = importer_texte($a['content']); |
---|
155 | |
---|
156 | $f = array( |
---|
157 | 'titre' => '', ## $a['title'], sur blogspot le titre n'est que le debut du content |
---|
158 | 'texte' => $texte, |
---|
159 | 'date_heure' => $a['date'], |
---|
160 | 'date_thread' => $a['date'], |
---|
161 | 'auteur' => $a['author'], |
---|
162 | 'email_auteur' => $a['email'], |
---|
163 | ); |
---|
164 | |
---|
165 | if (_MODE_FORUM) { |
---|
166 | $f['objet'] = 'article'; |
---|
167 | $f['id_objet'] = $id_objet; |
---|
168 | } else { |
---|
169 | $f['id_article'] = $id_objet; |
---|
170 | } |
---|
171 | |
---|
172 | $p = sql_updateq('spip_forum', |
---|
173 | $f, |
---|
174 | 'id_forum='.$id |
---|
175 | ); |
---|
176 | |
---|
177 | echo "<dd><a href='"._DIR_RESTREINT."?exec=articles&id_article=$id_objet'>forum $id</a></dd>\n"; |
---|
178 | |
---|
179 | |
---|
180 | } |
---|
181 | |
---|
182 | |
---|
183 | |
---|
184 | function get_id_auteur($name, $email='') { |
---|
185 | static $mem = array(); |
---|
186 | |
---|
187 | if (!isset($mem[$name])) { |
---|
188 | $s = sql_query("SELECT id_auteur FROM spip_auteurs WHERE nom="._q($name)); |
---|
189 | if ($t = sql_fetch($s)) |
---|
190 | $id = $t['id_auteur']; |
---|
191 | else |
---|
192 | $id = sql_insertq('spip_auteurs', array( |
---|
193 | 'nom' => $name, |
---|
194 | 'statut' => '1comite', |
---|
195 | 'email' => $email |
---|
196 | )); |
---|
197 | |
---|
198 | $mem[$name] = $id; |
---|
199 | } |
---|
200 | |
---|
201 | return $mem[$name]; |
---|
202 | } |
---|
203 | |
---|
204 | function get_id_rubrique($name, $desc='') { |
---|
205 | static $mem = array(); |
---|
206 | |
---|
207 | if (!isset($mem[$name])) { |
---|
208 | $s = sql_query("SELECT id_rubrique FROM spip_rubriques WHERE titre="._q($name)." AND id_parent=0"); |
---|
209 | if ($t = sql_fetch($s)) |
---|
210 | $id = $t['id_rubrique']; |
---|
211 | else |
---|
212 | $id = sql_insertq('spip_rubriques', array( |
---|
213 | 'titre' => $name, |
---|
214 | 'texte' => $desc, |
---|
215 | 'statut' => 'publie', |
---|
216 | 'id_parent' => 0 |
---|
217 | )); |
---|
218 | |
---|
219 | $mem[$name] = $id; |
---|
220 | } |
---|
221 | |
---|
222 | return $mem[$name]; |
---|
223 | } |
---|
224 | |
---|
225 | |
---|
226 | function get_id_mot($name) { |
---|
227 | static $mem = array(); |
---|
228 | |
---|
229 | if (!isset($mem[$name])) { |
---|
230 | $s = sql_query("SELECT id_mot FROM spip_mots WHERE titre="._q($name)); |
---|
231 | if ($t = sql_fetch($s)) |
---|
232 | $id = $t['id_mot']; |
---|
233 | else |
---|
234 | $id = sql_insertq('spip_mots', array( |
---|
235 | 'titre' => $name, |
---|
236 | 'id_groupe' => '1', |
---|
237 | 'type' => 'tag', |
---|
238 | )); |
---|
239 | |
---|
240 | $mem[$name] = $id; |
---|
241 | } |
---|
242 | |
---|
243 | return $mem[$name]; |
---|
244 | } |
---|
245 | |
---|
246 | |
---|
247 | function importer_blogspot(&$content) { |
---|
248 | $it = new SimpleXmlIterator( |
---|
249 | str_replace('xmlns=', 'ns=',$content) |
---|
250 | ); |
---|
251 | |
---|
252 | foreach ($it->xpath('entry') as $key => $val) { |
---|
253 | $id = ((string)$val->id); |
---|
254 | |
---|
255 | |
---|
256 | # echo htmlspecialchars($txt = (string) $val->content); |
---|
257 | # echo "$key <pre>\n".htmlspecialchars(var_export(/*ObjectToArray*/($val),true))."</pre><hr />"; |
---|
258 | # if ($n++>100) exit; |
---|
259 | |
---|
260 | |
---|
261 | if (preg_match(',\.post-(.*)$,', $id, $r)) { |
---|
262 | $ref = $r[1]; |
---|
263 | |
---|
264 | $a = array('id' => $ref); |
---|
265 | |
---|
266 | echo "<dt>$ref</dt>\n"; |
---|
267 | echo "<dd>".htmlspecialchars($tit = (string) $val->title)."</dd>"; |
---|
268 | |
---|
269 | |
---|
270 | ## etablir le type (post / comment) de l'item |
---|
271 | $type = $val->xpath('category[@scheme=\'http://schemas.google.com/g/2005#kind\']'); |
---|
272 | $a['type'] = preg_replace(',^.*#,', '', (string) $type[0]->attributes()->term); |
---|
273 | |
---|
274 | |
---|
275 | ## etablir l'url |
---|
276 | if ( |
---|
277 | $link = $val->xpath('link[@rel=\'alternate\']')) { |
---|
278 | $a['link'] = preg_replace(',[?].*,', '', (string) $link[0]->attributes()->href); |
---|
279 | #var_export($link); |
---|
280 | } |
---|
281 | |
---|
282 | ## si c'est un commentaire, aller chercher l'article parent |
---|
283 | if ($a['type'] == 'comment') { |
---|
284 | $link = $val->xpath('link[@rel=\'self\']'); |
---|
285 | $link = $link[0]->attributes()->href; |
---|
286 | preg_match(',(\d+)/comments/,', $link, $r ); |
---|
287 | $a['parent'] = $r[1]; |
---|
288 | } |
---|
289 | |
---|
290 | |
---|
291 | $terms = array(); |
---|
292 | foreach($val->xpath('category[@scheme="http://www.blogger.com/atom/ns#"]') as $t) |
---|
293 | $terms[] = (string)$t->attributes()->term; |
---|
294 | |
---|
295 | if ($terms) $a['terms'] = $terms; |
---|
296 | |
---|
297 | # var_dump($val->xpath('content')->attributes()->type); |
---|
298 | |
---|
299 | $a['title'] = nettoyer_html((string) $val->title); |
---|
300 | $a['content'] = nettoyer_html((string) $val->content); |
---|
301 | |
---|
302 | $a['date'] = date('Y-m-d H:i:s', strtotime((string) $val->published)); |
---|
303 | $a['author'] = (string) $val->author->name; |
---|
304 | $a['email'] = (string) $val->author->email; |
---|
305 | if($a['email'] == 'noreply@blogger.com') |
---|
306 | $a['email'] = ''; |
---|
307 | |
---|
308 | switch($a['type']) { |
---|
309 | case "post": |
---|
310 | $rub = get_id_rubrique( |
---|
311 | $settings['blog_name'], $settings['blog_description']); |
---|
312 | importer_post($a, $rub); |
---|
313 | break; |
---|
314 | |
---|
315 | case "comment": |
---|
316 | importer_comment($a); |
---|
317 | break; |
---|
318 | |
---|
319 | default: |
---|
320 | echo "type inconnu: ".$type."\n"; |
---|
321 | break; |
---|
322 | } |
---|
323 | } |
---|
324 | else { |
---|
325 | echo "<s>$id</s> <br />\n"; |
---|
326 | if (preg_match(',\.settings\.(\w+)$,', $id, $r)) { |
---|
327 | $settings[strtolower($r[1])] = (string) $val->content; |
---|
328 | } |
---|
329 | } |
---|
330 | } |
---|
331 | |
---|
332 | } |
---|
333 | |
---|
334 | |
---|
335 | |
---|
336 | function importer_texte($t) { |
---|
337 | |
---|
338 | ecrire_fichier('../tmp/x.html', $t); |
---|
339 | #$a = `/opt/local/bin/pandoc ../tmp/x.html -t mediawiki`; |
---|
340 | #echo "<pre>".htmlspecialchars($a)."</pre>\n"; |
---|
341 | #exit; |
---|
342 | |
---|
343 | |
---|
344 | # italiques |
---|
345 | #<span style="font-style: italic;">...</span> |
---|
346 | $t = preg_replace(',<span style="font-style: italic;">(.*)</span>,Ums', '{\1}', $t); |
---|
347 | $t = preg_replace(',<i>(.*)</i>,Ums', '{\1}', $t); |
---|
348 | |
---|
349 | # images |
---|
350 | foreach (extraire_balises($t, 'a') as $l) { |
---|
351 | if (preg_match(',^<a [^>]*><img [^>]*></a>$,Uims', $l) |
---|
352 | AND $href = extraire_attribut($l, 'href') |
---|
353 | AND $src = extraire_attribut(extraire_balise($l, 'img'), 'src') |
---|
354 | #AND $height = extraire_attribut(extraire_balise($l, 'img'), 'height') |
---|
355 | #AND $width = extraire_attribut(extraire_balise($l, 'img'), 'width') |
---|
356 | AND preg_match(',\.(jpg|gif|png)$,i', $src, $r) |
---|
357 | AND $extension = $r[1] |
---|
358 | ) { |
---|
359 | $doc = importer_doc(array('fichier' => $src, 'extension' => strtolower($extension), 'distant' => 'oui', 'mode' => 'image')); |
---|
360 | |
---|
361 | if (preg_match('@<div style="text-align: center;">'.preg_quote($l,'@').'((?:.*?\n){0,5})</div>@ms', $t, $r)) { |
---|
362 | $legende = trim($r[1]); |
---|
363 | sql_updateq('spip_documents', array('descriptif' => $legende), 'id_document='.$doc); |
---|
364 | $repl = "\n".'<doc'.$doc.'|center>'."\n"; |
---|
365 | $t = str_replace($r[0], $repl, $t); |
---|
366 | } |
---|
367 | else { |
---|
368 | $repl = "\n".'<img'.$doc.'|center>'."\n"; |
---|
369 | $t = str_replace($l, $repl, $t); |
---|
370 | } |
---|
371 | } else |
---|
372 | if (preg_match(',^<a [^>]*>(.*)</a>$,Uims', $l, $r) |
---|
373 | AND $href = extraire_attribut($l, 'href') |
---|
374 | ) { |
---|
375 | $repl = '['.$r[1].'->'.trim($href).']'; |
---|
376 | $t = str_replace($l, $repl, $t); |
---|
377 | } |
---|
378 | } |
---|
379 | |
---|
380 | # sauts de lignes |
---|
381 | $t = preg_replace(',<br />,', "\n_ ", $t); |
---|
382 | $t = preg_replace(',{\n_ },', "\n_ ", $t); |
---|
383 | $t = preg_replace(',\n_ \n_ ,', "\n\n", $t); |
---|
384 | |
---|
385 | ## videos |
---|
386 | |
---|
387 | # youtube |
---|
388 | foreach (extraire_balises($t, 'object') as $l) { |
---|
389 | if (preg_match(',http://(www\.)?youtube.com/v/[^"\']*,', $l, $r) |
---|
390 | AND $a = extraire_balise($l, 'embed') |
---|
391 | AND $height=extraire_attribut($a, 'height') |
---|
392 | AND $width=extraire_attribut($a, 'width') |
---|
393 | ) |
---|
394 | { |
---|
395 | $doc = importer_doc(array('fichier' => $r[0], 'hauteur' => $height, 'largeur' => $width, 'extension' => 'swf', 'distant' => 'oui', 'mode' => 'document')); |
---|
396 | $t = str_replace($l, '<emb'.$doc.'|center>', $t); |
---|
397 | } |
---|
398 | } |
---|
399 | |
---|
400 | |
---|
401 | $t = preg_replace(",\n_ </div>,S", "</div>\n_ ", $t); |
---|
402 | $t = preg_replace(',<div style="text-align: center;"></div>,S', '', $t); |
---|
403 | |
---|
404 | $t = preg_replace(',(\n_ )*<div class="blogger-post-footer">.*$,Sms', '', $t); |
---|
405 | $t = str_replace("\n\n</span>", "</span>\n\n", $t); |
---|
406 | $t = str_replace("\n\n</div>", "</div>\n\n", $t); |
---|
407 | $t = str_replace("\n\n_ ", "\n\n\n", $t); |
---|
408 | |
---|
409 | |
---|
410 | |
---|
411 | return $t; |
---|
412 | |
---|
413 | } |
---|
414 | |
---|
415 | |
---|
416 | function importer_doc($doc) { |
---|
417 | $s = sql_query($q = "SELECT id_document FROM spip_documents WHERE fichier=".sql_quote($doc['fichier'])); |
---|
418 | if ($t = sql_fetch($s)) |
---|
419 | $id = $t['id_document']; |
---|
420 | else { |
---|
421 | $id = sql_insertq('spip_documents', array( |
---|
422 | 'fichier' => $doc['fichier'], |
---|
423 | 'date' => date('Y-m-d H:i:s') |
---|
424 | )); |
---|
425 | } |
---|
426 | |
---|
427 | $p = sql_updateq('spip_documents', |
---|
428 | $doc, |
---|
429 | 'id_document='.$id |
---|
430 | ); |
---|
431 | |
---|
432 | return $id; |
---|
433 | } |
---|