[SPIP][PLUGINS] v3.0-->v3.2
[lhc/web/www.git] / www / plugins-dist / svp / teleporter / svn.php
1 <?php
2 /**
3 * Gestion du téléporteur HTTP.
4 *
5 * @plugin SVP pour SPIP
6 * @license GPL
7 * @package SPIP\SVP\Teleporteur
8 */
9
10
11 if (!defined('_SVN_COMMAND')) {
12 define('_SVN_COMMAND', "svn");
13 } // Securite : mettre le chemin absolu dans mes_options.php
14
15 /**
16 * Téléporter et déballer un composant SVN
17 *
18 * Déployer un repository SVN depuis une source et une révision données
19 *
20 * @uses teleporter_svn_read()
21 * @uses teleporter_nettoyer_vieille_version()
22 *
23 * @param string $methode
24 * Méthode de téléportation : http|git|svn|...
25 * @param string $source
26 * URL de la source SVN
27 * @param string $dest
28 * Chemin du répertoire de destination
29 * @param array $options
30 * Tableau d'options. Index possibles :
31 * - revision => 'nnn'
32 * - literal => --ignore-externals
33 * @return bool
34 * True si l'opération réussie, false sinon.
35 */
36 function teleporter_svn_dist($methode, $source, $dest, $options = array()) {
37 if (is_dir($dest)) {
38 $infos = teleporter_svn_read($dest);
39 if (!$infos) {
40 spip_log("Suppression de $dest qui n'est pas au format SVN", "teleport");
41 $old = teleporter_nettoyer_vieille_version($dest);
42 } elseif ($infos['source'] !== $source) {
43 spip_log("Suppression de $dest qui n'est pas sur le bon repository SVN", "teleport");
44 $old = teleporter_nettoyer_vieille_version($dest);
45 } elseif (!isset($options['revision'])
46 or $options['revision'] != $infos['revision']
47 ) {
48 $command = _SVN_COMMAND . " up ";
49 if (isset($options['revision'])) {
50 $command .= escapeshellarg("-r" . $options['revision']) . " ";
51 }
52 if (isset($options['ignore-externals'])) {
53 $command .= "--ignore-externals ";
54 }
55
56 $command .= escapeshellarg($dest);
57 spip_log($command, "teleport");
58 exec($command);
59 } else {
60 // Rien a faire !
61 spip_log("$dest deja a jour (Revision " . $options['revision'] . " SVN de $source)", "teleport");
62 }
63 }
64
65 if (!is_dir($dest)) {
66 $command = _SVN_COMMAND . " co ";
67 if (isset($options['revision'])) {
68 $command .= escapeshellarg("-r" . $options['revision']) . " ";
69 }
70 if (isset($options['ignore-externals'])) {
71 $command .= "--ignore-externals ";
72 }
73 $command .= escapeshellarg($source) . " " . escapeshellarg($dest);
74 spip_log($command, "teleport");
75 exec($command);
76 }
77
78 // verifier que tout a bien marche
79 $infos = teleporter_svn_read($dest);
80 if (!$infos) {
81 return false;
82 }
83
84 return true;
85 }
86
87 /**
88 * Lire source et révision d'un répertoire SVN
89 * et reconstruire la ligne de commande
90 *
91 * @param string $dest
92 * Chemin du répertoire SVN
93 * @param array $options
94 * Options
95 * @return array|string
96 * Chaîne vide si pas SVN ou erreur de lecture,
97 * Tableau sinon avec les index :
98 * - source : URL de la source SVN
99 * - revision : numéro de la révision SVN
100 * - dest : Chemin du répertoire
101 */
102 function teleporter_svn_read($dest, $options = array()) {
103
104 if (!is_dir("$dest/.svn")) {
105 return "";
106 }
107
108 // on veut lire ce qui est actuellement deploye
109 // et reconstituer la ligne de commande pour le deployer
110 exec(_SVN_COMMAND . " info " . escapeshellarg($dest), $output);
111 $output = implode("\n", $output);
112
113 // URL
114 // URL: svn://trac.rezo.net/spip/spip
115 if (!preg_match(",^URL[^:\w]*:\s+(.*)$,Uims", $output, $m)) {
116 return "";
117 }
118 $source = $m[1];
119
120 // Revision
121 // Revision: 18763
122 if (!preg_match(",^R..?vision[^:\w]*:\s+(\d+)$,Uims", $output, $m)) {
123 return "";
124 }
125
126 $revision = $m[1];
127
128 return array(
129 'source' => $source,
130 'revision' => $revision,
131 'dest' => $dest
132 );
133 }
134
135
136 /**
137 * Tester si la commande 'svn' est disponible
138 *
139 * @return bool
140 * true si on peut utiliser la commande svn
141 **/
142 function teleporter_svn_tester() {
143 static $erreurs = null;
144 if (is_null($erreurs)) {
145 exec(_SVN_COMMAND . " --version", $output, $erreurs);
146 }
147
148 return !$erreurs;
149 }