1 | <?php |
---|
2 | |
---|
3 | /* |
---|
4 | This file is part of Salvatore, the translation robot of Trad-lang (SPIP) |
---|
5 | |
---|
6 | Salvatore is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2 of the License, or |
---|
9 | (at your option) any later version. |
---|
10 | |
---|
11 | Trad-Lang is distributed in the hope that it will be useful, |
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | GNU General Public License for more details. |
---|
15 | |
---|
16 | You should have received a copy of the GNU General Public License |
---|
17 | along with Trad-Lang; if not, write to the Free Software |
---|
18 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
19 | |
---|
20 | Copyright 2003-2020 |
---|
21 | Florent Jugla <florent.jugla@eledo.com>, |
---|
22 | Philippe Riviere <fil@rezo.net>, |
---|
23 | Chryjs <chryjs!@!free!.!fr>, |
---|
24 | kent1 <kent1@arscenic.info> |
---|
25 | Cerdic <cedric@yterium.com> |
---|
26 | */ |
---|
27 | |
---|
28 | /** |
---|
29 | * Lire la date de derniere modif d'un fichier versionne |
---|
30 | * (retourne 0 si le fichier n'est pas versionne) |
---|
31 | * @param string $dir_repo |
---|
32 | * @param string $file |
---|
33 | * @return false|int |
---|
34 | */ |
---|
35 | function salvatore_svn_lastmodified_file($dir_repo, $file) { |
---|
36 | |
---|
37 | $d = getcwd(); |
---|
38 | chdir($dir_repo); |
---|
39 | $file = escapeshellarg($file); |
---|
40 | $lastmodified = exec('env LC_MESSAGES=en_US.UTF-8 svn info ' . $file . "| awk '/^Last Changed Date/ { print $4 \" \" $5 }'"); |
---|
41 | $lastmodified = strtotime($lastmodified); |
---|
42 | chdir($d); |
---|
43 | return $lastmodified; |
---|
44 | } |
---|
45 | |
---|
46 | /** |
---|
47 | * Afficher le status d'un ou plusieurs fichiers |
---|
48 | * @param string $dir_repo |
---|
49 | * @param string|array $file_or_files |
---|
50 | * @return string |
---|
51 | */ |
---|
52 | function salvatore_svn_status_file($dir_repo, $file_or_files) { |
---|
53 | |
---|
54 | if (is_array($file_or_files)) { |
---|
55 | $file_or_files = array_map('escapeshellarg', $file_or_files); |
---|
56 | $file_or_files = implode(' ', $file_or_files); |
---|
57 | } |
---|
58 | else { |
---|
59 | $file_or_files = escapeshellarg($file_or_files); |
---|
60 | } |
---|
61 | |
---|
62 | $d = getcwd(); |
---|
63 | chdir($dir_repo); |
---|
64 | $output = array(); |
---|
65 | exec("svn status $file_or_files 2>&1", $output); |
---|
66 | chdir($d); |
---|
67 | return implode("\n", $output); |
---|
68 | } |
---|
69 | |
---|
70 | |
---|
71 | /** |
---|
72 | * Commit une liste de fichiers avec un message et auteur fourni |
---|
73 | * |
---|
74 | * @param string $dir_repo |
---|
75 | * @param array $files |
---|
76 | * @param string $message |
---|
77 | * @param string $author |
---|
78 | * @param string $user |
---|
79 | * @param string $pass |
---|
80 | * @return array |
---|
81 | */ |
---|
82 | function salvatore_svn_commit_files($dir_repo, $files, $message, $author, $user=null, $pass=null) { |
---|
83 | |
---|
84 | // lister deja les fichiers qui necessitent un svn add (fichiers ajoutes qui ne sont pas dans le repo) |
---|
85 | $files_to_add = array(); |
---|
86 | foreach ($files as $file) { |
---|
87 | if (!salvatore_svn_lastmodified_file($dir_repo, $file)) { |
---|
88 | $files_to_add[] = $file; |
---|
89 | } |
---|
90 | } |
---|
91 | |
---|
92 | $files = array_map('escapeshellarg', $files); |
---|
93 | $files = implode(' ', $files); |
---|
94 | |
---|
95 | $files_to_add = array_map('escapeshellarg', $files_to_add); |
---|
96 | $files_to_add = implode(' ', $files_to_add); |
---|
97 | |
---|
98 | $d = getcwd(); |
---|
99 | chdir($dir_repo); |
---|
100 | $output = array(); |
---|
101 | $res = true; |
---|
102 | |
---|
103 | $auth = ""; |
---|
104 | $auth_disp = ""; |
---|
105 | if ($user) { |
---|
106 | $auth .= " --username=".escapeshellarg($user); |
---|
107 | $auth_disp .= " --username=".escapeshellarg('xxxxx'); |
---|
108 | } |
---|
109 | if ($user) { |
---|
110 | $auth .= " --password=".escapeshellarg($pass); |
---|
111 | $auth_disp .= " --password=".escapeshellarg('xxxxx'); |
---|
112 | } |
---|
113 | |
---|
114 | $commands = []; |
---|
115 | if ($files_to_add) { |
---|
116 | $commands[] = "svn add --quiet $files_to_add 2>&1"; |
---|
117 | } |
---|
118 | // TODO : activer le commit quand on sera en prod |
---|
119 | // $commands[] = "svn commit $files{$auth} --no-auth-cache --non-interactive --trust-server-cert -m " . escapeshellarg($message) . " 2>&1"; |
---|
120 | |
---|
121 | foreach ($commands as $command) { |
---|
122 | $output[] = "> " . ($auth ? str_replace($auth, $auth_disp, $command) : $command); |
---|
123 | $return_var = 0; |
---|
124 | exec($command, $output, $return_var); |
---|
125 | // si une erreur a eu lieu le signaler dans le retour |
---|
126 | if ($return_var) { |
---|
127 | $res = false; |
---|
128 | } |
---|
129 | } |
---|
130 | if ($res and $author and _SALVATORE_SVN_PROPSET) { |
---|
131 | if ($revision = exec("svn up . && env LC_MESSAGES=en_US.UTF-8 svn info . |awk '/^Last Changed Rev/ { print $4 }'")) { |
---|
132 | $command = "svn propset --revprop -r $revision svn:author ".escapeshellarg($author). " .{$auth} --no-auth-cache --non-interactive --trust-server-cert"; |
---|
133 | $output[] = "> " . ($auth ? str_replace($auth, $auth_disp, $command) : $command); |
---|
134 | exec($command, $output, $return_var); |
---|
135 | if ($return_var) { |
---|
136 | $res = false; |
---|
137 | } |
---|
138 | } |
---|
139 | } |
---|
140 | chdir($d); |
---|
141 | |
---|
142 | return array($res, implode("\n", $output)); |
---|
143 | } |
---|
144 | |
---|
145 | |
---|
146 | |
---|
147 | /** |
---|
148 | * Rien a faire : en svn le commit push, mais fonction symetrique de git |
---|
149 | * |
---|
150 | * @param string $dir_repo |
---|
151 | * @param null $user |
---|
152 | * @param null $pass |
---|
153 | * @return array |
---|
154 | */ |
---|
155 | function salvatore_svn_push_repository($dir_repo, $user=null, $pass=null) { |
---|
156 | return array(true, ''); |
---|
157 | } |
---|