00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 include_once( 'lib/ezutils/classes/ezini.php' );
00053 include_once( 'lib/ezutils/classes/ezhttptool.php' );
00054
00055 define( 'EZ_STATIC_CACHE_USER_AGENT', 'eZ Publish static cache generator' );
00056
00057 class eZStaticCache
00058 {
00059
00060
00061
00062 function eZStaticCache()
00063 {
00064 $ini =& eZINI::instance( 'staticcache.ini');
00065 $this->HostName = $ini->variable( 'CacheSettings', 'HostName' );
00066 $this->StaticStorageDir = $ini->variable( 'CacheSettings', 'StaticStorageDir' );
00067 $this->MaxCacheDepth = $ini->variable( 'CacheSettings', 'MaxCacheDepth' );
00068 $this->CachedURLArray = $ini->variable( 'CacheSettings', 'CachedURLArray' );
00069 $this->CachedSiteAccesses = $ini->variable( 'CacheSettings', 'CachedSiteAccesses' );
00070 $this->AlwaysUpdate = $ini->variable( 'CacheSettings', 'AlwaysUpdateArray' );
00071 }
00072
00073
00074
00075
00076 function hostName()
00077 {
00078 return $this->HostName;
00079 }
00080
00081
00082
00083
00084 function storageDirectory()
00085 {
00086 return $this->StaticStorageDir;
00087 }
00088
00089
00090
00091
00092 function maxCacheDepth()
00093 {
00094 return $this->MaxCacheDepth;
00095 }
00096
00097
00098
00099
00100 function cachedSiteAccesses()
00101 {
00102 return $this->CachedSiteAccesses;
00103 }
00104
00105
00106
00107
00108 function cachedURLArray()
00109 {
00110 return $this->CachedURLArray;
00111 }
00112
00113
00114
00115
00116
00117
00118 function alwaysUpdateURLArray()
00119 {
00120 return $this->AlwaysUpdate;
00121 }
00122
00123
00124
00125
00126
00127
00128 function generateAlwaysUpdatedCache( $quiet = false, $cli = false, $delay = true )
00129 {
00130 $hostname = $this->HostName;
00131 $staticStorageDir = $this->StaticStorageDir;
00132
00133 foreach ( $this->AlwaysUpdate as $uri )
00134 {
00135 if ( !$quiet and $cli )
00136 $cli->output( "caching: $uri ", false );
00137 $this->storeCache( $uri, $hostname, $staticStorageDir, array(), false, $delay );
00138 if ( !$quiet and $cli )
00139 $cli->output( "done" );
00140 }
00141 }
00142
00143 function generateNodeListCache( $nodeList )
00144 {
00145 $db =& eZDB::instance();
00146
00147 foreach ( $nodeList as $uri )
00148 {
00149
00150 $this->cacheURL( '/' . $uri['path_identification_string']);
00151
00152
00153
00154 $srcURL = $db->escapeString( $uri['path_identification_string'] );
00155 $destURL = $db->arrayQuery( "SELECT destination_url FROM ezurlalias WHERE source_url = '$srcURL'" );
00156
00157 $aliases = $db->arrayQuery( "SELECT source_url FROM ezurlalias WHERE destination_url = '{$destURL[0]['destination_url']}' AND destination_url <> '{$uri['path_identification_string']}'" );
00158
00159 foreach ( $aliases as $alias )
00160 {
00161 $this->cacheURL( '/' . $alias['source_url']);
00162 }
00163 }
00164 }
00165
00166
00167
00168
00169
00170
00171
00172
00173 function generateCache( $force = false, $quiet = false, $cli = false, $delay = true )
00174 {
00175 $staticURLArray = $this->cachedURLArray();
00176 $db =& eZDB::instance();
00177 $configSettingCount = count( $staticURLArray );
00178 $currentSetting = 0;
00179 foreach ( $staticURLArray as $url )
00180 {
00181 $currentSetting++;
00182 if ( strpos( $url, '*') === false )
00183 {
00184 if ( !$quiet and $cli )
00185 $cli->output( "caching: $url ", false );
00186 $this->cacheURL( $url, false, !$force, $delay );
00187 if ( !$quiet and $cli )
00188 $cli->output( "done" );
00189 }
00190 else
00191 {
00192 if ( !$quiet and $cli )
00193 $cli->output( "wildcard cache: $url" );
00194 $queryURL = ltrim( str_replace( '*', '%', $url ), '/' );
00195 $queryURL = $db->escapeString( $queryURL );
00196 $aliasArray = $db->arrayQuery( "SELECT source_url, destination_url FROM ezurlalias WHERE source_url LIKE '$queryURL' AND source_url NOT LIKE '%*' ORDER BY source_url" );
00197 $urlCount = count( $aliasArray );
00198 $currentURL = 0;
00199 foreach ( $aliasArray as $urlAlias )
00200 {
00201 $currentURL++;
00202 $url = "/" . $urlAlias['source_url'];
00203 preg_match( '/([0-9]+)$/', $urlAlias['destination_url'], $matches );
00204 $id = $matches[1];
00205 if ( $this->cacheURL( $url, (int) $id, !$force, $delay ) )
00206 {
00207 if ( !$quiet and $cli )
00208 {
00209 $cli->output( sprintf(" %5.1f%% CACHE $url", 100 * ($currentURL / $urlCount)));
00210 }
00211 }
00212 elseif ( !$quiet and $cli )
00213 {
00214 $cli->output( sprintf(" %5.1f%% SKIP $url", 100 * ($currentURL / $urlCount)));
00215 }
00216 }
00217
00218 if ( !$quiet and $cli )
00219 {
00220 $cli->output( sprintf("%5.1f%% done", 100 * ($currentSetting / $configSettingCount)));
00221 }
00222 }
00223 }
00224 }
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234 function cacheURL( $url, $nodeID = false, $skipExisting = false, $delay = true )
00235 {
00236
00237 $hostname = $this->HostName;
00238 $staticStorageDir = $this->StaticStorageDir;
00239
00240
00241 if ( substr_count( $url, "/") >= $this->MaxCacheDepth )
00242 return false;
00243
00244 $doCacheURL = false;
00245 foreach ( $this->CachedURLArray as $cacheURL )
00246 {
00247 if ( $url == $cacheURL )
00248 {
00249 $doCacheURL = true;
00250 }
00251 else if ( strpos( $cacheURL, '*') !== false )
00252 {
00253 if ( strpos( $url, str_replace( '*', '', $cacheURL ) ) === 0 )
00254 {
00255 $doCacheURL = true;
00256 }
00257 }
00258 }
00259
00260 if ( $doCacheURL == false )
00261 {
00262 return false;
00263 }
00264
00265 $this->storeCache( $url, $hostname, $staticStorageDir, $nodeID ? array( "/content/view/full/$nodeID" ) : array(), $skipExisting, $delay );
00266
00267 return true;
00268 }
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281 function storeCache( $url, $hostname, $staticStorageDir, $alternativeStaticLocations = array(), $skipUnlink = false, $delay = true )
00282 {
00283 if ( is_array( $this->CachedSiteAccesses ) and count ( $this->CachedSiteAccesses ) )
00284 {
00285 $dirs = array();
00286 foreach ( $this->CachedSiteAccesses as $dir )
00287 {
00288 $dirs[] = '/' . $dir ;
00289 }
00290 }
00291 else
00292 {
00293 $dirs = array ('');
00294 }
00295 $http = eZHTTPTool::instance();
00296
00297 foreach ( $dirs as $dir )
00298 {
00299 $cacheFiles = array();
00300 if ( !is_dir( $staticStorageDir . $dir ) )
00301 {
00302 eZDir::mkdir( $staticStorageDir . $dir, 0777, true );
00303 }
00304
00305 $cacheFiles[] = $this->buildCacheFilename( $staticStorageDir, $dir . $url );
00306 foreach ( $alternativeStaticLocations as $location )
00307 {
00308 $cacheFiles[] = $this->buildCacheFilename( $staticStorageDir, $dir . $location );
00309 }
00310
00311
00312 $content = false;
00313 foreach ( $cacheFiles as $file )
00314 {
00315 if ( !$skipUnlink || !file_exists( $file ) )
00316 {
00317 $fileName = "http://$hostname$dir$url";
00318 if ( $delay )
00319 {
00320 $this->addAction( 'store', array( $file, $fileName ) );
00321 }
00322 else
00323 {
00324
00325 if ( $content === false )
00326 {
00327 $content = $http->getDataByURL( $fileName, false, EZ_STATIC_CACHE_USER_AGENT );
00328 }
00329 if ( $content === false )
00330 {
00331 eZDebug::writeNotice( "Could not grab content (from $fileName), is the hostname correct and Apache running?",
00332 'Static Cache' );
00333 }
00334 else
00335 {
00336 $this->storeCachedFile( $file, $content );
00337 }
00338 }
00339 }
00340 }
00341 }
00342 }
00343
00344
00345
00346
00347
00348
00349
00350 function buildCacheFilename( $staticStorageDir, $url )
00351 {
00352 $file = "{$staticStorageDir}{$url}/index.html";
00353 $file = preg_replace( '#//+#', '/', $file );
00354 return $file;
00355 }
00356
00357
00358
00359
00360
00361
00362
00363 function storeCachedFile( $file, $content )
00364 {
00365 $dir = dirname( $file );
00366 if ( !is_dir( $dir ) )
00367 {
00368 eZDir::mkdir( $dir, 0777, true );
00369 }
00370
00371 $oldumask = umask( 0 );
00372
00373 $tmpFileName = $file . '.' . md5( $file. uniqid( "ezp". getmypid(), true ) );
00374
00375
00376 @unlink( $tmpFileName );
00377
00378
00379 $fp = fopen( $tmpFileName, 'w' );
00380 if ( $fp )
00381 {
00382 fwrite( $fp, $content . '<!-- Generated: '. date( 'Y-m-d H:i:s' ). " -->\n\n" );
00383 fclose( $fp );
00384 include_once( 'lib/ezfile/classes/ezfile.php' );
00385 eZFile::rename( $tmpFileName, $file );
00386 }
00387
00388 umask( $oldumask );
00389 }
00390
00391
00392
00393
00394
00395
00396 function removeURL( $url )
00397 {
00398 $dir = eZDir::path( array( $this->StaticStorageDir, $url ) );
00399
00400 @unlink( $dir . "/index.html" );
00401 @rmdir( $dir );
00402 }
00403
00404
00405
00406
00407
00408
00409 function addAction( $action, $parameters )
00410 {
00411 if (! isset( $GLOBALS['eZStaticCache-ActionList'] ) ) {
00412 $GLOBALS['eZStaticCache-ActionList'] = array();
00413 }
00414 $GLOBALS['eZStaticCache-ActionList'][] = array( $action, $parameters );
00415 }
00416
00417
00418
00419
00420
00421 function executeActions()
00422 {
00423 if (! isset( $GLOBALS['eZStaticCache-ActionList'] ) ) {
00424 return;
00425 }
00426
00427 $fileContentCache = array();
00428 $doneDestList = array();
00429
00430 $http = eZHTTPTool::instance();
00431
00432 foreach ( $GLOBALS['eZStaticCache-ActionList'] as $action )
00433 {
00434 list( $action, $parameters ) = $action;
00435
00436 switch( $action ) {
00437 case 'store':
00438 list( $destination, $source ) = $parameters;
00439 if ( ! isset( $doneDestList[$destination] ) )
00440 {
00441 if ( ! isset( $fileContentCache[$source] ) )
00442 {
00443 $fileContentCache[$source] = $http->getDataByURL( $source, false, EZ_STATIC_CACHE_USER_AGENT );
00444 }
00445 if ( $fileContentCache[$source] === false )
00446 {
00447 eZDebug::writeNotice( 'Could not grab content, is the hostname correct and Apache running?', 'Static Cache' );
00448 }
00449 else
00450 {
00451 eZStaticCache::storeCachedFile( $destination, $fileContentCache[$source] );
00452 $doneDestList[$destination] = 1;
00453 }
00454 }
00455 break;
00456 }
00457 }
00458 $GLOBALS['eZStaticCache-ActionList'] = array();
00459 }
00460
00461
00462
00463 var $HostName;
00464
00465 var $StaticStorage;
00466
00467 var $MaxCacheDepth;
00468
00469 var $CachedURLArray;
00470
00471 var $AlwaysUpdate;
00472 }
00473
00474 ?>