Changeset 4312
- Timestamp:
- 10/06/06 21:44:51 (2 years ago)
- Files:
-
- trunk/templates/export_style.php (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/templates/export_style.php
r4183 r4312 12 12 var $output; 13 13 var $static_output; 14 var $static_source; 14 15 var $dumped_files = array(); 15 16 var $base_url; 17 var $tmp_file_url; 16 18 var $sg; 17 19 var $package_element_paths = array(); … … 52 54 'desc' => 'Base URL for fetching files', 53 55 ); 56 $opts_config['static_base'] = array ( 57 'short' => 'sb', 58 'max' => 1, 59 'min' => 1, 60 'desc' => 'Path (local) to midcom-static', 61 ); 54 62 $opts_config['version'] = array ( 55 63 'short' => 'v', … … 57 65 'min' => 1, 58 66 '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', 59 73 ); 60 74 $opts_config['user'] = array ( … … 80 94 81 95 $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" ; 83 97 if (PEAR::isError($args)) 84 98 { … … 170 184 $this->base_url = $urlbase; 171 185 } 186 $this->static_source = $this->args->getValue('static_base'); 187 172 188 $this->packagename = 'style_' . str_replace(array('-',' '), array('','_'), $this->name); 173 189 … … 311 327 $name = $origname; 312 328 $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; 326 366 } 327 367 $fp = fopen($path, 'w'); … … 349 389 { 350 390 $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"; 351 397 } 352 398 $package_xml = <<<EOF … … 356 402 <summary>Style {$this->name}</summary> 357 403 <description> 358 Exported with style_export.php404 {$description} 359 405 </description> 360 406 <lead> … … 388 434 $package_xml .= " <file name=\"{$name}\" baseinstalldir=\"{$baseinstalldir}\" role=\"midgardelement\" />\n"; 389 435 } 436 // TODO: Find also old files in static (first use wget to make sure we have them all, then opendir/readdir to list them) 390 437 foreach ($this->dumped_files as $name) 391 438 { … … 442 489 $found = array(); 443 490 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; 448 508 //echo "DEBUG: found uri '{$uri}'\n"; 449 509 if (array_key_exists($uri, $found)) … … 452 512 continue; 453 513 } 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 461 538 462 539 // Other manipulation rules ?? 463 540 464 541 // 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 } 473 558 } 474 559 $found[$uri] = $normalized;
