Code

Fixed helpviewer - thanks to jan
[gosa.git] / gosa-core / update-gosa
1 #!/usr/bin/php5
2 <?php
3 /*
4  * This code is part of GOsa (http://www.gosa-project.org)
5  * Copyright (C) 2003-2008 GONICUS GmbH
6  *
7  * ID: $$Id: main.php 9254 2008-03-03 15:57:49Z cajus $$
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
24 define ("GOSA_HOME", dirname(__FILE__));
25 define ("LOCALE_DIR", GOSA_HOME."/locale");
26 define ("PLUGSTATE_DIR", GOSA_HOME."/state");
28 function print_usage()
29 {
30         ?>
31 update-gosa - class cache updated and plugin manager for GOsa
32 Usage: update-gosa install dsc     Install the plugin using the dsc information
33                                    placed in the plugin source directory.
35        update-gosa remove plugin   Remove the plugin named "plugin" from
36                                    the current configuration.
38        update-gosa lists           Lists installed plugins
40        update-gosa rescan-i18n     Rebuilds the translations
42        update-gosa rescan-classes  Rebuilds the class list
43        
44        update-gosa                 Shortcut for rescan-classes and rescan-i18n
45 <?php
46         exit (1);
47 }
50 function rmdirRecursive($path, $followLinks=false) {
51   $dir= opendir($path);
52   while($entry= readdir($dir)) {
53     if(is_file($path."/".$entry) || ((!$followLinks) && is_link($path."/".$entry))) {
54       unlink($path."/".$entry);
55     } elseif (is_dir($path."/".$entry) && $entry!='.' && $entry!='..') {
56       rmdirRecursive($path."/".$entry);
57     }
58   }
59   closedir($dir);
60   return rmdir($path);
61 }
64 /* Function to include all class_ files starting at a given directory base */
65 function get_classes($folder= ".")
66 {
67   static $base_dir= "";
68   static $result= array();
70   if ($base_dir == ""){
71     if ($folder == "."){
72       $base_dir= getcwd();
73     } else {
74       $base_dir= $folder;
75     }
76   }
78   $currdir=getcwd();
79   if ($folder){
80     chdir("$folder");
81   }
83   $dh = opendir(".");
84   while(false !== ($file = readdir($dh))){
86     if (preg_match("/.*\.svn.*/", $file) ||
87         preg_match("/.*smarty.*/i",$file) ||
88         preg_match("/.*\.tpl.*/",$file) ||
89         ($file==".") ||($file =="..")){
90       continue;
91     }
93     /* Recurse through all "common" directories */
94     if (is_dir($file)){
95       get_classes($file);
96       continue;
97     }
99     /* Only take care about .inc and .php files... */
100     if (!(preg_match('/\.php$/', $file) || preg_match('/\.inc$/', $file))){
101       continue;
102     }
104     /* Include existing class_ files */
105     $contents= file($file);
106     foreach($contents as $line){
107       $line= chop($line);
108       if (preg_match('/^\s*class\s*\w.*$/', $line)){
109         $class= preg_replace('/^\s*class\s*(\w+).*$/', '\1', $line);
110         $result[$class]= preg_replace("%$base_dir/%", "", "$currdir/$folder/$file");
111       }
112     }
113   }
115   closedir($dh);
116   chdir($currdir);
118   return ($result);
122 function rescan_classes()
124         echo "Updating class cache...\n";
125         $class_mapping= get_classes();
126         $filename= GOSA_HOME."/include/class_location.inc";
128         /* Sanity checks */
129         if (!file_exists($filename) || is_writable($filename)) {
131             if (!$handle= fopen($filename, 'w')) {
132                  echo "Cannot open file \"$filename\" - aborted\n";
133                  exit (1);
134             }
136         } else {
137             echo "File \"$filename\" is not writable - aborted\n";
138             exit (2);
139         }
141         fwrite ($handle, "<?php\n\$class_mapping= array(\n");
142         foreach ($class_mapping as $key => $value){
143           fwrite ($handle, "                \"$key\" => \"$value\",\n");
144         }
145         fwrite ($handle, " );\n");
147         fclose($handle);
151 function rescan_i18n()
153         echo "Updating internationalization...\n";
154         $languages= array();
155         $size= strlen(LOCALE_DIR);
157         /* Get all available messages.po files, sort them for languages */
158         $dir= new RecursiveDirectoryIterator(LOCALE_DIR);
159         $all= new RecursiveIteratorIterator($dir);
160         foreach ( $all as $element ){
161                 if ($element->isFile() && preg_match('/\/LC_MESSAGES\/messages.po$/', $element->getPathname())){
162                         $lang= preg_replace('/^.*\/([^\/]+)\/LC_MESSAGES\/.*$/', '\1', $element);
163                         if (!isset($languages[$lang])){
164                                 $languages[$lang]= array();
165                         }
166                         $languages[$lang][]= substr($element->getPathName(), $size+1);
167                 }
168         }
170         /* For each language, merge the target .mo to the compiled directory. */
171         foreach ($languages as $language => $po_files){
172                 if (!is_dir(LOCALE_DIR."/compiled/${language}/LC_MESSAGES")){
173                         if (!mkdir (LOCALE_DIR."/compiled/${language}/LC_MESSAGES", 0755, TRUE)){
174                                 echo "Failed to create '".LOCALE_DIR."/compiled/${language}/LC_MESSAGES'- aborted";
175                                 exit (3);
176                         }
177                 }
179                 /* Cat all these po files into one single file */
180                 system ("(cd ".LOCALE_DIR." && msgcat ".implode(" ", $po_files)." > compiled/${language}/LC_MESSAGES/messages.po)", $val);
181                 if ($val != 0){
182                         echo "Merging of message files failed - aborted";
183                         exit (4);
184                 }
185                 system ("(cd ".LOCALE_DIR."/compiled/${language}/LC_MESSAGES && msgfmt -o messages.mo messages.po && rm messages.po)", $val);
186                 if ($val != 0){
187                         echo "Compiling of message files failed - aborted";
188                         exit (5);
189                 }
190         }
194 function rescan_guide()
196         $master_guide= "doc/guide.xml";
197         echo "Updating Online Help Index...\n";
198         $master_guide_content="<?xml version=\"1.0\"?>\n".
199                 "<!--\n".
200                 "\tWARNING:\n".
201                 "\tThis file is automatically generated by update-online-help.\n".
202                 "\tIf you want to add entries, use doc/core/guide.xml or doc/plugins/\"Appropriate Plugin Directory\"/guide.xml.\n".
203                 "\tThen execute update-online-help to merge them into this file.\n".
204                 "-->\n\n".
205                 "<!--\n".
206                 "\tThis xml file specifies which class is documented in which help file.\n".
207                 "\tIf isset ( \$_SESSION['current_class_for_help'] ) then open the helpfile which is\n".
208                 "\tspecified for this class below.\n".
209                 "-->\n\n".
210                 "<!--\n".
211                 "\t<ENTRY NAME='class name' VALUE='displayed text' PATH='path to helpfiles' FILE='path to htmlfile' />\n".
212                 "\tLeave blank to display message \"There is no helpfile specified for this class.\"\n".
213                 "-->\n".
214                 "<ENTRIES>\n";
216         $guide= 'doc/core/guide.xml';
217         if(file_exists($guide) && is_readable($guide)) {
218                 $master_guide_content.= file_get_contents($guide);
219         }
220         
221         $plugins= scandir('doc/plugins');
222         foreach($plugins as $key => $plugin) {
223                 if($plugin != '.' && $plugin != '..') {
224                         if(is_dir('doc/plugins/'.$plugin)) {
225                                 $guide= 'doc/plugins/'.$plugin.'/guide.xml';
226                                 if(file_exists($guide) && is_readable($guide)) {
227                                         $master_guide_content.= file_get_contents($guide);
228                                 }
229                         }
230                 }
231         }
233         $master_guide_content.= "</ENTRIES>";
234         
235         $master_guide_content= preg_replace("/[ \t][ \t]*/", " ", $master_guide_content);
237         if(is_writable($master_guide)) {
238                 file_put_contents($master_guide, $master_guide_content);
239         }
244 function parse_ini($file)
246         global $description, $provides, $depends, $versions, $conflicts;
248         $res= "";
249         if (file_exists($file)){
250                 $tmp= parse_ini_file($file, TRUE);
252                 if (isset($tmp['gosa-plugin'])){
253                         $plugin= &$tmp['gosa-plugin'];
254                         if (isset($plugin['name'])&& isset($plugin['description'])){
255                                 $res= $plugin['name'];
256                                 $provides[$res]= $plugin[$res];
257                                 $description[$res]= $plugin['description'];
258                                 $versions[$res]= $plugin['version'];
259                                 if (isset($plugin['depends'])){
260                                         $depends[$res]= explode(',', preg_replace('/\s+/', '', $plugin['depends']));
261                                 }
262                                 if (isset($plugin['conflicts'])){
263                                         $conflicts[$res]= explode(',', preg_replace('/\s+/', '', $plugin['conflicts']));
264                                 }
265                         }
266                 }
267         }
269         return $res;
273 function dependency_check()
275         global $description, $provides, $depends;
277         foreach ($depends as $name => $pl_depends){
278                 foreach ($pl_depends as $pl){
279                         if (!in_array($pl, $provides)){
280                                 echo "! Error: plugin '$name' depends on '$pl' which is not provided by any plugin\n\n";
281                                 exit (1);
282                         }
283                 }
284         }
288 function load_plugins()
290         if (!is_dir(PLUGSTATE_DIR)){
291                 if (!mkdir (PLUGSTATE_DIR, 0755, TRUE)){
292                         echo "Cannot create plugstate dir '".PLUGSTATE_DIR."' - aborted\n";
293                         exit (2);
294                 }
295         }
296         $dir= new DirectoryIterator(PLUGSTATE_DIR);
297         foreach ($dir as $entry){
298                 if ($dir->isDir() && !preg_match('/^\./', $dir->__toString())){
299                         $file= $dir->getPathName()."/plugin.dsc";
300                         if (parse_ini($file) == ""){
301                                 echo "! Warning: plugin ".$dir->getPathName()." is missing declarations\n";
302                         }
303                 }
304         }
308 function list_plugins()
310         global $description, $versions;
311         $count= 0;
313         /* Load plugin list */
314         load_plugins();
316         /* Show plugins */
317         foreach ($description as $name => $dsc){
318                 if ($count == 0){
319                         echo "Plugin\t\t|Version |Description\n";
320                         echo "----------------------------------------------------------------------------\n";
321                 }
322                 $ver= $versions[$name];
323                 echo "$name\t\t|$ver\t |$dsc\n";
324                 $count++;
325         }
327         /* Yell about non existing plugins... */
328         if ($count == 0){
329                 echo "No plugins found...\n\n";
330         } else {
331                 # Check for dependencies
332                 dependency_check();
333                 echo "\n";
334         }
338 function install_plugin($file)
340         global $description, $provides, $depends, $conflicts;
342         /* Load plugin list */
343         load_plugins();
345         /* Load .dsc file */
346         if (file_exists($file)){
347                 $tmp= parse_ini_file($file, TRUE);
349                 if (isset($tmp['gosa-plugin'])){
350                         $plugin= &$tmp['gosa-plugin'];
351                         if (isset($plugin['name'])&& isset($plugin['description'])){
352                                 $name= $plugin['name'];
353                                 $description= $plugin['description'];
354                                 $depends= array();
355                                 if (isset($plugin['depends'])){
356                                         $depends= explode(',', preg_replace('/\s+/', '', $plugin['depends']));
357                                 }
359                                 /* Already installed? */
360                                 if (isset($provides[$name])){
361                                         echo "! Error: plugin already installed\n\n";
362                                         exit (3);
363                                 }
365                                 /* Check if dependencies are fullfilled */
366                                 foreach ($depends as $dep){
367                                         $found= false;
368                                         foreach ($provides as $provide => $dummy){
369                                                 if ($dep == $provide){
370                                                         $found= true;
371                                                         break;
372                                                 }
373                                         }
374                                         if (!$found){
375                                                 echo "! Error: plugin depends on '$dep', but this is not installed\n\n";
376                                                 exit (3);
377                                         }
378                                 }
380                                 /* Check for conflicts */
381                                 foreach ($conflicts as $conf){
382                                         if (!in_array($conf, $provides)){
383                                                 echo "! Warning: plugin conflicts with '$conf'\n\n";
384                                         }
385                                 }
387                                 /* Create plugstate directory and touch plugin.lst */
388                                 if (!mkdir (PLUGSTATE_DIR."/$name", 0755, TRUE)){
389                                         echo "Failed to create '".PLUGSTATE_DIR."/$name - aborted";
390                                         exit (3);
391                                 }
392                                 if (!$handle= fopen(PLUGSTATE_DIR."/$name/plugin.lst", 'w')) {
393                                         echo "Cannot open file '$filename' - aborted\n";
394                                         exit (1);
395                                 }
397                                 echo "Installing plugin '$name'...\n";
399                                 /* Copy and fill plugin.lst */
400                                 $path= dirname($file);
401                                 $dir= new RecursiveDirectoryIterator($path);
402                                 $all= new RecursiveIteratorIterator($dir);
403                                 foreach ( $all as $entry ){
404                                         $source= $path."/".substr($entry->getPathName(), strlen($path) + 1);
406                                         /* Skip description - it belongs to the state dir */
407                                         if (preg_match('/\/plugin.dsc$/', $source)){
408                                                 copy ($source, PLUGSTATE_DIR."/$name/plugin.dsc");
409                                                 continue;
410                                         }
412                                         /* Skip well known directories */
413                                         if (preg_match('/^\.+$/', $source) || preg_match('/\/\.svn\//', $source)) {
414                                                 continue;
415                                         }
417                                         /* Calculate destination */
418                                         if (preg_match("%^.*locale/%", $source)){
419                                                 $dest= GOSA_HOME."/locale/plugin/$name/".preg_replace("%^.*locale/%", "", $source);
420                                         } else {
421                                                 $dest= GOSA_HOME."/plugins/".substr($entry->getPathName(), strlen($path) + 1);
422                                         }
424                                         /* Destination exists in case of directories? */
425                                         if ($entry->isDir()){
426                                                 if (!is_dir($dest)){
427                                                         mkdir($dest, 0755, TRUE);
428                                                         fwrite ($handle, "$dest\n");
429                                                 }
430                                         } else {
431                                                 if (!is_dir(dirname($dest))){
432                                                         mkdir(dirname($dest), 0755, TRUE);
433                                                         fwrite ($handle, dirname($dest)."\n");
434                                                 }
435                                         }
437                                         /* Copy files */
438                                         if ($entry->isFile()){
439                                                 copy ($source, $dest);
440                                         }
442                                         /* Note what we did... */
443                                         fwrite ($handle, "$dest\n");
444                                 }
446                                 fclose($handle);
447                         }
448                 }
449         }
450         
451         /* Update caches */
452         rescan_classes();
453         rescan_i18n();
454         rescan_guide();
458 function remove_plugin($name)
460         global $description, $depends;
462         /* Load plugin list */
463         load_plugins();
465         /* Present? */
466         if (!isset($description[$name])){
467                 echo "! Error: cannot find a plugin named '$name'\n\n";
468                 exit (1);
469         }
471         /* Depends? */
472         foreach ($depends as $sname => $pl_depends){
473                 if (in_array($name, $pl_depends)){
474                         echo "! Error: plugin '$sname' depends on '$name' - cannot remove it\n\n";
475                         exit (1);
476                 }
477         }
479         /* Load information */
480         if (!file_exists(PLUGSTATE_DIR."/$name/plugin.lst")){
481                 echo "! Error: cannot remove plugin '$name' - no install history found\n\n";
482                 exit (1);
483         }
485         echo "Removing plugin '$name'...\n";
486         $contents= file(PLUGSTATE_DIR."/$name/plugin.lst");
487         $cnv= array();
488         foreach($contents as $line){
489                 $entry= chop($line);
490                 $cnv[strlen($entry).":$entry"]= $entry;
491         }
492         krsort($cnv);
494         /* Remove files first */
495         clearstatcache();
496         foreach ($cnv as $entry){
497                 if (is_dir($entry)){
498                         rmdir($entry);
499                         continue;
500                 }
501                 if (file_exists($entry)){
502                         unlink($entry);
503                 }
504         }
506         /* Remove state directory for plugin */
507         rmdirRecursive(PLUGSTATE_DIR."/$name");
509         /* Update caches */
510         rescan_classes();
511         rescan_i18n();
512         rescan_guide();
517 /* Fill global values */
518 $description= $provides= $depends= $versions= $conflicts= array();
520 /* Action specified? */
521 if ($argc < 2){
522         rescan_classes();
523         rescan_i18n();
524         rescan_guide();
525         exit (0);
528 switch ($argv[1]){
529         case 'install':
530                 if (isset($argv[2])){
531                         install_plugin($argv[2]);
532                 } else {
533                         echo "Usage: update-gosa install dsc-file\n\n";
534                         exit (1);
535                 }
536                 break;
537         case 'list':
538                 list_plugins();
539                 break;
540         case 'remove':
541                 if (isset($argv[2])){
542                         remove_plugin($argv[2]);
543                 } else {
544                         echo "Usage: update-gosa remove plugin-name\n\n";
545                         exit (1);
546                 }
547                 break;
548         case 'rescan-i18n':
549                 rescan_i18n();
550                 break;
551         case 'rescan-classes':
552                 rescan_classes();
553                 break;
554         default:
555                 echo "Error: Supplied command not known\n\n";
556                 print_usage();
557                 break;
561 ?>