Changeset 73

Show
Ignore:
Timestamp:
09/06/05 14:56:10
Author:
hspath
Message:

- minor changes to meet PEAR Coding Standards
- improved caching functions (partially from patSite)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/pat/patXMLRenderer.php

    r68 r73  
    504504            $oldMask = @umask( 0 ); 
    505505 
    506             if( !@$this->_mkdir_recursive( $this->cacheDir, 0777 ) ) { 
     506            if ( !@$this->_mkdir_recursive( $this->cacheDir, 0777 ) ) { 
    507507                @umask( $oldMask ); 
    508508                die( '<b>patXMLRenderer:</b> Could not create cachedir \''.$this->cacheDir.'\'.' ); 
     
    727727 
    728728        // get default skin 
    729         foreach( $this->skins as $name => $skin ) 
    730             if( $skin['default'] ) 
     729        foreach ( $this->skins as $name => $skin ) 
     730            if ( $skin['default'] ) 
    731731                return $skin; 
    732732 
     
    851851            if ( isset( $_SERVER['HTTP_REFERER'] ) ) { 
    852852                $referer = strtolower( $_SERVER['HTTP_REFERER'] ); 
    853                 if( str_replace( $this->refererBlocks, '', $referer ) === $referer ) 
     853                if ( str_replace( $this->refererBlocks, '', $referer ) === $referer ) 
    854854                    $this->logger->writeRefererLog( $_SERVER['HTTP_REFERER'], $this->xmlFile ); 
    855855            } 
     
    13091309        if ( $cache != 'off' ) 
    13101310        { 
    1311             $this->cacheAble[( $this->parsers+1 )] = true; 
    1312             if( $content = $this->getCachedContent( $file ) ) 
     1311            $this->cacheAble[ $this->parsers+1 ] = true; 
     1312            if ( $content = $this->getCachedContent( $file ) ) 
    13131313                return $content; 
    13141314        } 
     
    13241324            if ( !xml_parse( $parser, $data, feof($fp) ) ) 
    13251325            { 
    1326                 die( sprintf( "XML error: %s at line %d in file %s"
     1326                die( sprintf( 'XML error: %s at line %d in file %s'
    13271327                            xml_error_string( xml_get_error_code( $parser ) ), 
    13281328                            xml_get_current_line_number( $parser ), 
     
    13961396        xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, true ); 
    13971397 
    1398         return $parser; 
     1398        return $parser; 
    13991399    } 
    14001400 
     
    14091409        $cacheFile = $this->getCacheFileName( $xmlFile ); 
    14101410 
    1411         // check, if cache file exists 
    1412         if ( file_exists( $cacheFile['cache'] ) ) 
    1413         { 
    1414             if ( filemtime( $xmlFile ) > filemtime( $cacheFile['cache'] ) ) 
    1415                 $cachable   = false; 
    1416             else 
     1411        // check if cache file exists and is up-to-date 
     1412        if ( file_exists( $cacheFile['cache'] )  and  filemtime( $xmlFile ) <= filemtime( $cacheFile['cache'] ) ) 
     1413        { 
     1414            $fp = fopen( $cacheFile['info'], 'r' ); 
     1415            flock( $fp, LOCK_SH ); 
     1416 
     1417            while ( $line = trim( fgets( $fp, 4096 ) ) ) 
    14171418            { 
    1418                 $cachable   = true; 
    1419                 $fp         = fopen( $cacheFile['info'], 'r' ); 
    1420                 flock( $fp, LOCK_SH ); 
    1421  
    1422                 while ( $line = trim( fgets( $fp, 4096 ) ) ) 
     1419                list( $command, $param ) = explode( '=', $line ); 
     1420 
     1421                switch ( $command ) 
    14231422                { 
    1424                     list( $command, $param ) = explode( '=', $line ); 
    1425                     switch( $command ) 
    1426                     { 
    1427                     case    'validUntil': 
    1428                         if( $param <  time() ) 
    1429                         { 
    1430                             $cachable   = false; 
    1431                             break 2; 
    1432                         } 
    1433                         break; 
    1434  
    1435                     case    'checkExternal': 
    1436                         if( !file_exists( $param ) || ( filemtime( $param ) >  filemtime( $cacheFile['cache'] ) ) ) 
    1437                         { 
    1438                             $cachable   = false; 
    1439                             break 2; 
    1440                         } 
    1441                         break; 
    1442                     case    'checkTemplate': 
    1443                         if( !file_exists( $param ) || ( filemtime( $param ) >  filemtime( $cacheFile['cache'] ) ) ) 
    1444                         { 
    1445                             $cachable   = false; 
    1446                             break 2; 
    1447                         } 
    1448                         break; 
    1449                     case    'action': 
    1450                         if( $param == 'startCache' ) 
    1451                             break 2; 
    1452                         break; 
    1453                     default: 
    1454                         break; 
    1455                     } 
     1423                case 'validUntil': 
     1424                    if ( $param <  time() ) 
     1425                        return false; 
     1426                    break; 
     1427 
     1428                case 'checkExternal': 
     1429                case 'checkTemplate': 
     1430                    if ( !file_exists( $param ) || ( filemtime( $param ) >  filemtime( $cacheFile['cache'] ) ) ) 
     1431                        return false; 
     1432                    break; 
     1433 
    14561434                } 
    1457                 flock( $fp, LOCK_UN ); 
    1458                 fclose( $fp ); 
    1459  
    1460                 return ( $cachable ) 
    1461                     ? @implode( '', @file( $cacheFile['cache'] ) ) 
    1462                     : false; 
    14631435            } 
    1464         } 
    1465         else 
    1466             return false; 
     1436            flock( $fp, LOCK_UN ); 
     1437            fclose( $fp ); 
     1438 
     1439            return @implode( '', @file( $cacheFile['cache'] ) ); 
     1440        } 
     1441 
     1442        return false; 
    14671443    } 
    14681444 
     
    14721448     * @param   string  $xmlFile    filename of the xml file 
    14731449     * @param   string  $content    content of the cached file 
    1474      * @todo    see comment in the code 
    14751450     */ 
    14761451    function writeCache( $xmlFile, $content ) 
     
    14811456        $tmp        = dirname( $cacheFile['cache'] ); 
    14821457 
    1483         // TODO: if ( $tmp != $cacheFile ) :TODO: what was this for? 
    14841458        $this->_mkdir_recursive( $tmp, 0700 ); 
    14851459 
     
    15041478            while ( $tmpl = array_pop( $templates ) ) 
    15051479            { 
    1506                 $tmplFile   =   ( $this->tmpl->basedir!="" ) ? $this->tmpl->basedir."/".$tmpl : $tmpl; 
    1507                 fputs( $fp, "checkTemplate=".$tmplFile."\n" ); 
     1480                $tmplFile   =   ( $this->tmpl->basedir!='' ) ? $this->tmpl->basedir.'/'.$tmpl : $tmpl; 
     1481                fputs( $fp, 'checkTemplate='.$tmplFile."\n" ); 
    15081482            } 
    1509             fputs( $fp, "action=StartCache\n" ); 
    15101483            flock( $fp, LOCK_UN ); 
    15111484            fclose( $fp ); 
     
    15221495            fputs( $fp, "cachetype=force\n" ); 
    15231496            fputs( $fp, 'validUntil='.( time() + $cachetime )."\n" ); 
    1524             fputs( $fp, "action=StartCache\n" ); 
    15251497            flock( $fp, LOCK_UN ); 
    15261498            fclose( $fp ); 
     
    15591531     * build the cache filename for a xmlfile 
    15601532     * 
    1561      * @param   string  $xmlFile    filename of the xml file 
    1562      * @return  string  $cacheFile  filename of the cache file 
     1533     * @param   string  $xmlFile    filename of the XML file 
     1534     * @return  string  $cacheFile  filenames of the cache file 
    15631535     */ 
    15641536    function getCacheFileName( $xmlFile ) 
    15651537    { 
    1566         // create name of the cache file 
    1567         $pos        = strrpos( $xmlFile, '.' ); 
    1568         if ( $pos != false ) 
    1569             $base   = $this->cacheDir.'/'.md5( substr( $xmlFile, 0, $pos ) ); 
    1570  
    1571         $cacheFile  = array( 
    1572             'cache'     => $base.'.cache', 
    1573             'info'      => $base.'.info', 
     1538        $base = $this->cacheDir . '/' . md5( $xmlFile ); 
     1539 
     1540        $cacheFile = array( 
     1541            'cache' => $base.'.cache', 
     1542            'info'  => $base.'.info', 
    15741543        ); 
    15751544        return $cacheFile; 
     
    15771546 
    15781547    /** 
    1579      *   get url for Randy 
    1580      * 
    1581      *   @return string  $randy  url for randy 
     1548     * get URL for Randy 
     1549     * 
     1550     * @return  string  $randy      URL for randy 
    15821551     */ 
    15831552    function getSelfUrl() 
    15841553    { 
    1585         $self = $_SERVER['PHP_SELF'].'?'; 
    1586  
    1587         for ( $i=0; $i<count( $this->keepVars ); $i++
    1588             if ( $GLOBALS[$this->keepVars[$i]]
    1589                 $self .= '&'.$this->keepVars[$i].'='.$GLOBALS[$this->keepVars[$i]]; 
    1590  
    1591         return $self; 
    1592     } 
    1593  
    1594     /** 
    1595      * create a string representation of an XML tag 
     1554        $self = $_SERVER['PHP_SELF'] . '?'; 
     1555 
     1556        foreach ( $this->keepVars as $keepVar
     1557            if ( isset( $GLOBALS[$keepVar] )
     1558                $self .= '&'. $keepVar .'='. $GLOBALS[$keepVar]; 
     1559 
     1560        return $self; 
     1561    } 
     1562 
     1563    /** 
     1564     * creates a string representation of an XML tag 
    15961565     * 
    15971566     * @param   string  $name       name of the tag, including the namespace 
     
    16001569     * @return  string  $string     string representation of the tag 
    16011570     */ 
    1602     function tagToString( $name, $attributes, $content = "" ) 
    1603     { 
    1604         $string     =   '<'.$name; 
     1571    function tagToString( $name, $attributes, $content = '' ) 
     1572    { 
     1573        $string = '<'.$name; 
     1574 
    16051575        foreach ( $attributes as $key => $value ) 
    1606             $string .=  sprintf( ' %s="%s"', strtolower( $key ), $this->replaceEntities( $value ) ); 
    1607  
    1608         if ( $content ) 
    1609             $string .=  '>'.$content.'</'.$name.'>'; 
    1610         else 
    1611             $string .=  '/>'; 
    1612  
    1613         return  $string; 
    1614     } 
    1615  
    1616     /** 
    1617      * replace XML entities in a string 
    1618      * 
    1619      * @access  public 
    1620      * @param   string  $string     string in whuich entities should be replaced 
     1576            $string .= ' '. strtolower( $key ) .'="'. $this->replaceEntities( $value ) .'"'; 
     1577 
     1578        $string .= ( strlen($content) > 0 ) 
     1579            ? '>'. $content .'</'. $name .'>' 
     1580            : '/>'; 
     1581 
     1582        return $string; 
     1583    } 
     1584 
     1585    /** 
     1586     * replaces XML entities in a string 
     1587     * 
     1588     * @access  public 
     1589     * @param   string  $string     string in which entities should be replaced 
    16211590     * @return  string  $string 
    16221591     */ 
     
    16361605    { 
    16371606        list( $usec, $sec ) = explode( ' ', microtime() ); 
    1638         return ( (float)$usec + (float)$sec )
    1639     } 
    1640  
    1641     /** 
    1642      *  Attempts to create all non-existing directories of a specified path 
     1607        return (float)$usec + (float)$sec
     1608    } 
     1609 
     1610    /** 
     1611     *  attempts to create all non-existing directories of a specified path 
    16431612     * 
    16441613     *  Despite of its name, it doesn't use recursion.