Show
Ignore:
Timestamp:
02/20/12 07:02:25 (1 year ago)
Author:
mbrevda
Message:

upstream changes - add hookable templates so that any module can offer templates for backups

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • modules/branches/2.10/backup/functions.inc/templates.php

    r12414 r13482  
    2222  } 
    2323   
    24   /*todo: selete servers from backups 
     24  /*todo: select servers from backups 
    2525  $sql = 'DELETE FROM backup_details WHERE server = ?'; 
    2626  $ret = $db->query($sql, $id); 
     
    3535  global $db; 
    3636   
    37   //return a blank if no id was set, all servers if 'all' was passed 
    38   //otherwise, a specifc server 
     37  //return a blank if no id was set, all templates if 'all' was passed 
     38  //otherwise, a specifc template 
    3939 
    4040  switch ($id) { 
    4141    case '': 
    42       $ret = array( 
     42      return array( 
    4343          'id'    => '', 
    4444          'name'    => '', 
     45          'data'    => array(), 
    4546          'desc'    => '', 
    4647          'items'   => array(), 
    4748          'immortal'  => '' 
    4849          ); 
    49       return $ret; 
    5050      break; 
    5151    case 'all': 
    5252    case 'all_detailed': 
     53      $templates = array(); 
    5354      $ret = sql('SELECT * FROM backup_templates ORDER BY name', 'getAll', DB_FETCHMODE_ASSOC); 
    54       $templates = array(); 
    55       //set index to server id for easy retrieval 
    56       foreach ($ret as $s) { 
    57         //set index to  id for easy retrieval 
    58         $templates[$s['id']] = $s; 
    59          
    60         //default name in one is missing 
    61         if (!$templates[$s['id']]['name']) { 
    62           $templates[$s['id']]['name'] = _('Template') . ' ' . $s['id']; 
    63         } 
    64          
    65         //add details if requested 
     55     
     56      //get hooks from other modules 
     57      $hook_temps = mod_func_iterator('hook_backup_get_template'); 
     58       
     59      //sanatizes hooks and push them in to the stack 
     60      foreach ($hook_temps as $mod => $template) { 
     61        foreach ($template as $my => $temp) { 
     62          $ret[] = backup_template_sanitize($temp, $mod); 
     63        } 
     64         
     65      } 
     66     
     67      //sanatize/argument templates 
     68      foreach ($ret as $temp) { 
    6669        if ($id == 'all_detailed') { 
    67           $templates[$s['id']] = backup_get_template($s['id']); 
    68         } 
    69       } 
    70  
     70          //backup_get_template() dose its own sanatization 
     71          $templates[$temp['id']] = backup_get_template($temp['id']); 
     72        } else { 
     73          $templates[$temp['id']] = backup_template_sanitize($temp); 
     74        } 
     75      } 
     76     
     77      //this ugliness is here pending requiring php-53 
     78      //to migrate, just copy the functions in place of the string of same name 
     79       function _anon_func1($val) { 
     80        return $val; 
     81      } 
     82       
     83       function _anon_func2($a, $b) { 
     84        if ($a['name'] == $b['name']) { 
     85          return 0; 
     86        } else { 
     87          return $a['name'] > $b['name'] ? 1 : -1; 
     88        } 
     89      } 
     90       
     91      //remove any false values (templates that didnt pass sanitization) 
     92      $templates = array_filter($templates, '_anon_func1'); 
     93     
     94      //sort templaes based on template name 
     95      uasort($templates, '_anon_func2'); 
     96   
    7197      return $templates; 
    7298      break; 
    7399    default: 
     100      //if the id is preceded by a module name, ask that module for the details 
     101      if (strpos($id, '-') !== false) { 
     102         
     103        //get data by breaking up backup id 
     104        list($mod, $id) = explode('-', $id); 
     105     
     106        //return template data if $mod's function exsists 
     107        if (function_exists($mod . '_hook_backup_get_template')) { 
     108          return backup_template_sanitize( 
     109                call_user_func($mod . '_hook_backup_get_template', $id), 
     110                $mod 
     111              ); 
     112        } 
     113         
     114      } 
     115       
    74116      $sql = 'SELECT * FROM backup_templates WHERE id = ?'; 
    75117      $ret = $db->getAll($sql, array($id), DB_FETCHMODE_ASSOC); 
     
    105147      } 
    106148 
    107  
    108       //default a name 
    109       $ret['name'] = $ret['name'] ? $ret['name'] : 'Template ' . $ret['id']; 
    110        
     149      $ret = backup_template_sanitize($ret); 
     150 
    111151      return $ret; 
    112152      break; 
     
    278318   
    279319} 
     320 
     321/** 
     322 * 
     323 * Sanatize a template and ensures it is in format we excpet 
     324 * @pram array - a template 
     325 * @pram string - module name, default to backup 
     326 * 
     327 * @returns mixed - the sanatized template or fals on error 
     328 */ 
     329function backup_template_sanitize($temp, $mod = 'backup') { 
     330 
     331  //backup's id's are just a number. Add the module name for consistancy with hooks 
     332  //this can get a bit tricky - make sure we dotn already have a mod name set! 
     333  if ($mod == 'backup' && strpos($temp['id'], '-') === false) { 
     334    $temp['id'] = 'backup-' . $temp['id']; 
     335  } 
     336  $id = explode('-', $temp['id']); 
     337 
     338  //ensure we have a name 
     339  $temp['name'] = $temp['name'] ? $temp['name'] : $id[0] . ' ' . $id[1]; 
     340               
     341  //hooked templates are ALWAYS immortal 
     342  //partly as we dont have delete hooks, and partly because there is no 
     343  //need for modules to create templates that are NOT hard coded 
     344  if ($mod != 'backup') { 
     345    $temp['immortal'] = 'true'; 
     346  } 
     347   
     348  //unserialize data 
     349  if (!isset($temp['data'])) { 
     350    $temp['data'] = array(); 
     351  } elseif(is_string($temp['data'])) { 
     352    $temp['data'] = unserialize($temp['data']); 
     353  }  
     354 
     355  return $temp; 
     356} 
    280357?> 
  • modules/branches/2.10/package.php

    r13408 r13482  
    440440  $final_status[$mod] = $mod . ' version ' . $ver  
    441441            . ' has been sucsessfuly packaged!' . PHP_EOL; 
    442   echo $final_status[$mod]; 
     442  echo PHP_EOL . $final_status[$mod]; 
    443443   
    444444}