[SPIP] +2.1.12
[velocampus/web/www.git] / www / ecrire / inc / suivi_versions.php
1 <?php
2
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
5 * *
6 * Copyright (c) 2001-2011 *
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')) return;
14
15 include_spip('inc/revisions');
16 include_spip('inc/diff');
17
18 // http://doc.spip.org/@afficher_para_modifies
19 function afficher_para_modifies ($texte, $court = false) {
20 // Limiter la taille de l'affichage
21 if ($court) $max = 200;
22 else $max = 2000;
23
24 $paras = explode ("\n",$texte);
25 for ($i = 0; $i < count($paras) AND strlen($texte_ret) < $max; $i++) {
26 if (strpos($paras[$i], '"diff-')) $texte_ret .= $paras[$i]."\n\n";
27 # if (strlen($texte_ret) > $max) $texte_ret .= '(...)';
28 }
29 $texte = $texte_ret;
30 return $texte;
31 }
32
33
34 // Retourne le titre de la rubrique demandee, pour affichage de la chaine
35 // "deplace de XX vers YY"
36 // http://doc.spip.org/@titre_rubrique
37 function titre_rubrique($id_rubrique) {
38 if (!$id = intval($id_rubrique))
39 return _T('info_sans_titre');
40
41 return typo(sql_getfetsel('titre', 'spip_rubriques', "id_rubrique=$id"));
42 }
43
44
45 // http://doc.spip.org/@afficher_suivi_versions
46 function afficher_suivi_versions ($debut = 0, $id_secteur = 0, $uniq_auteur = false, $lang = "", $court = false) {
47
48 changer_typo($lang);
49 $lang_dir = lang_dir($lang);
50 $nb_aff = 10;
51 $champs = array('surtitre', 'titre', 'soustitre', 'descriptif', 'nom_site', 'url_site', 'chapo', 'texte', 'ps');
52
53 if ($uniq_auteur) {
54 $req_where = " AND articles.statut IN ('prepa','prop','publie')";
55 $req_where .= " AND versions.id_auteur = $uniq_auteur";
56 } else {
57 $req_where = " AND articles.statut IN ('prop','publie')";
58 }
59
60 if (strlen($lang) > 0)
61 $req_where .= " AND articles.lang=" . sql_quote($lang);
62
63 if ($id_secteur > 0)
64 $req_where .= " AND articles.id_secteur = ".intval($id_secteur);
65
66 $req_where = "versions.id_version > 1 $req_where";
67
68 $req_sel = "versions.id_version, versions.id_auteur, versions.date, versions.id_article, articles.statut, articles.titre";
69
70 $req_from = 'spip_versions AS versions LEFT JOIN spip_articles AS articles ON versions.id_article = articles.id_article';
71
72 $revisions = '';
73 $items = array();
74 $result = sql_select($req_sel, $req_from, $req_where, '', 'versions.date DESC', "$debut, $nb_aff");
75 while ($row = sql_fetch($result)) {
76 $id_article = $row['id_article'];
77 if (autoriser('voir','article',$id_article)){
78 $statut = $row['statut'];
79 $id_version = $row['id_version'];
80 $id_auteur = $row['id_auteur'];
81 $date = $row['date'];
82 $titre = typo(supprime_img($row['titre'],''));
83
84 // l'id_auteur peut etre un numero IP (edition anonyme)
85 if ($id_auteur == intval($id_auteur)
86 AND $row_auteur = sql_fetsel('nom,email', 'spip_auteurs', "id_auteur = ".sql_quote($id_auteur))) {
87 $nom = typo($row_auteur["nom"]);
88 $email = $row_auteur['email'];
89 } else {
90 $nom = $id_auteur;
91 $email = '';
92 }
93
94 $aff = revisions_bouton($id_article, $id_auteur, $id_version, $titre, $statut, $date, $lang_dir, $nom);
95 if (!$court) {
96 $bouton_id = "b$id_version-$id_article-$id_auteur";
97 $aff = bouton_block_depliable($aff,false,$bouton_id)
98 . debut_block_depliable(false,$bouton_id)
99 . safehtml(revisions_diff ($id_article, $id_version, $court))
100 . fin_block();
101 }
102 $revisions .= "\n<div class='tr_liste' style='padding: 5px; border-top: 1px solid #aaaaaa;'>$aff</div>";
103 }
104 }
105 if (!$revisions) return '';
106 else return
107 revisions_entete_boite($court, $debut, $id_secteur, $lang, $nb_aff, $req_from, $req_where, $uniq_auteur)
108 . $revisions
109 . fin_block()
110 . fin_cadre();
111 }
112
113 // http://doc.spip.org/@revisions_diff
114 function revisions_diff ($id_article, $id_version, $court=true)
115 {
116 $textes = revision_comparee($id_article, $id_version, 'diff');
117 if (!is_array($textes)) return $textes;
118 $rev = '';
119 $nb = 0;
120 foreach ($textes as $var => $t) {
121 if ($n=strlen($t)) {
122 if ($court)
123 $nb += $n;
124 else {
125 $aff = propre_diff($t);
126 if ($GLOBALS['les_notes']) {
127 $aff .= '<p>'.$GLOBALS['les_notes'].'</p>';
128 $GLOBALS['les_notes'] = '';
129 }
130 $rev .= "<blockquote class='serif1'>$aff</blockquote>";
131 }
132 }
133 }
134 return $court ? _T('taille_octets', array('taille' => $nb)) : $rev;
135 }
136
137 // http://doc.spip.org/@revisions_bouton
138 function revisions_bouton($id_article, $id_auteur, $id_version, $titre, $statut, $date, $lang_dir, $nom)
139 {
140 $titre_bouton = "<span class='arial2'>";
141 $titre_bouton .= puce_statut($statut);
142 $titre_bouton .= "\n&nbsp;<a class='$statut' style='font-weight: bold;' href='" . generer_url_ecrire("articles_versions","id_article=$id_article") . "'>$titre</a>";
143 $titre_bouton .= "<span class='arial1' dir='$lang_dir'>";
144 $titre_bouton .= "\n".date_relative($date)." "; # laisser un peu de privacy aux redacteurs
145 $titre_bouton .= "</span>";
146 if (strlen($nom)>0) $titre_bouton .= "($nom)";
147 $titre_bouton .= "</span>";
148 return $titre_bouton;
149 }
150
151 // http://doc.spip.org/@revisions_entete_boite
152 function revisions_entete_boite($court, $debut, $id_secteur, $lang, $nb_aff, $req_from, $req_where, $uniq_auteur)
153 {
154
155 $titre_table = '<b>' . _T('icone_suivi_revisions').aide('suivimodif') . '</b>';
156 if ($court)
157 $titre_table = afficher_plus(generer_url_ecrire("suivi_revisions"))
158 . $titre_table;
159
160 $total = sql_countsel($req_from, $req_where);
161 if ($total >= 150) $total = 149;
162 $id_liste = 't'.substr(md5("$req_where 149"),0,8);
163 $bouton = bouton_block_depliable($titre_table,true,$id_liste);
164 $revisions = debut_cadre('liste',"historique-24.gif",'',$bouton)
165 . debut_block_depliable(true,$id_liste);
166
167 if ($total > $nb_aff) {
168 $nb_tranches = ceil($total / $nb_aff);
169
170 $revisions .= "\n<div class='arial2' style='background-color: #dddddd; padding: 5px;'>\n";
171
172 for ($i = 0; $i < $nb_tranches; $i++) {
173 if ($i > 0) $revisions .= " | ";
174 if ($i*$nb_aff == $debut)
175 $revisions .= "<b>";
176 else {
177 $next = ($i * $nb_aff);
178 $revisions .= "<a href='".generer_url_ecrire('suivi_revisions', "debut=$next&id_secteur=$id_secteur&id_auteur=$uniq_auteur&lang_choisie=$lang")."'>";
179 }
180 $revisions .= (($i * $nb_aff) + 1);
181 if ($i*$nb_aff == $debut) $revisions .= "</b>";
182 else $revisions .= "</a>";
183 }
184 $revisions .= "</div>";
185 }
186 return $revisions;
187 }
188
189 // retourne un array() des champs modifies a la version id_version
190 // le format =
191 // - diff => seulement les modifs (suivi_revisions)
192 // - apercu => idem, mais en plus tres cout s'il y en a bcp
193 // - complet => tout, avec surlignage des modifications (articles_versions)
194 // http://doc.spip.org/@revision_comparee
195 function revision_comparee($id_article, $id_version, $format='diff', $id_diff=NULL) {
196 include_spip('inc/diff');
197
198 // chercher le numero de la version precedente
199 if (!$id_diff) {
200 $id_diff = sql_getfetsel("id_version", "spip_versions", "id_article=" . intval($id_article) . " AND id_version < " . intval($id_version), "", "id_version DESC", "1");
201 }
202
203 if ($id_version && $id_diff) {
204
205 // si l'ordre est inverse, on remet a l'endroit
206 if ($id_diff > $id_version) {
207 $t = $id_version;
208 $id_version = $id_diff;
209 $id_diff = $t;
210 }
211
212 $old = recuperer_version($id_article, $id_diff);
213 $new = recuperer_version($id_article, $id_version);
214
215 $textes = array();
216
217 // Mode "diff": on ne s'interesse qu'aux champs presents dans $new
218 // Mode "complet": on veut afficher tous les champs
219 switch ($format) {
220 case 'complet':
221 $champs = liste_champs_versionnes('spip_articles');
222 break;
223 case 'diff':
224 case 'apercu':
225 default:
226 $champs = array_keys($new);
227 break;
228 }
229
230 foreach ($champs as $champ) {
231 // si la version precedente est partielle,
232 // il faut remonter dans le temps
233 $id_ref = $id_diff-1;
234 while (!isset($old[$champ])
235 AND $id_ref>0) {
236 $prev = recuperer_version($id_article, $id_ref--);
237 if (isset($prev[$champ]))
238 $old[$champ] = $prev[$champ];
239 }
240 if (!strlen($new[$champ]) && !strlen($old[$champ])) continue;
241
242 // si on n'a que le vieux, ou que le nouveau, on ne
243 // l'affiche qu'en mode "complet"
244 if ($format == 'complet')
245 $textes[$champ] = strlen($new[$champ])
246 ? $new[$champ] : $old[$champ];
247
248 // si on a les deux, le diff nous interesse, plus ou moins court
249 if (isset($new[$champ])
250 AND isset($old[$champ])) {
251 // cas particulier : id_rubrique
252 if (in_array($champ, array('id_rubrique'))) {
253 $textes[$champ] = _T('version_deplace_rubrique',
254 array('from'=> titre_rubrique($old[$champ])
255 ,'to'=>titre_rubrique($new[$champ]))
256 );
257 }
258
259 // champs textuels
260 else {
261 $diff = new Diff(new DiffTexte);
262 $n = preparer_diff($new[$champ]);
263 $o = preparer_diff($old[$champ]);
264 $textes[$champ] = afficher_diff($diff->comparer($n,$o));
265 if ($format == 'diff' OR $format == 'apercu')
266 $textes[$champ] = afficher_para_modifies($textes[$champ], ($format == 'apercu'));
267 }
268 }
269 }
270 }
271
272 // que donner par defaut ? (par exemple si id_version=1)
273 if (!$textes)
274 $textes = recuperer_version($id_article, $id_version);
275
276 return $textes;
277 }
278
279 ?>