Changeset 28651


Ignore:
Timestamp:
Jun 9, 2014, 7:20:43 PM (10 years ago)
Author:
mistic100
Message:

feature 3076: Enhance plugin update system

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/admin/include/plugins.class.php

    r28169 r28651  
    5656    }
    5757  }
     58  function update($old_version, $new_version, &$errors=array()) {}
    5859}
    5960
     
    8687  private static function build_maintain_class($plugin_id)
    8788  {
    88     $file_to_include = PHPWG_PLUGINS_PATH . $plugin_id . '/maintain.inc.php';
     89    $file_to_include = PHPWG_PLUGINS_PATH . $plugin_id . '/maintain';
    8990    $classname = $plugin_id.'_maintain';
    9091
    91     if (file_exists($file_to_include))
    92     {
    93       include_once($file_to_include);
     92    if (file_exists($file_to_include.'.class.php'))
     93    {
     94      include_once($file_to_include.'.class.php');
     95      return new $classname($plugin_id);
     96    }
     97
     98    if (file_exists($file_to_include.'.inc.php'))
     99    {
     100      include_once($file_to_include.'.inc.php');
    94101
    95102      if (class_exists($classname))
    96103      {
    97         $plugin_maintain = new $classname($plugin_id);
    98       }
    99       else
    100       {
    101         $plugin_maintain = new DummyPlugin_maintain($plugin_id);
    102       }
    103     }
    104     else
    105     {
    106       $plugin_maintain = new DummyPlugin_maintain($plugin_id);
    107     }
    108 
    109     return $plugin_maintain;
     104        return new $classname($plugin_id);
     105      }
     106    }
     107
     108    return new DummyPlugin_maintain($plugin_id);
    110109  }
    111110
     
    122121      $crt_db_plugin = $this->db_plugins_by_id[$plugin_id];
    123122    }
    124    
     123
    125124    $plugin_maintain = self::build_maintain_class($plugin_id);
    126125
     
    188187;';
    189188        pwg_query($query);
    190        
     189
    191190        $plugin_maintain->deactivate();
    192191        break;
     
    207206;';
    208207        pwg_query($query);
    209        
     208
    210209        $plugin_maintain->uninstall();
    211210        break;
     
    236235  /**
    237236   * Get plugins defined in the plugin directory
    238    */ 
     237   */
    239238  function get_fs_plugins()
    240239  {
     
    257256              'author'=>'',
    258257            );
    259           $plg_data = implode( '', file($path.'/main.inc.php') );
     258          $plg_data = file_get_contents($path.'/main.inc.php', null, null, 0, 2048);
    260259
    261260          if ( preg_match("|Plugin Name: (.*)|", $plg_data, $val) )
     
    327326  {
    328327    global $conf;
    329    
     328
    330329    $versions_to_check = array();
    331330    $url = PEM_URL . '/api/get_version_list.php?category_id='. $conf['pem_plugins_category'] .'&format=php';
     
    424423      return false;
    425424    }
    426    
     425
    427426    global $conf;
    428427
     
    478477    return false;
    479478  }
    480  
     479
    481480  /**
    482481   * Sort $server_plugins
  • trunk/admin/include/themes.class.php

    r26998 r28651  
    8787      if (class_exists($classname))
    8888      {
    89         $theme_maintain = new $classname($theme_id);
    90       }
    91       else
    92       {
    93         $theme_maintain = new DummyTheme_maintain($theme_id);
    94       }
    95     }
    96     else
    97     {
    98       $theme_maintain = new DummyTheme_maintain($theme_id);
    99     }
    100 
    101     return $theme_maintain;
     89        return new $classname($theme_id);
     90      }
     91    }
     92   
     93    return new DummyTheme_maintain($theme_id);
    10294  }
    10395
  • trunk/include/functions_plugins.inc.php

    r28587 r28651  
    3636 * Used to declare maintenance methods of a plugin.
    3737 */
    38 class PluginMaintain 
     38class PluginMaintain
    3939{
    4040  /** @var string $plugin_id */
     
    6666
    6767  /**
    68    * Tests if the plugin needs to be updated and call an update function
    69    *
    70    * @param string $version version exposed by the plugin (potentially new)
    71    * @param string $on_update name of a method to call when an update is needed
    72    *          it receives the previous version as first parameter
    73    */
    74   function autoUpdate($version, $on_update=null)
    75   {
    76     global $pwg_loaded_plugins;
    77    
    78     $current_version = $pwg_loaded_plugins[$this->plugin_id]['version'];
    79    
    80     if ( $version == 'auto' or $current_version == 'auto'
    81         or safe_version_compare($current_version, $version, '<')
    82       )
    83     {
    84       if (!empty($on_update))
    85       {
    86         call_user_func(array(&$this, $on_update), $current_version);
    87       }
    88      
    89       if ($version != 'auto')
    90       {
    91         $query = '
    92 UPDATE '. PLUGINS_TABLE .'
    93   SET version = "'. $version .'"
    94   WHERE id = "'. $this->plugin_id .'"
    95 ;';
    96         pwg_query($query);
    97        
    98         $pwg_loaded_plugins[$this->plugin_id]['version'] = $version;
    99       }
    100     }
    101   }
     68   * @param string $old_version
     69   * @param string $new_version
     70   * @param array &$errors - used to return error messages
     71   */
     72  function update($old_version, $new_version, &$errors=array()) {}
    10273}
    10374
     
    10576 * Used to declare maintenance methods of a theme.
    10677 */
    107 class ThemeMaintain 
     78class ThemeMaintain
    10879{
    10980  /** @var string $theme_id */
     
    12798
    12899  function delete() {}
    129  
    130   /**
    131    * Tests if the theme needs to be updated and call an update function
    132    *
    133    * @param string $version version exposed by the theme (potentially new)
    134    * @param string $on_update name of a method to call when an update is needed
    135    *          it receives the previous version as first parameter
    136    */
    137   function autoUpdate($version, $on_update=null)
    138   {
    139     $query = '
    140 SELECT version
    141   FROM '. THEMES_TABLE .'
    142   WHERE id = "'. $this->theme_id .'"
    143 ;';
    144     list($current_version) = pwg_db_fetch_row(pwg_query($query));
    145    
    146     if ( $version == 'auto' or $current_version == 'auto'
    147         or safe_version_compare($current_version, $version, '<')
    148       )
    149     {
    150       if (!empty($on_update))
    151       {
    152         call_user_func(array(&$this, $on_update), $current_version);
    153       }
    154      
    155       if ($version != 'auto')
    156       {
    157         $query = '
    158 UPDATE '. THEMES_TABLE .'
    159   SET version = "'. $version .'"
    160   WHERE id = "'. $this->theme_id .'"
    161 ;';
    162         pwg_query($query);
    163       }
    164     }
    165   }
    166100}
    167101
     
    251185 * @param mixed $data data to transmit to all handlers
    252186 * @param mixed $args,... optional arguments
    253  * @return mixed $data 
     187 * @return mixed $data
    254188 */
    255189function trigger_change($event, $data=null)
     
    400334  WHERE '. implode(' AND ', $clauses);
    401335  }
    402  
     336
    403337  return query2array($query);
    404338}
    405339
    406340/**
    407  * Loads a plugin, it includes the main.inc.php file and updates _$pwg_loaded_plugins_.
     341 * Loads a plugin in memory.
     342 * It performs autoupdate, includes the main.inc.php file and updates *$pwg_loaded_plugins*.
    408343 *
    409344 * @param string $plugin
     
    412347{
    413348  $file_name = PHPWG_PLUGINS_PATH.$plugin['id'].'/main.inc.php';
    414   if ( file_exists($file_name) )
    415   {
     349  if (file_exists($file_name))
     350  {
     351    autoupdate_plugin($plugin);
    416352    global $pwg_loaded_plugins;
    417353    $pwg_loaded_plugins[ $plugin['id'] ] = $plugin;
    418     include_once( $file_name );
     354    include_once($file_name);
     355  }
     356}
     357
     358/**
     359 * Performs update task of a plugin.
     360 * Autoupdate is only performed if the plugin has a maintain.class.php file.
     361 *
     362 * @since 2.7
     363 *
     364 * @param array &$plugin (id, version, state) will be updated if version changes
     365 */
     366function autoupdate_plugin(&$plugin)
     367{
     368  $maintain_file = PHPWG_PLUGINS_PATH.$plugin['id'].'/maintain.class.php';
     369
     370  // autoupdate is applicable only to plugins with 2.7 architecture
     371  if (file_exists($maintain_file))
     372  {
     373    // try to find the filesystem version in lines 2 to 10 of main.inc.php
     374    $fh = fopen(PHPWG_PLUGINS_PATH.$plugin['id'].'/main.inc.php', 'r');
     375    $fs_version = null;
     376    $i = -1;
     377
     378    while (($line = fgets($fh))!==false && $fs_version==null && $i<10)
     379    {
     380      $i++;
     381      if ($i < 2) continue; // first lines are typically "<?php" and "/*"
     382
     383      if (preg_match('#Version: ([\\w.-]+)#', $line, $matches))
     384      {
     385        $fs_version = $matches[1];
     386      }
     387    }
     388
     389    fclose($fh);
     390
     391    if ($fs_version != null)
     392    {
     393      global $pwg_loaded_plugins, $page;
     394
     395      // if version is auto (dev) or superior
     396      if (
     397          $fs_version == 'auto' or $plugin['version'] == 'auto'
     398          or safe_version_compare($plugin['version'], $fs_version, '<')
     399        )
     400      {
     401        // call update method
     402        include_once($maintain_file);
     403
     404        $classname = $plugin['id'].'_maintain';
     405        $plugin_maintain = new $classname($plugin['id']);
     406        $plugin_maintain->update($plugin['version'], $fs_version, $page['errors']);
     407
     408        $plugin['version'] = $fs_version;
     409
     410        // update database (only on production)
     411        if ($plugin['version'] != 'auto')
     412        {
     413          $query = '
     414UPDATE '. PLUGINS_TABLE .'
     415  SET version = "'. $plugin['version'] .'"
     416  WHERE id = "'. $plugin['id'] .'"
     417;';
     418          pwg_query($query);
     419        }
     420      }
     421    }
    419422  }
    420423}
Note: See TracChangeset for help on using the changeset viewer.