Subversion Repositories Sites.tela-botanica.org

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 david 1
<?php
2
 
3
/***************************************************************************\
4
 *  SPIP, Systeme de publication pour l'internet                           *
5
 *                                                                         *
6
 *  Copyright (c) 2001-2005                                                *
7
 *  Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James  *
8
 *                                                                         *
9
 *  Ce programme est un logiciel libre distribue sous licence GNU/GPL.     *
10
 *  Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne.   *
11
\***************************************************************************/
12
 
13
 
14
//
15
// Ce fichier ne sera execute qu'une fois
16
if (defined("_ECRIRE_INC_REVISIONS")) return;
17
define("_ECRIRE_INC_REVISIONS", "1");
18
 
19
$GLOBALS['agregation_versions'] = 10;
20
 
21
function separer_paras($texte, $paras = "") {
22
	if (!$paras) $paras = array();
23
	while (preg_match("/(\r\n?){2,}|\n{2,}/", $texte, $regs)) {
24
		$p = strpos($texte, $regs[0]) + strlen($regs[0]);
25
		$paras[] = substr($texte, 0, $p);
26
		$texte = substr($texte, $p);
27
	}
28
	if ($texte) $paras[] = $texte;
29
	return $paras;
30
}
31
 
32
function replace_fragment($id_article, $version_min, $version_max, $id_fragment, $fragment) {
33
	global $flag_gz;
34
 
35
	$fragment = serialize($fragment);
36
	$compress = 0;
37
	if ($flag_gz) {
38
		$s = gzcompress($fragment);
39
		if (strlen($s) < strlen($fragment)) {
40
			//echo "gain gz: ".(100 - 100 * strlen($s) / strlen($fragment))."%<br />";
41
			$compress = 1;
42
			$fragment = $s;
43
		}
44
	}
45
	// Attention a bien echapper le $fragment qui est en binaire
46
	return "($id_article, $version_min, $version_max, $id_fragment, $compress, '"
47
		.mysql_escape_string($fragment)."')";
48
}
49
 
50
function exec_replace_fragments($replaces) {
51
	if (count($replaces)) {
52
		$query = "REPLACE spip_versions_fragments (id_article, version_min, version_max, id_fragment, compress, fragment) ".
53
			"VALUES ".join(", ", $replaces);
54
		spip_query($query);
55
	}
56
}
57
function exec_delete_fragments($id_article, $deletes) {
58
	if (count($deletes)) {
59
		$query = "DELETE FROM spip_versions_fragments WHERE id_article=$id_article AND ((".
60
			join(") OR (", $deletes)."))";
61
		spip_query($query);
62
	}
63
}
64
 
65
 
66
//
67
// Ajouter les fragments de la derniere version (tableau associatif id_fragment => texte)
68
//
69
function ajouter_fragments($id_article, $id_version, $fragments) {
70
	global $flag_gz, $agregation_versions;
71
 
72
	$replaces = array();
73
	foreach ($fragments as $id_fragment => $texte) {
74
		$nouveau = true;
75
		// Recuperer la version la plus recente
76
		$query = "SELECT compress, fragment, version_min, version_max FROM spip_versions_fragments ".
77
			"WHERE id_article=$id_article AND id_fragment=$id_fragment AND version_min<=$id_version ".
78
			"ORDER BY version_min DESC LIMIT 0,1";
79
		$result = spip_query($query);
80
		if ($row = spip_fetch_array($result)) {
81
			$fragment = $row['fragment'];
82
			$version_min = $row['version_min'];
83
			if ($row['compress'] > 0) $fragment = gzuncompress($fragment);
84
			$fragment = unserialize($fragment);
85
			if (is_array($fragment)) {
86
				unset($fragment[$id_version]);
87
				// Si le fragment n'est pas trop gros, prolonger celui-ci
88
				$nouveau = count($fragment) >= $agregation_versions
89
					&& strlen($row['fragment']) > 1000;
90
			}
91
		}
92
		if ($nouveau) {
93
			$fragment = array($id_version => $texte);
94
			$version_min = $id_version;
95
		}
96
		else {
97
			// Ne pas dupliquer les fragments non modifies
98
			$modif = true;
99
			for ($i = $id_version - 1; $i >= $version_min; $i--) {
100
				if (isset($fragment[$i])) {
101
					$modif = ($fragment[$i] != $texte);
102
					break;
103
				}
104
			}
105
			if ($modif) $fragment[$id_version] = $texte;
106
		}
107
 
108
		// Preparer l'enregistrement du fragment
109
		$replaces[] = replace_fragment($id_article, $version_min, $id_version, $id_fragment, $fragment);
110
	}
111
 
112
	exec_replace_fragments($replaces);
113
}
114
 
115
//
116
// Supprimer tous les fragments d'un article lies a un intervalle de versions
117
// (essaie d'eviter une trop grande fragmentation)
118
//
119
function supprimer_fragments($id_article, $version_debut, $version_fin) {
120
	global $flag_gz, $agregation_versions;
121
 
122
	$replaces = array();
123
	$deletes = array();
124
 
125
	// D'abord, vider les fragments inutiles
126
	$query = "DELETE FROM spip_versions_fragments WHERE id_article=$id_article ".
127
		"AND version_min>=$version_debut AND version_max<=$version_fin";
128
	spip_query($query);
129
 
130
	// Fragments chevauchant l'ensemble de l'intervalle, s'ils existent
131
	$query = "SELECT id_fragment, compress, fragment, version_min, version_max FROM spip_versions_fragments ".
132
		"WHERE id_article=$id_article AND version_min<$version_debut AND version_max>$version_fin";
133
	$result = spip_query($query);
134
 
135
	while ($row = spip_fetch_array($result)) {
136
		$id_fragment = $row['id_fragment'];
137
		$fragment = $row['fragment'];
138
		if ($row['compress'] > 0) $fragment = gzuncompress($fragment);
139
		$fragment = unserialize($fragment);
140
		for ($i = $version_fin; $i >= $version_debut; $i--) {
141
			if (isset($fragment[$i])) {
142
				// Recopier le dernier fragment si implicite
143
				if (!isset($fragment[$version_fin + 1]))
144
					$fragment[$version_fin + 1] = $fragment[$i];
145
				unset($fragment[$i]);
146
			}
147
		}
148
 
149
		$replaces[] = replace_fragment($id_article,
150
			$row['version_min'], $row['version_max'], $id_fragment, $fragment);
151
	}
152
 
153
	// Fragments chevauchant le debut de l'intervalle, s'ils existent
154
	$query = "SELECT id_fragment, compress, fragment, version_min, version_max FROM spip_versions_fragments ".
155
		"WHERE id_article=$id_article AND version_min<$version_debut ".
156
		"AND version_max>=$version_debut AND version_max<=$version_fin";
157
	$result = spip_query($query);
158
 
159
	$deb_fragment = array();
160
	while ($row = spip_fetch_array($result)) {
161
		$id_fragment = $row['id_fragment'];
162
		$fragment = $row['fragment'];
163
		$version_min = $row['version_min'];
164
		$version_max = $row['version_max'];
165
		if ($row['compress'] > 0) $fragment = gzuncompress($fragment);
166
		$fragment = unserialize($fragment);
167
		for ($i = $version_debut; $i <= $version_max; $i++) {
168
			if (isset($fragment[$i])) unset($fragment[$i]);
169
		}
170
 
171
		// Stocker temporairement le fragment pour agregation
172
		$deb_fragment[$id_fragment] = $fragment;
173
		// Ajuster l'intervalle des versions
174
		$deb_version_min[$id_fragment] = $version_min;
175
		$deb_version_max[$id_fragment] = $version_debut - 1;
176
	}
177
 
178
	// Fragments chevauchant la fin de l'intervalle, s'ils existent
179
	$query = "SELECT id_fragment, compress, fragment, version_min, version_max FROM spip_versions_fragments ".
180
		"WHERE id_article=$id_article AND version_max>$version_fin ".
181
		"AND version_min>=$version_debut AND version_min<=$version_fin";
182
	$result = spip_query($query);
183
 
184
	while ($row = spip_fetch_array($result)) {
185
		$id_fragment = $row['id_fragment'];
186
		$fragment = $row['fragment'];
187
		$version_min = $row['version_min'];
188
		$version_max = $row['version_max'];
189
		if ($row['compress'] > 0) $fragment = gzuncompress($fragment);
190
		$fragment = unserialize($fragment);
191
		for ($i = $version_fin; $i >= $version_min; $i--) {
192
			if (isset($fragment[$i])) {
193
				// Recopier le dernier fragment si implicite
194
				if (!isset($fragment[$version_fin + 1]))
195
					$fragment[$version_fin + 1] = $fragment[$i];
196
				unset($fragment[$i]);
197
			}
198
		}
199
 
200
		// Virer l'ancien enregistrement (la cle primaire va changer)
201
		$deletes[] = "id_fragment=$id_fragment AND version_min=$version_min";
202
		// Essayer l'agregation
203
		$agreger = false;
204
		if (isset($deb_fragment[$id_fragment])) {
205
			$agreger = (count($deb_fragment[$id_fragment]) + count($fragment) <= $agregation_versions);
206
			if ($agreger) {
207
				$fragment = $deb_fragment[$id_fragment] + $fragment;
208
				$version_min = $deb_version_min[$id_fragment];
209
			}
210
			else {
211
				$replaces[] = replace_fragment($id_article,
212
					$deb_version_min[$id_fragment], $deb_version_max[$id_fragment],
213
					$id_fragment, $deb_fragment[$id_fragment]);
214
			}
215
			unset($deb_fragment[$id_fragment]);
216
		}
217
		if (!$agreger) {
218
			// Ajuster l'intervalle des versions
219
			$version_min = $version_fin + 1;
220
		}
221
		$replaces[] = replace_fragment($id_article, $version_min, $version_max, $id_fragment, $fragment);
222
	}
223
 
224
	// Ajouter fragments restants
225
	if (is_array($deb_fragment) && count($deb_fragment) > 0) {
226
		foreach ($deb_fragment as $id_fragment => $fragment) {
227
			$replaces[] = replace_fragment($id_article,
228
				$deb_version_min[$id_fragment], $deb_version_max[$id_fragment],
229
				$id_fragment, $deb_fragment[$id_fragment]);
230
		}
231
	}
232
 
233
	exec_replace_fragments($replaces);
234
	exec_delete_fragments($id_article, $deletes);
235
}
236
 
237
//
238
// Recuperer les fragments d'une version donnee
239
// renvoie un tableau associatif (id_fragment => texte)
240
//
241
function recuperer_fragments($id_article, $id_version) {
242
	$fragments = array();
243
 
244
	if ($id_version == 0) return array();
245
 
246
	$query = "SELECT id_fragment, version_min, compress, fragment FROM spip_versions_fragments ".
247
		"WHERE id_article=$id_article AND version_min<=$id_version AND version_max>=$id_version";
248
	$result = spip_query($query);
249
 
250
	while ($row = spip_fetch_array($result)) {
251
		$id_fragment = $row['id_fragment'];
252
		$version_min = $row['version_min'];
253
		$fragment = $row['fragment'];
254
		if ($row['compress'] > 0) $fragment = gzuncompress($fragment);
255
		$fragment = unserialize($fragment);
256
		for ($i = $id_version; $i >= $version_min; $i--) {
257
			if (isset($fragment[$i])) {
258
				$fragments[$id_fragment] = $fragment[$i];
259
				break;
260
			}
261
		}
262
	}
263
	return $fragments;
264
}
265
 
266
 
267
//
268
// Apparier des paragraphes deux a deux entre une version originale
269
// et une version modifiee
270
//
271
function apparier_paras($src, $dest, $flou = true) {
272
	$src_dest = array();
273
	$dest_src = array();
274
 
275
	$t1 = $t2 = array();
276
 
277
	$md1 = $md2 = array();
278
	$gz_min1 = $gz_min2 = array();
279
	$gz_trans1 = $gz_trans2 = array();
280
	$l1 = $l2 = array();
281
 
282
	// Nettoyage de la ponctuation pour faciliter l'appariement
283
	foreach($src as $key => $val) {
284
		$t1[$key] = preg_replace("/[[:punct:][:space:]]+/", " ", $val);
285
	}
286
	foreach($dest as $key => $val) {
287
		$t2[$key] = preg_replace("/[[:punct:][:space:]]+/", " ", $val);
288
	}
289
 
290
	// Premiere passe : chercher les correspondance exactes
291
	foreach($t1 as $key => $val) $md1[$key] = md5($val);
292
	foreach($t2 as $key => $val) $md2[md5($val)][$key] = $key;
293
	foreach($md1 as $key1 => $h) {
294
		if (count($md2[$h])) {
295
			$key2 = reset($md2[$h]);
296
			if ($t1[$key1] == $t2[$key2]) {
297
				$src_dest[$key1] = $key2;
298
				$dest_src[$key2] = $key1;
299
				unset($t1[$key1]);
300
				unset($t2[$key2]);
301
				unset($md2[$h][$key2]);
302
			}
303
		}
304
	}
305
 
306
	if ($flou) {
307
		// Deuxieme passe : recherche de correlation par test de compressibilite
308
		foreach($t1 as $key => $val) {
309
			$l1[$key] = strlen(gzcompress($val));
310
		}
311
		foreach($t2 as $key => $val) {
312
			$l2[$key] = strlen(gzcompress($val));
313
		}
314
		foreach($t1 as $key1 => $s1) {
315
			foreach($t2 as $key2 => $s2) {
316
				$r = strlen(gzcompress($s1.$s2));
317
				$taux = 1.0 * $r / ($l1[$key1] + $l2[$key2]);
318
				if (!$gz_min1[$key1] || $gz_min1[$key1] > $taux) {
319
					$gz_min1[$key1] = $taux;
320
					$gz_trans1[$key1] = $key2;
321
				}
322
				if (!$gz_min2[$key2] || $gz_min2[$key2] > $taux) {
323
					$gz_min2[$key2] = $taux;
324
					$gz_trans2[$key2] = $key1;
325
				}
326
			}
327
		}
328
 
329
		// Depouiller les resultats de la deuxieme passe :
330
		// ne retenir que les correlations reciproques
331
		foreach($gz_trans1 as $key1 => $key2) {
332
			if ($gz_trans2[$key2] == $key1 && $gz_min1[$key1] < 0.9) {
333
				$src_dest[$key1] = $key2;
334
				$dest_src[$key2] = $key1;
335
			}
336
		}
337
	}
338
 
339
	// Retourner les mappings
340
	return array($src_dest, $dest_src);
341
}
342
 
343
//
344
// Recuperer les champs d'une version donnee
345
//
346
function recuperer_version($id_article, $id_version) {
347
	$query = "SELECT champs FROM spip_versions ".
348
		"WHERE id_article=$id_article AND id_version=$id_version";
349
	$result = spip_query($query);
350
 
351
	if (!($row = spip_fetch_array($result))) return false;
352
 
353
	$fragments = recuperer_fragments($id_article, $id_version);
354
	$champs = unserialize($row['champs']);
355
	$textes = array();
356
	foreach ($champs as $nom_champ => $code) {
357
		$textes[$nom_champ] = "";
358
		$code = explode(' ', $code);
359
		foreach ($code as $id_fragment) {
360
			$textes[$nom_champ] .= $fragments[$id_fragment];
361
		}
362
	}
363
	return $textes;
364
}
365
 
366
function supprimer_versions($id_article, $version_min, $version_max) {
367
	$query = "DELETE FROM spip_versions WHERE id_article=$id_article ".
368
		"AND id_version>=$version_min AND id_version<=$version_max";
369
	spip_query($query);
370
	supprimer_fragments($id_article, $version_min, $version_max);
371
}
372
 
373
//
374
// Ajouter une version a un article
375
//
376
function ajouter_version($id_article, $champs, $titre_version = "") {
377
	global $connect_id_auteur;
378
 
379
	// Eviter les validations entremelees
380
	$lock = "ajout_version $id_article";
381
	spip_get_lock($lock, 10);
382
 
383
	// Examiner la derniere version
384
	$query = "SELECT id_version, (id_auteur=$connect_id_auteur AND date > DATE_SUB(NOW(), INTERVAL 1 HOUR) AND permanent!='oui') AS flag ".
385
		"FROM spip_versions WHERE id_article=$id_article ".
386
		"ORDER BY id_version DESC LIMIT 0,1";
387
	$result = spip_query($query);
388
	if ($row = spip_fetch_array($result)) {
389
		$nouveau = !$row['flag'];
390
		$id_version = $row['id_version'];
391
		if ($nouveau) $id_version_new = $id_version + 1;
392
		else $id_version_new = $id_version;
393
	}
394
	else {
395
		$nouveau = true;
396
		$id_version_new = 1;
397
	}
398
	$query = "SELECT id_fragment FROM spip_versions_fragments ".
399
		"WHERE id_article=$id_article ORDER BY id_fragment DESC LIMIT 0,1";
400
	$result = spip_query($query);
401
	if ($row = spip_fetch_array($result))
402
		$id_fragment_next = $row['id_fragment'] + 1;
403
	else
404
		$id_fragment_next = 1;
405
 
406
	// Generer les nouveaux fragments
407
	$fragments = array();
408
	$paras_old = recuperer_fragments($id_article, $id_version);
409
	$paras_new = $paras_champ = array();
410
	foreach ($champs as $nom_champ => $texte) {
411
		$codes[$nom_champ] = array();
412
		$paras_new = separer_paras($texte, $paras_new);
413
		$paras_champ[$nom_champ] = count($paras_new);
414
	}
415
 
416
	// Apparier les fragments de maniere optimale
417
	$n = count($paras_new);
418
	if ($n) {
419
		// Tables d'appariement dans les deux sens
420
		list($trans, $trans_rev) = apparier_paras($paras_old, $paras_new);
421
		reset($champs);
422
		$nom_champ = '';
423
		for ($i = 0; $i < $n; $i++) {
424
			while ($i >= $paras_champ[$nom_champ]) list($nom_champ, ) = each($champs);
425
			// Lier au fragment existant si possible, sinon creer un nouveau fragment
426
			if (isset($trans_rev[$i])) $id_fragment = $trans_rev[$i];
427
			else $id_fragment = $id_fragment_next++;
428
			$codes[$nom_champ][] = $id_fragment;
429
			$fragments[$id_fragment] = $paras_new[$i];
430
		}
431
	}
432
	foreach ($champs as $nom_champ => $t) {
433
		$codes[$nom_champ] = join(' ', $codes[$nom_champ]);
434
		if (!strlen($codes[$nom_champ])) unset($codes[$nom_champ]);
435
	}
436
 
437
	// Enregistrer les modifications
438
	ajouter_fragments($id_article, $id_version_new, $fragments);
439
	if (!$codes) $codes = array();
440
	$codes = addslashes(serialize($codes));
441
	$permanent = empty($titre_version) ? 'non' : 'oui';
442
	$titre_version = addslashes($titre_version);
443
	if ($nouveau) {
444
		$query = "INSERT spip_versions (id_article, id_version, titre_version, permanent, date, id_auteur, champs) ".
445
			"VALUES ($id_article, $id_version_new, '$titre_version', '$permanent', NOW(), '$connect_id_auteur', '$codes')";
446
		spip_query($query);
447
	}
448
	else {
449
		$query = "UPDATE spip_versions SET date=NOW(), id_auteur=$connect_id_auteur, champs='$codes', ".
450
			"permanent='$permanent', titre_version='$titre_version' ".
451
		 	"WHERE id_article=$id_article AND id_version=$id_version";
452
		spip_query($query);
453
	}
454
	$query = "UPDATE spip_articles SET id_version=$id_version_new WHERE id_article=$id_article";
455
	spip_query($query);
456
 
457
	spip_release_lock($lock);
458
 
459
	return $id_version_new;
460
}
461
 
462
// les textes "diff" ne peuvent pas passer dans propre directement,
463
// car ils contiennent des <span> et <div> parfois mal places
464
function propre_diff($texte) {
465
 
466
	$span_diff = array();
467
	if (preg_match_all(',</?(span|div) (class|rem)="diff-[^>]+>,', $texte, $regs, PREG_SET_ORDER)) {
468
		foreach ($regs as $c => $reg)
469
			$texte = str_replace($reg[0], '@@@SPIP_DIFF'.$c.'@@@', $texte);
470
	}
471
 
472
	// [ ...<span diff> -> lien ]
473
	// < tag <span diff> >
474
	$texte = preg_replace(',<([^>]*@@@SPIP_DIFF[0-9]+@@@),',
475
		'&lt;\1', $texte);
476
	# attention ici astuce seulement deux @@ finals car on doit eviter
477
	# deux patterns a suivre, afin de pouvoir prendre [ mais eviter [[
478
	$texte = preg_replace(',(^|[^[])[[]([^[\]]*@@@SPIP_DIFF[0-9]+@@),',
479
		'\1&#91;\2', $texte);
480
 
481
	$texte = propre($texte);
482
 
483
	// un blockquote mal ferme peut gener l'affichage, et title plante safari
484
	$texte = preg_replace(',<(/?(blockquote|title)[^>]*)>,i', '&lt;\1>', $texte);
485
 
486
	// Dans les <cadre> c'est un peu plus complique
487
	if (preg_match_all(',<textarea (.*)</textarea>,Uims', $texte, $area, PREG_SET_ORDER)) {
488
		foreach ($area as $reg) {
489
			$remplace = preg_replace(',@@@SPIP_DIFF[0-9]+@@@,', '**', $reg[0]);
490
			if ($remplace <> $reg[0])
491
				$texte = str_replace($reg[0], $remplace, $texte);
492
		}
493
	}
494
 
495
	// replacer les valeurs des <span> et <div> diff-
496
	if (is_array($regs))
497
	foreach ($regs as $c => $reg) {
498
		$texte = str_replace('@@@SPIP_DIFF'.$c.'@@@', $reg[0], $texte);
499
		$GLOBALS['les_notes'] = str_replace('@@@SPIP_DIFF'.$c.'@@@', $reg[0], $GLOBALS['les_notes']);
500
	}
501
 
502
	return $texte;
503
}
504
 
505
?>