/cronjobs/updateviewcount.php

https://github.com/GunioRobot/ezpublish · PHP · 237 lines · 218 code · 11 blank · 8 comment · 11 complexity · ffe6a4a54fd2216104afac8c078835e3 MD5 · raw file

  1. <?php
  2. /**
  3. * File containing the updateviewcount.php cronjob
  4. *
  5. * @copyright Copyright (C) 1999-2011 eZ Systems AS. All rights reserved.
  6. * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
  7. * @version //autogentag//
  8. * @package kernel
  9. */
  10. set_time_limit( 0 );
  11. $cli->output( "Update content view count..." );
  12. $dt = new eZDateTime();
  13. $startTime = $dt->day() . "/" . date( 'M', time() ) . "/" . $dt->year() . ":" . $dt->hour() . ":" . $dt->minute() . ":" . $dt->second();
  14. $cli->output( "Started at " . $dt->toString() . "\n" );
  15. eZDB::instance()->setIsSQLOutputEnabled( false );
  16. $startLine = "";
  17. $hasStartLine = false;
  18. $updateViewLogPath = eZSys::instance()->varDirectory() . "/" . eZINI::instance()->variable( 'FileSettings', 'LogDir' ) . "/updateview.log";
  19. if ( is_file( $updateViewLogPath ) )
  20. {
  21. $fh = fopen( $updateViewLogPath, "r" );
  22. if ( $fh )
  23. {
  24. while ( !feof ( $fh ) )
  25. {
  26. $line = fgets( $fh, 1024 );
  27. if ( preg_match( "/\[/", $line ) )
  28. {
  29. $startLine = $line;
  30. $hasStartLine = true;
  31. }
  32. }
  33. fclose( $fh );
  34. }
  35. }
  36. $cli->output( "Start line:\n" . $startLine );
  37. $lastLine = "";
  38. $logFileIni = eZINI::instance( 'logfile.ini' );
  39. $logFilePath = $logFileIni->variable( 'AccessLogFileSettings', 'StorageDir' ) . '/' . $logFileIni->variable( 'AccessLogFileSettings', 'LogFileName' );
  40. $prefixes = $logFileIni->variable( 'AccessLogFileSettings', 'SitePrefix' );
  41. $pathPrefixes = $logFileIni->variable( 'AccessLogFileSettings', 'PathPrefix' );
  42. $pathPrefixesCount = count( $pathPrefixes );
  43. $nodeIDHashCounter = array();
  44. $pathHashCounter = array();
  45. $contentHash = array();
  46. $nonContentHash = array();
  47. if ( is_file( $logFilePath ) )
  48. {
  49. $handle = fopen( $logFilePath, "r" );
  50. if ( $handle )
  51. {
  52. $startParse = false;
  53. $stopParse = false;
  54. while ( !feof ($handle) and !$stopParse )
  55. {
  56. $line = fgets($handle, 1024);
  57. if ( empty( $line ) )
  58. {
  59. continue;
  60. }
  61. if ( $line != "" )
  62. $lastLine = $line;
  63. if ( $startParse or !$hasStartLine )
  64. {
  65. $logPartArray = preg_split( "/[\"]+/", $line );
  66. list( $ip, $timePart ) = explode( '[', $logPartArray[0] );
  67. list( $time, $rest ) = explode( ' ', $timePart );
  68. if ( $time == $startTime )
  69. $stopParse = true;
  70. list( $requireMethod, $url ) = explode( ' ', $logPartArray[1] );
  71. $url = preg_replace( "/\?.*/", "", $url);
  72. foreach ( $prefixes as $prefix )
  73. {
  74. $urlChanged = preg_replace( '/^\/' . preg_quote( $prefix, '/' ) . '(\/|$)/', '/', $url );
  75. if ( $urlChanged != $url )
  76. {
  77. $url = $urlChanged;
  78. break;
  79. }
  80. }
  81. if ( strpos( $url, 'content/view/full/' ) !== false )
  82. {
  83. $url = str_replace( "content/view/full/", "", $url );
  84. $url = str_replace( "/", "", $url );
  85. $url = preg_replace( "/\?(.*)/", "", $url );
  86. if ( !isset( $nodeIDHashCounter[$url] ) )
  87. {
  88. $nodeIDHashCounter[$url] = 1;
  89. }
  90. else
  91. {
  92. ++$nodeIDHashCounter[$url];
  93. }
  94. }
  95. else
  96. {
  97. $urlArray = explode( '/', $url );
  98. $firstElement = $urlArray[1];
  99. if ( isset( $contentHash[$firstElement] ) )
  100. {
  101. if ( !isset( $pathHashCounter[$url] ) )
  102. {
  103. $pathHashCounter[$url] = 1;
  104. }
  105. else
  106. {
  107. ++$pathHashCounter[$url];
  108. }
  109. }
  110. else if ( !isset( $nonContentHash[$firstElement] ) && ( $firstElement != "" || $url === '/' ) )
  111. {
  112. //check in database, if found, add to contentHash, else add to nonContentHash.
  113. $result = eZURLAliasML::fetchNodeIDByPath( $firstElement );
  114. // Fix for sites using PathPrefix
  115. $pathPrefixIndex = 0;
  116. while ( !$result )
  117. {
  118. if ( $pathPrefixIndex >= $pathPrefixesCount )
  119. break;
  120. // Try prepending each of the existing pathPrefixes, to see if one of them matches an existing node
  121. $result = eZURLAliasML::fetchNodeIDByPath( $pathPrefixes[$pathPrefixIndex] . '/' . $firstElement );
  122. $pathPrefixIndex++;
  123. }
  124. if ( $result )
  125. {
  126. $contentHash[$firstElement] = 1;
  127. if ( !isset( $pathHashCounter[$url] ) )
  128. {
  129. $pathHashCounter[$url] = 1;
  130. }
  131. else
  132. {
  133. ++$pathHashCounter[$url];
  134. }
  135. }
  136. else if ( $firstElement != "content" )
  137. {
  138. $nonContentHash[$firstElement] = 1;
  139. }
  140. }
  141. }
  142. }
  143. if ( $line == $startLine )
  144. {
  145. $startParse = true;
  146. }
  147. }
  148. fclose( $handle );
  149. }
  150. else
  151. {
  152. $cli->output( "Warning: Cannot open apache log-file '$logFilePath' for reading, please check permissions and try again." );
  153. }
  154. }
  155. else
  156. {
  157. $cli->output( "Warning: apache log-file '$logFilePath' doesn't exist, please check your ini-settings and try again." );
  158. }
  159. // Process the content of $pathHashCounter to transform it into $nodeIDHashCounter
  160. foreach ( $pathHashCounter as $path => $count )
  161. {
  162. $nodeID = eZURLAliasML::fetchNodeIDByPath( $path );
  163. // Support for PathPrefix
  164. for ( $pathPrefixIndex = 0; !$nodeID && $pathPrefixIndex < $pathPrefixesCount; ++$pathPrefixIndex )
  165. {
  166. // Try prepending each of the existing pathPrefixes, to see if one of them matches an existing node
  167. $nodeID = eZURLAliasML::fetchNodeIDByPath( $pathPrefixes[$pathPrefixIndex] . $path );
  168. }
  169. if ( $nodeID )
  170. {
  171. if ( !isset( $nodeIDHashCounter[$nodeID] ) )
  172. {
  173. $nodeIDHashCounter[$nodeID] = 1;
  174. }
  175. else
  176. {
  177. ++$nodeIDHashCounter[$nodeID];
  178. }
  179. }
  180. }
  181. foreach ( $nodeIDHashCounter as $nodeID => $count )
  182. {
  183. if ( eZContentObjectTreeNode::fetch( $nodeID ) != null )
  184. {
  185. $counter = eZViewCounter::fetch( $nodeID );
  186. if ( $counter == null )
  187. {
  188. $counter = eZViewCounter::create( $nodeID );
  189. $counter->setAttribute( 'count', $count );
  190. $counter->store();
  191. }
  192. else
  193. {
  194. $counter->increase( $count );
  195. }
  196. }
  197. }
  198. $dt = new eZDateTime();
  199. $fh = fopen( $updateViewLogPath, "w" );
  200. if ( $fh )
  201. {
  202. fwrite(
  203. $fh,
  204. "# Finished at " . $dt->toString() . "\n" .
  205. "# Last updated entry:" . "\n" .
  206. $lastLine . "\n"
  207. );
  208. fclose( $fh );
  209. }
  210. $cli->output( "Finished at " . $dt->toString() . "\n" );
  211. $cli->output( "View count have been updated!\n" );
  212. ?>