[ Index ]
 

Code source de eZ Publish 3.9.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/kernel/classes/ -> ezscript.php (source)

   1  <?php
   2  //
   3  // Definition of eZScript class
   4  //
   5  // Created on: <06-Aug-2003 11:06:35 amos>
   6  //
   7  // SOFTWARE NAME: eZ publish
   8  // SOFTWARE RELEASE: 3.9.0
   9  // BUILD VERSION: 17785
  10  // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS
  11  // SOFTWARE LICENSE: GNU General Public License v2.0
  12  // NOTICE: >
  13  //   This program is free software; you can redistribute it and/or
  14  //   modify it under the terms of version 2.0  of the GNU General
  15  //   Public License as published by the Free Software Foundation.
  16  //
  17  //   This program is distributed in the hope that it will be useful,
  18  //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  19  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20  //   GNU General Public License for more details.
  21  //
  22  //   You should have received a copy of version 2.0 of the GNU General
  23  //   Public License along with this program; if not, write to the Free
  24  //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25  //   MA 02110-1301, USA.
  26  //
  27  //
  28  
  29  /*! \file ezscript.php
  30  */
  31  
  32  /*!
  33    \class eZScript ezscript.php
  34    \brief Handles the basics of script execution
  35  
  36    By using this class for script execution startup, initializing
  37    and shutdown the amount code required to write a new script is
  38    reduced significantly.
  39  
  40    It is also recommended to use the eZCLI class in addition to this
  41    class.
  42  
  43    What this class will handle is:
  44    - Startup of database
  45    - Startup/shutdown of session
  46    - Debug initialize and display
  47    - Text codec initialize
  48  
  49    This class consists of the static functions startup(), initialize()
  50    and shutdown().
  51  
  52    A typical usage:
  53  \code
  54  $script =& eZScript::instance();
  55  
  56  $script->startup();
  57  
  58  // Read arguments and modify script accordingly
  59  
  60  $script->initialize();
  61  
  62  // Do the actual script here
  63  
  64  $script->shutdown(); // Finish execution
  65  
  66  \endcode
  67  
  68  */
  69  
  70  include_once ( 'lib/ezutils/classes/ezcli.php' );
  71  include_once ( 'lib/ezdb/classes/ezdb.php' );
  72  include_once ( 'access.php' );
  73  
  74  class eZScript
  75  {
  76      /*!
  77       Constructor
  78      */
  79      function eZScript( $settings = array() )
  80      {
  81          $settings = array_merge( array( 'debug-message' => false,
  82                                          'debug-output' => false,
  83                                          'debug-include' => false,
  84                                          'debug-levels' => false,
  85                                          'debug-accumulator' => false,
  86                                          'debug-timing' => false,
  87                                          'use-session' => false,
  88                                          'use-extensions' => false,
  89                                          'use-modules' => false,
  90                                          'user' => false,
  91                                          'description' => 'eZ publish script',
  92                                          'site-access' => false,
  93                                          'min_version' => false,
  94                                          'max_version' => false ),
  95                                   $settings );
  96          $this->DebugMessage = $settings['debug-message'];
  97          $this->UseDebugOutput = $settings['debug-output'];
  98          $this->AllowedDebugLevels = $settings['debug-levels'];
  99          $this->UseDebugAccumulators = $settings['debug-accumulator'];
 100          $this->UseDebugTimingPoints = $settings['debug-timing'];
 101          $this->UseIncludeFiles = $settings['debug-include'];
 102          $this->UseSession = $settings['use-session'];
 103          $this->UseModules = $settings['use-modules'];
 104          $this->UseExtensions = $settings['use-extensions'];
 105          $this->User = $settings['user'];
 106          $this->SiteAccess = $settings['site-access'];
 107          $this->Description = $settings['description'];
 108          $this->MinVersion = $settings['min_version'];
 109          $this->MaxVersion = $settings['max_version'];
 110          $this->ExitCode = false;
 111          $this->IsQuiet = false;
 112          $this->ShowVerbose = false;
 113          $this->IsInitialized = false;
 114          $this->CurrentOptions = false;
 115          $this->CurrentOptionConfig = false;
 116          $this->CurrentStandardOptions = false;
 117          $this->CurrentExcludeOptions = false;;
 118          $this->CurrentOptionHelp = false;
 119  
 120          $this->IterationTrueString = '.';
 121          $this->IterationFalseString = '~';
 122          $this->IterationNumericStrings = false;
 123          $this->IterationWrapNumeric = false;
 124          $this->IterationIndex = 0;
 125          $this->IterationColumn = 0;
 126          $this->IterationColumnMax = 70;
 127          $this->IterationMax = false;
 128          $this->InitializationErrorMessage = 'unknown error';
 129      }
 130  
 131      /*!
 132       Checks if the script is run on correct eZ publish version.
 133      */
 134      function validateVersion()
 135      {
 136          include_once ( 'lib/version.php' );
 137          $versionValidated = false;
 138          $ezversion = eZPublishSDK::version();
 139          if ( $this->MinVersion !== false )
 140          {
 141              if ( $this->MaxVersion !== false )
 142              {
 143                  if ( version_compare( $this->MinVersion, $ezversion , 'le' ) &&
 144                       version_compare( $this->MaxVersion, $ezversion , 'ge' ) )
 145                  {
 146                      return true;
 147                  }
 148                  return false;
 149              }
 150              if ( version_compare( $this->MinVersion, $ezversion , 'le' ) )
 151              {
 152                  return true;
 153              }
 154              return false;
 155          }
 156          else
 157          {
 158              if ( version_compare( $this->MaxVersion, $ezversion , 'ge' ) )
 159              {
 160                  return true;
 161              }
 162              return false;
 163          }
 164      }
 165  
 166      /*!
 167       Checks if the script is run in CLI mode, if not it exits with a warning.
 168       The PHP local is also initialized if it is used.
 169  
 170       Call this at the very start of your script and always before getOptions() and initialize().
 171      */
 172      function startup()
 173      {
 174          error_reporting( E_ALL );
 175  
 176          eZDebug::setHandleType( EZ_HANDLE_TO_PHP );
 177  
 178          if ( php_sapi_name() != 'cli' )
 179          {
 180              $cli =& eZCLI::instance();
 181              $cli->output( "PHP is currently using the '" . php_sapi_name() . "' interface. Make sure it is using the 'cli' interface." );
 182              exit( 1 );
 183          }
 184  
 185          // Make sure compatability functions are available.
 186          require_once ( 'lib/compat.php' );
 187  
 188          include_once ( "lib/ezutils/classes/ezini.php" );
 189          $ini =& eZINI::instance();
 190          $phpLocale = trim( $ini->variable( 'RegionalSettings', 'SystemLocale' ) );
 191          if ( $phpLocale != '' )
 192          {
 193              setlocale( LC_ALL, explode( ',', $phpLocale ) );
 194          }
 195      }
 196  
 197      /*!
 198       Initializes all settings which are required for the script to run,
 199       must be called after startup() and getOptions().
 200  
 201       If you modify the eZScript object using the set* functions you must make sure that
 202       is done before this function is called.
 203      */
 204      function initialize()
 205      {
 206          while( @ob_end_clean() );
 207          include_once ( "lib/ezutils/classes/ezdebugsetting.php" );
 208  
 209          $debugINI =& eZINI::instance( 'debug.ini' );
 210          eZDebugSetting::setDebugINI( $debugINI );
 211  
 212          // Initialize text codec settings
 213          $this->updateTextCodecSettings();
 214  
 215          // Initialize debug settings
 216          $this->updateDebugSettings( $this->UseDebugOutput );
 217  
 218          // Set the different permissions/settings.
 219          include_once ( 'lib/ezi18n/classes/ezcodepage.php' );
 220          $ini =& eZINI::instance();
 221          $iniFilePermission = $ini->variable( 'FileSettings', 'StorageFilePermissions' );
 222          $iniDirPermission = $ini->variable( 'FileSettings', 'StorageDirPermissions' );
 223          $iniVarDirectory = eZSys::cacheDirectory() ;
 224  
 225          eZCodepage::setPermissionSetting( array( 'file_permission' => octdec( $iniFilePermission ),
 226                                                   'dir_permission'  => octdec( $iniDirPermission ),
 227                                                   'var_directory'   => $iniVarDirectory ) );
 228  
 229          include_once ( 'lib/ezutils/classes/ezexecution.php' );
 230  
 231          eZExecution::addCleanupHandler( 'eZDBCleanup' );
 232          eZExecution::addFatalErrorHandler( 'eZFatalError' );
 233  
 234          eZDebug::setHandleType( EZ_HANDLE_FROM_PHP );
 235  
 236          if ( $this->UseExtensions )
 237          {
 238              // Check for extension
 239              include_once ( 'lib/ezutils/classes/ezextension.php' );
 240              include_once ( 'kernel/common/ezincludefunctions.php' );
 241              eZExtension::activateExtensions( 'default' );
 242              // Extension check end
 243          }
 244  
 245          include_once ( "access.php" );
 246          $siteaccess = $this->SiteAccess;
 247          if ( $siteaccess )
 248          {
 249              $access = array( 'name' => $siteaccess,
 250                               'type' => EZ_ACCESS_TYPE_STATIC );
 251          }
 252          else
 253          {
 254              $ini =& eZINI::instance();
 255              $siteaccess = $ini->variable( 'SiteSettings', 'DefaultAccess' );
 256              $access = array( 'name' => $siteaccess,
 257                               'type' => EZ_ACCESS_TYPE_DEFAULT );
 258          }
 259  
 260          $access = changeAccess( $access );
 261          $GLOBALS['eZCurrentAccess'] =& $access;
 262  
 263          if ( $this->UseExtensions )
 264          {
 265              // Check for siteaccess extension
 266              eZExtension::activateExtensions( 'access' );
 267              // Extension check end
 268          }
 269  
 270          // Set the global setting which is read by the session lib
 271          $GLOBALS['eZSiteBasics']['session-required'] = $this->UseSession;
 272  
 273          if ( $this->UseSession )
 274          {
 275              // include ezsession override implementation
 276              include_once ( "lib/ezutils/classes/ezsession.php" );
 277              include_once ( 'lib/ezdb/classes/ezdb.php' );
 278              $db =& eZDB::instance();
 279              if ( $db->isConnected() )
 280              {
 281                  eZSessionStart();
 282              }
 283              else
 284              {
 285                  $this->IsInitialized = false;
 286                  $this->InitializationErrorMessage = 'database error: ' . $db->errorMessage();
 287                  return;
 288              }
 289          }
 290  
 291          if ( $this->User )
 292          {
 293              $userLogin = $this->User['login'];
 294              $userPassword = $this->User['password'];
 295              include_once ( 'kernel/classes/datatypes/ezuser/ezuser.php' );
 296  
 297              if ( $userLogin and $userPassword )
 298              {
 299                  $userID = eZUser::loginUser( $userLogin, $userPassword );
 300                  if ( !$userID )
 301                  {
 302                      $cli =& eZCLI::instance();
 303                      if ( $this->isLoud() )
 304                          $cli->warning( 'Failed to login with user ' . $userLogin );
 305                      include_once ( 'lib/ezutils/classes/ezexecution.php' );
 306                      eZExecution::cleanup();
 307                      eZExecution::setCleanExit();
 308                  }
 309              }
 310          }
 311  
 312          // Initialize module handling
 313          if ( $this->UseModules )
 314          {
 315              $moduleRepositories = array();
 316              $moduleINI =& eZINI::instance( 'module.ini' );
 317              $globalModuleRepositories = $moduleINI->variable( 'ModuleSettings', 'ModuleRepositories' );
 318              $extensionRepositories = $moduleINI->variable( 'ModuleSettings', 'ExtensionRepositories' );
 319  
 320              if ( $this->UseExtensions )
 321                  $extensionDirectory = eZExtension::baseDirectory();
 322              else
 323                  $extensionDirectory = array();
 324  
 325              $globalExtensionRepositories = array();
 326              foreach ( $extensionRepositories as $extensionRepository )
 327              {
 328                  $modulePath = $extensionDirectory . '/' . $extensionRepository . '/modules';
 329                  if ( file_exists( $modulePath ) )
 330                  {
 331                      $globalExtensionRepositories[] = $modulePath;
 332                  }
 333              }
 334              $moduleRepositories = array_merge( $moduleRepositories, $globalModuleRepositories, $globalExtensionRepositories );
 335              include_once ( 'lib/ezutils/classes/ezmodule.php' );
 336              eZModule::setGlobalPathList( $moduleRepositories );
 337          }
 338          $this->IsInitialized = true;
 339      }
 340  
 341      function isInitialized()
 342      {
 343          return $this->IsInitialized;
 344      }
 345  
 346      function initializationError()
 347      {
 348          return $this->InitializationErrorMessage;
 349      }
 350  
 351      /*!
 352       Shuts down the currently running script, the following things will be done:
 353       - Remove current session (if sessions are used)
 354       - Print debug messages (if debug is enabled)
 355       - Call cleanup function using eZExecution
 356       - Sets the clean exit flag, that way an exit, die or other stops will not issue an error
 357  
 358       If an exit code is set, PHP will exit with that code set (this means that this function never returns),
 359       otherwise this function returns normally.
 360      */
 361      function shutdown( $exitCode = false, $exitText = false )
 362      {
 363          $cli =& eZCLI::instance();
 364          if ( class_exists( 'ezdb' )
 365               and eZDB::hasInstance() )
 366          {
 367              $db =& eZDB::instance( false, array( 'show_errors' => false ) );
 368              // Perform transaction check
 369              $transactionCounterCheck = eZDB::checkTransactionCounter();
 370              if ( isset( $transactionCounterCheck['error'] ) )
 371                  $cli->error( $transactionCounterCheck['error'] );
 372  
 373              if ( $this->UseSession and
 374                   $db->isConnected() )
 375              {
 376                  include_once ( 'kernel/classes/datatypes/ezuser/ezuser.php' );
 377                  eZUser::logoutCurrent();
 378                  eZSessionRemove();
 379              }
 380          }
 381  
 382          $webOutput = $cli->isWebOutput();
 383  
 384          if ( $this->UseDebugOutput or
 385               eZDebug::isDebugEnabled() )
 386          {
 387              if ( $this->DebugMessage )
 388                  fputs( STDERR, $this->DebugMessage );
 389              fputs( STDERR, eZDebug::printReport( false, $webOutput, true,
 390                                                   $this->AllowedDebugLevels, $this->UseDebugAccumulators,
 391                                                   $this->UseDebugTimingPoints, $this->UseIncludeFiles ) );
 392          }
 393  
 394          include_once ( 'lib/ezutils/classes/ezexecution.php' );
 395          eZExecution::cleanup();
 396          eZExecution::setCleanExit();
 397          $this->IsInitialized = false;
 398          if ( $exitCode !== false )
 399              $this->ExitCode = $exitCode;
 400          if ( $this->ExitCode !== false )
 401          {
 402              if ( $exitText !== false )
 403                  $cli->output( $exitText );
 404              exit( $this->ExitCode );
 405          }
 406      }
 407  
 408      /*!
 409       Sets the text message which is shown before the debug list.
 410       There will be a default message which should suit most scripts.
 411       \note This requires that setUseDebugOutput is set to true or that
 412             the user has enabled debug in the arguments.
 413      */
 414      function setDebugMessage( $message )
 415      {
 416          $this->DebugMessage = $message;
 417      }
 418  
 419      /*!
 420       Sets whether debug output should be enabled or not.
 421       \note This can also be called by the argument parser if the user specifies to show debug.
 422      */
 423      function setUseDebugOutput( $useDebug )
 424      {
 425          $this->UseDebugOutput = $useDebug;
 426      }
 427  
 428      /*!
 429       Sets whether accumulators should be shown on debug output or not.
 430       \note This requires that setUseDebugOutput is set to true or that
 431             the user has enabled debug in the arguments.
 432      */
 433      function setUseDebugAccumulators( $useAccumulators )
 434      {
 435          $this->UseDebugAccumulators = $useAccumulators;
 436      }
 437  
 438      /*!
 439       Sets whether timing points should be shown on debug output or not.
 440       \note This requires that setUseDebugOutput is set to true or that
 441             the user has enabled debug in the arguments.
 442      */
 443      function setUseDebugTimingPoints( $useTimingPoints )
 444      {
 445          $this->UseDebugTimingPoints = $useTimingPoints;
 446      }
 447  
 448      /*!
 449       Sets whether include files should be shown on debug output or not.
 450       \note This requires that setUseDebugOutput is set to true or that
 451             the user has enabled debug in the arguments.
 452      */
 453      function setUseIncludeFiles( $useIncludeFiles )
 454      {
 455          $this->UseIncludeFiles = $useIncludeFiles;
 456      }
 457  
 458      /*!
 459       Sets which debug levels are to be shown on debug output, this must be an array
 460       with EZ_LEVEL_* definitions taken from eZDebug.
 461       \note This requires that setUseDebugOutput is set to true or that
 462             the user has enabled debug in the arguments.
 463      */
 464      function setAllowedDebugLevels( $allowedDebugLevels )
 465      {
 466          $this->AllowedDebugLevels = $allowedDebugLevels;
 467      }
 468  
 469      /*!
 470       Sets whether session is to be used or not.
 471       \note This will only work if it is set before initialized() is called.
 472       \note If session is enabled the current session data will be removed on shutdown().
 473      */
 474      function setUseSession( $useSession )
 475      {
 476          $this->UseSession = $useSession;
 477      }
 478  
 479      /*!
 480       Sets whether extension support is to be added or not.
 481       \note This will only work if it is set before initialized() is called.
 482      */
 483      function setUseExtensions( $useExtensions )
 484      {
 485          $this->UseExtensions = $useExtensions;
 486      }
 487  
 488      /*!
 489       Sets the current site access to \a $siteAccess.
 490       \note This will only work if it is set before initialized() is called.
 491       \note This will be filled in if getOptions() is used and the user specifices it in the arguments.
 492      */
 493      function setUseSiteAccess( $siteAccess )
 494      {
 495          $this->SiteAccess = $siteAccess;
 496      }
 497  
 498      /*!
 499       \return the currently set siteaccess or \c false if none is set.
 500      */
 501      function usedSiteAccess()
 502      {
 503          return $this->SiteAccess;
 504      }
 505  
 506      function setUseModules( $useModules )
 507      {
 508          $this->UseModules = $useModules;
 509      }
 510  
 511      function setUser( $userLogin, $userPassword )
 512      {
 513          $this->User = array( 'login' => $userLogin,
 514                               'password' => $userPassword );
 515      }
 516  
 517      /*!
 518       Controls whether verbose output is used or not, use \c false to turn it off,
 519       \c true to turn it on or a number to select the verbose level (\c true == 1).
 520       The actual behaviour of verbose output depends on the script, however enabling
 521       it will make sure iteration looping displays the iteration name instead of a dot.
 522      */
 523      function setShowVerboseOutput( $verbose )
 524      {
 525          if ( $verbose === true )
 526              $verbose = 1;
 527          $this->ShowVerbose = $verbose;
 528      }
 529  
 530      /*!
 531       \return the verbosity level for the script, will be \c false or a number in the range 1 and up.
 532      */
 533      function verboseOutputLevel()
 534      {
 535          return $this->ShowVerbose;
 536      }
 537  
 538      /*!
 539       \return the currently set options if getOptions() has been run or \c false if no options are set.
 540      */
 541      function currentOptions()
 542      {
 543          return $this->CurrentOptions;
 544      }
 545  
 546      /*!
 547       \return the current option configuration, this will be a mix of the standard options and script specified.
 548      */
 549      function currentOptionConfig()
 550      {
 551          return $this->CurrentOptionConfig;
 552      }
 553  
 554      /*!
 555       Sets the current exit code which will be set with an exit() call in shutdown().
 556       If you don't want shutdown() to exit automatically set it to \c false.
 557      */
 558      function setExitCode( $code = false )
 559      {
 560          $this->ExitCode = $code;
 561      }
 562  
 563      function exitCode()
 564      {
 565          return $this->ExitCode;
 566      }
 567  
 568      /*!
 569       Sets whether any output should be used or not.
 570       \sa isQuiet, isLoud
 571       \note it will also call eZCLI::setIsQuiet()
 572      */
 573      function setIsQuiet( $isQuiet )
 574      {
 575          $cli =& eZCLI::instance();
 576          $this->IsQuiet = $isQuiet;
 577          $cli->setIsQuiet( $isQuiet );
 578      }
 579  
 580      /*!
 581       \return \c true if output is not allowed.
 582       \sa isLoud
 583      */
 584      function isQuiet()
 585      {
 586          return $this->IsQuiet;
 587      }
 588  
 589      /*!
 590       \return \c true if output is allowed.
 591       \sa isQuiet
 592      */
 593      function isLoud()
 594      {
 595          return !$this->IsQuiet;
 596      }
 597  
 598      function setIterationData( $trueString, $falseString,
 599                                 $numericStrings = false, $wrapNumeric = false )
 600      {
 601          $this->IterationTrueString = $trueString;
 602          $this->IterationFalseString = $falseString;
 603          $this->IterationNumericStrings = $numericStrings;
 604          $this->IterationWrapNumeric = $wrapNumeric;
 605      }
 606  
 607      function resetIteration( $iterationMax = false, $startIndex = 0 )
 608      {
 609          $this->IterationIndex = $startIndex;
 610          $this->IterationColumn = 0;
 611          $this->IterationMax = $iterationMax;
 612      }
 613  
 614      function iterate( &$cli, $status, $text = false )
 615      {
 616          if ( !$this->IterationNumericStrings )
 617              $status = (bool)$status;
 618          if ( $this->verboseOutputLevel() === false or
 619               $text === false )
 620          {
 621              if ( is_bool( $status ) )
 622              {
 623                  $statusText = $status ? $this->IterationTrueString : $this->IterationFalseString;
 624              }
 625              else
 626              {
 627                  if ( $this->IterationWrapNumeric )
 628                      $status = $status % count( $this->IterationNumericStrings );
 629                  if ( $status < count( $this->IterationNumericStrings ) )
 630                      $statusText = $this->IterationNumericStrings[$status];
 631                  else
 632                      $statusText = ' ';
 633              }
 634              $endLine = false;
 635              $changeLine = false;
 636              ++$this->IterationIndex;
 637              ++$this->IterationColumn;
 638              $iterationColumn = $this->IterationColumn;
 639              if ( $this->IterationColumn >= $this->IterationColumnMax )
 640              {
 641                  $this->IterationColumn = 0;
 642                  $changeLine = true;
 643              }
 644              if ( $this->IterationMax !== false )
 645              {
 646                  if ( $this->IterationIndex >= $this->IterationMax )
 647                  {
 648                      $this->IterationColumn = 0;
 649                      $changeLine = true;
 650                  }
 651              }
 652              if ( $changeLine )
 653              {
 654                  if ( $this->IterationMax !== false )
 655                  {
 656                      $spacing = $this->IterationColumnMax - $iterationColumn;
 657                      $percent = ( $this->IterationIndex * 100 ) / $this->IterationMax;
 658                      if ( $percent > 100.0 )
 659                          $percent = 100;
 660                      else
 661                          $spacing += 1;
 662                      $percentText = number_format( $percent, 2 ) . '%';
 663                      $statusText .= str_repeat( ' ', $spacing );
 664                      $statusText .= $percentText;
 665                  }
 666                  $endLine = true;
 667              }
 668              $cli->output( $statusText, $endLine );
 669          }
 670          else
 671          {
 672              $statusLevel = $status;
 673              if ( is_bool( $status ) )
 674                  $statusLevel = $status ? 0 : 1;
 675              if ( $statusLevel > 0 )
 676              {
 677                  --$statusLevel;
 678                  $statusLevels = array( 'warning', 'failure' );
 679                  if ( $statusLevel > count( $statusLevels ) )
 680                      $statusLevel = count( $statusLevels ) - 1;
 681                  $levelText = $statusLevels[$statusLevel];
 682                  $cli->output( $cli->stylize( $levelText, $text ) );
 683              }
 684              else
 685              {
 686                  $cli->output( $text );
 687              }
 688          }
 689      }
 690  
 691      function showHelp( $useStandardOptions = false, $optionConfig = false, $optionHelp = false, $argumentConfig = false, $arguments = false )
 692      {
 693          if ( $useStandardOptions === false )
 694          {
 695              $useStandardOptions = $this->CurrentStandardOptions;
 696          }
 697          if ( $optionConfig === false )
 698          {
 699              $optionConfig = $this->CurrentOptionConfig;
 700          }
 701          if ( $optionHelp === false )
 702          {
 703              $optionHelp = $this->CurrentOptionHelp;
 704          }
 705          if ( $argumentConfig === false )
 706          {
 707              $argumentConfig = $this->ArgumentConfig;
 708          }
 709          $optionList = array();
 710          foreach ( $optionConfig['list'] as $configItem )
 711          {
 712              if ( in_array( $configItem['name'], $this->CurrentExcludeOptions ) )
 713                  continue;
 714              $optionText = '-';
 715              if ( $configItem['is-long-option'] )
 716                  $optionText .= '-';
 717              $optionText .= $configItem['name'];
 718              if ( $configItem['has-value'] and $configItem['is-long-option'] )
 719                  $optionText .= "=VALUE";
 720              $hasMultipleValues = ( $configItem['quantifier']['min'] > 1 or
 721                                     $configItem['quantifier']['max'] === false or
 722                                     $configItem['quantifier']['max'] > 1 );
 723              if ( $hasMultipleValues )
 724                  $optionText .= "...";
 725              $optionDescription = '';
 726              if ( isset( $optionHelp[$configItem['name']] ) )
 727                  $optionDescription = $optionHelp[$configItem['name']];
 728              $optionList[] = array( $optionText, $optionDescription );
 729          }
 730          if ( $arguments === false )
 731          {
 732              $arguments = $_SERVER['argv'];
 733              $program = $arguments[0];
 734          }
 735          $cli =& eZCLI::instance();
 736          $generalOptionList = array();
 737          $generalOptionList = array();
 738          if ( $useStandardOptions )
 739          {
 740              $generalOptionList[] = array( '-h,--help', 'display this help and exit');
 741              $generalOptionList[] = array( '-q,--quiet', 'do not give any output except when errors occur' );
 742              if ( $useStandardOptions['siteaccess'] )
 743                  $generalOptionList[] = array( '-s,--siteaccess', "selected siteaccess for operations,\nif not specified default siteaccess is used" );
 744              if ( $useStandardOptions['debug'] )
 745                  $generalOptionList[] = array( '-d,--debug...', ( "display debug output at end of execution,\n" .
 746                                                                   "the following debug items can be controlled: \n" .
 747                                                                   "all, accumulator, include, timing, error, warning, debug or notice." ) );
 748              if ( $useStandardOptions['colors'] )
 749              {
 750                  $generalOptionList[] = array( '-c,--colors', 'display output using ANSI colors (default)' );
 751                  $generalOptionList[] = array( '--no-colors', 'do not use ANSI coloring' );
 752              }
 753              if ( $useStandardOptions['user'] )
 754              {
 755                  $generalOptionList[] = array( '-l,--login USER', 'login with USER and use it for all operations' );
 756                  $generalOptionList[] = array( '-p,--password PWD', 'use PWD as password for USER' );
 757              }
 758              if ( $useStandardOptions['log'] )
 759              {
 760                  $generalOptionList[] = array( '--logfiles', 'create log files' );
 761                  $generalOptionList[] = array( '--no-logfiles', 'do not create log files (default)' );
 762              }
 763              if ( $useStandardOptions['verbose'] )
 764              {
 765                  $generalOptionList[] = array( '-v,--verbose...', "display more information, \nused multiple times will increase amount of information" );
 766              }
 767          }
 768          $description = $this->Description;
 769          $helpText =  "Usage: " . $program;
 770          if ( count( $optionList ) > 0 or count( $generalOptionList ) > 0 )
 771          {
 772              $helpText .= " [OPTION]...";
 773          }
 774          if ( $argumentConfig && isset( $argumentConfig['list'] ) && is_array( $argumentConfig['list'] ) )
 775          {
 776              foreach ( $argumentConfig['list'] as $argumentItem )
 777              {
 778                  $argumentName = strtoupper( $argumentItem['name'] );
 779                  $quantifier = $argumentItem['quantifier'];
 780                  if ( $quantifier['min'] > 1 or $quantifier['max'] === false or $quantifier['max'] > 1 )
 781                      $helpText .= " [$argumentName]...";
 782                  else
 783                      $helpText .= " [$argumentName]";
 784              }
 785          }
 786          if ( $description )
 787              $helpText .= "\n" . $description . "\n";
 788          if ( count( $generalOptionList ) > 0 )
 789          {
 790              $helpText .= "\nGeneral options:\n";
 791              $maxLength = 0;
 792              foreach ( $generalOptionList as $optionItem )
 793              {
 794                  $maxLength = max( strlen( $optionItem[0] ), $maxLength );
 795              }
 796              $spacingLength = $maxLength + 2;
 797              foreach ( $generalOptionList as $optionItem )
 798              {
 799                  $option = $optionItem[0];
 800                  $optionDescription = $optionItem[1];
 801                  $optionLines = explode( "\n", $option );
 802                  $optionDescriptionLines = explode( "\n", $optionDescription );
 803                  $count = max( count( $optionLines ), count( $optionDescriptionLines ) );
 804                  for ( $i = 0; $i < $count; ++$i )
 805                  {
 806                      $optionText = '';
 807                      if ( isset( $optionLines[$i] ) )
 808                          $optionText = $optionLines[$i];
 809                      $optionDescriptionText = '';
 810                      if ( isset( $optionDescriptionLines[$i] ) )
 811                          $optionDescriptionText = $optionDescriptionLines[$i];
 812                      $spacing = $spacingLength - strlen( $optionText );
 813                      if ( $optionText or $optionDescriptionText )
 814                          $helpText .= '  ';
 815                      $helpText .= $optionText;
 816                      if ( $i > 0 )
 817                          $spacing += 2;
 818                      if ( $optionDescriptionText )
 819                          $helpText .= str_repeat( ' ', $spacing ) . $optionDescriptionText;
 820                      $helpText .= "\n";
 821                  }
 822              }
 823          }
 824          if ( count( $optionList ) > 0 )
 825          {
 826              $helpText .= "\nOptions:\n";
 827              $maxLength = 0;
 828              foreach ( $optionList as $optionItem )
 829              {
 830                  $maxLength = max( strlen( $optionItem[0] ), $maxLength );
 831              }
 832              $spacingLength = $maxLength + 2;
 833              foreach ( $optionList as $optionItem )
 834              {
 835                  $option = $optionItem[0];
 836                  $optionDescription = $optionItem[1];
 837                  $optionLines = explode( "\n", $option );
 838                  $optionDescriptionLines = explode( "\n", $optionDescription );
 839                  $count = max( count( $optionLines ), count( $optionDescriptionLines ) );
 840                  for ( $i = 0; $i < $count; ++$i )
 841                  {
 842                      $optionText = '';
 843                      if ( isset( $optionLines[$i] ) )
 844                          $optionText = $optionLines[$i];
 845                      $optionDescriptionText = '';
 846                      if ( isset( $optionDescriptionLines[$i] ) )
 847                          $optionDescriptionText = $optionDescriptionLines[$i];
 848                      $spacing = $spacingLength - strlen( $optionText );
 849                      if ( $optionText or $optionDescriptionText )
 850                          $helpText .= '  ';
 851                      $helpText .= $optionText;
 852                      if ( $i > 0 )
 853                          $spacing += 2;
 854                      if ( $optionDescriptionText )
 855                          $helpText .= str_repeat( ' ', $spacing ) . $optionDescriptionText;
 856                      $helpText .= "\n";
 857                  }
 858              }
 859          }
 860          $cli->output( $helpText );
 861      }
 862  
 863      function getOptions( $config = '', $argumentConfig = '', $optionHelp = false,
 864                           $arguments = false, $useStandardOptions = true )
 865      {
 866          if ( is_string( $config ) )
 867              $config = eZCLI::parseOptionString( $config, $optionConfig );
 868          if ( is_string( $argumentConfig ) )
 869              $argumentConfig = eZCLI::parseOptionString( $argumentConfig, $tmpArgumentConfig );
 870  
 871          if ( $useStandardOptions )
 872          {
 873              if ( !is_array( $useStandardOptions ) )
 874                  $useStandardOptions = array();
 875              $useStandardOptions = array_merge( array( 'debug' => true,
 876                                                        'colors' => true,
 877                                                        'log' => true,
 878                                                        'siteaccess' => true,
 879                                                        'verbose' => true,
 880                                                        'user' => false ),
 881                                                 $useStandardOptions );
 882          }
 883  
 884          if ( $useStandardOptions )
 885          {
 886              $optionConfig = $config;
 887              $excludeOptions = array();
 888              $optionString = "[h|help][q|quiet]";
 889              $excludeOptions[] = 'h';
 890              $excludeOptions[] = 'help';
 891              $excludeOptions[] = 'q';
 892              $excludeOptions[] = 'quiet';
 893              if ( $useStandardOptions['debug'] )
 894              {
 895                  $optionString .= "[d;*|debug;*]";
 896                  $excludeOptions[] = 'd';
 897                  $excludeOptions[] = 'debug';
 898              }
 899              if ( $useStandardOptions['colors'] )
 900              {
 901                  $optionString .= "[c|colors][no-colors]";
 902                  $excludeOptions[] = 'c';
 903                  $excludeOptions[] = 'colors';
 904                  $excludeOptions[] = 'no-colors';
 905              }
 906              if ( $useStandardOptions['log'] )
 907              {
 908                  $optionString .= "[logfiles][no-logfiles]";
 909                  $excludeOptions[] = 'logfiles';
 910                  $excludeOptions[] = 'no-logfiles';
 911              }
 912              if ( $useStandardOptions['siteaccess'] )
 913              {
 914                  $optionString .= "[s:|siteaccess:]";
 915                  $excludeOptions[] = 's';
 916                  $excludeOptions[] = 'siteaccess';
 917              }
 918              if ( $useStandardOptions['user'] )
 919              {
 920                  $optionString .= "[l:|login:][p:|password:]";
 921                  $excludeOptions[] = 'l';
 922                  $excludeOptions[] = 'login';
 923                  $excludeOptions[] = 'p';
 924                  $excludeOptions[] = 'password';
 925              }
 926              if ( $useStandardOptions['verbose'] )
 927              {
 928                  $optionString .= "[v*|verbose*]";
 929                  $excludeOptions[] = 'v';
 930                  $excludeOptions[] = 'verbose';
 931              }
 932              $config = eZCLI::parseOptionString( $optionString, $optionConfig );
 933          }
 934          $cli =& eZCLI::instance();
 935          $options = $cli->getOptions( $config, $argumentConfig, $arguments );
 936          $this->CurrentOptionConfig = $config;
 937          $this->CurrentOptions = $options;
 938          $this->CurrentStandardOptions = $useStandardOptions;
 939          $this->CurrentExcludeOptions = $excludeOptions;
 940          $this->CurrentOptionHelp = $optionHelp;
 941          $this->ArgumentConfig = $argumentConfig;
 942          if ( !$options )
 943          {
 944              if ( !$this->IsInitialized )
 945                  $this->initialize();
 946              $this->shutdown( 1 );
 947              exit;
 948          }
 949          if ( $useStandardOptions )
 950          {
 951              if ( $options['quiet'] )
 952                  $this->setIsQuiet( true );
 953              $useColors = true;
 954              if ( $options['colors'] )
 955                  $useColors = true;
 956              if ( $options['no-colors'] )
 957                  $useColors = false;
 958              $cli->setUseStyles( $useColors );
 959              if ( $options['debug'] )
 960              {
 961                  $debugOptiom = $options['debug'];
 962                  $levels = array();
 963                  foreach ( $options['debug'] as $debugOption )
 964                  {
 965                      $levels = array_merge( $levels, explode( ',', $debugOption ) );
 966                  }
 967                  $allowedDebugLevels = array();
 968                  $useDebugAccumulators = false;
 969                  $useDebugTimingpoints = false;
 970                  $useIncludeFiles = false;
 971                  foreach ( $levels as $level )
 972                  {
 973                      if ( $level == 'all' )
 974                      {
 975                          $useDebugAccumulators = true;
 976                          $allowedDebugLevels = false;
 977                          $useDebugTimingpoints = true;
 978                          break;
 979                      }
 980                      if ( $level == 'accumulator' )
 981                      {
 982                          $useDebugAccumulators = true;
 983                          continue;
 984                      }
 985                      if ( $level == 'timing' )
 986                      {
 987                          $useDebugTimingpoints = true;
 988                          continue;
 989                      }
 990                      if ( $level == 'include' )
 991                      {
 992                          $useIncludeFiles = true;
 993                      }
 994                      if ( $level == 'error' )
 995                          $level = EZ_LEVEL_ERROR;
 996                      else if ( $level == 'warning' )
 997                          $level = EZ_LEVEL_WARNING;
 998                      else if ( $level == 'debug' )
 999                          $level = EZ_LEVEL_DEBUG;
1000                      else if ( $level == 'notice' )
1001                          $level = EZ_LEVEL_NOTICE;
1002                      else if ( $level == 'timing' )
1003                          $level = EZ_LEVEL_TIMING;
1004                      $allowedDebugLevels[] = $level;
1005                  }
1006                  $this->setUseDebugOutput( true );
1007                  $this->setAllowedDebugLevels( $allowedDebugLevels );
1008                  $this->setUseDebugAccumulators( $useDebugAccumulators );
1009                  $this->setUseDebugTimingPoints( $useDebugTimingpoints );
1010                  $this->setUseIncludeFiles( $useIncludeFiles );
1011                  $this->setDebugMessage( "\n\n" . str_repeat( '#', 36 ) . $cli->style( 'emphasize' ) . " DEBUG " . $cli->style( 'emphasize-end' )  . str_repeat( '#', 36 ) . "\n" );
1012              }
1013              if ( count( $options['verbose'] ) > 0 )
1014              {
1015                  $this->setShowVerboseOutput( count( $options['verbose'] ) );
1016              }
1017              if ( $options['help'] )
1018              {
1019                  if ( !$this->IsInitialized )
1020                      $this->initialize();
1021                  $this->showHelp();
1022                  $this->shutdown();
1023                  exit;
1024              }
1025              if ( isset( $options['siteaccess'] ) and $options['siteaccess'] )
1026                  $this->setUseSiteAccess( $options['siteaccess'] );
1027  
1028              if ( isset( $options['login'] ) and $options['login'] )
1029                  $this->setUser( $options['login'], isset( $options['password'] ) ? $options['password'] : false );
1030          }
1031          return $options;
1032      }
1033  
1034      function &instance( $settings = array() )
1035      {
1036          $implementation =& $GLOBALS['eZScriptInstance'];
1037          if ( !isset( $implementation ) or
1038               get_class( $implementation ) != 'ezscript' )
1039          {
1040              $implementation = new eZScript( $settings );
1041          }
1042          return $implementation;
1043      }
1044  
1045      /*!
1046       \static
1047       Reads settings from site.ini and passes them to eZDebug.
1048      */
1049      function updateDebugSettings( $useDebug = null )
1050      {
1051          global $debugOutput;
1052          global $useLogFiles;
1053          $ini =& eZINI::instance();
1054          $cli =& eZCLI::instance();
1055          $debugSettings = array();
1056          $debugSettings['debug-enabled'] = ( $ini->variable( 'DebugSettings', 'DebugOutput' ) == 'enabled' and
1057                                              $ini->variable( 'DebugSettings', 'ScriptDebugOutput' ) == 'enabled' );
1058          if ( $useDebug !== null )
1059              $debugSettings['debug-enabled'] = $useDebug;
1060          $debugSettings['debug-by-ip'] = $ini->variable( 'DebugSettings', 'DebugByIP' ) == 'enabled';
1061          $debugSettings['debug-ip-list'] = $ini->variable( 'DebugSettings', 'DebugIPList' );
1062          if ( isset( $debugOutput ) )
1063              $debugSettings['debug-enabled'] = $debugOutput;
1064          $debugSettings['debug-log-files-enabled'] = $useLogFiles;
1065          if ( $cli->useStyles() and
1066               !$cli->isWebOutput() )
1067          {
1068              $debugSettings['debug-styles'] = $cli->terminalStyles();
1069          }
1070          $logList = $ini->variable( 'DebugSettings', 'AlwaysLog' );
1071          $logMap = array( 'notice' => EZ_LEVEL_NOTICE,
1072                           'warning' => EZ_LEVEL_WARNING,
1073                           'error' => EZ_LEVEL_ERROR,
1074                           'debug' => EZ_LEVEL_DEBUG );
1075          $debugSettings['always-log'] = array();
1076          foreach ( $logMap as $name => $level )
1077          {
1078              $debugSettings['always-log'][$level] = in_array( $name, $logList );
1079          }
1080          eZDebug::updateSettings( $debugSettings );
1081      }
1082  
1083      /*!
1084       \static
1085       Reads settings from i18n.ini and passes them to eZTextCodec.
1086      */
1087      function updateTextCodecSettings()
1088      {
1089          $ini =& eZINI::instance( 'i18n.ini' );
1090          $i18nSettings = array();
1091          $i18nSettings['internal-charset'] = $ini->variable( 'CharacterSettings', 'Charset' );
1092          $i18nSettings['http-charset'] = $ini->variable( 'CharacterSettings', 'HTTPCharset' );
1093          $i18nSettings['mbstring-extension'] = $ini->variable( 'CharacterSettings', 'MBStringExtension' ) == 'enabled';
1094          include_once ( 'lib/ezi18n/classes/eztextcodec.php' );
1095          eZTextCodec::updateSettings( $i18nSettings );
1096      }
1097  
1098      /// \privatesection
1099      var $InitializationErrorMessage;
1100      var $DebugMessage;
1101      var $UseDebugOutput;
1102      var $UseSession;
1103      var $UseExtensions;
1104      var $UseModules;
1105      var $User;
1106      var $SiteAccess;
1107      var $ExitCode;
1108      var $IsQuiet;
1109      var $ShowVerbose;
1110  }
1111  
1112  function eZDBCleanup()
1113  {
1114      if ( class_exists( 'ezdb' )
1115           and eZDB::hasInstance() )
1116      {
1117          $db =& eZDB::instance();
1118          $db->setIsSQLOutputEnabled( false );
1119      }
1120  //     session_write_close();
1121  }
1122  
1123  function eZFatalError()
1124  {
1125      $cli =& eZCLI::instance();
1126      $endl = $cli->endlineString();
1127      $webOutput = $cli->isWebOutput();
1128      $bold = $cli->style( 'bold' );
1129      $unbold = $cli->style( 'bold-end' );
1130      $par = $cli->style( 'paragraph' );
1131      $unpar = $cli->style( 'paragraph-end' );
1132  
1133      $allowedDebugLevels = true;
1134      $useDebugAccumulators = true;
1135      $useDebugTimingpoints = true;
1136  
1137      eZDebug::setHandleType( EZ_HANDLE_NONE );
1138      if ( !$webOutput )
1139          fputs( STDERR, $endl );
1140      fputs( STDERR, $bold . "Fatal error" . $unbold . ": eZ publish did not finish its request$endl" );
1141      fputs( STDERR, $par . "The execution of eZ publish was abruptly ended, the debug output is present below." . $unpar . $endl );
1142      fputs( STDERR, eZDebug::printReport( false, $webOutput, true ) );
1143  }
1144  
1145  /*!
1146    Dummy function, required by some scripts in eZ publish.
1147  */
1148  function eZUpdateDebugSettings( $useDebug = null )
1149  {
1150  }
1151  
1152  /*!
1153    Dummy function, required by some scripts in eZ publish.
1154  */
1155  function eZUpdateTextCodecSettings()
1156  {
1157  }
1158  
1159  ?>


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7