PEAR
[ class tree: PEAR ] [ index: PEAR ] [ all elements ]

Source for file v1.php

Documentation is available at v1.php

  1. <?php
  2. /**
  3.  * package.xml parsing class, package.xml version 1.0
  4.  *
  5.  * PHP versions 4 and 5
  6.  *
  7.  * @category   pear
  8.  * @package    PEAR
  9.  * @author     Greg Beaver <cellog@php.net>
  10.  * @copyright  1997-2009 The Authors
  11.  * @license    http://opensource.org/licenses/bsd-license.php New BSD License
  12.  * @version    CVS: $Id: v1.php 313023 2011-07-06 19:17:11Z dufuz $
  13.  * @link       http://pear.php.net/package/PEAR
  14.  * @since      File available since Release 1.4.0a1
  15.  */
  16. /**
  17.  * package.xml abstraction class
  18.  */
  19. require_once 'PEAR/PackageFile/v1.php';
  20. /**
  21.  * Parser for package.xml version 1.0
  22.  * @category   pear
  23.  * @package    PEAR
  24.  * @author     Greg Beaver <cellog@php.net>
  25.  * @copyright  1997-2009 The Authors
  26.  * @license    http://opensource.org/licenses/bsd-license.php New BSD License
  27.  * @version    Release: @PEAR-VER@
  28.  * @link       http://pear.php.net/package/PEAR
  29.  * @since      Class available since Release 1.4.0a1
  30.  */
  31. {
  32.     var $_registry;
  33.     var $_config;
  34.     var $_logger;
  35.     /**
  36.      * BC hack to allow PEAR_Common::infoFromString() to sort of
  37.      * work with the version 2.0 format - there's no filelist though
  38.      * @param PEAR_PackageFile_v2 
  39.      */
  40.     function fromV2($packagefile)
  41.     {
  42.         $info $packagefile->getArray(true);
  43.         $ret = new PEAR_PackageFile_v1;
  44.         $ret->fromArray($info['old']);
  45.     }
  46.  
  47.     function setConfig(&$c)
  48.     {
  49.         $this->_config &$c;
  50.         $this->_registry &$c->getRegistry();
  51.     }
  52.  
  53.     function setLogger(&$l)
  54.     {
  55.         $this->_logger &$l;
  56.     }
  57.  
  58.     /**
  59.      * @param string contents of package.xml file, version 1.0
  60.      * @return bool success of parsing
  61.      */
  62.     function &parse($data$file$archive = false)
  63.     {
  64.         if (!extension_loaded('xml')) {
  65.             return PEAR::raiseError('Cannot create xml parser for parsing package.xml, no xml extension');
  66.         }
  67.         $xp xml_parser_create();
  68.         if (!$xp{
  69.             $a &PEAR::raiseError('Cannot create xml parser for parsing package.xml');
  70.             return $a;
  71.         }
  72.         xml_set_object($xp$this);
  73.         xml_set_element_handler($xp'_element_start_1_0''_element_end_1_0');
  74.         xml_set_character_data_handler($xp'_pkginfo_cdata_1_0');
  75.         xml_parser_set_option($xpXML_OPTION_CASE_FOLDINGfalse);
  76.  
  77.         $this->element_stack = array();
  78.         $this->_packageInfo = array('provides' => array());
  79.         $this->current_element = false;
  80.         unset($this->dir_install);
  81.         $this->_packageInfo['filelist'= array();
  82.         $this->filelist =$this->_packageInfo['filelist'];
  83.         $this->dir_names = array();
  84.         $this->in_changelog = false;
  85.         $this->d_i = 0;
  86.         $this->cdata '';
  87.         $this->_isValid = true;
  88.  
  89.         if (!xml_parse($xp$data1)) {
  90.             $code xml_get_error_code($xp);
  91.             $line xml_get_current_line_number($xp);
  92.             xml_parser_free($xp);
  93.             $a &PEAR::raiseError(sprintf("XML error: %s at line %d",
  94.                            $str xml_error_string($code)$line)2);
  95.             return $a;
  96.         }
  97.  
  98.         xml_parser_free($xp);
  99.  
  100.         $pf = new PEAR_PackageFile_v1;
  101.         $pf->setConfig($this->_config);
  102.         if (isset($this->_logger)) {
  103.             $pf->setLogger($this->_logger);
  104.         }
  105.         $pf->setPackagefile($file$archive);
  106.         $pf->fromArray($this->_packageInfo);
  107.         return $pf;
  108.     }
  109.     // {{{ _unIndent()
  110.  
  111.     /**
  112.      * Unindent given string
  113.      *
  114.      * @param string $str The string that has to be unindented.
  115.      * @return string 
  116.      * @access private
  117.      */
  118.     function _unIndent($str)
  119.     {
  120.         // remove leading newlines
  121.         $str preg_replace('/^[\r\n]+/'''$str);
  122.         // find whitespace at the beginning of the first line
  123.         $indent_len strspn($str" \t");
  124.         $indent substr($str0$indent_len);
  125.         $data '';
  126.         // remove the same amount of whitespace from following lines
  127.         foreach (explode("\n"$stras $line{
  128.             if (substr($line0$indent_len== $indent{
  129.                 $data .= substr($line$indent_len"\n";
  130.             elseif (trim(substr($line0$indent_len))) {
  131.                 $data .= ltrim($line);
  132.             }
  133.         }
  134.         return $data;
  135.     }
  136.  
  137.     // Support for package DTD v1.0:
  138.     // {{{ _element_start_1_0()
  139.  
  140.     /**
  141.      * XML parser callback for ending elements.  Used for version 1.0
  142.      * packages.
  143.      *
  144.      * @param resource  $xp    XML parser resource
  145.      * @param string    $name  name of ending element
  146.      *
  147.      * @return void 
  148.      *
  149.      * @access private
  150.      */
  151.     function _element_start_1_0($xp$name$attribs)
  152.     {
  153.         array_push($this->element_stack$name);
  154.         $this->current_element $name;
  155.         $spos sizeof($this->element_stack- 2;
  156.         $this->prev_element ($spos >= 0$this->element_stack[$spos'';
  157.         $this->current_attributes $attribs;
  158.         $this->cdata '';
  159.         switch ($name{
  160.             case 'dir':
  161.                 if ($this->in_changelog{
  162.                     break;
  163.                 }
  164.                 if (array_key_exists('name'$attribs&& $attribs['name'!= '/'{
  165.                     $attribs['name'preg_replace(array('!\\\\+!''!/+!')array('/''/'),
  166.                         $attribs['name']);
  167.                     if (strrpos($attribs['name']'/'=== strlen($attribs['name']- 1{
  168.                         $attribs['name'substr($attribs['name']0,
  169.                             strlen($attribs['name']- 1);
  170.                     }
  171.                     if (strpos($attribs['name']'/'=== 0{
  172.                         $attribs['name'substr($attribs['name']1);
  173.                     }
  174.                     $this->dir_names[$attribs['name'];
  175.                 }
  176.                 if (isset($attribs['baseinstalldir'])) {
  177.                     $this->dir_install $attribs['baseinstalldir'];
  178.                 }
  179.                 if (isset($attribs['role'])) {
  180.                     $this->dir_role $attribs['role'];
  181.                 }
  182.                 break;
  183.             case 'file':
  184.                 if ($this->in_changelog{
  185.                     break;
  186.                 }
  187.                 if (isset($attribs['name'])) {
  188.                     $path '';
  189.                     if (count($this->dir_names)) {
  190.                         foreach ($this->dir_names as $dir{
  191.                             $path .= $dir '/';
  192.                         }
  193.                     }
  194.                     $path .= preg_replace(array('!\\\\+!''!/+!')array('/''/'),
  195.                         $attribs['name']);
  196.                     unset($attribs['name']);
  197.                     $this->current_path $path;
  198.                     $this->filelist[$path$attribs;
  199.                     // Set the baseinstalldir only if the file don't have this attrib
  200.                     if (!isset($this->filelist[$path]['baseinstalldir']&&
  201.                         isset($this->dir_install))
  202.                     {
  203.                         $this->filelist[$path]['baseinstalldir'$this->dir_install;
  204.                     }
  205.                     // Set the Role
  206.                     if (!isset($this->filelist[$path]['role']&& isset($this->dir_role)) {
  207.                         $this->filelist[$path]['role'$this->dir_role;
  208.                     }
  209.                 }
  210.                 break;
  211.             case 'replace':
  212.                 if (!$this->in_changelog{
  213.                     $this->filelist[$this->current_path]['replacements'][$attribs;
  214.                 }
  215.                 break;
  216.             case 'maintainers':
  217.                 $this->_packageInfo['maintainers'= array();
  218.                 $this->m_i = 0; // maintainers array index
  219.                 break;
  220.             case 'maintainer':
  221.                 // compatibility check
  222.                 if (!isset($this->_packageInfo['maintainers'])) {
  223.                     $this->_packageInfo['maintainers'= array();
  224.                     $this->m_i = 0;
  225.                 }
  226.                 $this->_packageInfo['maintainers'][$this->m_i= array();
  227.                 $this->current_maintainer =$this->_packageInfo['maintainers'][$this->m_i];
  228.                 break;
  229.             case 'changelog':
  230.                 $this->_packageInfo['changelog'= array();
  231.                 $this->c_i = 0; // changelog array index
  232.                 $this->in_changelog = true;
  233.                 break;
  234.             case 'release':
  235.                 if ($this->in_changelog{
  236.                     $this->_packageInfo['changelog'][$this->c_i= array();
  237.                     $this->current_release &$this->_packageInfo['changelog'][$this->c_i];
  238.                 else {
  239.                     $this->current_release &$this->_packageInfo;
  240.                 }
  241.                 break;
  242.             case 'deps':
  243.                 if (!$this->in_changelog{
  244.                     $this->_packageInfo['release_deps'= array();
  245.                 }
  246.                 break;
  247.             case 'dep':
  248.                 // dependencies array index
  249.                 if (!$this->in_changelog{
  250.                     $this->d_i++;
  251.                     isset($attribs['type']($attribs['type'strtolower($attribs['type'])) : false;
  252.                     $this->_packageInfo['release_deps'][$this->d_i$attribs;
  253.                 }
  254.                 break;
  255.             case 'configureoptions':
  256.                 if (!$this->in_changelog{
  257.                     $this->_packageInfo['configure_options'= array();
  258.                 }
  259.                 break;
  260.             case 'configureoption':
  261.                 if (!$this->in_changelog{
  262.                     $this->_packageInfo['configure_options'][$attribs;
  263.                 }
  264.                 break;
  265.             case 'provides':
  266.                 if (empty($attribs['type']|| empty($attribs['name'])) {
  267.                     break;
  268.                 }
  269.                 $attribs['explicit'= true;
  270.                 $this->_packageInfo['provides']["$attribs[type];$attribs[name]"$attribs;
  271.                 break;
  272.             case 'package' :
  273.                 if (isset($attribs['version'])) {
  274.                     $this->_packageInfo['xsdversion'trim($attribs['version']);
  275.                 else {
  276.                     $this->_packageInfo['xsdversion''1.0';
  277.                 }
  278.                 if (isset($attribs['packagerversion'])) {
  279.                     $this->_packageInfo['packagerversion'$attribs['packagerversion'];
  280.                 }
  281.                 break;
  282.         }
  283.     }
  284.  
  285.     // }}}
  286.     // {{{ _element_end_1_0()
  287.  
  288.     /**
  289.      * XML parser callback for ending elements.  Used for version 1.0
  290.      * packages.
  291.      *
  292.      * @param resource  $xp    XML parser resource
  293.      * @param string    $name  name of ending element
  294.      *
  295.      * @return void 
  296.      *
  297.      * @access private
  298.      */
  299.     function _element_end_1_0($xp$name)
  300.     {
  301.         $data trim($this->cdata);
  302.         switch ($name{
  303.             case 'name':
  304.                 switch ($this->prev_element{
  305.                     case 'package':
  306.                         $this->_packageInfo['package'$data;
  307.                         break;
  308.                     case 'maintainer':
  309.                         $this->current_maintainer['name'$data;
  310.                         break;
  311.                 }
  312.                 break;
  313.             case 'extends' :
  314.                 $this->_packageInfo['extends'$data;
  315.                 break;
  316.             case 'summary':
  317.                 $this->_packageInfo['summary'$data;
  318.                 break;
  319.             case 'description':
  320.                 $data $this->_unIndent($this->cdata);
  321.                 $this->_packageInfo['description'$data;
  322.                 break;
  323.             case 'user':
  324.                 $this->current_maintainer['handle'$data;
  325.                 break;
  326.             case 'email':
  327.                 $this->current_maintainer['email'$data;
  328.                 break;
  329.             case 'role':
  330.                 $this->current_maintainer['role'$data;
  331.                 break;
  332.             case 'version':
  333.                 if ($this->in_changelog{
  334.                     $this->current_release['version'$data;
  335.                 else {
  336.                     $this->_packageInfo['version'$data;
  337.                 }
  338.                 break;
  339.             case 'date':
  340.                 if ($this->in_changelog{
  341.                     $this->current_release['release_date'$data;
  342.                 else {
  343.                     $this->_packageInfo['release_date'$data;
  344.                 }
  345.                 break;
  346.             case 'notes':
  347.                 // try to "de-indent" release notes in case someone
  348.                 // has been over-indenting their xml ;-)
  349.                 // Trim only on the right side
  350.                 $data rtrim($this->_unIndent($this->cdata));
  351.                 if ($this->in_changelog{
  352.                     $this->current_release['release_notes'$data;
  353.                 else {
  354.                     $this->_packageInfo['release_notes'$data;
  355.                 }
  356.                 break;
  357.             case 'warnings':
  358.                 if ($this->in_changelog{
  359.                     $this->current_release['release_warnings'$data;
  360.                 else {
  361.                     $this->_packageInfo['release_warnings'$data;
  362.                 }
  363.                 break;
  364.             case 'state':
  365.                 if ($this->in_changelog{
  366.                     $this->current_release['release_state'$data;
  367.                 else {
  368.                     $this->_packageInfo['release_state'$data;
  369.                 }
  370.                 break;
  371.             case 'license':
  372.                 if ($this->in_changelog{
  373.                     $this->current_release['release_license'$data;
  374.                 else {
  375.                     $this->_packageInfo['release_license'$data;
  376.                 }
  377.                 break;
  378.             case 'dep':
  379.                 if ($data && !$this->in_changelog{
  380.                     $this->_packageInfo['release_deps'][$this->d_i]['name'$data;
  381.                 }
  382.                 break;
  383.             case 'dir':
  384.                 if ($this->in_changelog{
  385.                     break;
  386.                 }
  387.                 array_pop($this->dir_names);
  388.                 break;
  389.             case 'file':
  390.                 if ($this->in_changelog{
  391.                     break;
  392.                 }
  393.                 if ($data{
  394.                     $path '';
  395.                     if (count($this->dir_names)) {
  396.                         foreach ($this->dir_names as $dir{
  397.                             $path .= $dir '/';
  398.                         }
  399.                     }
  400.                     $path .= $data;
  401.                     $this->filelist[$path$this->current_attributes;
  402.                     // Set the baseinstalldir only if the file don't have this attrib
  403.                     if (!isset($this->filelist[$path]['baseinstalldir']&&
  404.                         isset($this->dir_install))
  405.                     {
  406.                         $this->filelist[$path]['baseinstalldir'$this->dir_install;
  407.                     }
  408.                     // Set the Role
  409.                     if (!isset($this->filelist[$path]['role']&& isset($this->dir_role)) {
  410.                         $this->filelist[$path]['role'$this->dir_role;
  411.                     }
  412.                 }
  413.                 break;
  414.             case 'maintainer':
  415.                 if (empty($this->_packageInfo['maintainers'][$this->m_i]['role'])) {
  416.                     $this->_packageInfo['maintainers'][$this->m_i]['role''lead';
  417.                 }
  418.                 $this->m_i++;
  419.                 break;
  420.             case 'release':
  421.                 if ($this->in_changelog{
  422.                     $this->c_i++;
  423.                 }
  424.                 break;
  425.             case 'changelog':
  426.                 $this->in_changelog = false;
  427.                 break;
  428.         }
  429.         array_pop($this->element_stack);
  430.         $spos sizeof($this->element_stack- 1;
  431.         $this->current_element ($spos > 0$this->element_stack[$spos'';
  432.         $this->cdata '';
  433.     }
  434.  
  435.     // }}}
  436.     // {{{ _pkginfo_cdata_1_0()
  437.  
  438.     /**
  439.      * XML parser callback for character data.  Used for version 1.0
  440.      * packages.
  441.      *
  442.      * @param resource  $xp    XML parser resource
  443.      * @param string    $name  character data
  444.      *
  445.      * @return void 
  446.      *
  447.      * @access private
  448.      */
  449.     function _pkginfo_cdata_1_0($xp$data)
  450.     {
  451.         if (isset($this->cdata)) {
  452.             $this->cdata .= $data;
  453.         }
  454.     }
  455.  
  456.     // }}}
  457. }
  458. ?>

Documentation generated on Wed, 06 Jul 2011 23:31:32 +0000 by phpDocumentor 1.4.3. PEAR Logo Copyright © PHP Group 2004.