314b36d29870985b8a5dc00d2b47904dd6b4f2f8
[lhc/web/www.git] / www / plugins-dist / revisions / afficher_diff / champ.php
1 <?php
2
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
5 * *
6 * Copyright (c) 2001-2016 *
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 if (!defined('_ECRIRE_INC_VERSION')) {
14 return;
15 }
16
17 include_spip('inc/diff');
18
19 /**
20 * Afficher le diff d'un champ texte generique
21 *
22 * @param string $champ
23 * @param string $old
24 * @param string $new
25 * @param string $format
26 * apercu, diff ou complet
27 * @return string
28 */
29 function afficher_diff_champ_dist($champ, $old, $new, $format = 'diff') {
30 // ne pas se compliquer la vie !
31 if ($old == $new) {
32 $out = ($format != 'complet' ? '' : $new);
33 } else {
34 if ($f = charger_fonction($champ, 'afficher_diff', true)) {
35 return $f($champ, $old, $new, $format);
36 }
37
38 $diff = new Diff(new DiffTexte);
39 $n = preparer_diff($new);
40 $o = preparer_diff($old);
41
42 $out = afficher_diff($diff->comparer($n, $o));
43 if ($format == 'diff' or $format == 'apercu') {
44 $out = afficher_para_modifies($out, ($format == 'apercu'));
45 }
46 }
47
48 return $out;
49 }
50
51
52 /**
53 * https://code.spip.net/@afficher_para_modifies
54 *
55 * @param string $texte
56 * @param bool $court
57 * @return string
58 */
59 function afficher_para_modifies($texte, $court = false) {
60 // Limiter la taille de l'affichage
61 if ($court) {
62 $max = 200;
63 } else {
64 $max = 2000;
65 }
66
67 $texte_ret = '';
68 $paras = explode("\n", $texte);
69 for ($i = 0; $i < count($paras) and strlen($texte_ret) < $max; $i++) {
70 if (strpos($paras[$i], '"diff-')) {
71 $texte_ret .= $paras[$i] . "\n\n";
72 }
73 # if (strlen($texte_ret) > $max) $texte_ret .= '(...)';
74 }
75 $texte = $texte_ret;
76
77 return $texte;
78 }