Changeset 25406


Ignore:
Timestamp:
Nov 9, 2013, 12:29:38 AM (10 years ago)
Author:
mistic100
Message:

feature 2998: Maintenance class for plugin

Location:
trunk
Files:
3 edited

Legend:

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

    r25133 r25406  
    2222// +-----------------------------------------------------------------------+
    2323
     24/**
     25 * class DummyPlugin_maintain
     26 * used when a plugin uses the old procedural declaration of maintenance methods
     27 */
     28class DummyPlugin_maintain extends PluginMaintain
     29{
     30  function install($plugin_version, &$errors=array())
     31  {
     32    return $this->__call(__FUNCTION__, func_get_args());
     33  }
     34  function activate($plugin_version, &$errors=array())
     35  {
     36    return $this->__call(__FUNCTION__, func_get_args());
     37  }
     38  function deactivate()
     39  {
     40    return $this->__call(__FUNCTION__, func_get_args());
     41  }
     42  function uninstall()
     43  {
     44    return $this->__call(__FUNCTION__, func_get_args());
     45  }
     46
     47  function __call($name, $arguments)
     48  {
     49    if (is_callable('plugin_'.$name))
     50    {
     51      array_unshift($arguments, $this->plugin_id);
     52      return call_user_func_array('plugin_'.$name, $arguments);
     53    }
     54    return null;
     55  }
     56}
     57
     58
    2459class plugins
    2560{
     
    3166  /**
    3267   * Initialize $fs_plugins and $db_plugins_by_id
    33   */
     68   */
    3469  function plugins()
    3570  {
     
    4277  }
    4378
    44  /**
     79  /**
     80   * Returns the maintain class of a plugin
     81   * or build a new class with the procedural methods
     82   * @param string $plugin_id
     83   */
     84  private static function build_maintain_class($plugin_id)
     85  {
     86    $file_to_include = PHPWG_PLUGINS_PATH . $plugin_id . '/maintain.inc.php';
     87    $classname = $plugin_id.'_maintain';
     88
     89    if (file_exists($file_to_include))
     90    {
     91      include_once($file_to_include);
     92
     93      if (class_exists($classname))
     94      {
     95        $plugin_maintain = new $classname($plugin_id);
     96      }
     97      else
     98      {
     99        $plugin_maintain = new DummyPlugin_maintain($plugin_id);
     100      }
     101    }
     102    else
     103    {
     104      $plugin_maintain = new DummyPlugin_maintain($plugin_id);
     105    }
     106
     107    return $plugin_maintain;
     108  }
     109
     110  /**
    45111   * Perform requested actions
    46   * @param string - action
    47   * @param string - plugin id
    48   * @param array - errors
    49   */
     112   * @param string - action
     113   * @param string - plugin id
     114   * @param array - errors
     115   */
    50116  function perform_action($action, $plugin_id)
    51117  {
     
    54120      $crt_db_plugin = $this->db_plugins_by_id[$plugin_id];
    55121    }
    56     $file_to_include = PHPWG_PLUGINS_PATH . $plugin_id . '/maintain.inc.php';
     122   
     123    $plugin_maintain = self::build_maintain_class($plugin_id);
    57124
    58125    $errors = array();
     
    65132          break;
    66133        }
    67         if (file_exists($file_to_include))
    68         {
    69           include_once($file_to_include);
    70           if (function_exists('plugin_install'))
    71           {
    72             plugin_install($plugin_id, $this->fs_plugins[$plugin_id]['version'], $errors);
    73           }
    74         }
     134
     135        $plugin_maintain->install($this->fs_plugins[$plugin_id]['version'], $errors);
     136
    75137        if (empty($errors))
    76138        {
    77139          $query = '
    78 INSERT INTO ' . PLUGINS_TABLE . ' (id,version) VALUES (\''
    79 . $plugin_id . '\',\'' . $this->fs_plugins[$plugin_id]['version'] . '\'
    80 )';
     140INSERT INTO '. PLUGINS_TABLE .' (id,version)
     141  VALUES (\''. $plugin_id .'\', \''. $this->fs_plugins[$plugin_id]['version'] .'\')
     142;';
    81143          pwg_query($query);
    82144        }
     
    94156          break;
    95157        }
    96         if (empty($errors) and file_exists($file_to_include))
    97         {
    98           include_once($file_to_include);
    99           if (function_exists('plugin_activate'))
    100           {
    101             plugin_activate($plugin_id, $crt_db_plugin['version'], $errors);
    102           }
    103         }
     158
    104159        if (empty($errors))
    105160        {
     161          $plugin_maintain->activate($crt_db_plugin['version'], $errors);
     162        }
     163
     164        if (empty($errors))
     165        {
    106166          $query = '
    107 UPDATE ' . PLUGINS_TABLE . '
    108 SET state=\'active\', version=\''.$this->fs_plugins[$plugin_id]['version'].'\'
    109 WHERE id=\'' . $plugin_id . '\'';
     167UPDATE '. PLUGINS_TABLE .'
     168  SET state=\'active\',
     169    version=\''. $this->fs_plugins[$plugin_id]['version'] .'\'
     170  WHERE id=\''. $plugin_id .'\'
     171;';
    110172          pwg_query($query);
    111173        }
     
    117179          break;
    118180        }
     181
    119182        $query = '
    120 UPDATE ' . PLUGINS_TABLE . ' SET state=\'inactive\' WHERE id=\'' . $plugin_id . '\'';
     183UPDATE '. PLUGINS_TABLE .'
     184  SET state=\'inactive\'
     185  WHERE id=\''. $plugin_id .'\'
     186;';
    121187        pwg_query($query);
    122         if (file_exists($file_to_include))
    123         {
    124           include_once($file_to_include);
    125           if (function_exists('plugin_deactivate'))
    126           {
    127             plugin_deactivate($plugin_id);
    128           }
    129         }
     188       
     189        $plugin_maintain->deactivate();
    130190        break;
    131191
     
    139199          $this->perform_action('deactivate', $plugin_id);
    140200        }
     201
    141202        $query = '
    142 DELETE FROM ' . PLUGINS_TABLE . ' WHERE id=\'' . $plugin_id . '\'';
     203DELETE FROM '. PLUGINS_TABLE .'
     204  WHERE id=\''. $plugin_id .'\'
     205;';
    143206        pwg_query($query);
    144         if (file_exists($file_to_include))
    145         {
    146           include_once($file_to_include);
    147           if (function_exists('plugin_uninstall'))
    148           {
    149             plugin_uninstall($plugin_id);
    150           }
    151         }
     207       
     208        $plugin_maintain->uninstall();
    152209        break;
    153210
     
    167224          break;
    168225        }
     226
    169227        deltree(PHPWG_PLUGINS_PATH . $plugin_id, PHPWG_PLUGINS_PATH . 'trash');
    170228        break;
    171229    }
     230
    172231    return $errors;
    173232  }
    174233
    175234  /**
    176   * Get plugins defined in the plugin directory
    177   */ 
     235   * Get plugins defined in the plugin directory
     236   */ 
    178237  function get_fs_plugins()
    179238  {
  • trunk/admin/include/themes.class.php

    r25018 r25406  
    2222// +-----------------------------------------------------------------------+
    2323
     24/**
     25 * class DummyTheme_maintain
     26 * used when a theme uses the old procedural declaration of maintenance methods
     27 */
     28class DummyTheme_maintain extends ThemeMaintain
     29{
     30  function activate($theme_version, &$errors=array())
     31  {
     32    return $this->__call(__FUNCTION__, func_get_args());
     33  }
     34  function deactivate()
     35  {
     36    return $this->__call(__FUNCTION__, func_get_args());
     37  }
     38  function delete()
     39  {
     40    return $this->__call(__FUNCTION__, func_get_args());
     41  }
     42
     43  function __call($name, $arguments)
     44  {
     45    if (is_callable('theme_'.$name))
     46    {
     47      array_unshift($arguments, $this->theme_id);
     48      return call_user_func_array('theme_'.$name, $arguments);
     49    }
     50    return null;
     51  }
     52}
     53
     54
    2455class themes
    2556{
     
    3970      $this->db_themes_by_id[$db_theme['id']] = $db_theme;
    4071    }
     72  }
     73
     74  /**
     75   * Returns the maintain class of a theme
     76   * or build a new class with the procedural methods
     77   * @param string $theme_id
     78   */
     79  private static function build_maintain_class($theme_id)
     80  {
     81    $file_to_include = PHPWG_THEMES_PATH.'/'.$theme_id.'/admin/maintain.inc.php';
     82    $classname = $theme_id.'_maintain';
     83
     84    if (file_exists($file_to_include))
     85    {
     86      include_once($file_to_include);
     87
     88      if (class_exists($classname))
     89      {
     90        $theme_maintain = new $classname($theme_id);
     91      }
     92      else
     93      {
     94        $theme_maintain = new DummyTheme_maintain($theme_id);
     95      }
     96    }
     97    else
     98    {
     99      $theme_maintain = new DummyTheme_maintain($theme_id);
     100    }
     101
     102    return $theme_maintain;
    41103  }
    42104
     
    56118    }
    57119
    58     $file_to_include = PHPWG_THEMES_PATH.'/'.$theme_id.'/admin/maintain.inc.php';
     120    $theme_maintain = self::build_maintain_class($theme_id);
    59121
    60122    $errors = array();
     
    86148        }
    87149
    88 
    89150        if ($this->fs_themes[$theme_id]['mobile']
    90151            and !empty($conf['mobile_theme'])
     
    95156        }
    96157
    97         if (file_exists($file_to_include))
    98         {
    99           include($file_to_include);
    100           if (function_exists('theme_activate'))
    101           {
    102             theme_activate($theme_id, $this->fs_themes[$theme_id]['version'], $errors);
    103           }
    104         }
     158        $theme_maintain->activate($this->fs_themes[$theme_id]['version'], $errors);
    105159
    106160        if (empty($errors))
     
    142196
    143197          $query = '
    144 SELECT
    145     id
     198SELECT id
    146199  FROM '.THEMES_TABLE.'
    147200  WHERE id != \''.$theme_id.'\'
     
    160213        }
    161214
    162         if (file_exists($file_to_include))
    163         {
    164           include($file_to_include);
    165           if (function_exists('theme_deactivate'))
    166           {
    167             theme_deactivate($theme_id);
    168           }
    169         }
     215        $theme_maintain->deactivate();
    170216
    171217        $query = '
     
    204250        }
    205251
    206         if (file_exists($file_to_include))
    207         {
    208           include($file_to_include);
    209           if (function_exists('theme_delete'))
    210           {
    211             theme_delete($theme_id);
    212           }
    213         }
     252        $theme_maintain->delete();
    214253
    215254        deltree(PHPWG_THEMES_PATH.$theme_id, PHPWG_THEMES_PATH . 'trash');
  • trunk/include/functions_plugins.inc.php

    r25018 r25406  
    3232
    3333define('PHPWG_PLUGINS_PATH', PHPWG_ROOT_PATH.'plugins/');
    34 
    3534define('EVENT_HANDLER_PRIORITY_NEUTRAL', 50);
     35
     36
     37/**
     38 * class PluginMaintain
     39 * used to declare maintenance methods of a plugin
     40 */
     41abstract class PluginMaintain
     42{
     43  protected $plugin_id;
     44 
     45  function __construct($id)
     46  {
     47    $this->plugin_id = $id;
     48  }
     49 
     50  abstract function install($plugin_version, &$errors=array());
     51  abstract function activate($plugin_version, &$errors=array());
     52  abstract function deactivate();
     53  abstract function uninstall();
     54 
     55  /*
     56   * test if a plugin needs to be updated and call a update function
     57   * @param: string $version, version exposed by the plugin
     58   * @param: string $on_update, name of a method to call when an update is needed
     59   *          it receives the previous version as first parameter
     60   */
     61  function autoUpdate($version, $on_update=null)
     62  {
     63    global $pwg_loaded_plugins;
     64   
     65    $current_version = $pwg_loaded_plugins[$this->plugin_id]['version'];
     66   
     67    if ( $version == 'auto' or $current_version == 'auto'
     68        or version_compare($current_version, $version, '<')
     69      )
     70    {
     71      if (!empty($on_update))
     72      {
     73        call_user_func(array(&$this, $on_update), $current_version);
     74      }
     75     
     76      if ($version != 'auto')
     77      {
     78        $query = '
     79UPDATE '. PLUGINS_TABLE .'
     80  SET version = "'. $version .'"
     81  WHERE id = "'. $this->plugin_id .'"
     82;';
     83        pwg_query($query);
     84       
     85        $pwg_loaded_plugins[$this->plugin_id]['version'] = $version;
     86      }
     87    }
     88  }
     89}
     90
     91/**
     92 * class ThemeMaintain
     93 * used to declare maintenance methods of a theme
     94 */
     95abstract class ThemeMaintain
     96{
     97  protected $theme_id;
     98 
     99  function __construct($id)
     100  {
     101    $this->theme_id = $id;
     102  }
     103 
     104  abstract function activate($theme_version, &$errors=array());
     105  abstract function deactivate();
     106  abstract function delete();
     107 
     108  /*
     109   * test if a theme needs to be updated and call a update function
     110   * @param: string $version, version exposed by the theme
     111   * @param: string $on_update, name of a method to call when an update is needed
     112   *          it receives the previous version as first parameter
     113   */
     114  function autoUpdate($version, $on_update=null)
     115  {
     116    $query = '
     117SELECT version
     118  FROM '. THEMES_TABLE .'
     119  WHERE id = "'. $this->theme_id .'"
     120;';
     121    list($current_version) = pwg_db_fetch_row(pwg_query($query));
     122   
     123    if ( $version == 'auto' or $current_version == 'auto'
     124        or version_compare($current_version, $version, '<')
     125      )
     126    {
     127      if (!empty($on_update))
     128      {
     129        call_user_func(array(&$this, $on_update), $current_version);
     130      }
     131     
     132      if ($version != 'auto')
     133      {
     134        $query = '
     135UPDATE '. THEMES_TABLE .'
     136  SET version = "'. $version .'"
     137  WHERE id = "'. $this->theme_id .'"
     138;';
     139        pwg_query($query);
     140      }
     141    }
     142  }
     143}
     144
    36145
    37146/* Register a event handler.
     
    254363}
    255364
    256 /*
    257  * test if a plugin needs to be updated and call a update function
    258  * @param: string $plugin_id, id of the plugin as seen in PLUGINS_TABLE and $pwg_loaded_plugins
    259  * @param: string $version, version exposed by the plugin
    260  * @param: callable $on_update, function to call when and update is needed
    261  *          it receives the previous version as first parameter
    262  */
    263 function request_plugin_update($plugin_id, $version, $on_update)
    264 {
    265   global $pwg_loaded_plugins;
    266  
    267   if (
    268     $version == 'auto' or
    269     $pwg_loaded_plugins[$plugin_id]['version'] == 'auto' or
    270     version_compare($pwg_loaded_plugins[$plugin_id]['version'], $version, '<')
    271   )
    272   {
    273     // call update function
    274     if (!empty($on_update))
    275     {
    276       call_user_func($on_update, $pwg_loaded_plugins[$plugin_id]['version']);
    277     }
    278    
    279     // update plugin version in database
    280     if ($version != 'auto')
    281     {
    282       $query = '
    283 UPDATE '. PLUGINS_TABLE .'
    284 SET version = "'. $version .'"
    285 WHERE id = "'. $plugin_id .'"';
    286       pwg_query($query);
    287      
    288       $pwg_loaded_plugins[$plugin_id]['version'] = $version;
    289     }
    290   }
    291 }
    292 
    293365?>
Note: See TracChangeset for help on using the changeset viewer.