2907dee9607b93e709f393b04a19da96a09c920a
1 #!/usr/bin/php5
2 <?php
4 define ("LOCALE_DIR", dirname(__FILE__)."/locale");
5 define ("PLUGSTATE_DIR", dirname(__FILE__)."/state");
7 function print_usage()
8 {
9 ?>
10 update-gosa - class cache updated and plugin manager for GOsa
11 Usage: update-gosa install-plugin dir Install the plugin placed in "dir" to
12 the GOsa tree.
14 update-gosa remove-plugin plugin Remove the plugin named "plugin" from
15 the current configuration.
17 update-gosa list-plugins Lists installed plugins
19 update-gosa rescan-i18n Rebuilds the translations
21 update-gosa rescan-classes Rebuilds the class list
23 update-gosa Shortcut for rescan-classes and rescan-i18n
24 <?php
25 exit (1);
26 }
29 /* Function to include all class_ files starting at a given directory base */
30 function get_classes($folder= ".")
31 {
32 static $base_dir= "";
33 static $result= array();
35 if ($base_dir == ""){
36 $base_dir= getcwd();
37 }
39 $currdir=getcwd();
40 if ($folder){
41 chdir("$folder");
42 }
44 $dh = opendir(".");
45 while(false !== ($file = readdir($dh))){
47 if (preg_match("/.*\.svn.*/", $file) ||
48 preg_match("/.*smarty.*/i",$file) ||
49 preg_match("/.*\.tpl.*/",$file) ||
50 ($file==".") ||($file =="..")){
51 continue;
52 }
54 /* Recurse through all "common" directories */
55 if (is_dir($file)){
56 get_classes($file);
57 continue;
58 }
60 /* Only take care about .inc and .php files... */
61 if (!(preg_match('/\.php$/', $file) || preg_match('/\.inc$/', $file))){
62 continue;
63 }
65 /* Include existing class_ files */
66 $contents= file($file);
67 foreach($contents as $line){
68 $line= chop($line);
69 if (preg_match('/^\s*class\s*\w.*$/', $line)){
70 $class= preg_replace('/^\s*class\s*(\w+).*$/', '\1', $line);
71 $result[$class]= preg_replace("%$base_dir/%", "", "$currdir/$folder/$file");
72 }
73 }
74 }
76 closedir($dh);
77 chdir($currdir);
79 return ($result);
80 }
83 function rescan_classes()
84 {
85 $class_mapping= get_classes();
86 $filename= "include/class_location.inc";
88 /* Sanity checks */
89 if (!file_exists($filename) || is_writable($filename)) {
91 if (!$handle= fopen($filename, 'w')) {
92 echo "Cannot open file \"$filename\" - aborted\n";
93 exit (1);
94 }
96 } else {
97 echo "File \"$filename\" is not writable - aborted\n";
98 exit (2);
99 }
101 fwrite ($handle, "<?php\n\$class_mapping= array(\n");
102 foreach ($class_mapping as $key => $value){
103 fwrite ($handle, " \"$key\" => \"$value\",\n");
104 }
105 fwrite ($handle, " );\n?>");
107 fclose($handle);
108 }
111 function rescan_i18n()
112 {
113 $languages= array();
114 $size= strlen(LOCALE_DIR);
116 /* Get all available messages.po files, sort them for languages */
117 $dir= new RecursiveDirectoryIterator(LOCALE_DIR);
118 $all= new RecursiveIteratorIterator($dir);
119 foreach ( $all as $element ){
120 if ($element->isFile() && preg_match('/\/LC_MESSAGES\/messages.po$/', $element->getPathname())){
121 $lang= preg_replace('/^.*\/([^\/]+)\/LC_MESSAGES\/.*$/', '\1', $element);
122 if (!isset($languages[$lang])){
123 $languages[$lang]= array();
124 }
125 $languages[$lang][]= substr($element->getPathName(), $size+1);
126 }
127 }
129 /* For each language, merge the target .mo to the compiled directory. */
130 foreach ($languages as $language => $po_files){
131 if (!is_dir(LOCALE_DIR."/compiled/${language}/LC_MESSAGES")){
132 if (!mkdir (LOCALE_DIR."/compiled/${language}/LC_MESSAGES", 0755, TRUE)){
133 echo "Failed to create '".LOCALE_DIR."/compiled/${language}/LC_MESSAGES'- aborted";
134 exit (3);
135 }
136 }
138 /* Cat all these po files into one single file */
139 system ("(cd ".LOCALE_DIR." && msgcat ".implode(" ", $po_files)." > compiled/${language}/LC_MESSAGES/messages.po)", $val);
140 if ($val != 0){
141 echo "Merging of message files failed - aborted";
142 exit (4);
143 }
144 system ("(cd ".LOCALE_DIR."/compiled/${language}/LC_MESSAGES && msgfmt -o messages.mo messages.po && rm messages.po)", $val);
145 if ($val != 0){
146 echo "Compiling of message files failed - aborted";
147 exit (5);
148 }
149 }
150 }
153 function parse_ini($file)
154 {
155 global $description, $provides, $depends;
157 $res= "";
158 if (file_exists($file)){
159 $tmp= parse_ini_file($file, TRUE);
161 if (isset($tmp['gosa-plugin'])){
162 $plugin= &$tmp['gosa-plugin'];
163 if (isset($plugin['name'])&& isset($plugin['description']) && isset($plugin['provides'])){
164 $res= $plugin['name'];
165 $provides[$res]= $plugin['provides'];
166 $description[$res]= $plugin['description'];
167 if (isset($plugin['depends'])){
168 $depends[$res]= explode(',', preg_replace('/\s+/', '', $plugin['depends']));
169 }
170 }
171 }
172 }
174 return $res;
175 }
178 function dependency_check()
179 {
180 global $description, $provides, $depends;
182 foreach ($depends as $name => $pl_depends){
183 foreach ($pl_depends as $pl){
184 if (!in_array($pl, $provides)){
185 echo "! Error: plugin '$name' depends on '$pl' which is not provided by any plugin\n\n";
186 exit (1);
187 }
188 }
189 }
190 }
193 function load_plugins()
194 {
195 if (!file_exists(PLUGSTATE_DIR)){
196 if (!mkdir (PLUGSTATE_DIR", 0755, TRUE)){
197 echo "Cannot create plugstate dir '".PLUGSTATE_DIR."' - aborted\n";
198 exit (2);
199 }
200 }
201 $dir= new DirectoryIterator(PLUGSTATE_DIR);
202 foreach ($dir as $entry){
203 if ($dir->isDir() && !preg_match('/^\./', $dir->__toString())){
204 $file= $dir->getPathName()."/plugin.dsc";
205 if (!parse_ini($file)){
206 echo "! Warning: plugin ".$dir->getPathName()." is missing declarations\n";
207 }
208 }
209 }
210 }
213 function list_plugins()
214 {
215 global $description;
216 $count= 0;
218 /* Load plugin list */
219 load_plugins();
221 /* Show plugins */
222 foreach ($description as $name => $dsc){
223 if ($count == 0){
224 echo "Plugin\t\t| Description\n";
225 echo "------------------------------------------------------------------------\n";
226 }
227 echo "* $name\t\t| ".$dsc."\n";
228 $count++;
229 }
231 /* Yell about non existing plugins... */
232 if ($count == 0){
233 echo "No plugins found...\n\n";
234 } else {
235 # Check for dependencies
236 dependency_check();
237 echo "\n";
238 }
239 }
242 function install_plugin($name)
243 {
244 global $description, $provides, $depends;
246 /* Load plugin list */
247 load_plugins();
249 # go to the directory, load dsc file
250 # check if it already there
251 # check if all dependencies are fullfilled
252 # copy plugin
253 # update classlist
254 # update i18n
256 #if (isset($)){
257 #}
258 }
261 /* Fill global values */
262 $description= $provides= $depends= array();
264 /* Action specified? */
265 if ($argc < 2){
266 rescan_classes();
267 rescan_i18n();
268 exit (0);
269 }
270 switch ($argv[1]){
271 case 'install-plugin':
272 if (isset($argv[2])){
273 install_plugin($argv[2]);
274 } else {
275 echo "Usage: update-gosa install-plugin directory\n\n";
276 exit (1);
277 }
278 break;
279 case 'list-plugins':
280 list_plugins();
281 break;
282 case 'remove-plugin':
283 echo "remove\n";
284 break;
285 case 'rescan-i18n':
286 rescan_i18n();
287 break;
288 case 'rescan-classes':
289 rescan_classes();
290 break;
291 default:
292 echo "Error: Supplied command not known\n\n";
293 print_usage();
294 break;
295 }
298 ?>