Changeset 4312

Show
Ignore:
Timestamp:
10/06/06 21:44:51 (2 years ago)
Author:
rambo
Message:

UNTESTED: attempt to 1. handle images inside CSS files 2. faciliate dump/import/dump cycle so that all the static images are always included properly

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/templates/export_style.php

    r4183 r4312  
    1212    var $output; 
    1313    var $static_output; 
     14    var $static_source; 
    1415    var $dumped_files = array(); 
    1516    var $base_url; 
     17    var $tmp_file_url; 
    1618    var $sg; 
    1719    var $package_element_paths = array(); 
     
    5254                'desc'  => 'Base URL for fetching files', 
    5355        ); 
     56        $opts_config['static_base'] = array ( 
     57                'short' => 'sb', 
     58                'max'   => 1, 
     59                'min'   => 1, 
     60                'desc'  => 'Path (local) to midcom-static', 
     61        ); 
    5462        $opts_config['version'] = array ( 
    5563                'short' => 'v', 
     
    5765                'min'   => 1, 
    5866                'desc'  => 'Style version number', 
     67        ); 
     68        $opts_config['description'] = array ( 
     69                'short' => 'd', 
     70                'max'   => 1, 
     71                'min'   => 1, 
     72                'desc'  => 'Description of the style', 
    5973        ); 
    6074        $opts_config['user'] = array ( 
     
    8094         
    8195        $args = Console_Getargs::factory($opts_config); 
    82         $header = "Usage: " .basename($_SERVER['SCRIPT_NAME'])." [options]\n\n" ; 
     96        $header = "Usage: " .basename($GLOBALS['argv'][0])." [options]\n\n" ; 
    8397        if (PEAR::isError($args)) 
    8498        { 
     
    170184            $this->base_url = $urlbase; 
    171185        } 
     186        $this->static_source = $this->args->getValue('static_base'); 
     187         
    172188        $this->packagename = 'style_' . str_replace(array('-',' '), array('','_'), $this->name); 
    173189 
     
    311327        $name = $origname; 
    312328        $path = $this->static_output . "/{$name}"; 
    313         $count = 1; 
    314         while (file_exists($path)) 
    315         { 
    316             if ($count > 15) 
    317             { 
    318                 // Report error 
    319                 echo "ERROR: Too many retries for dumping file {$origname}\n"; 
    320                 return false; 
    321             } 
    322             $count++; 
    323             preg_match('/(.*)\.(.*)$/', $origname, $matches); 
    324             $name = "{$matches[1]}_{$count}.{$matches[2]}"; 
    325             $path = $this->static_output . "/{$name}"; 
     329        // If we dump by http, do not overwrite old files 
     330        if (!strstr($uri, $this->static_source)) 
     331        { 
     332            $count = 1; 
     333            preg_match('/(.*)\.(.*)$/', $origname, $matches_ext); 
     334            $namepart = $matches_ext[1]; 
     335            $extpart = $matches_ext[2]; 
     336            while (file_exists($path)) 
     337            { 
     338                if ($count > 15) 
     339                { 
     340                    // Report error 
     341                    echo "ERROR: Too many retries for dumping file {$origname}\n"; 
     342                    return false; 
     343                } 
     344                $count++; 
     345                $name = "{$namepart}_{$count}.{$extpart}"; 
     346                $path = $this->static_output . "/{$name}"; 
     347            } 
     348        } 
     349        if (strtolower($extpart) == 'css') 
     350        { 
     351            $this->tmp_file_url = $uri; 
     352            // The file is a stylesheet, look inside for more file references... 
     353            $css_files = $this->find_file_references($data); 
     354            foreach ($css_files as $uri => $normalized) 
     355            { 
     356                $name = $this->dump_file($normalized); 
     357                if (!$name) 
     358                { 
     359                    // file dump failure 
     360                    continue; 
     361                } 
     362                $newuri = "{$name}"; 
     363                $element->value = str_replace($uri, $newuri, $data); 
     364            } 
     365            $this->tmp_file_url = false; 
    326366        } 
    327367        $fp = fopen($path, 'w'); 
     
    349389        { 
    350390            $version = '1.0.' . time(); 
     391        } 
     392        $description = $this->args->getValue('description'); 
     393        if (empty($description)) 
     394        { 
     395            $path = $this->args->getValue('style_path'); 
     396            $description = "Style '{$path}' exported with style_export.php"; 
    351397        } 
    352398        $package_xml = <<<EOF 
     
    356402    <summary>Style {$this->name}</summary> 
    357403    <description> 
    358         Exported with style_export.php 
     404        {$description} 
    359405    </description> 
    360406    <lead> 
     
    388434            $package_xml .= "            <file name=\"{$name}\" baseinstalldir=\"{$baseinstalldir}\" role=\"midgardelement\" />\n"; 
    389435        } 
     436        // TODO: Find also old files in static (first use wget to make sure we have them all, then opendir/readdir to list them) 
    390437        foreach ($this->dumped_files as $name) 
    391438        { 
     
    442489        $found = array(); 
    443490         
    444         $regex = "/(src|<link.*?href)=(['\"])(.*?)\\2/i"; 
    445         preg_match_all($regex, $content, $matches); 
    446         foreach ($matches[3] as $uri) 
    447         { 
     491        $merged = array(); 
     492        $regex_src = "/(src|<link.*?href)=(['\"])(.*?)\\2/i"; 
     493        preg_match_all($regex_src, $content, $matches_src); 
     494        foreach ($matches_src[3] as $uri) 
     495        { 
     496            $merged[] = $uri; 
     497        } 
     498        $regex_url = "/url\s*\(([\"'ÂŽ])?(.*?)\\1?\)//i"; 
     499        preg_match_all($regex_url, $content, $matches_url); 
     500        foreach ($matches_url[2] as $uri) 
     501        { 
     502            $merged[] = $uri; 
     503        } 
     504         
     505        foreach ($merged as $uri) 
     506        { 
     507            $normalized = false; 
    448508            //echo "DEBUG: found uri '{$uri}'\n"; 
    449509            if (array_key_exists($uri, $found)) 
     
    452512                continue; 
    453513            } 
    454             if (   stristr($uri, 'midcom-static') 
    455                 || stristr($uri, 'midcom_static')) 
    456             { 
    457                 // SKip files already in static 
    458                 //echo "DEBUG: uri recognized as static, skipping\n"; 
    459                 continue; 
    460             } 
     514            if (stristr($uri, $this->packagename)) 
     515            { 
     516                // Reference to the styles midcom-static 
     517                if (empty($this->static_source)) 
     518                { 
     519                    // Cannot create local path 
     520                    continue; 
     521                } 
     522                $normalized = "{$this->static_source}/{$this->packagename}/" . basename($uri); 
     523            } 
     524            else 
     525            { 
     526                if (   stristr($uri, 'midcom-static') 
     527                    || stristr($uri, 'midcom_static') 
     528                    || (   preg_match('/^\.\./', $uri) 
     529                        && stristr($this->tmp_file_url, 'midcom-static')) 
     530                    ) 
     531                { 
     532                    // SKip files already in static 
     533                    //echo "DEBUG: uri recognized as static, skipping\n"; 
     534                    continue; 
     535                } 
     536            } 
     537             
    461538             
    462539            // Other manipulation rules ?? 
    463540             
    464541            // Normalize uri 
    465             if (!preg_match('|^https?://|', $uri)) 
    466             { 
    467                 $normalized = $this->base_url . $uri; 
    468             } 
    469             else 
    470             { 
    471                 // No need to normalize 
    472                 $normalized = $uri; 
     542            if (empty($normalized)) 
     543            { 
     544                if (   preg_match('/^\.\./', $uri) 
     545                    && $this->tmp_file_url) 
     546                { 
     547                    $normalized = dirname($this->tmp_file_url) . $uri; 
     548                } 
     549                else if (!preg_match('|^https?://|', $uri)) 
     550                { 
     551                    $normalized = $this->base_url . $uri; 
     552                } 
     553                else 
     554                { 
     555                    // No need to normalize 
     556                    $normalized = $uri; 
     557                } 
    473558            } 
    474559            $found[$uri] = $normalized;