| 1 | #!/usr/bin/env php |
| 2 | <?php |
| 3 | /** |
| 4 | * @copyright Copyright (C) 1999-2011 eZ Systems AS. All rights reserved. |
| 5 | * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2 |
| 6 | * @version 2011.7 |
| 7 | * @package kernel |
| 8 | */ |
| 9 | |
| 10 | /* No more than one instance of a cronjob script can be run at any given time. |
| 11 | If a script uses more time than the configured MaxScriptExecutionTime (see |
| 12 | cronjob.ini), the next instance of it will try to gracefully steal the |
| 13 | cronjob script mutex. If the process has been running for more than two |
| 14 | times MaxScriptExecutionTime, the original process will be killed. |
| 15 | */ |
| 16 | |
| 17 | /* Set a default time zone if none is given. The time zone can be overridden |
| 18 | in config.php or php.ini. |
| 19 | */ |
| 20 | if ( !ini_get( "date.timezone" ) ) |
| 21 | { |
| 22 | date_default_timezone_set( "UTC" ); |
| 23 | } |
| 24 | |
| 25 | require 'autoload.php'; |
| 26 | require_once( 'kernel/common/i18n.php' ); |
| 27 | |
| 28 | eZContentLanguage::setCronjobMode(); |
| 29 | |
| 30 | $cli = eZCLI::instance(); |
| 31 | $script = eZScript::instance( array( 'debug-message' => '', |
| 32 | 'use-session' => true, |
| 33 | 'use-modules' => true, |
| 34 | 'use-extensions' => true ) ); |
| 35 | |
| 36 | $script->startup(); |
| 37 | |
| 38 | $webOutput = $cli->isWebOutput(); |
| 39 | |
| 40 | function help() |
| 41 | { |
| 42 | $argv = $_SERVER['argv']; |
| 43 | $cli = eZCLI::instance(); |
| 44 | $cli->output( "Usage: " . $argv[0] . " [OPTION]... [PART]\n" . |
| 45 | "Executes eZ Publish cronjobs.\n" . |
| 46 | "\n" . |
| 47 | "General options:\n" . |
| 48 | " -h,--help display this help and exit \n" . |
| 49 | " -q,--quiet do not give any output except when errors occur\n" . |
| 50 | " -s,--siteaccess selected siteaccess for operations, if not specified default siteaccess is used\n" . |
| 51 | " -d,--debug display debug output at end of execution\n" . |
| 52 | " -c,--colors display output using ANSI colors\n" . |
| 53 | " --sql display sql queries\n" . |
| 54 | " --logfiles create log files\n" . |
| 55 | " --no-logfiles do not create log files (default)\n" . |
| 56 | " --no-colors do not use ANSI coloring (default)\n" ); |
| 57 | } |
| 58 | |
| 59 | function changeSiteAccessSetting( &$siteaccess, $optionData ) |
| 60 | { |
| 61 | global $cronPart; |
| 62 | $cli = eZCLI::instance(); |
| 63 | if ( file_exists( 'settings/siteaccess/' . $optionData ) ) |
| 64 | { |
| 65 | $siteaccess = $optionData; |
| 66 | $cli->output( "Using siteaccess $siteaccess for cronjob" ); |
| 67 | } |
| 68 | elseif ( isExtensionSiteaccess( $optionData ) ) |
| 69 | { |
| 70 | $siteaccess = $optionData; |
| 71 | $cli->output( "Using extension siteaccess $siteaccess for cronjob" ); |
| 72 | |
| 73 | eZExtension::prependExtensionSiteAccesses( $siteaccess ); |
| 74 | } |
| 75 | else |
| 76 | { |
| 77 | $cli->notice( "Siteaccess $optionData does not exist, using default siteaccess" ); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | Look in the ActiveExtensions for $siteaccessName |
| 83 | We only need to look in ActiveExtensions and not ActiveAccessExtensions |
| 84 | Return true if siteaccessName exists in an extension, false if not. |
| 85 | */ |
| 86 | function isExtensionSiteaccess( $siteaccessName ) |
| 87 | { |
| 88 | $ini = eZINI::instance(); |
| 89 | $extensionDirectory = $ini->variable( 'ExtensionSettings', 'ExtensionDirectory' ); |
| 90 | $activeExtensions = $ini->variable( 'ExtensionSettings', 'ActiveExtensions' ); |
| 91 | |
| 92 | foreach ( $activeExtensions as $extensionName ) |
| 93 | { |
| 94 | $possibleExtensionPath = $extensionDirectory . '/' . $extensionName . '/settings/siteaccess/' . $siteaccessName; |
| 95 | if ( file_exists( $possibleExtensionPath ) ) |
| 96 | return true; |
| 97 | } |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | $siteaccess = false; |
| 102 | $debugOutput = false; |
| 103 | $allowedDebugLevels = false; |
| 104 | $useDebugAccumulators = false; |
| 105 | $useDebugTimingpoints = false; |
| 106 | $useIncludeFiles = false; |
| 107 | $useColors = false; |
| 108 | $isQuiet = false; |
| 109 | $useLogFiles = false; |
| 110 | $showSQL = false; |
| 111 | $cronPart = false; |
| 112 | |
| 113 | $optionsWithData = array( 's' ); |
| 114 | $longOptionsWithData = array( 'siteaccess' ); |
| 115 | |
| 116 | $readOptions = true; |
| 117 | |
| 118 | for ( $i = 1; $i < count( $argv ); ++$i ) |
| 119 | { |
| 120 | $arg = $argv[$i]; |
| 121 | if ( $readOptions and |
| 122 | strlen( $arg ) > 0 and |
| 123 | $arg[0] == '-' ) |
| 124 | { |
| 125 | if ( strlen( $arg ) > 1 and |
| 126 | $arg[1] == '-' ) |
| 127 | { |
| 128 | $flag = substr( $arg, 2 ); |
| 129 | if ( in_array( $flag, $longOptionsWithData ) ) |
| 130 | { |
| 131 | $optionData = $argv[$i+1]; |
| 132 | ++$i; |
| 133 | } |
| 134 | if ( $flag == 'help' ) |
| 135 | { |
| 136 | help(); |
| 137 | exit(); |
| 138 | } |
| 139 | else if ( $flag == 'siteaccess' ) |
| 140 | { |
| 141 | changeSiteAccessSetting( $siteaccess, $optionData ); |
| 142 | } |
| 143 | else if ( $flag == 'debug' ) |
| 144 | { |
| 145 | $debugOutput = true; |
| 146 | } |
| 147 | else if ( $flag == 'quiet' ) |
| 148 | { |
| 149 | $isQuiet = true; |
| 150 | } |
| 151 | else if ( $flag == 'colors' ) |
| 152 | { |
| 153 | $useColors = true; |
| 154 | } |
| 155 | else if ( $flag == 'no-colors' ) |
| 156 | { |
| 157 | $useColors = false; |
| 158 | } |
| 159 | else if ( $flag == 'no-logfiles' ) |
| 160 | { |
| 161 | $useLogFiles = false; |
| 162 | } |
| 163 | else if ( $flag == 'logfiles' ) |
| 164 | { |
| 165 | $useLogFiles = true; |
| 166 | } |
| 167 | else if ( $flag == 'sql' ) |
| 168 | { |
| 169 | $showSQL = true; |
| 170 | } |
| 171 | } |
| 172 | else |
| 173 | { |
| 174 | $flag = substr( $arg, 1, 1 ); |
| 175 | $optionData = false; |
| 176 | if ( in_array( $flag, $optionsWithData ) ) |
| 177 | { |
| 178 | if ( strlen( $arg ) > 2 ) |
| 179 | { |
| 180 | $optionData = substr( $arg, 2 ); |
| 181 | } |
| 182 | else |
| 183 | { |
| 184 | $optionData = $argv[$i+1]; |
| 185 | ++$i; |
| 186 | } |
| 187 | } |
| 188 | if ( $flag == 'h' ) |
| 189 | { |
| 190 | help(); |
| 191 | exit(); |
| 192 | } |
| 193 | else if ( $flag == 'q' ) |
| 194 | { |
| 195 | $isQuiet = true; |
| 196 | } |
| 197 | else if ( $flag == 'c' ) |
| 198 | { |
| 199 | $useColors = true; |
| 200 | } |
| 201 | else if ( $flag == 'd' ) |
| 202 | { |
| 203 | $debugOutput = true; |
| 204 | if ( strlen( $arg ) > 2 ) |
| 205 | { |
| 206 | $levels = explode( ',', substr( $arg, 2 ) ); |
| 207 | $allowedDebugLevels = array(); |
| 208 | foreach ( $levels as $level ) |
| 209 | { |
| 210 | if ( $level == 'all' ) |
| 211 | { |
| 212 | $useDebugAccumulators = true; |
| 213 | $allowedDebugLevels = false; |
| 214 | $useDebugTimingpoints = true; |
| 215 | break; |
| 216 | } |
| 217 | if ( $level == 'accumulator' ) |
| 218 | { |
| 219 | $useDebugAccumulators = true; |
| 220 | continue; |
| 221 | } |
| 222 | if ( $level == 'timing' ) |
| 223 | { |
| 224 | $useDebugTimingpoints = true; |
| 225 | continue; |
| 226 | } |
| 227 | if ( $level == 'include' ) |
| 228 | { |
| 229 | $useIncludeFiles = true; |
| 230 | } |
| 231 | if ( $level == 'error' ) |
| 232 | $level = eZDebug::LEVEL_ERROR; |
| 233 | else if ( $level == 'warning' ) |
| 234 | $level = eZDebug::LEVEL_WARNING; |
| 235 | else if ( $level == 'debug' ) |
| 236 | $level = eZDebug::LEVEL_DEBUG; |
| 237 | else if ( $level == 'notice' ) |
| 238 | $level = eZDebug::LEVEL_NOTICE; |
| 239 | else if ( $level == 'timing' ) |
| 240 | $level = eZDebug::EZ_LEVEL_TIMING; |
| 241 | $allowedDebugLevels[] = $level; |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | else if ( $flag == 's' ) |
| 246 | { |
| 247 | changeSiteAccessSetting( $siteaccess, $optionData ); |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | else |
| 252 | { |
| 253 | if ( $cronPart === false ) |
| 254 | { |
| 255 | $readOptions = false; |
| 256 | $cronPart = $arg; |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | $script->setUseDebugOutput( $debugOutput ); |
| 261 | $script->setAllowedDebugLevels( $allowedDebugLevels ); |
| 262 | $script->setUseDebugAccumulators( $useDebugAccumulators ); |
| 263 | $script->setUseDebugTimingPoints( $useDebugTimingpoints ); |
| 264 | $script->setUseIncludeFiles( $useIncludeFiles ); |
| 265 | $script->setIsQuiet( $isQuiet ); |
| 266 | |
| 267 | if ( $webOutput ) |
| 268 | $useColors = true; |
| 269 | |
| 270 | $cli->setUseStyles( $useColors ); |
| 271 | $script->setDebugMessage( "\n\n" . str_repeat( '#', 36 ) . $cli->style( 'emphasize' ) . " DEBUG " . $cli->style( 'emphasize-end' ) . str_repeat( '#', 36 ) . "\n" ); |
| 272 | |
| 273 | $script->setUseSiteAccess( $siteaccess ); |
| 274 | |
| 275 | $script->initialize(); |
| 276 | if ( !$script->isInitialized() ) |
| 277 | { |
| 278 | $cli->error( 'Error initializing script: ' . $script->initializationError() . '.' ); |
| 279 | $script->shutdown( 0 ); |
| 280 | } |
| 281 | |
| 282 | if ( $cronPart ) |
| 283 | { |
| 284 | $cli->output( "Running cronjob part '$cronPart'" ); |
| 285 | } |
| 286 | |
| 287 | |
| 288 | $ini = eZINI::instance( 'cronjob.ini' ); |
| 289 | $scriptDirectories = $ini->variable( 'CronjobSettings', 'ScriptDirectories' ); |
| 290 | |
| 291 | /* Include extension directories */ |
| 292 | $extensionDirectories = $ini->variable( 'CronjobSettings', 'ExtensionDirectories' ); |
| 293 | $scriptDirectories = array_merge( $scriptDirectories, eZExtension::expandedPathList( $extensionDirectories, 'cronjobs' ) ); |
| 294 | |
| 295 | $scriptGroup = 'CronjobSettings'; |
| 296 | if ( $cronPart !== false ) |
| 297 | $scriptGroup = "CronjobPart-$cronPart"; |
| 298 | $scripts = $ini->variable( $scriptGroup, 'Scripts' ); |
| 299 | |
| 300 | if ( !is_array( $scripts ) or empty( $scripts ) ) |
| 301 | { |
| 302 | $cli->notice( 'Notice: No scripts found for execution.' ); |
| 303 | $script->shutdown( 0 ); |
| 304 | } |
| 305 | |
| 306 | $index = 0; |
| 307 | |
| 308 | foreach ( $scripts as $cronScript ) |
| 309 | { |
| 310 | foreach ( $scriptDirectories as $scriptDirectory ) |
| 311 | { |
| 312 | $scriptFile = $scriptDirectory . '/' . $cronScript; |
| 313 | if ( file_exists( $scriptFile ) ) |
| 314 | break; |
| 315 | } |
| 316 | if ( file_exists( $scriptFile ) ) |
| 317 | { |
| 318 | if ( $index > 0 ) |
| 319 | { |
| 320 | $cli->output(); |
| 321 | } |
| 322 | if ( !$isQuiet ) |
| 323 | { |
| 324 | $startTime = new eZDateTime(); |
| 325 | $cli->output( 'Running ' . $cli->stylize( 'emphasize', $scriptFile ) . ' at: ' . $startTime->toString( true ) ); |
| 326 | } |
| 327 | |
| 328 | eZDebug::addTimingPoint( "Script $scriptFile starting" ); |
| 329 | eZRunCronjobs::runScript( $cli, $scriptFile ); |
| 330 | eZDebug::addTimingPoint( "Script $scriptFile done" ); |
| 331 | ++$index; |
| 332 | // The transaction check |
| 333 | $transactionCounterCheck = eZDB::checkTransactionCounter(); |
| 334 | if ( isset( $transactionCounterCheck['error'] ) ) |
| 335 | $cli->error( $transactionCounterCheck['error'] ); |
| 336 | |
| 337 | if ( !$isQuiet ) |
| 338 | { |
| 339 | $endTime = new eZDateTime(); |
| 340 | $cli->output( 'Completing ' . $cli->stylize( 'emphasize', $scriptFile ) . ' at: ' . $endTime->toString( true ) ); |
| 341 | $elapsedTime = new eZTime( $endTime->timeStamp() - $startTime->timeStamp() ); |
| 342 | $cli->output( 'Elapsed time: ' . sprintf( '%02d:%02d:%02d', $elapsedTime->hour(), $elapsedTime->minute(), $elapsedTime->second() ) ); |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | $script->shutdown(); |
| 348 | |
| 349 | ?> |
| 350 | |