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

apply new design pattern for maintain.inc.php

File:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/skeleton/trunk/maintain.inc.php

    r24349 r25407  
    22defined('PHPWG_ROOT_PATH') or die('Hacking attempt!');
    33
    4 defined('SKELETON_ID') or define('SKELETON_ID', basename(dirname(__FILE__)));
    5 include_once(PHPWG_PLUGINS_PATH . SKELETON_ID . '/include/install.inc.php');
     4/**
     5 * This class is used to expose maintenance methods to the plugins manager
     6 * It must extends PluginMaintain and be named "PLUGINID_maintain"
     7 * where PLUGINID is the directory name of your plugin
     8 */
     9class skeleton_maintain extends PluginMaintain
     10{
     11  /*
     12   * My pattern uses a single installation method, which handles both installation
     13   * and activation, where Piwigo always calls 'activate' just after 'install'
     14   * As a result I use a marker in order to not execute the installation method twice
     15   *
     16   * The installation function is called by main.inc.php and maintain.inc.php
     17   * in order to install and/or update the plugin.
     18   *
     19   * That's why all operations must be conditionned :
     20   *    - use "if empty" for configuration vars
     21   *    - use "IF NOT EXISTS" for table creation
     22   */
     23  private $installed = false;
    624
    7 /**
    8  * plugin installation
    9  *
    10  * perform here all needed step for the plugin installation
    11  * such as create default config, add database tables,
    12  * add fields to existing tables, create local folders...
    13  */
    14 function plugin_install()
    15 {
    16   skeleton_install();
    17   define('skeleton_installed', true);
    18 }
     25  /**
     26   * plugin installation
     27   *
     28   * perform here all needed step for the plugin installation
     29   * such as create default config, add database tables,
     30   * add fields to existing tables, create local folders...
     31   */
     32  function install($plugin_version, &$errors=array())
     33  {
     34    global $conf, $prefixeTable;
    1935
    20 /**
    21  * plugin activation
    22  *
    23  * this function is triggered adter installation, by manual activation
    24  * or after a plugin update
    25  * for this last case you must manage updates tasks of your plugin in this function
    26  */
    27 function plugin_activate()
    28 {
    29   if (!defined('skeleton_installed')) // a plugin is activated just after its installation
     36    // add config parameter
     37    if (empty($conf['skeleton']))
     38    {
     39      $conf['skeleton'] = serialize(array(
     40        'option1' => 10,
     41        'option2' => true,
     42        'option3' => 'two',
     43        ));
     44
     45      conf_update_param('skeleton', $conf['skeleton']);
     46    }
     47    else
     48    {
     49      // if you need to test the "old" configuration you must check if not yet unserialized
     50      $old_conf = is_string($conf['skeleton']) ? unserialize($conf['skeleton']) : $conf['skeleton'];
     51
     52      if (empty($old_conf['option3']))
     53      { // use case: this parameter was added in a new version
     54        $old_conf['option3'] = 'two';
     55      }
     56
     57      $conf['skeleton'] = serialize($old_conf);
     58      conf_update_param('skeleton', $conf['skeleton']);
     59    }
     60
     61    // add a new table
     62    pwg_query('
     63CREATE TABLE IF NOT EXISTS `'. $prefixeTable .'skeleton` (
     64  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
     65  `field1` mediumint(8) DEFAULT NULL,
     66  `field2` varchar(64) NOT NULL,
     67  PRIMARY KEY (`id`)
     68) ENGINE=MyISAM DEFAULT CHARSET=utf8
     69;');
     70
     71    // add a new column to existing table
     72    $result = pwg_query('SHOW COLUMNS FROM `'.IMAGES_TABLE.'` LIKE "skeleton";');
     73    if (!pwg_db_num_rows($result))
     74    {
     75      pwg_query('ALTER TABLE `' . IMAGES_TABLE . '` ADD `skeleton` TINYINT(1) NOT NULL DEFAULT 0;');
     76    }
     77
     78    // create a local directory
     79    if (!file_exists(PHPWG_ROOT_PATH . PWG_LOCAL_DIR . 'skeleton/'))
     80    {
     81      mkdir(PHPWG_ROOT_PATH . PWG_LOCAL_DIR . 'skeleton/', 0755);
     82    }
     83
     84    $this->installed = true;
     85  }
     86
     87  /**
     88   * plugin activation
     89   *
     90   * this function is triggered after installation, by manual activation
     91   * or after a plugin update
     92   * for this last case you must manage updates tasks of your plugin in this function
     93   */
     94  function activate($plugin_version, &$errors=array())
    3095  {
    31     skeleton_install();
     96    if (!$this->installed)
     97    {
     98      $this->install($plugin_version, $errors);
     99    }
     100  }
     101
     102  /**
     103   * plugin deactivation
     104   *
     105   * triggered before uninstallation or by manual deactivation
     106   */
     107  function deactivate()
     108  {
     109  }
     110
     111  /**
     112   * plugin uninstallation
     113   *
     114   * perform here all cleaning tasks when the plugin is removed
     115   * you should revert all changes made by 'install'
     116   */
     117  function uninstall()
     118  {
     119    global $prefixeTable;
     120
     121    // delete configuration
     122    conf_delete_param('skeleton');
     123
     124    // delete table
     125    pwg_query('DROP TABLE `'. $prefixeTable .'skeleton`;');
     126
     127    // delete field
     128    pwg_query('ALTER TABLE `'. IMAGES_TABLE .'` DROP `skeleton`;');
     129
     130    // delete local folder
     131    // use a recursive function if you plan to have nested directories
     132    $dir = PHPWG_ROOT_PATH . PWG_LOCAL_DIR . 'skeleton/';
     133    foreach (scandir($dir) as $file)
     134    {
     135      if ($file == '.' or $file == '..') continue;
     136      unlink($dir.$file);
     137    }
     138    rmdir($dir);
    32139  }
    33140}
    34 
    35 /**
    36  * plugin unactivation
    37  *
    38  * triggered before uninstallation or by manual unactivation
    39  */
    40 function plugin_unactivate()
    41 {
    42 }
    43 
    44 /**
    45  * plugin uninstallation
    46  *
    47  * perform here all cleaning tasks when the plugin is removed
    48  * you should revert all changes made by plugin_install()
    49  */
    50 function plugin_uninstall()
    51 {
    52   skeleton_uninstall();
    53 }
Note: See TracChangeset for help on using the changeset viewer.