Code

Update FAQ
[gosa.git] / gosa-core / update-gosa
index 9d3b2738dfbb6e0f6a07b12f02af23a60f7def49..13f2f78e5c7639661dc12cd55c83e9767f83ce35 100755 (executable)
@@ -1,5 +1,25 @@
-#!/usr/bin/php5
+#!/usr/bin/php
 <?php
+/*
+ * This code is part of GOsa (http://www.gosa-project.org)
+ * Copyright (C) 2003-2010 GONICUS GmbH
+ *
+ * ID: $$Id: main.php 9254 2008-03-03 15:57:49Z cajus $$
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
 
 define ("GOSA_HOME", dirname(__FILE__));
 define ("LOCALE_DIR", GOSA_HOME."/locale");
@@ -15,10 +35,12 @@ Usage: update-gosa install dsc     Install the plugin using the dsc information
        update-gosa remove plugin   Remove the plugin named "plugin" from
                                    the current configuration.
 
-       update-gosa lists           Lists installed plugins
+       update-gosa list           Lists installed plugins
 
        update-gosa rescan-i18n     Rebuilds the translations
 
+       update-gosa rescan-images   Rebuilds the themes master image
+
        update-gosa rescan-classes  Rebuilds the class list
        
        update-gosa                 Shortcut for rescan-classes and rescan-i18n
@@ -41,6 +63,20 @@ function rmdirRecursive($path, $followLinks=false) {
 }
 
 
+function get_themes()
+{
+  $themes= array();
+  $d = dir(GOSA_HOME."/html/themes");
+  while (false !== ($entry = $d->read())) {
+    if ($entry[0] != '.') {
+      $themes[]= basename($entry);
+    }
+  }
+  $d->close();
+
+  return $themes;
+}
+
 /* Function to include all class_ files starting at a given directory base */
 function get_classes($folder= ".")
 {
@@ -48,7 +84,11 @@ function get_classes($folder= ".")
   static $result= array();
 
   if ($base_dir == ""){
-    $base_dir= getcwd();
+    if ($folder == "."){
+      $base_dir= getcwd();
+    } else {
+      $base_dir= $folder;
+    }
   }
 
   $currdir=getcwd();
@@ -57,7 +97,7 @@ function get_classes($folder= ".")
   }
 
   $dh = opendir(".");
-  while(false !== ($file = readdir($dh))){
+  while(is_resource($dh) && false !== ($file = readdir($dh))){
 
     if (preg_match("/.*\.svn.*/", $file) ||
         preg_match("/.*smarty.*/i",$file) ||
@@ -67,7 +107,7 @@ function get_classes($folder= ".")
     }
 
     /* Recurse through all "common" directories */
-    if (is_dir($file)){
+    if (is_dir($file) && !file_exists("{$file}/excludeFromAutoLoad")){
       get_classes($file);
       continue;
     }
@@ -88,7 +128,7 @@ function get_classes($folder= ".")
     }
   }
 
-  closedir($dh);
+  @closedir($dh);
   chdir($currdir);
 
   return ($result);
@@ -153,7 +193,7 @@ function rescan_i18n()
                }
 
                /* Cat all these po files into one single file */
-               system ("(cd ".LOCALE_DIR." && msgcat ".implode(" ", $po_files)." > compiled/${language}/LC_MESSAGES/messages.po)", $val);
+               system ("(cd ".LOCALE_DIR." && msgcat --use-first ".implode(" ", $po_files)." > compiled/${language}/LC_MESSAGES/messages.po)", $val);
                if ($val != 0){
                        echo "Merging of message files failed - aborted";
                        exit (4);
@@ -164,6 +204,60 @@ function rescan_i18n()
                        exit (5);
                }
        }
+
+       echo "! Warning: you may need to reload your webservice!\n";
+}
+
+
+function rescan_guide()
+{
+       $master_guide= "doc/guide.xml";
+       echo "Updating Online Help Index...\n";
+       $master_guide_content="<?xml version=\"1.0\"?>\n".
+               "<!--\n".
+               "\tWARNING:\n".
+               "\tThis file is automatically generated by update-online-help.\n".
+               "\tIf you want to add entries, use doc/core/guide.xml or doc/plugins/\"Appropriate Plugin Directory\"/guide.xml.\n".
+               "\tThen execute update-online-help to merge them into this file.\n".
+               "-->\n\n".
+               "<!--\n".
+               "\tThis xml file specifies which class is documented in which help file.\n".
+               "\tIf isset ( \$_SESSION['current_class_for_help'] ) then open the helpfile which is\n".
+               "\tspecified for this class below.\n".
+               "-->\n\n".
+               "<!--\n".
+               "\t<ENTRY NAME='class name' VALUE='displayed text' PATH='path to helpfiles' FILE='path to htmlfile' />\n".
+               "\tLeave blank to display message \"There is no helpfile specified for this class.\"\n".
+               "-->\n".
+               "<ENTRIES>\n";
+
+       $guide= 'doc/core/guide.xml';
+       if(file_exists($guide) && is_readable($guide)) {
+               $master_guide_content.= file_get_contents($guide);
+       }
+       
+       if(file_exists('doc/plugins')) {
+               $plugins= scandir('doc/plugins');
+               foreach($plugins as $key => $plugin) {
+                       if($plugin != '.' && $plugin != '..') {
+                               if(is_dir('doc/plugins/'.$plugin)) {
+                                       $guide= 'doc/plugins/'.$plugin.'/guide.xml';
+                                       if(file_exists($guide) && is_readable($guide)) {
+                                               $master_guide_content.= file_get_contents($guide);
+                                       }
+                               }
+                       }
+               }
+       }
+
+       $master_guide_content.= "</ENTRIES>";
+       
+       $master_guide_content= preg_replace("/[ \t][ \t]*/", " ", $master_guide_content);
+
+       if((file_exists($master_guide) && is_writable($master_guide)) || is_writable('doc')) {
+               file_put_contents($master_guide, $master_guide_content);
+       }
+
 }
 
 
@@ -179,9 +273,9 @@ function parse_ini($file)
                        $plugin= &$tmp['gosa-plugin'];
                        if (isset($plugin['name'])&& isset($plugin['description'])){
                                $res= $plugin['name'];
-                               $provides[$res]= $plugin[$res];
                                $description[$res]= $plugin['description'];
                                $versions[$res]= $plugin['version'];
+                               $provides[$res]= $res;
                                if (isset($plugin['depends'])){
                                        $depends[$res]= explode(',', preg_replace('/\s+/', '', $plugin['depends']));
                                }
@@ -290,7 +384,14 @@ function install_plugin($file)
 
                                /* Check if dependencies are fullfilled */
                                foreach ($depends as $dep){
-                                       if (!in_array($dep, $provides)){
+                                       $found= false;
+                                       foreach ($provides as $provide => $dummy){
+                                               if ($dep == $provide){
+                                                       $found= true;
+                                                       break;
+                                               }
+                                       }
+                                       if (!$found){
                                                echo "! Error: plugin depends on '$dep', but this is not installed\n\n";
                                                exit (3);
                                        }
@@ -335,7 +436,11 @@ function install_plugin($file)
 
                                        /* Calculate destination */
                                        if (preg_match("%^.*locale/%", $source)){
-                                               $dest= GOSA_HOME."/locale/plugin/$name/".preg_replace("%^.*locale/%", "", $source);
+                                               $dest= GOSA_HOME."/locale/plugins/$name/".preg_replace("%^.*locale/%", "", $source);
+                                       } elseif (preg_match("%^.*help/%", $source)) {
+                                               $dest= GOSA_HOME."/doc/plugins/$name/".preg_replace("%^.*help/%", "", $source);
+                                       } elseif (preg_match("%^.*html/%", $source)) {
+                                               $dest= GOSA_HOME."/html/plugins/$name/".preg_replace("%^.*html/%", "", $source);
                                        } else {
                                                $dest= GOSA_HOME."/plugins/".substr($entry->getPathName(), strlen($path) + 1);
                                        }
@@ -370,6 +475,7 @@ function install_plugin($file)
        /* Update caches */
        rescan_classes();
        rescan_i18n();
+       rescan_guide();
 }
 
 
@@ -388,7 +494,7 @@ function remove_plugin($name)
 
        /* Depends? */
        foreach ($depends as $sname => $pl_depends){
-               if (!in_array($name, $pl_depends)){
+               if (in_array($name, $pl_depends)){
                        echo "! Error: plugin '$sname' depends on '$name' - cannot remove it\n\n";
                        exit (1);
                }
@@ -427,9 +533,246 @@ function remove_plugin($name)
        /* Update caches */
        rescan_classes();
        rescan_i18n();
+       rescan_guide();
 }
 
 
+function rescan_images($path, $theme)
+{
+  $widths= array();
+  $heights= array();
+  $paths= array();
+  $posX= array();
+  $posY= array();
+  $baseLength= strlen($path);
+  $heightStats= array();
+  $warnings= array();
+  $checksums= array();
+  $styles= array();
+  $duplicates= array();
+
+  echo "Updating master image for theme '$theme'...";
+
+  // Check for image magick convert
+  if (!function_exists("imageFilter")){
+    exec("which convert", $res, $ret);
+    if ($ret != 0) {
+      die("Your system has no bundled gd support for imageFilter function. Please install imagemagick in order to use an external command.\n");
+    }
+  }
+  
+  // Scan for images in the given path
+  flush();
+  foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)) as $fileInfo) {
+  
+      // We're only interested in png files
+      $indexPath= substr($fileInfo->getPathname(), $baseLength + 1);
+      $path= $fileInfo->getPathname();
+      if (preg_match('/\.png$/', $indexPath) && !preg_match('/\.svn/', $path) && !preg_match('/themes\/[^\/]+\/images\/img.png$/', $path)){
+  
+         // Grey image if it is not already one
+         if (preg_match('/grey/', $indexPath)) {
+           echo "!";
+           $warnings[]= "! Warning: skipped possible *grey* image $path";
+           flush();
+           continue;
+         }
+
+         // New image if it is not already one
+         if (preg_match('/new/', $indexPath) && !preg_match('/new\.png$/', $indexPath)) {
+           echo "!";
+           $warnings[]= "! Warning: skipped possible *new* image $path";
+           flush();
+           continue;
+         }
+
+         // Catch available themes
+         if (preg_match('/themes\//', $indexPath) && !preg_match('/themes\/'.$theme.'\//', $indexPath)) {
+           continue;
+         }
+
+         // Load image
+         $img= imageCreateFromPng($path);
+         $width= imageSX($img);
+         $height= imageSY($img);
+         imageDestroy($img);
+         $greyIndexPath= preg_replace('/\.png$/', '-grey.png', $indexPath);
+
+         // Is this image already there?
+         $checksum= md5_file($path);
+         if (in_array($checksum, $checksums)) {
+           $warnings[]= "! Warning: images $indexPath seems to be a duplicate of ".array_search($checksum, $checksums);
+           $duplicates[$indexPath]= array_search($checksum, $checksums);
+           $duplicates[$greyIndexPath]= preg_replace('/\.png$/', '-grey.png', array_search($checksum, $checksums));
+           continue;
+         } else {
+           $checksums[$indexPath]= $checksum;
+         }
+
+         // Ordinary image
+         $widths[$indexPath]= $width;
+         $heights[$indexPath]= $height;
+         $paths[$indexPath]= $path;
+
+         // Grey image
+         $widths[$greyIndexPath]= $width;
+         $heights[$greyIndexPath]= $height;
+         $paths[$greyIndexPath]= $path;
+
+         // Feed height statistics
+         if (!isset($heightStats[$height])) {
+           $heightStats[$height]= 1;
+         } else {
+           $heightStats[$height]++;
+         }
+      }
+  
+    echo ".";
+    flush();
+  }
+  echo "\n";
+
+  // Do some stupid height calculation
+  arsort($heightStats, SORT_NUMERIC);
+  reset($heightStats);
+  $popular= current($heightStats);
+
+  krsort($heightStats);
+  reset($heightStats);
+  $max= current($heightStats);
+
+  $maxHeight= (floor($max / $popular) + 1) * $popular * 6;
+  
+  // Sort to get biggest values
+  arsort($widths, SORT_NUMERIC);
+  reset($widths);
+  echo "Calculating master image dimensions: ";
+  flush();
+  
+  // Build container image
+  $cursorX= 0;
+  $cursorY= 0;
+  $colWidth= 0;
+  $rowHeight= 0;
+  $colX= 0;
+  $colY= 0;
+  $maxY= 0;
+  $maxX= 0;
+
+  // Walk thru width sorted images
+  foreach ($widths as $imagePath => $imageWidth) {
+    $imageHeight= $heights[$imagePath];
+
+    // First element in this column
+    if ($colWidth == 0) {
+      $colWidth= $imageWidth;
+    }
+
+    // First element in this row
+    if ($rowHeight < $imageHeight) {
+      $rowHeight= $imageHeight;
+    }
+
+    // Does the image match here?
+    if ($cursorX + $imageWidth > $colX + $colWidth) {
+
+      if ($cursorY + $imageHeight >= $maxHeight) {
+
+        // Reached max height, move to the next column
+        $colX+= $colWidth;
+        $cursorX= $colX;
+        $colWidth= $imageWidth;
+        $rowHeight= $imageHeight;
+        $colY= $cursorY= 0;
+
+      } else {
+
+        // Next row
+        $colY+= $rowHeight;
+        $cursorY= $colY;
+        $cursorX= $colX;
+        $rowHeight= $imageHeight;
+      }
+
+      $maxY=($colY + $imageHeight > $maxY)?$colY+$imageHeight:$maxY;
+    }
+
+    // Save calculated position
+    $posX[$imagePath]= $cursorX;
+    $posY[$imagePath]= $cursorY;
+
+    // Move X cursor to the next position
+    $cursorX+= $imageWidth;
+
+    $maxX=($colX+$imageWidth > $maxX)?$colX+$imageWidth:$maxX;
+  }
+  
+  // Print maximum dimensions
+  echo $maxY."x".$maxX."\n";
+  echo "Processing";
+  flush();
+
+  // Create result image
+  $dst= imageCreateTrueColor($maxX, $maxY);
+  imageAlphaBlending($dst, true);
+  $transparent = imagecolorallocatealpha($dst, 0, 0, 0, 127);
+  imageFill($dst, 0, 0, $transparent);
+  imageSaveAlpha($dst, true);
+
+  // Finally assemble picture
+  foreach ($heights as $imagePath => $imageHeight) {
+    $imageWidth= $widths[$imagePath];
+    $x= $posX[$imagePath];
+    $y= $posY[$imagePath];
+
+    // Insert source image...
+
+    // Eventually convert it to grey before
+    if (preg_match('/-grey\.png$/', $imagePath)) {
+      if (!function_exists("imageFilter")){
+        exec("convert ".$paths[$imagePath]." -colorspace Gray /tmp/grey-converted.png");
+        $src= imageCreateFromPng("/tmp/grey-converted.png");
+      } else {
+        $src= imageCreateFromPng($paths[$imagePath]);
+        imageFilter($src, IMG_FILTER_GRAYSCALE);
+      }
+    } else {
+      $src= imageCreateFromPng($paths[$imagePath]);
+    }
+
+    // Merge image
+    imageCopyResampled($dst, $src, $x, $y, 0, 0, $imageWidth, $imageHeight, $imageWidth, $imageHeight);
+    imageDestroy($src);
+
+    // Store style
+    $styles[$imagePath]= "background-position:-".$x."px -".$y."px;width:".$imageWidth."px;height:".$imageHeight."px";
+
+    echo ".";
+    flush();
+  }
+
+  /* Add duplicates */
+  foreach ($duplicates as $imagePath => $realPath) {
+    $styles[$imagePath]= $styles[$realPath];
+  }
+
+  imagePNG($dst, GOSA_HOME."/html/themes/$theme/images/img.png", 9);
+  imageDestroy($dst);
+
+  // Show warnings images
+  foreach ($warnings as $warn) {
+    echo "$warn\n";
+  }
+
+  // Write styles
+  echo "Writing styles...";
+  $fp = fopen(GOSA_HOME."/ihtml/themes/$theme/img.styles", 'w');
+  fwrite($fp, serialize($styles));
+  fclose($fp);
+
+  echo "\n";
+}
+
 
 /* Fill global values */
 $description= $provides= $depends= $versions= $conflicts= array();
@@ -438,7 +781,11 @@ $description= $provides= $depends= $versions= $conflicts= array();
 if ($argc < 2){
        rescan_classes();
        rescan_i18n();
-        exit (0);
+       rescan_guide();
+    foreach (get_themes() as $theme) {
+      rescan_images(GOSA_HOME."/html", $theme);
+    }
+    exit (0);
 }
 
 switch ($argv[1]){
@@ -467,6 +814,11 @@ switch ($argv[1]){
         case 'rescan-classes':
                 rescan_classes();
                 break;
+        case 'rescan-images':
+                foreach (get_themes() as $theme) {
+                  rescan_images("html", $theme);
+                }
+                break;
         default:
                 echo "Error: Supplied command not known\n\n";
                 print_usage();