eZ Publish  [4.0]
updateviewcount.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of  class
00004 //
00005 // Created on: <07-Jul-2003 10:06:19 wy>
00006 //
00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00008 // SOFTWARE NAME: eZ Publish
00009 // SOFTWARE RELEASE: 4.0.x
00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00011 // SOFTWARE LICENSE: GNU General Public License v2.0
00012 // NOTICE: >
00013 //   This program is free software; you can redistribute it and/or
00014 //   modify it under the terms of version 2.0  of the GNU General
00015 //   Public License as published by the Free Software Foundation.
00016 //
00017 //   This program is distributed in the hope that it will be useful,
00018 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //   GNU General Public License for more details.
00021 //
00022 //   You should have received a copy of version 2.0 of the GNU General
00023 //   Public License along with this program; if not, write to the Free
00024 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 //   MA 02110-1301, USA.
00026 //
00027 //
00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00029 //
00030 
00031 /*! \file updateviewcount.php
00032 */
00033 //include_once( "lib/ezutils/classes/ezsys.php" );
00034 //include_once( "lib/ezdb/classes/ezdb.php" );
00035 //include_once( "lib/ezutils/classes/ezini.php" );
00036 //include_once( "kernel/classes/ezviewcounter.php" );
00037 //include_once( "kernel/classes/ezcontentobjecttreenode.php" );
00038 //include_once( 'lib/ezlocale/classes/ezdatetime.php' );
00039 
00040 set_time_limit( 0 );
00041 
00042 if ( !$isQuiet )
00043     $cli->output( "Update content view count..."  );
00044 
00045 
00046 $dt = new eZDateTime();
00047 $year = $dt->year();
00048 $month = date( 'M', time() );
00049 $day = $dt->day();
00050 $hour = $dt->hour();
00051 $minute = $dt->minute();
00052 $second = $dt->second();
00053 $startTime = $day . "/" . $month . "/" . $year . ":" . $hour . ":" . $minute . ":" . $second;
00054 
00055 $cli->output( "Started at " . $dt->toString() . "\n"  );
00056 $nodeIDArray = array();
00057 
00058 $pathArray = array();
00059 
00060 $contentArray = array();
00061 
00062 $nonContentArray = array();
00063 
00064 $ini = eZINI::instance();
00065 $logFileIni = eZINI::instance( 'logfile.ini' );
00066 $fileDir = $logFileIni->variable( 'AccessLogFileSettings', 'StorageDir' );
00067 $fileName = $logFileIni->variable( 'AccessLogFileSettings', 'LogFileName' );
00068 
00069 $prefixes = $logFileIni->variable( 'AccessLogFileSettings', 'SitePrefix' );
00070 
00071 $ini = eZINI::instance();
00072 $logDir = $ini->variable( 'FileSettings', 'LogDir' );
00073 
00074 $db = eZDB::instance();
00075 $db->setIsSQLOutputEnabled( false );
00076 
00077 $sys = eZSys::instance();
00078 $varDir = $sys->varDirectory();
00079 $updateViewLog = "updateview.log";
00080 
00081 $startLine = "";
00082 $hasStartLine = false;
00083 
00084 $updateViewLogPath = $varDir . "/" . $logDir . "/" . $updateViewLog;
00085 if ( is_file( $updateViewLogPath ) )
00086 {
00087     $fh = fopen( $updateViewLogPath, "r" );
00088     if ( $fh )
00089     {
00090         while ( !feof ( $fh ) )
00091         {
00092             $line = fgets( $fh, 1024 );
00093             if ( preg_match( "/\[/", $line ) )
00094             {
00095                 $startLine = $line;
00096                 $hasStartLine = true;
00097             }
00098         }
00099         fclose( $fh );
00100     }
00101 }
00102 
00103 $cli->output( "Start line:\n" . $startLine );
00104 $lastLine = "";
00105 $logFilePath = $fileDir . '/' . $fileName;
00106 
00107 if ( is_file( $logFilePath ) )
00108 {
00109     $handle = fopen( $logFilePath, "r" );
00110     if ( $handle )
00111     {
00112         $startParse = false;
00113         $stopParse = false;
00114         while ( !feof ($handle) and !$stopParse )
00115         {
00116             $line = fgets($handle, 1024);
00117             if ( !empty( $line ) )
00118             {
00119                 if ( $line != "" )
00120                     $lastLine = $line;
00121 
00122                 if ( $startParse or !$hasStartLine )
00123                 {
00124                     $logPartArray = preg_split( "/[\"]+/", $line );
00125                     $timeIPPart = $logPartArray[0];
00126                     list( $ip, $timePart ) = explode( '[', $timeIPPart );
00127                     list( $time, $rest ) = explode( ' ', $timePart );
00128 
00129                     if ( $time == $startTime )
00130                         $stopParse = true;
00131                     $requirePart = $logPartArray[1];
00132 
00133                     list( $requireMethod, $url ) = explode( ' ', $requirePart );
00134                     $url = preg_replace( "/\?.*/", "", $url);
00135                     foreach ( $prefixes as $prefix )
00136                     {
00137                         $urlChanged = preg_replace( '/^\/' . preg_quote( $prefix, '/' ) . '\//', '', $url );
00138                         if ( $urlChanged != $url )
00139                         {
00140                             $url = $urlChanged;
00141                             break;
00142                         }
00143                     }
00144 
00145                     if ( preg_match( "/content\/view\/full\//", $url ) )
00146                     {
00147                         $url = str_replace( "content/view/full/", "", $url );
00148                         $url = str_replace( "/", "", $url );
00149                         $url = preg_replace( "/\?(.*)/", "", $url );
00150                         $nodeIDArray[] = $url;
00151                     }
00152                     else
00153                     {
00154                         $urlArray = explode( '/', $url );
00155                         $firstElement = $urlArray[0];
00156                         if ( in_array( $firstElement, $contentArray ) )
00157                         {
00158                             $pathArray[] = $url;
00159                         }
00160                         else if ( in_array( $firstElement, $nonContentArray ) )
00161                         {
00162                             // do nothing
00163                         }
00164                         else
00165                         {
00166                             if ( $firstElement != "" )
00167                             {
00168                                 $pathIdentificationString = $db->escapeString( $firstElement );
00169 
00170                                 //check in database, if fount, add to contentArray, else add to nonContentArray.
00171                                 $result = eZURLAliasML::fetchNodeIDByPath( $pathIdentificationString );
00172                                 if ( $result )
00173                                 {
00174                                     $contentArray[] = $firstElement;
00175                                     $pathArray[] = $url;
00176                                 }
00177                                 else
00178                                 {
00179                                     if ( $firstElement != "content" )
00180                                         $nonContentArray[] = $firstElement;
00181                                 }
00182                             }
00183                         }
00184                     }
00185                 }
00186                 if ( $line == $startLine )
00187                 {
00188                     $startParse = true;
00189                 }
00190             }
00191         }
00192         fclose( $handle );
00193     }
00194     else
00195     {
00196         $cli->output( "Warning: Cannot open apache log-file '$logFilePath' for reading, please check permissions and try again." );
00197     }
00198 }
00199 else
00200 {
00201     $cli->output( "Warning: apache log-file '$logFilePath' doesn't exist, please check your ini-settings and try again." );
00202 }
00203 
00204 foreach ( $nodeIDArray as $nodeID )
00205 {
00206     $nodeObject = eZContentObjectTreeNode::fetch( $nodeID );
00207     if ( $nodeObject != null )
00208     {
00209         $counter = eZViewCounter::fetch( $nodeID );
00210         if ( $counter == null )
00211             $counter = eZViewCounter::create( $nodeID );
00212         $counter->increase();
00213     }
00214 }
00215 
00216 foreach ( $pathArray as $path )
00217 {
00218     $nodeID = eZURLAliasML::fetchNodeIDByPath( $path );
00219 
00220     if ( $nodeID )
00221     {
00222         $counter = eZViewCounter::fetch( $nodeID );
00223         if ( $counter == null )
00224             $counter = eZViewCounter::create( $nodeID );
00225         $counter->increase();
00226     }
00227 }
00228 
00229 $dt = new eZDateTime();
00230 
00231 $fh = fopen( $updateViewLogPath, "w" );
00232 if ( $fh )
00233 {
00234     fwrite( $fh, "# Finished at " . $dt->toString() . "\n" );
00235     fwrite( $fh, "# Last updated entry:" . "\n" );
00236     fwrite( $fh, $lastLine . "\n" );
00237     fclose( $fh );
00238 }
00239 
00240 $cli->output( "Finished at " . $dt->toString() . "\n"  );
00241 if ( !$isQuiet )
00242     $cli->output( "View count have been updated!\n" );
00243 
00244 ?>