Beispiel #1
0
 /**
  * Prepare the tmp table for updates, use the deletion rules for it
  *
  * @param $objRun
  * @return bool
  */
 public static function clearTable($objRun)
 {
     $Database = Database::getInstance();
     // perform the deletion rules defined in the converter for every internal target table
     foreach ($objRun->targets as $strTarget) {
         if (!in_array($strTarget, $objRun->cleared)) {
             $objConverter = false;
             foreach ($objRun->steps as $intStep) {
                 $objStep = StepModel::findByPk($intStep);
                 if ($objStep->action == 'converter' && !$objConverter) {
                     $objConverter = ConverterModel::findOneBy(array('id=?', 'targetTable=?'), array($objStep->converter, str_replace('cvx_', '', $strTarget)));
                 }
             }
             // there are tables not bound on a converter
             if (!$objConverter) {
                 return $strTarget;
             } else {
                 $objConverter->deleteRules = Helper::arrayOnly($objConverter->deleteRules);
                 $objConverter->fieldsTarget = Helper::arrayOnly($objConverter->fieldsTarget);
                 $objConverter->fieldsSource = Helper::arrayOnly($objConverter->fieldsSource);
                 // for updates we need all existent keys of the source and the target table
                 if ($objConverter->allowUpdate || $objConverter->deleteOnStart != '' && $objConverter->deleteOnStart != 'all') {
                     // source
                     $strSource = $objConverter->sourceType == 'InternalTable' ? $objConverter->sourceTable : 'cvx_' . $objConverter->id . '_source';
                     $objKeys = $Database->prepare("SELECT " . $objConverter->keySource . " FROM " . $strSource)->execute();
                     $arrSourceKeys = $objKeys->fetchEach($objConverter->keySource);
                     if ($objConverter->sourceHasFieldnames) {
                         // seems to be an artefact
                         //                            array_shift($arrSourceKeys);
                     }
                     // target
                     $objKeys = $Database->prepare("SELECT " . $objConverter->keyTarget . " FROM " . $strTarget)->execute();
                     $arrTargetKeys = $objKeys->fetchEach($objConverter->keyTarget);
                     $objConverter->targetKeys = serialize($arrTargetKeys);
                 }
                 $objConverter->save();
                 $blnOk = true;
                 // delete all data in target table
                 if ($objConverter->deleteOnStart == 'all') {
                     if (!($objDeletion = $Database->prepare("TRUNCATE TABLE " . $strTarget)->execute())) {
                         $blnOk = false;
                     }
                 } elseif ($objConverter->deleteOnStart == 'missing') {
                     // delete data missing in target, but existing in the source table
                     if (!($objDeletion = $Database->prepare("DELETE FROM " . $strTarget . " WHERE " . $objConverter->keyTarget . " IN ('" . implode("','", array_diff($arrTargetKeys, $arrSourceKeys)) . "')")->execute())) {
                         $blnOk = false;
                     }
                 } elseif ($objConverter->deleteOnStart == 'existent') {
                     // delete data already existing in target table
                     if (!($objDeletion = $Database->prepare("DELETE FROM " . $strTarget . " WHERE " . $objConverter->keyTarget . " IN ('" . implode("','", array_diff($arrTargetKeys, array_diff($arrTargetKeys, $arrSourceKeys))) . "')")->execute())) {
                         $blnOk = false;
                     }
                 }
                 // delete depending on rules
                 if ($objConverter->deleteOnRules) {
                     $strWhere = '';
                     foreach ($objConverter->deleteRules as $k => $v) {
                         $strWhere .= ($k > 0 ? ' ' . $v['type'] . ' ' : '') . $v['field'] . ' ';
                         switch ($v['operator']) {
                             case 'gteq':
                                 $strWhere .= '>= \'' . addslashes($v['value']) . '\'';
                                 break;
                             case 'gt':
                                 $strWhere .= '> \'' . addslashes($v['value']) . '\'';
                                 break;
                             case 'lt':
                                 $strWhere .= '< \'' . addslashes($v['value']) . '\'';
                                 break;
                             case 'lteq':
                                 $strWhere .= '<= \'' . addslashes($v['value']) . '\'';
                                 break;
                             case 'begins':
                                 $strWhere .= 'LIKE \'' . addslashes($v['value']) . '%\'';
                                 break;
                             case 'ends':
                                 $strWhere .= 'LIKE \'%' . addslashes($v['value']) . '\'';
                                 break;
                             default:
                                 $strWhere .= '= \'' . addslashes($v['value']) . '\'';
                                 break;
                         }
                     }
                     if (!($objDeletion = $Database->prepare("DELETE FROM " . $strTarget . " WHERE " . $strWhere)->execute())) {
                         $blnOk = false;
                     }
                 }
                 if ($blnOk) {
                     Tracking::log($objRun->id, $objRun->rootRun, sprintf($GLOBALS['TL_LANG']['tl_convertx_job']['temporaryTargetCleared'], $strTarget), 'entry');
                     return $strTarget;
                 } else {
                     Tracking::log($objRun->id, $objRun->rootRun, sprintf($GLOBALS['TL_LANG']['tl_convertx_job']['temporaryTargetNotCleared'], $strTarget), 'entry', 'error');
                     return false;
                 }
             }
             // fill exactly 1 of the temp source tables at a time
             continue;
         }
     }
 }
Beispiel #2
0
 /**
  * Find the internal and external source tables (tmp versions)
  *
  * @param $intPid
  * @return array
  */
 public static function findTmpSourceTables($intPid)
 {
     $objSteps = StepModel::findBy(array('tl_convertx_step.pid=?', 'tl_convertx_step.published=?', 'tl_convertx_step.action=?'), array($intPid, 1, 'converter'));
     if (!$objSteps) {
         return array();
     }
     while ($objSteps->next()) {
         $objConverter = ConverterModel::findByPk($objSteps->converter);
         switch ($objConverter->sourceType) {
             case 'InternalTable':
                 // for internal sources we don't need tmp tables
                 break;
             default:
                 // tmp tables for external data get a name based on the converter id
                 $arrSources[] = 'cvx_' . $objConverter->id . '_source';
                 break;
         }
     }
     return (array) $arrSources;
 }
Beispiel #3
0
 /**
  * Do a step
  *
  * @param $objRun
  * @return mixed
  */
 public static function doStep($objRun)
 {
     $objStep = StepModel::findByPk($objRun->step);
     // -----------------------------------------
     // use the class of the step
     $strClass = 'Delahaye\\ConvertX\\Step\\' . ucfirst($objStep->action);
     Tracking::log($objRun->id, $objRun->rootRun, sprintf('%s: %s', $GLOBALS['TL_LANG']['tl_convertx_job']['step_' . $objStep->action], $objStep->title));
     $objReturn = $strClass::doStep($objRun, $objStep);
     if ($objReturn->error) {
         Tracking::log($objRun->id, $objRun->rootRun, $objReturn->error, 'entry', 'error', $objReturn->details);
         if ($objStep->abortOnError) {
             $objReturn->title = $GLOBALS['TL_LANG']['tl_convertx_job']['abortion'];
             Tracking::log($objRun->id, $objRun->rootRun, sprintf($GLOBALS['TL_LANG']['tl_convertx_job']['abortionInStep'], $objStep->id, $objStep->action, $objStep->title), 'step', 'error');
             return $objReturn;
         }
     }
     return $objReturn;
 }