4 |
david |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
//
|
|
|
4 |
// Ce fichier ne sera execute qu'une fois
|
|
|
5 |
if (defined("_ECRIRE_INC_DIFF")) return;
|
|
|
6 |
define("_ECRIRE_INC_DIFF", "1");
|
|
|
7 |
|
|
|
8 |
|
|
|
9 |
|
|
|
10 |
function separer_paras($texte, $paras = "") {
|
|
|
11 |
if (!$paras) $paras = array();
|
|
|
12 |
while (preg_match("/(\r\n?){2,}|\n{2,}/", $texte, $regs)) {
|
|
|
13 |
$p = strpos($texte, $regs[0]) + strlen($regs[0]);
|
|
|
14 |
$paras[] = substr($texte, 0, $p);
|
|
|
15 |
$texte = substr($texte, $p);
|
|
|
16 |
}
|
|
|
17 |
if ($texte) $paras[] = $texte;
|
|
|
18 |
return $paras;
|
|
|
19 |
}
|
|
|
20 |
|
|
|
21 |
//
|
|
|
22 |
// Ajouter les fragments de la derniere version (tableau associatif id_fragment => texte)
|
|
|
23 |
//
|
|
|
24 |
function ajouter_fragments($id_article, $id_version, $fragments) {
|
|
|
25 |
global $flag_gz;
|
|
|
26 |
|
|
|
27 |
$replaces = array();
|
|
|
28 |
foreach ($fragments as $id_fragment => $texte) {
|
|
|
29 |
$nouveau = true;
|
|
|
30 |
// Recuperer la version la plus recente
|
|
|
31 |
$query = "SELECT compress, fragment, version_min, version_max FROM spip_versions_fragments ".
|
|
|
32 |
"WHERE id_article=$id_article AND id_fragment=$id_fragment AND version_min<=$id_version ".
|
|
|
33 |
"ORDER BY version_min DESC LIMIT 0,1";
|
|
|
34 |
$result = spip_query($query);
|
|
|
35 |
if ($row = spip_fetch_array($result)) {
|
|
|
36 |
$fragment = $row['fragment'];
|
|
|
37 |
$version_min = $row['version_min'];
|
|
|
38 |
if ($row['compress'] > 0) $fragment = gzuncompress($fragment);
|
|
|
39 |
$fragment = unserialize($fragment);
|
|
|
40 |
if (is_array($fragment)) {
|
|
|
41 |
unset($fragment[$id_version]);
|
|
|
42 |
// Si moins de cinq revisions distinctes dans le fragment, prolonger celui-ci
|
|
|
43 |
if (count($fragment) < 5) $nouveau = false;
|
|
|
44 |
}
|
|
|
45 |
}
|
|
|
46 |
if ($nouveau) {
|
|
|
47 |
$fragment = array($id_version => $texte);
|
|
|
48 |
$version_min = $id_version;
|
|
|
49 |
}
|
|
|
50 |
else {
|
|
|
51 |
// Ne pas dupliquer les fragments non modifies
|
|
|
52 |
$modif = true;
|
|
|
53 |
for ($i = $id_version - 1; $i >= $version_min; $i--) {
|
|
|
54 |
if (isset($fragment[$i])) {
|
|
|
55 |
$modif = ($fragment[$i] != $texte);
|
|
|
56 |
break;
|
|
|
57 |
}
|
|
|
58 |
}
|
|
|
59 |
if ($modif) $fragment[$id_version] = $texte;
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
// Preparer l'enregistrement du fragment
|
|
|
63 |
$fragment = serialize($fragment);
|
|
|
64 |
$compress = 0;
|
|
|
65 |
$version_max = $id_version;
|
|
|
66 |
if ($flag_gz) {
|
|
|
67 |
$s = gzcompress($fragment);
|
|
|
68 |
if (strlen($s) < strlen($fragment)) {
|
|
|
69 |
//echo "gain gz: ".(100 - 100 * strlen($s) / strlen($fragment))."%<br>";
|
|
|
70 |
$compress = 1;
|
|
|
71 |
$fragment = $s;
|
|
|
72 |
}
|
|
|
73 |
}
|
|
|
74 |
// (attention a bien echapper le $fragment qui est en binaire)
|
|
|
75 |
$replaces[] = "($id_article, $version_min, $version_max, $id_fragment, $compress, '"
|
|
|
76 |
.mysql_escape_string($fragment)."')";
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
if (count($replaces)) {
|
|
|
80 |
$query = "REPLACE spip_versions_fragments (id_article, version_min, version_max, id_fragment, compress, fragment) ".
|
|
|
81 |
"VALUES ".join(", ", $replaces);
|
|
|
82 |
spip_query($query);
|
|
|
83 |
}
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
//
|
|
|
87 |
// Recuperer les fragments d'une version donnee
|
|
|
88 |
// renvoie un tableau associatif (id_fragment => texte)
|
|
|
89 |
//
|
|
|
90 |
function recuperer_fragments($id_article, $id_version) {
|
|
|
91 |
$fragments = array();
|
|
|
92 |
|
|
|
93 |
$query = "SELECT id_fragment, version_min, compress, fragment FROM spip_versions_fragments ".
|
|
|
94 |
"WHERE id_article=$id_article AND version_min<=$id_version AND version_max>=$id_version";
|
|
|
95 |
$result = spip_query($query);
|
|
|
96 |
|
|
|
97 |
while ($row = spip_fetch_array($result)) {
|
|
|
98 |
$id_fragment = $row['id_fragment'];
|
|
|
99 |
$version_min = $row['version_min'];
|
|
|
100 |
$fragment = $row['fragment'];
|
|
|
101 |
if ($row['compress'] > 0) $fragment = gzuncompress($fragment);
|
|
|
102 |
$fragment = unserialize($fragment);
|
|
|
103 |
for ($i = $id_version; $i >= $version_min; $i--) {
|
|
|
104 |
if (isset($fragment[$i])) {
|
|
|
105 |
$fragments[$id_fragment] = $fragment[$i];
|
|
|
106 |
break;
|
|
|
107 |
}
|
|
|
108 |
}
|
|
|
109 |
}
|
|
|
110 |
return $fragments;
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
|
|
|
114 |
//
|
|
|
115 |
// Apparier des paragraphes deux a deux entre une version originale
|
|
|
116 |
// et une version modifiee
|
|
|
117 |
//
|
|
|
118 |
function apparier_paras($src, $dest) {
|
|
|
119 |
$src_dest = array();
|
|
|
120 |
$dest_src = array();
|
|
|
121 |
|
|
|
122 |
$t1 = $t2 = array();
|
|
|
123 |
|
|
|
124 |
$md1 = $md2 = array();
|
|
|
125 |
$gz_min1 = $gz_min2 = array();
|
|
|
126 |
$gz_trans1 = $gz_trans2 = array();
|
|
|
127 |
$l1 = $l2 = array();
|
|
|
128 |
|
|
|
129 |
// Nettoyage de la ponctuation pour faciliter l'appariement
|
|
|
130 |
foreach($src as $key => $val) {
|
|
|
131 |
$t1[$key] = preg_replace("/[[:punct:][:space:]]+/", " ", $val);
|
|
|
132 |
}
|
|
|
133 |
foreach($dest as $key => $val) {
|
|
|
134 |
$t2[$key] = preg_replace("/[[:punct:][:space:]]+/", " ", $val);
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
// Hash pour premiere passe
|
|
|
138 |
foreach($t1 as $key => $val) $md1[md5($val)] = $key;
|
|
|
139 |
foreach($t2 as $key => $val) $md2[md5($val)] = $key;
|
|
|
140 |
|
|
|
141 |
// Premiere passe : chercher les correspondance exactes
|
|
|
142 |
foreach($md1 as $h => $key1) {
|
|
|
143 |
if (isset($md2[$h])) {
|
|
|
144 |
$key2 = $md2[$h];
|
|
|
145 |
if ($t1[$key1] == $t2[$key2]) {
|
|
|
146 |
$src_dest[$key1] = $key2;
|
|
|
147 |
$dest_src[$key2] = $key1;
|
|
|
148 |
unset($t1[$key1]);
|
|
|
149 |
unset($t2[$key2]);
|
|
|
150 |
}
|
|
|
151 |
}
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
// Deuxieme passe : recherche de correlation par test de compressibilite
|
|
|
155 |
foreach($t1 as $key => $val) {
|
|
|
156 |
$l1[$key] = strlen(gzcompress($val));
|
|
|
157 |
}
|
|
|
158 |
foreach($t2 as $key => $val) {
|
|
|
159 |
$l2[$key] = strlen(gzcompress($val));
|
|
|
160 |
}
|
|
|
161 |
foreach($t1 as $key1 => $s1) {
|
|
|
162 |
//echo "<br>";
|
|
|
163 |
foreach($t2 as $key2 => $s2) {
|
|
|
164 |
$r = strlen(gzcompress($s1.$s2));
|
|
|
165 |
//$k += strlen($s1) + strlen($s2);
|
|
|
166 |
$taux = 1.0 * $r / ($l1[$key1] + $l2[$key2]);
|
|
|
167 |
//echo "<li>$key1 => $key2 : $taux</li>";
|
|
|
168 |
if (!$gz_min1[$key1] || $gz_min1[$key1] > $taux) {
|
|
|
169 |
$gz_min1[$key1] = $taux;
|
|
|
170 |
$gz_trans1[$key1] = $key2;
|
|
|
171 |
}
|
|
|
172 |
if (!$gz_min2[$key2] || $gz_min2[$key2] > $taux) {
|
|
|
173 |
$gz_min2[$key2] = $taux;
|
|
|
174 |
$gz_trans2[$key2] = $key1;
|
|
|
175 |
}
|
|
|
176 |
}
|
|
|
177 |
}
|
|
|
178 |
//echo "$k octets compresses<p>";
|
|
|
179 |
|
|
|
180 |
// Depouiller les resultats de la deuxieme passe :
|
|
|
181 |
// ne retenir que les correlations reciproques
|
|
|
182 |
foreach($gz_trans1 as $key1 => $key2) {
|
|
|
183 |
if ($gz_trans2[$key2] == $key1 && $gz_min1[$key1] < 0.9) {
|
|
|
184 |
$src_dest[$key1] = $key2;
|
|
|
185 |
$dest_src[$key2] = $key1;
|
|
|
186 |
}
|
|
|
187 |
}
|
|
|
188 |
|
|
|
189 |
/*echo "<br>";
|
|
|
190 |
foreach ($gz_trans1 as $a => $b) {
|
|
|
191 |
echo "$a => $b<br>";
|
|
|
192 |
echo "<blockquote><div style='border: 1px solid black'>".$t1[$a]."</div>";
|
|
|
193 |
echo "<div style='border: 1px solid black'>".$t2[$b]."</div></blockquote>";
|
|
|
194 |
}
|
|
|
195 |
echo "<br>";
|
|
|
196 |
foreach ($gz_trans2 as $b => $a) echo "$a $b<br>";*/
|
|
|
197 |
|
|
|
198 |
// Retourner les mappings
|
|
|
199 |
return array($src_dest, $dest_src);
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
|
|
|
203 |
//
|
|
|
204 |
// Recuperer les champs d'une version donnee
|
|
|
205 |
//
|
|
|
206 |
function recuperer_version($id_article, $id_version) {
|
|
|
207 |
$query = "SELECT chapo, texte, ps, extra FROM spip_versions ".
|
|
|
208 |
"WHERE id_article=$id_article AND id_version=$id_version";
|
|
|
209 |
$result = spip_query($query);
|
|
|
210 |
|
|
|
211 |
if (!($row = spip_fetch_array($result))) return false;
|
|
|
212 |
|
|
|
213 |
$codes['chapo'] = $row['chapo'];
|
|
|
214 |
$codes['texte'] = $row['texte'];
|
|
|
215 |
$codes['ps'] = $row['ps'];
|
|
|
216 |
|
|
|
217 |
$fragments = recuperer_fragments($id_article, $id_version);
|
|
|
218 |
$textes = array();
|
|
|
219 |
foreach ($codes as $var => $code) {
|
|
|
220 |
$textes[$var] = "";
|
|
|
221 |
$code = explode(' ', $code);
|
|
|
222 |
foreach ($code as $id_fragment) {
|
|
|
223 |
$textes[$var] .= $fragments[$id_fragment];
|
|
|
224 |
}
|
|
|
225 |
}
|
|
|
226 |
return $textes;
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
//
|
|
|
230 |
// Ajouter une version a un article
|
|
|
231 |
//
|
|
|
232 |
function ajouter_version($id_article, $chapo, $texte, $ps, $extra) {
|
|
|
233 |
global $connect_id_auteur;
|
|
|
234 |
|
|
|
235 |
// Eviter les validations entremelees
|
|
|
236 |
$lock = "ajout_version $id_article";
|
|
|
237 |
spip_get_lock($lock, 5);
|
|
|
238 |
|
|
|
239 |
// Examiner la derniere version
|
|
|
240 |
$query = "SELECT id_version, (id_auteur=$connect_id_auteur AND date > DATE_SUB(NOW(), INTERVAL 1 HOUR) AND permanent!='oui') AS flag ".
|
|
|
241 |
"FROM spip_versions WHERE id_article=$id_article ".
|
|
|
242 |
"ORDER BY id_version DESC LIMIT 0,1";
|
|
|
243 |
$result = spip_query($query);
|
|
|
244 |
if ($row = spip_fetch_array($result)) {
|
|
|
245 |
$nouveau = !$row['flag'];
|
|
|
246 |
$id_version = $row['id_version'];
|
|
|
247 |
if ($nouveau) $id_version_new = $id_version + 1;
|
|
|
248 |
else $id_version_new = $id_version;
|
|
|
249 |
}
|
|
|
250 |
else {
|
|
|
251 |
$nouveau = true;
|
|
|
252 |
$id_version_new = 1;
|
|
|
253 |
}
|
|
|
254 |
$query = "SELECT id_fragment FROM spip_versions_fragments ".
|
|
|
255 |
"WHERE id_article=$id_article ORDER BY id_fragment DESC LIMIT 0,1";
|
|
|
256 |
$result = spip_query($query);
|
|
|
257 |
if ($row = spip_fetch_array($result))
|
|
|
258 |
$id_fragment_next = $row['id_fragment'] + 1;
|
|
|
259 |
else
|
|
|
260 |
$id_fragment_next = 1;
|
|
|
261 |
|
|
|
262 |
// Generer les nouveaux fragments
|
|
|
263 |
$fragments = array();
|
|
|
264 |
$paras_old = recuperer_fragments($id_article, $id_version);
|
|
|
265 |
$paras_new = $paras_var = array();
|
|
|
266 |
$vars = array('chapo', 'texte', 'ps');
|
|
|
267 |
foreach ($vars as $var) {
|
|
|
268 |
$codes[$var] = array();
|
|
|
269 |
$paras_new = separer_paras($$var, $paras_new);
|
|
|
270 |
$paras_var[$var] = count($paras_new);
|
|
|
271 |
}
|
|
|
272 |
|
|
|
273 |
// Apparier les fragments de maniere optimale
|
|
|
274 |
$n = count($paras_new);
|
|
|
275 |
if ($n) {
|
|
|
276 |
list($trans, $trans_rev) = apparier_paras($paras_old, $paras_new);
|
|
|
277 |
reset($vars);
|
|
|
278 |
$var = '';
|
|
|
279 |
for ($i = 0; $i < $n; $i++) {
|
|
|
280 |
while ($i >= $paras_var[$var]) list(, $var) = each($vars);
|
|
|
281 |
// Lier au fragment existant si possible, sinon creer un nouveau fragment
|
|
|
282 |
if (isset($trans_rev[$i])) $id_fragment = $trans_rev[$i];
|
|
|
283 |
else $id_fragment = $id_fragment_next++;
|
|
|
284 |
$codes[$var][] = $id_fragment;
|
|
|
285 |
$fragments[$id_fragment] = $paras_new[$i];
|
|
|
286 |
}
|
|
|
287 |
}
|
|
|
288 |
foreach ($vars as $var) $codes[$var] = join(' ', $codes[$var]);
|
|
|
289 |
|
|
|
290 |
// Enregistrer les modifications
|
|
|
291 |
ajouter_fragments($id_article, $id_version_new, $fragments);
|
|
|
292 |
$code_chapo = addslashes($codes['chapo']);
|
|
|
293 |
$code_texte = addslashes($codes['texte']);
|
|
|
294 |
$code_ps = addslashes($codes['ps']);
|
|
|
295 |
if ($nouveau) {
|
|
|
296 |
$query = "INSERT spip_versions (id_article, id_version, permanent, date, id_auteur, chapo, texte, ps) ".
|
|
|
297 |
"VALUES ($id_article, $id_version_new, 'non', NOW(), '$connect_id_auteur', '$code_chapo', ".
|
|
|
298 |
"'$code_texte', '$code_ps')";
|
|
|
299 |
spip_query($query);
|
|
|
300 |
}
|
|
|
301 |
else {
|
|
|
302 |
$query = "UPDATE spip_versions SET date=NOW(), id_auteur=$connect_id_auteur, ".
|
|
|
303 |
"chapo='$code_chapo', texte='$code_texte', ps='$code_ps' ".
|
|
|
304 |
"WHERE id_article=$id_article AND id_version=$id_version";
|
|
|
305 |
spip_query($query);
|
|
|
306 |
}
|
|
|
307 |
$query = "UPDATE spip_articles SET id_version=$id_version_new WHERE id_article=$id_article";
|
|
|
308 |
spip_query($query);
|
|
|
309 |
|
|
|
310 |
spip_release_lock($lock);
|
|
|
311 |
|
|
|
312 |
return $id_version_new;
|
|
|
313 |
}
|
|
|
314 |
|
|
|
315 |
|
|
|
316 |
?>
|