Code

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