Changeset 4164

Show
Ignore:
Timestamp:
09/21/06 19:38:37 (2 years ago)
Author:
bergie
Message:

Preliminary support for installing styles packages under other SGs than SG0

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/support/Role_Midgardelement/Midgardelement.php

    r4133 r4164  
    3030{ 
    3131    /** 
     32     * @var integer 
     33     */ 
     34    var $sitegroup = 0; 
     35     
     36    /** 
     37     * This method looks for one level of styling in the tree 
     38     */ 
     39    function probeStyle($name, $up = 0) 
     40    { 
     41        $qb = new MidgardQueryBuilder('midgard_style'); 
     42        $qb->add_constraint('up', '=', $up); 
     43        $qb->add_constraint('name', '=', $name); 
     44        $qb->add_constraint('sitegroup', '=', $this->sitegroup); 
     45        $styles = @$qb->execute(); 
     46 
     47        if (count($styles) == 0) 
     48        { 
     49            // This part of the style path is missing, create 
     50            $new_style = new midgard_style(); 
     51            $new_style->up = $up; 
     52            $new_style->name = $name; 
     53            $new_style->sitegroup = $this->sitegroup; 
     54 
     55            $stat = $new_style->create(); 
     56            if (!$stat) 
     57            { 
     58                PEAR::raiseError("Failed to create Midgard style \"{$subStyleNames}\", check config directives in the Midgard conf.d file \"{$init_file}\". Error was " . mgd_errstr(), mgd_errno(), PEAR_ERROR_DIE); 
     59                return null; 
     60            } 
     61             
     62            if ($up == 0) 
     63            { 
     64                // Aegir v1 compatibility fix 
     65                $new_style->parameter('Display', 'Shared', 'YES'); 
     66            } 
     67             
     68            return $new_style; 
     69        } 
     70 
     71        return $styles[0]; 
     72    } 
     73     
     74    function probeSitegroup() 
     75    { 
     76        $sitegroup_config = $this->config->get('midgard_sitegroup'); 
     77        if ($sitegroup_config == 'root') 
     78        { 
     79            // PEAR doesn't allow 0 values so we have to type root manually 
     80            $this->sitegroup = 0; 
     81            return true; 
     82        } 
     83         
     84        $sitegroup = mgd_get_sitegroup($sitegroup_config); 
     85        if ($sitegroup) 
     86        { 
     87            // We found SG from the database 
     88            $this->sitegroup = $sitegroup->id; 
     89            return true; 
     90        } 
     91         
     92        PEAR::raiseError("Failed to load Midgard sitegroup \"{$sitegroup_config}\". Error: " . mgd_errstr(), mgd_errno(), PEAR_ERROR_DIE); 
     93        return false; 
     94    } 
     95 
     96    /** 
    3297     * This is called for each file to set up the directories and files 
    3398     * @param PEAR_PackageFile_v1|PEAR_PackageFile_v2 
     
    47112         
    48113        // Get packaging variables 
     114        // $style_name = $pkg->getPackage(); 
    49115        $init_file = basename($installing_paths[0]); 
    50         $style_name = $pkg->getPackage(); 
     116        mgd_config_init($init_file); 
     117        if (!isset($_MIDGARD)) 
     118        { 
     119            PEAR::raiseError("Failed to initialize Midgard using configuration file \"{$init_file}\".", mgd_errno(), PEAR_ERROR_DIE); 
     120        } 
     121         
    51122        $element_name = str_replace('.php', '', basename($file)); 
     123         
     124        // Get the sitegroup value 
     125        $this->probeSitegroup(); 
    52126 
    53         /* 
    54          Get style elements path from baseinstalldir attribute 
    55          ie. /template_Textual/layouts 
    56         */ 
    57         $parts = explode("/",$atts["baseinstalldir"]); 
    58         $path_items = array(); 
    59         for($i=0;$i<count($parts);$i++) { 
    60             if(!empty($parts[$i])) 
    61                 $path_items[] = $parts[$i]; 
     127        /** 
     128         * Get style elements path from baseinstalldir attribute 
     129         * ie. /template_Textual/layouts 
     130         */ 
     131        $style_parts = explode('/', $atts['baseinstalldir']); 
     132        $style = null; 
     133        $up = 0; 
     134        foreach ($style_parts as $style) 
     135        { 
     136            // Sanitize the style names 
     137            $style_name = str_replace('/', '_', trim($style)); 
     138            if (empty($style_name)) 
     139            { 
     140                // This part doesn't have a proper name, skip 
     141                continue; 
     142            } 
     143             
     144            $style = $this->probeStyle($style_name, $up); 
     145             
     146            if (!is_null($style)) 
     147            { 
     148                $up = $style->id; 
     149            } 
    62150        } 
    63  
    64         /* 
    65           We assume that this is the root style 
    66           ie. template_Textual 
    67         */ 
    68         $rootStyleName = $path_items[0]; 
    69  
    70         /* 
    71           Collect rest of the path to own array, 
    72           we will check if exists or create all 
    73           these. 
    74         */ 
    75         $subStyleNames = array(); 
    76         for($i=1;$i<count($path_items);$i++) { 
    77             $subStyleNames[] = $path_items[$i]; 
    78         } 
    79  
    80         // Initialize Midgard session 
    81         mgd_config_init($init_file); 
    82  
    83         /* 
    84           Theres no substyles so lets create the element in ROOT 
    85         */ 
    86         if(empty($subStyleNames)) { 
    87  
    88             $qb = new MidgardQueryBuilder('midgard_style'); 
    89             $qb->add_constraint('up', '=', 0); 
    90             $qb->add_constraint('name', '=', $style_name); 
    91             $styles = @$qb->execute(); 
    92  
    93             if (count($styles) == 0) 
    94             { 
    95                 // Create missing style template 
    96                 $new_style = new midgard_style(); 
    97                 $new_style->up = 0; 
    98                 $new_style->name = $style_name; 
    99                 $stat = $new_style->create(); 
    100                 if (!$stat) 
    101                 { 
    102                     PEAR::raiseError("Failed to create Midgard style \"{$style_name}\", check config directives in the Midgard conf.d file \"{$init_file}\". Error was " . mgd_errstr()); 
    103                 } 
    104                 $style = new midgard_style(); 
    105                 $style->get_by_id($new_style->id); 
    106             } 
    107             else 
    108             { 
    109                 $style = $styles[0]; 
    110             } 
    111  
    112             // Aegir v1 compatibility fix 
    113             $style->parameter('Display', 'Shared', 'YES'); 
    114  
     151         
     152        if (!is_null($style)) 
     153        { 
     154            // We have the style in DB, update the element contents 
    115155            $qb = new MidgardQueryBuilder('midgard_element'); 
    116156            $qb->add_constraint('style', '=', $style->id); 
    117157            $qb->add_constraint('name', '=', $element_name); 
     158            $qb->add_constraint('sitegroup', '=', $this->sitegroup); 
    118159            $elements = @$qb->execute(); 
    119160 
     
    124165                $new_element->style = $style->id; 
    125166                $new_element->name = $element_name; 
     167                $new_element->sitegroup = $this->sitegroup; 
    126168                $stat = $new_element->create(); 
    127169                if (!$stat) 
    128170                { 
    129                     PEAR::raiseError("Failed to create element \"{$element_name}\" under Midgard style \"{$style->name}\", error " . mgd_errstr()); 
     171                    PEAR::raiseError("Failed to create element \"{$element_name}\" under Midgard style \"{$style->name}\", error " . mgd_errstr(), mgd_errno(), PEAR_ERROR_DIE); 
    130172                } 
    131173                $element = new midgard_element(); 
     
    142184            if (!$stat) 
    143185            { 
    144                 PEAR::raiseError("Failed to update element \"{$element_name}\" under Midgard style \"{$style->name}\", error " . mgd_errstr()); 
    145                 die(); 
    146             } 
    147  
    148         } else { 
    149  
    150             $qb = new MidgardQueryBuilder('midgard_style'); 
    151             $qb->add_constraint('name', '=', $rootStyleName); 
    152             $styles = @$qb->execute(); 
    153  
    154             if(count($styles) == 0) { 
    155                 PEAR::raiseError("Failed to find Midgard style \"{$rootStyleName}\", check config directives in the Midgard conf.d file \"{$init_file}\". Error was " . mgd_errstr()); 
    156             } else { 
    157                 $root_style = $styles[0]; 
    158  
    159                 /* 
    160                   We loop through all the substyles 
    161                   we have collected and check if they exist, 
    162                   Create all that doesn't and finally 
    163                   save the element under the last substyle 
    164                 */ 
    165  
    166                 $subStyleCount = count($subStyleNames); 
    167                 $lastSubStyle = ''; 
    168                 $tmpIDs = array(); 
    169                 for($i=0;$i<$subStyleCount;$i++) { 
    170                     $parentID = ($i==0)?$root_style->id:$tmpIDs[$i-1]; 
    171  
    172                     $qb = new MidgardQueryBuilder('midgard_style'); 
    173                     $qb->add_constraint('up', '=', $parentID); 
    174                     $qb->add_constraint('name', '=', $subStyleNames[$i]); 
    175                     $sstyles = @$qb->execute(); 
    176  
    177                     if (count($sstyles) == 0) 
    178                     { 
    179                         $new_sstyle = new midgard_style(); 
    180                         $new_sstyle->up = $parentID; 
    181                         $new_sstyle->name = $subStyleNames[$i]; 
    182                         $tmpIDs[$i] = $new_sstyle->id; 
    183  
    184                         $stat = $new_sstyle->create(); 
    185                         if (!$stat) 
    186                         { 
    187                             PEAR::raiseError("Failed to create Midgard style \"{$subStyleNames}\", check config directives in the Midgard conf.d file \"{$init_file}\". Error was " . mgd_errstr()); 
    188                         } 
    189                         $sstyle = new midgard_style(); 
    190                         $sstyle->get_by_id($new_sstyle->id); 
    191                     } 
    192                     else 
    193                     { 
    194                         $sstyle = $sstyles[0]; 
    195                         $tmpIDs[$i] = $sstyle->id; 
    196                     } 
    197  
    198                     if($i==$subStyleCount-1) { 
    199                         $lastSubStyle = $sstyle; 
    200                     } 
    201                 } 
    202              
    203                 $qb = new MidgardQueryBuilder('midgard_element'); 
    204                 $qb->add_constraint('style', '=', $lastSubStyle->id); 
    205                 $qb->add_constraint('name', '=', $element_name); 
    206                 $elements = @$qb->execute(); 
    207              
    208                 if (count($elements) == 0) 
    209                 { 
    210                     // Create missing element 
    211                     $new_element = new midgard_element(); 
    212                     $new_element->style = $lastSubStyle->id; 
    213                     $new_element->name = $element_name; 
    214                     $stat = $new_element->create(); 
    215                     if (!$stat) 
    216                     { 
    217                         PEAR::raiseError("Failed to create element \"{$element_name}\" under Midgard style \"{$lastSubStyle->name}\", error " . mgd_errstr()); 
    218                     } 
    219                     $element = new midgard_element(); 
    220                     $element->get_by_id($new_element->id); 
    221                 } 
    222                 else 
    223                 { 
    224                     $element = $elements[0]; 
    225                 } 
    226              
    227                 $element->value = file_get_contents($installing_paths[3]); 
    228                 $stat = $element->update(); 
    229                 if (!$stat) 
    230                 { 
    231                     PEAR::raiseError("Failed to update element \"{$element_name}\" under Midgard style \"{$style->name}\", error " . mgd_errstr()); 
    232                     die(); 
    233                 } 
    234              
     186                PEAR::raiseError("Failed to update element \"{$element_name}\" under Midgard style \"{$style->name}\", error " . mgd_errstr(), mgd_errno(), PEAR_ERROR_DIE); 
    235187            } 
    236188        } 
  • trunk/support/Role_Midgardelement/Midgardelement.xml

    r3307 r4164  
    1616            <group>File Locations</group> 
    1717        </midgard_config_file> 
     18        <midgard_sitegroup> 
     19            <type>string</type> 
     20            <default>root</default> 
     21            <doc>Midgard sitegroup to use</doc> 
     22            <prompt>Midgard sitegroup ID ('root' for SG0)</prompt> 
     23            <group>Advanced</group> 
     24        </midgard_sitegroup> 
    1825    </config_vars> 
    1926</role> 
  • trunk/support/Role_Midgardelement/package.xml

    r4133 r4164  
    1919        <active>yes</active> 
    2020    </developer> 
    21     <date>2006-09-19</date> 
     21    <date>2006-09-21</date> 
    2222    <time>20:55:14</time> 
    2323    <version> 
    24         <release>1.0.4</release> 
     24        <release>1.1.0</release> 
    2525        <api>1.0.0</api> 
    2626    </version> 
     
    3030    </stability> 
    3131    <license uri="http://www.php.net/license/3_01.txt">PHP License</license> 
    32     <notes>Fixed problems in installing overlapping element names</notes> 
     32    <notes>Added support for substyles and uploading styles to specific sitegroups</notes> 
    3333    <contents> 
    3434        <dir name="/"> 
     
    4545                            <prompt>Which Midgard conf.d file to use?</prompt> 
    4646                            <default>midgard</default> 
     47                            <type>string</type> 
     48                        </param> 
     49                    </paramgroup> 
     50                    <paramgroup> 
     51                        <id>sgSetup</id> 
     52                        <param> 
     53                            <name>midgard_sitegroup</name> 
     54                            <prompt>Which Midgard sitegroup to use? (type 'root' for SG0, otherwise use ID)</prompt> 
     55                            <default>root</default> 
    4756                            <type>string</type> 
    4857                        </param> 
  • trunk/support/Role_Midgardelement/setup.php

    r3307 r4164  
    5151        switch ($phrase) { 
    5252            case 'dbSetup': 
    53                 if ($answers['midgard_config_file'] != '') { 
     53                if ($answers['midgard_config_file'] != '')  
     54                { 
    5455                    $doingit = "setting midgard_config_file to {$answers['midgard_config_file']} ..."; 
    5556                    $this->_ui->outputData($outputPrefix.$doingit); 
    5657                    $this->_config->set('midgard_config_file', $answers['midgard_config_file']); 
    5758                    $e = $this->_config->store(); 
    58                     if (PEAR::isError($e)) { 
     59                    if (PEAR::isError($e))  
     60                    { 
     61                        $this->_ui->outputData($outputPrefix.$e->getMessage()); 
     62                        return false; 
     63                    } 
     64                    return true; 
     65                } 
     66                return false; 
     67                break; 
     68            case 'sgSetup': 
     69                if ($answers['midgard_sitegroup'] != '')  
     70                { 
     71                    $doingit = "setting midgard_sitegroup to {$answers['midgard_sitegroup']} ..."; 
     72                    $this->_ui->outputData($outputPrefix.$doingit); 
     73                    $this->_config->set('midgard_sitegroup', $answers['midgard_sitegroup']); 
     74                    $e = $this->_config->store(); 
     75                    if (PEAR::isError($e))  
     76                    { 
    5977                        $this->_ui->outputData($outputPrefix.$e->getMessage()); 
    6078                        return false;