root/trunk/midgard/tools/midrepository/common.php

Revision 7101, 5.7 kB (checked in by bergius, 5 years ago)

Initial commit of the MidRepository? workflow extension
Issue number:
Obtained from:
Submitted by:
Reviewed by:

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?
2 // +----------------------------------------------------------------------+
3 // | MidRepository: Common functions                                      |
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) 2003 David Schmitter, Dataflow Solutions GmbH          |
6 // +----------------------------------------------------------------------+
7 // | This program is free software; you can redistribute it and/or modify |
8 // | it under the terms of the GNU General Public License as published    |
9 // | by the Free Software Foundation; either version 2 of the License,    |
10 // | or (at your option) any later version.                               |
11 // |                                                                      |
12 // | This program is distributed in the hope that it will be useful,      |
13 // | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
14 // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        |
15 // | GNU General Public License for more details.                         |
16 // |                                                                      |
17 // | You should have received a copy of the GNU General Public License    |
18 // | along with this program; if not, write to the Free Software          |
19 // | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, |
20 // | USA                                                                  |
21 // +----------------------------------------------------------------------+
22 // | Author: David Schmitter <schmitt@dataflow.ch>                        |
23 // +----------------------------------------------------------------------+
24 //
25
26
27 function handle_pear_error ($error_obj) {
28   if (DEBUG) echo ($error_obj->getMessage()."\n".$error_obj->getDebugInfo() . '<br>');
29 }
30
31 /**
32  * Get a VCSession for a branch and register it in the global VCSessionRegistry.
33  * @param string $branch
34  * @return object $VCSession
35  */
36 function &getSession($branch) {
37   global $VCSConf;
38   if (!($vcs = VCSessionRegistry::getVCSession($branch))) {
39     if (isset($VCSConf[$branch])) {
40       $vcs = & new VCSession(&$VCSConf[$branch]);
41       VCSessionRegistry::addVCSession($branch,$vcs);
42     } else {
43       return false;
44     }
45   }
46   return $vcs;
47 }
48
49 /**
50  * Initialize the default VCSession for this branch and register it in the global VCSessionRegistry.
51  */
52 function vcInit() {
53   $VCSession = new VCSession();
54   VCSessionRegistry::addDefaultVCSession($VCSession);
55 }
56
57 /**
58  * Schedule a command for execution via at
59  * @param string $cmd
60  */
61 function batchStart($cmd) {
62   $pipe = popen ("/usr/bin/at now &> /dev/null", "w");
63   fwrite($pipe, "$cmd\n");
64   pclose($pipe);
65 }
66
67 /**
68  * Copies the values of $a2 into $a1 without destroying the keys.
69  * @param array $a1
70  * @param array $a2
71  * @return array $a3
72  */
73 function &array_copy(&$a1, &$a2) {
74   foreach($a2 as $key => $val) {
75     $a1[$key] = $val;
76   }
77   return $a1;
78 }
79
80 /**
81  * Makes a language id out of a language code or a language id.
82  * @param mixed $lang
83  * @return int $lang
84  */
85 function reallyILang($lang) {
86   if (is_numeric($lang)) {
87     return $lang;
88   } else {
89     $langobj = mgd_get_language_by_code($lang);
90     if (!is_object($langobj)) {
91       trigger_error("I don't know this language code", E_USER_WARNING);
92       return 0;
93     } else {
94       return $langobj->id;
95     }
96   }
97 }
98
99 /**
100  * Makes a language id out of a language code or a language id.
101  * @param mixed $lang
102  * @return string $lang
103  */
104 function reallySLang($lang) {
105   if (is_string($lang)) {
106     return $lang;
107   } else {
108     $langobj = mgd_get_language($lang);
109     if (!is_object($langobj)) {
110       trigger_error("I don't know this language id", E_USER_WARNING);
111       return 0;
112     } else {
113       return $langobj->code;
114     }
115   }
116 }
117
118 /**
119  * Creates the 0/0/ - f/f/ hierarchy.
120  * @param string $base the directory in which to create the hierarchy
121  */
122 function create_hash_dirs ($base) {
123   for ($i = 0; $i < 16; $i++) {
124     $top = "$base/" . strtolower(sprintf('%X', $i));
125     mkdir($top);
126     for ($j = 0; $j < 16; $j++) {
127       mkdir("$top/" . strtolower(sprintf('%X', $j)));
128     }
129   }
130 }
131
132 /**
133  * Gets the relative path of an object in the 0/0/ - f/f/ hierarchy.
134  * @param string $guid guid of the obj
135  * @return string $path
136  */
137 function get_hash_path($guid) {
138   return "{$guid{0}}/{$guid{1}}/$guid";
139 }
140
141 /**
142  * Gets the directory in the 0/0/ - f/f/ hierarchy that an object is in
143  * @param string $guid guid of the obj
144  * @return string $dir
145  */
146 function get_hash_dir($guid) {
147   return "{$guid{0}}/{$guid{1}}";
148 }
149
150 /**
151  * Checks if $spec has the format of a guid
152  * @param string $spec
153  * @return boolean $isGuid
154  */
155 function is_guid($spec) {
156   if (strlen($spec) == 32 and strspn($spec, '0123456789abcdef') == 32) {
157     return true;
158   } else {
159     return false;
160   }
161 }
162
163 /**
164  * Gets all directories where changes should be placed for import for this branch
165  * @param int $branchid
166  * @return array $dirs;
167  */
168 function &getOutgoingDirs($branchid) {
169   $dirs = array();
170   foreach ($GLOBALS['VCSConf'] as $dbconf) {
171     if (@$dbconf['branchid'] == $branchid and !isset($dbconf['vcconf'])) {
172       $dirs[] = $dbconf['fsconf']['import'];
173     }
174   }
175   return $dirs;
176 }
177
178 /**
179  * Subtracts the last piece of a path.
180  */
181 function path_minus_one($path) {
182   $lastslash = strrpos($path, '/');
183   if ($lastslash == 0) { //pos 0 or no slash left
184     return '';
185   }
186   return substr($path, 0, $lastslash);
187 }
188
189 function isRoot() {
190   $midgard = mgd_get_midgard();
191   if (!$midgard->root) {
192     return MGD_ERR_ACCESS_DENIED;
193   } else return true;
194 }
195
196 if (!function_exists("file_get_contents")) {
197   function file_get_contents($filename) {
198     $fp = @fopen($filename, "r");
199     if (!($fp)) {
200       return 0;
201     }
202     $temp = '';
203     while (!feof($fp)) {
204       $temp .= fread($fp, 4096);
205     }
206     return $temp;
207   }
208 }
209
210 ?>
Note: See TracBrowser for help on using the browser.