PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/OX/Maintenance/Statistics.php

https://bitbucket.org/valmy/openx
PHP | 245 lines | 92 code | 40 blank | 113 comment | 12 complexity | 18291a07358c99811e19af627db4b0ba MD5 | raw file
  1. <?php
  2. /*
  3. +---------------------------------------------------------------------------+
  4. | OpenX v2.8 |
  5. | ========== |
  6. | |
  7. | Copyright (c) 2003-2009 OpenX Limited |
  8. | For contact details, see: http://www.openx.org/ |
  9. | |
  10. | This program is free software; you can redistribute it and/or modify |
  11. | it under the terms of the GNU General Public License as published by |
  12. | the Free Software Foundation; either version 2 of the License, or |
  13. | (at your option) any later version. |
  14. | |
  15. | This program is distributed in the hope that it will be useful, |
  16. | but WITHOUT ANY WARRANTY; without even the implied warranty of |
  17. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
  18. | GNU General Public License for more details. |
  19. | |
  20. | You should have received a copy of the GNU General Public License |
  21. | along with this program; if not, write to the Free Software |
  22. | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
  23. +---------------------------------------------------------------------------+
  24. $Id$
  25. */
  26. require_once MAX_PATH . '/lib/OA.php';
  27. require_once MAX_PATH . '/lib/OA/ServiceLocator.php';
  28. require_once MAX_PATH . '/lib/OA/Task/Runner.php';
  29. require_once LIB_PATH . '/Dal/Maintenance/Statistics/Factory.php';
  30. require_once LIB_PATH . '/Maintenance/Statistics/Task/SetUpdateRequirements.php';
  31. require_once LIB_PATH . '/Maintenance/Statistics/Task/SummariseIntermediate.php';
  32. require_once LIB_PATH . '/Maintenance/Statistics/Task/DeduplicateConversions.php';
  33. require_once LIB_PATH . '/Maintenance/Statistics/Task/ManageConversions.php';
  34. require_once LIB_PATH . '/Maintenance/Statistics/Task/SummariseFinal.php';
  35. require_once LIB_PATH . '/Maintenance/Statistics/Task/ManageCampaigns.php';
  36. require_once LIB_PATH . '/Maintenance/Statistics/Task/LogCompletion.php';
  37. require_once LIB_PATH . '/Plugin/Component.php';
  38. /**
  39. * A class for preparing the tasks that need to be run as part of the
  40. * Maintenance Statistics Engine process.
  41. *
  42. * @static
  43. * @package OpenXMaintenance
  44. * @subpackage Statistics
  45. * @author Andrew Hill <andrew.hill@openx.org>
  46. */
  47. class OX_Maintenance_Statistics
  48. {
  49. /**
  50. * Text report of the details logged by the run tasks.
  51. *
  52. * @var string
  53. */
  54. var $report;
  55. /**
  56. * The date/time that the intermediate tables were last updated.
  57. *
  58. * @var PEAR::Date
  59. */
  60. var $oLastDateIntermediate;
  61. /**
  62. * Should the intermediate tables be updated?
  63. *
  64. * @var boolean
  65. */
  66. var $updateIntermediate;
  67. /**
  68. * Did the operation interval remain the same since the last run?
  69. * Set to false when the OI changes, and a non-standard OI calculation
  70. * needs to be performed.
  71. *
  72. * @var boolean
  73. */
  74. var $sameOI = true;
  75. /**
  76. * The date/time to update the intermediate tables to, if appropriate.
  77. *
  78. * @var PEAR::Date
  79. */
  80. var $oUpdateIntermediateToDate;
  81. /**
  82. * The date/time that the final tables were last updated.
  83. *
  84. * @var PEAR::Date
  85. */
  86. var $oLastDateFinal;
  87. /**
  88. * Should the final tables be updated?
  89. *
  90. * @var boolean
  91. */
  92. var $updateFinal;
  93. /**
  94. * The date/time to update the intermediate tables to, if appropriate.
  95. *
  96. * @var PEAR::Date
  97. */
  98. var $oUpdateFinalToDate;
  99. /**
  100. * An OA_Task_Runner instance to store the MSE tasks.
  101. *
  102. * @var OA_Task_Runner
  103. */
  104. var $oTaskRunner;
  105. /** @var array array of addMaintenanceStatisticsTask components. */
  106. private $aComponents;
  107. /**
  108. * The method to run the Maintenance Statistics Engine process.
  109. *
  110. * @static
  111. */
  112. function run()
  113. {
  114. OA::switchLogIdent('maintenance');
  115. // Get the configuration
  116. $aConf = $GLOBALS['_MAX']['CONF'];
  117. // Log the start of the process
  118. OA::debug('Running Maintenance Statistics Engine', PEAR_LOG_INFO);
  119. // Set longer time out, and ignore user abort
  120. if (!ini_get('safe_mode')) {
  121. @set_time_limit($aConf['maintenance']['timeLimitScripts']);
  122. @ignore_user_abort(true);
  123. }
  124. // Run the following code as the "Maintenance" user
  125. OA_Permission::switchToSystemProcessUser('Maintenance');
  126. // Ensure the the current time is registered with the OA_ServiceLocator
  127. $oServiceLocator =& OA_ServiceLocator::instance();
  128. $oDate = &$oServiceLocator->get('now');
  129. if (!$oDate) {
  130. // Record the current time, and register with the OA_ServiceLocator
  131. $oDate = new Date();
  132. $oServiceLocator->register('now', $oDate);
  133. }
  134. $this->aComponents = OX_Component::getListOfRegisteredComponentsForHook('addMaintenanceStatisticsTask');
  135. // addMaintenanceStatisticsTask hook
  136. if (!empty($this->aComponents) && is_array($this->aComponents)) {
  137. foreach ($this->aComponents as $componentId) {
  138. if ($obj = OX_Component::factoryByComponentIdentifier($componentId)) {
  139. $obj->beforeMse();
  140. }
  141. }
  142. }
  143. // Initialise the task runner object, for storing/running the tasks
  144. $this->oTaskRunner = new OA_Task_Runner();
  145. // Register this object as the controlling class for the process,
  146. // so that tasks run by the task runner can locate this class to
  147. // update the report, etc.
  148. $oServiceLocator =& OA_ServiceLocator::instance();
  149. $oServiceLocator->register('Maintenance_Statistics_Controller', $this);
  150. // Create and register an instance of the OA_Dal_Maintenance_Statistics DAL
  151. // class for the following tasks to use
  152. if (!$oServiceLocator->get('OX_Dal_Maintenance_Statistics')) {
  153. $oFactory = new OX_Dal_Maintenance_Statistics_Factory();
  154. $oDal = $oFactory->factory();
  155. $oServiceLocator->register('OX_Dal_Maintenance_Statistics', $oDal);
  156. }
  157. // Add the task to set the update requirements
  158. $oSetUpdateRequirements = new OX_Maintenance_Statistics_Task_SetUpdateRequirements();
  159. $this->oTaskRunner->addTask($oSetUpdateRequirements);
  160. // Add the task to migrate the bucket data into the statistics tables
  161. $oSummariseIntermediate = new OX_Maintenance_Statistics_Task_MigrateBucketData();
  162. $this->oTaskRunner->addTask($oSummariseIntermediate);
  163. // Add the task to handle the de-duplication and rejection of empty conversions
  164. $oDeDuplicateConversions = new OX_Maintenance_Statistics_Task_DeDuplicateConversions();
  165. $this->oTaskRunner->addTask($oDeDuplicateConversions);
  166. // Add the task to handle the updating of "intermediate" statistics with
  167. // conversion information, as a legacy issue until all code obtains
  168. // conversion data from the standard conversion statistics tables
  169. $oManageConversions = new OX_Maintenance_Statistics_Task_ManageConversions();
  170. $this->oTaskRunner->addTask($oManageConversions);
  171. // Add the task to summarise the intermediate statistics into final form
  172. $oSummariseFinal = new OX_Maintenance_Statistics_Task_SummariseFinal();
  173. $this->oTaskRunner->addTask($oSummariseFinal);
  174. // Add the task to log the completion of the task
  175. $oLogCompletion = new OX_Maintenance_Statistics_Task_LogCompletion();
  176. $this->oTaskRunner->addTask($oLogCompletion);
  177. // Add the task to manage (enable/disable) campaigns
  178. $oManageCampaigns = new OX_Maintenance_Statistics_Task_ManageCampaigns();
  179. $this->oTaskRunner->addTask($oManageCampaigns);
  180. // addMaintenanceStatisticsTask hook
  181. if (!empty($this->aComponents) && is_array($this->aComponents)) {
  182. foreach ($this->aComponents as $componentId) {
  183. if ($obj = OX_Component::factoryByComponentIdentifier($componentId)) {
  184. $this->oTaskRunner->addTask($obj->addMaintenanceStatisticsTask(), $obj->getExistingClassName(), $obj->getOrder());
  185. }
  186. }
  187. }
  188. // Run the MSE process tasks
  189. $this->oTaskRunner->runTasks();
  190. // addMaintenanceStatisticsTask hook
  191. if (!empty($this->aComponents) && is_array($this->aComponents)) {
  192. foreach ($this->aComponents as $componentId) {
  193. if ($obj = OX_Component::factoryByComponentIdentifier($componentId)) {
  194. $obj->afterMse();
  195. }
  196. }
  197. }
  198. // Return to the "normal" user
  199. OA_Permission::switchToSystemProcessUser();
  200. // Log the end of the process
  201. OA::debug('Maintenance Statistics Engine Completed (Started at ' . $oDate->format('%Y-%m-%d %H:%M:%S') . ' ' . $oDate->tz->getShortName() . ')', PEAR_LOG_INFO);
  202. OA::switchLogIdent();
  203. }
  204. }
  205. ?>