/** * A method to perform the migration of logged bucket-based supplementary * raw statistics data from the bucket table(s) into a final statistics table. * * @param string $statisticsTableName The name of the statistics table the * data is to be migrated to. * @param array $aMigrationDetails An array containing the details of the * bucket data to migrate into the statistics * table. See the * Plugins_DeliveryLog::getStatisticsMigration() * method for details. * @param array $aDates An array containing the PEAR Date objects representing the * start and end dates for the operation interval being migrated, * indexed by "start" and "end", respectively. * @return mixed A PEAR_Error or MDB2_Error object on failure, otherwise, the number * of rows of raw data that were migrated from the bucket table to the * statistics table. */ function summariseBucketsRawSupplementary($statisticsTableName, $aMigrationDetails, $aDates) { // Perform basic checking of the parameters; assumes that $aMigrationDetails // has already been checked by the Plugins_DeliveryLog::testStatisticsMigration() // method if ($aMigrationDetails['method'] != 'rawSupplementary') { $message = "OX_Dal_Maintenance_Statistics::summariseBucketsRawSupplementary() called with migration map method '{$aMigrationDetails['method']}' != 'rawSupplementary'."; $oError = new PEAR_Error($message, MAX_ERROR_INVALIDARGS); return $oError; } if (count($aMigrationDetails['masterTablePrimaryKeys']) != count($aMigrationDetails['bucketTablePrimaryKeys'])) { $message = "OX_Dal_Maintenance_Statistics::summariseBucketsRawSupplementary() called with different number of 'masterTablePrimaryKeys' and 'bucketTablePrimaryKeys' columns."; $oError = new PEAR_Error($message, MAX_ERROR_INVALIDARGS); return $oError; } if (count($aMigrationDetails['masterTableKeys']) != count($aMigrationDetails['bucketTableKeys'])) { $message = "OX_Dal_Maintenance_Statistics::summariseBucketsRawSupplementary() called with different number of 'masterTableKeys' and 'bucketTableKeys' columns."; $oError = new PEAR_Error($message, MAX_ERROR_INVALIDARGS); return $oError; } if (count($aMigrationDetails['source']) != count($aMigrationDetails['destination'])) { $message = "OX_Dal_Maintenance_Statistics::summariseBucketsRawSupplementary() called with different number of 'source' and 'destination' columns."; $oError = new PEAR_Error($message, MAX_ERROR_INVALIDARGS); return $oError; } if (!is_a($aDates['start'], 'Date') || !is_a($aDates['end'], 'Date')) { $message = "OX_Dal_Maintenance_Statistics::summariseBucketsRawSupplementary() called with invalid start/end date parameters -- not Date objects."; $oError = new PEAR_Error($message, MAX_ERROR_INVALIDARGS); return $oError; } if (!OX_OperationInterval::checkIntervalDates($aDates['start'], $aDates['end'])) { $message = "OX_Dal_Maintenance_Statistics::summariseBucketsRawSupplementary() called with invalid start/end date parameters -- not operation interval bounds."; $oError = new PEAR_Error($message, MAX_ERROR_INVALIDARGS); return $oError; } // Ensure that tables exist before trying to run commands based on // plugin components $oTable = new OA_DB_Table(); if (!$oTable->extistsTable($statisticsTableName)) { $message = "OX_Dal_Maintenance_Statistics::summariseBucketsRawSupplementary() called with invalid statistics table '{$statisticsTableName}'."; $oError = new PEAR_Error($message, MAX_ERROR_INVALIDREQUEST); return $oError; } if (!$oTable->extistsTable($aMigrationDetails['masterTable'])) { $message = "OX_Dal_Maintenance_Statistics::summariseBucketsRawSupplementary() called with invalid master table '{$aMigrationDetails['masterTable']}'."; $oError = new PEAR_Error($message, MAX_ERROR_INVALIDREQUEST); return $oError; } if (!$oTable->extistsTable($aMigrationDetails['bucketTable'])) { $message = "OX_Dal_Maintenance_Statistics::summariseBucketsRawSupplementary() called with invalid bucket table '{$aMigrationDetails['bucketTable']}'."; $oError = new PEAR_Error($message, MAX_ERROR_INVALIDREQUEST); return $oError; } // Prepare the previously migrated raw data statistics table columns array $aMasterColumns = array(); foreach ($aMigrationDetails['masterTablePrimaryKeys'] as $value) { $aMasterColumns[] = $this->oDbh->quoteIdentifier($value, true); } foreach ($aMigrationDetails['masterTableKeys'] as $value) { $aMasterColumns[] = $this->oDbh->quoteIdentifier($value, true); } // Prepare the query to locate the data in columns in the statistics // table which contains the previously migrated raw bucket data, // which will then be used to locate the supplementary raw data and // also to ensure that when this supplementary raw data is migrated // to its statistics table, the supplementary raw data can be // connected with the previously migrated raw data $query = "\n SELECT\n " . implode(', ', $aMasterColumns) . "\n FROM\n " . $this->oDbh->quoteIdentifier($aMigrationDetails['masterTable'], true) . "\n WHERE\n " . $this->oDbh->quoteIdentifier($aMigrationDetails['masterDateTimeColumn'], true) . " >= " . $this->oDbh->quote($aDates['start']->format('%Y-%m-%d %H:%M:%S'), 'timestamp') . "\n AND\n " . $this->oDbh->quoteIdentifier($aMigrationDetails['masterDateTimeColumn'], true) . " <= " . $this->oDbh->quote($aDates['end']->format('%Y-%m-%d %H:%M:%S'), 'timestamp'); // Prevent any strange database error from causing execution to halt // by overriding the error handler, run the query, and return the // MDB2_Error object, if required PEAR::pushErrorHandling(null); $rsResult = $this->oDbh->query($query); PEAR::popErrorHandling(); if (PEAR::isError($rsResult)) { return $rsResult; } // Were any rows found for previously migrated summarised raw data? if ($rsResult->numRows() == 0) { return 0; } // Ensure that the required arrays are sorted by key ksort($aMigrationDetails['masterTableKeys']); ksort($aMigrationDetails['bucketTableKeys']); ksort($aMigrationDetails['source']); ksort($aMigrationDetails['destination']); // Prepare the destination columns array $aDestinationColumns = array(); foreach ($aMigrationDetails['bucketTablePrimaryKeys'] as $value) { $aDestinationColumns[] = $this->oDbh->quoteIdentifier($value, true); } foreach ($aMigrationDetails['destination'] as $value) { $aDestinationColumns[] = $this->oDbh->quoteIdentifier($value, true); } $counter = 0; while ($aRow = $rsResult->fetchRow()) { // Prepare the select column statements array $aSelectColumnStatements = array(); foreach ($aMigrationDetails['bucketTablePrimaryKeys'] as $value) { $aSelectColumnStatements[] = $this->oDbh->quote($aRow[$value], 'text') . ' AS ' . $this->oDbh->quoteIdentifier($value, true); } foreach ($aMigrationDetails['destination'] as $key => $value) { $aSelectColumnStatements[] = $this->oDbh->quoteIdentifier($aMigrationDetails['source'][$key], true) . ' AS ' . $this->oDbh->quoteIdentifier($value, true); } // Prepare the where statementes array $aWhereStatements = array(); foreach ($aMigrationDetails['masterTableKeys'] as $key => $value) { $aWhereStatements[] = $this->oDbh->quoteIdentifier($aMigrationDetails['bucketTableKeys'][$key], true) . ' = ' . $this->oDbh->quote($aRow[$value], 'text'); } // Prepare the query to migrate the supplementary raw data from bucket // table to the statistics table $query = "\n INSERT INTO\n " . $this->oDbh->quoteIdentifier($statisticsTableName, true) . "\n (" . implode(', ', $aDestinationColumns) . ")\n SELECT\n " . implode(', ', $aSelectColumnStatements) . "\n FROM\n " . $this->oDbh->quoteIdentifier($aMigrationDetails['bucketTable'], true) . "\n WHERE\n " . implode(' AND ', $aWhereStatements); // Prevent any strange database error from causing execution to halt // by overriding the error handler, run the query, and return the // result (either the number or rows affected, or an MDB2_Error // object on query/database error) PEAR::pushErrorHandling(null); $result = $this->oDbh->exec($query); PEAR::popErrorHandling(); if (PEAR::isError($result)) { return $result; } $counter += $result; } return $counter; }
/** * A method to test the checkIntervalDates() method. */ function testCheckIntervalDates() { $conf =& $GLOBALS['_MAX']['CONF']; // Set the operation interval $conf['maintenance']['operationInterval'] = 30; // Test less than one operation interval $start = new Date('2004-09-26 00:00:00'); $end = new Date('2004-09-26 00:15:00'); $this->assertFalse(OX_OperationInterval::checkIntervalDates($start, $end)); // Test more than one operation inteterval $start = new Date('2004-09-26 00:00:00'); $end = new Date('2004-09-26 00:45:00'); $this->assertFalse(OX_OperationInterval::checkIntervalDates($start, $end)); // Test exactly one operation interval $start = new Date('2004-09-26 00:00:00'); $end = new Date('2004-09-26 00:29:59'); $this->assertTrue(OX_OperationInterval::checkIntervalDates($start, $end)); // Set the operation interval $conf['maintenance']['operationInterval'] = 60; // Test less than one operation interval/hour $start = new Date('2004-09-26 00:00:00'); $end = new Date('2004-09-26 00:30:00'); $this->assertFalse(OX_OperationInterval::checkIntervalDates($start, $end)); // Test more than one operation inteterval/hour $start = new Date('2004-09-26 00:00:00'); $end = new Date('2004-09-26 01:15:00'); $this->assertFalse(OX_OperationInterval::checkIntervalDates($start, $end)); // Test exactly one operation interval/hour $start = new Date('2004-09-26 00:00:00'); $end = new Date('2004-09-26 00:59:59'); $this->assertTrue(OX_OperationInterval::checkIntervalDates($start, $end)); // Set the operation interval $conf['maintenance']['operationInterval'] = 120; // Test less than one hour $start = new Date('2004-09-26 00:00:00'); $end = new Date('2004-09-26 00:15:00'); $this->assertFalse(OX_OperationInterval::checkIntervalDates($start, $end)); // Test more than one hour $start = new Date('2004-09-26 00:00:00'); $end = new Date('2004-09-26 04:00:00'); $this->assertFalse(OX_OperationInterval::checkIntervalDates($start, $end)); // Test exactly one hour $start = new Date('2004-09-26 00:00:00'); $end = new Date('2004-09-26 00:59:59'); $this->assertTrue(OX_OperationInterval::checkIntervalDates($start, $end)); }
/** * A private method to test the parameters of the getAdTargetingStatistics() * and getZoneTargetingStatistics methods. * * @access private * @param integer $id The ad or zone ID. * @param PEAR::Date $oStartDate The start date of the operation interval. * @param PEAR::Date $oEndDate The end date of the operation interval. * @return boolean True if the parameters are okay, false otherwise. */ function _testParameters($id, $oStartDate, $oEndDate) { // Ensure the parameters are valid if (empty($id) || !is_int($id)) { return false; } if (empty($oStartDate) || !is_a($oStartDate, 'Date')) { return false; } if (empty($oEndDate) || !is_a($oEndDate, 'Date')) { return false; } // Ensure that the date range specified is indeed an operation interval if (!OX_OperationInterval::checkIntervalDates($oStartDate, $oEndDate)) { return false; } return true; }