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

feature 3076: Enhance plugin update system

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.