From be0c4cea806c532f41a5974d3c6ceb2326004490 Mon Sep 17 00:00:00 2001 From: Michael RICOIS Date: Tue, 13 Jan 2015 13:15:33 +0000 Subject: [PATCH] set Cache file and Min Max --- scripts/jobs/setCache.php | 216 +++++++++++++++++++++++++++++++++++++ scripts/jobs/setMinMax.php | 151 -------------------------- 2 files changed, 216 insertions(+), 151 deletions(-) create mode 100644 scripts/jobs/setCache.php delete mode 100644 scripts/jobs/setMinMax.php diff --git a/scripts/jobs/setCache.php b/scripts/jobs/setCache.php new file mode 100644 index 00000000..0a602549 --- /dev/null +++ b/scripts/jobs/setCache.php @@ -0,0 +1,216 @@ + array( + __DIR__ . '/../../library/Zend/autoload_classmap.php', + __DIR__ . '/../../library/Application/autoload_classmap.php', + __DIR__ . '/../../library/Scores/autoload_classmap.php', + __DIR__ . '/../../application/autoload_classmap.php', + ), + 'Zend_Loader_StandardAutoloader' => array( + 'prefixes' => array( + 'Zend' => __DIR__ . '/../../library/Zend', + 'Application' => __DIR__ . '/../../library/Application', + 'Scores' => __DIR__ . '/../../library/Scores', + ), + 'fallback_autoloader' => true + ) +)); + +// Create application, bootstrap, and run +$application = new Zend_Application( + APPLICATION_ENV, + APPLICATION_PATH . '/configs/application.ini' +); + +try { + $opts = new Zend_Console_Getopt( + //Options + array( + 'help|?' => "Aide.", + 'minmax' => "Défini les minimums et maximum", + 'table' => "Créer le cache des tables", + 'overwrite' => "Ecrase les fichiers présents", + 'key' => "Agit uniquement sur une clé", + 'verbose' => "Mode manuel", + ) + ); + $opts->parse(); +} catch (Zend_Console_Getopt_Exception $e) { + echo $e->getUsageMessage(); + exit; +} + +//Usage +if(isset($opts->help)) +{ + echo $opts->getUsageMessage(); + exit; +} + +$config = new Zend_Config($application->getOptions()); +try { + $db = Zend_Db::factory($config->profil->db->metier); +} catch ( Exception $e ) { + exit ( $e->getMessage() ); +} +Zend_Db_Table::setDefaultAdapter($db); + +$minmaxSqlLimit = array( + 'dateCrea_ent' => array( + 'dateCrea_ent>17000101', + 'dateCrea_ent array( + 'dateCrea_etab>17000101', + 'dateCrea_etab array(), + 'nbPart' => array(), + 'age_etab' => array(), + 'age_entrep' => array(), + 'nbEtab' => array(), + 'eff_entrep' => array(), + 'eff_etab' => array(), + 'capital' => array(), + 'dateImmat' => array( + 'dateImmat>17000101', + 'dateImmat array(), + 'bilCloture' => array(), + 'bilEE' => array(), + 'bilFL' => array(), + 'bilFK' => array(), + 'bilFR' => array(), + 'bilGF' => array(), + 'bilGP' => array(), + 'bilGW' => array(), + 'bilHD' => array(), + 'bilHH' => array(), + 'bilHL' => array(), + 'bilHM' => array(), + 'bilHN' => array(), + 'bilYP' => array(), +); + +echo "Début\n"; + +if ($opts->minmax && $opts->key === null) { + $sql = 'TRUNCATE TABLE ciblage.fields_minmax'; + if ( !$db->query($sql) ) { + echo "Impossible de vider la table fields_minmax\n"; + } +} + +$fields = new Scores_Ciblage_FieldList(); +$list = $fields->getItemsStruct(); +foreach ( $list as $key => $item ) { + + if ( $opts->key !== null && $opts->key != $key) continue; + + // --- Gestion des tables + if ( $opts->table && array_key_exists('sql', $item) ) { + + $file = APPLICATION_PATH . '/../library/Scores/Table/'.$key.'.php'; + if ( $opts->overwrite && file_exists($file) ) { + unlink($file); + } + if ( file_exists($file) ) continue; + + echo "Create cache for ".$key." => ".$file; + $values = $fields->getItemValues($key); + file_put_contents($file, " $v ) { + file_put_contents($file, "\t'".$k."' => \"".$v."\",\n", FILE_APPEND); + } + file_put_contents($file, ");", FILE_APPEND); + echo " - OK\n"; + } + + // --- Gestion des minimums et maximum + elseif ( $opts->minmax && array_key_exists('minmax', $item) ) { + + if ( $opts->key ) { + $sql = "DELETE FROM ciblage.fields_minmax WHERE cle='".$key."'"; + if ( !$db->query($sql) ) { + echo "Impossible de vider la table fields_minmax\n"; + } + } + + // --- Détermination des minimums et maximums + $sql = 'SELECT MIN('.$key.') AS min, MAX('.$key.') AS max FROM jo.etablissements_act'; + + $where = array(); + if ( array_key_exists($key, $minmaxSqlLimit) ) { + $where = $minmaxSqlLimit[$key]; + } + if ( count($where)>0 ) { + $sql.= ' WHERE '; + $i = 0; + foreach ( $where as $w ) { + if ( $i>0 ) { + $sql.= ' AND '; + } + $sql.= $w; + $i++; + } + } + if ($opts->verbose) echo $sql."\n"; + + try { + $stmt = $db->query($sql); + $result = $stmt->fetchObject(); + $data = array( + 'cle' => $key, + 'min' => $result->min, + 'max' => $result->max, + ); + $db->insert('ciblage.fields_minmax', $data); + if ($opts->verbose) echo $key.' => min:'.$result->min.' max:'.$result->max."\n"; + } catch ( Zend_Db_Exception $e ) { + if ($opts->verbose) { + echo $key." => Erreur\n"; + } + continue; + } + + // --- Cache fichier + $file = APPLICATION_PATH . '/../library/Scores/Table/'.$key.'.php'; + if ( $opts->overwrite && file_exists($file) ) { + unlink($file); + } + + if ( file_exists($file) ) continue; + + echo "Create cache for ".$key." => ".$file; + $values = $fields->getItemValues($key); + file_put_contents($file, " ".$values['min'].",\n", FILE_APPEND); + file_put_contents($file, "\t'max'=> ".$values['max'].",\n", FILE_APPEND); + file_put_contents($file, ");", FILE_APPEND); + echo " - OK\n"; + } +} + +echo "Fin\n"; diff --git a/scripts/jobs/setMinMax.php b/scripts/jobs/setMinMax.php deleted file mode 100644 index c65927e9..00000000 --- a/scripts/jobs/setMinMax.php +++ /dev/null @@ -1,151 +0,0 @@ - array( - __DIR__ . '/../../library/Zend/autoload_classmap.php', - __DIR__ . '/../../library/Application/autoload_classmap.php', - __DIR__ . '/../../library/Scores/autoload_classmap.php', - __DIR__ . '/../../application/autoload_classmap.php', - ), - 'Zend_Loader_StandardAutoloader' => array( - 'prefixes' => array( - 'Zend' => __DIR__ . '/../../library/Zend', - 'Application' => __DIR__ . '/../../library/Application', - 'Scores' => __DIR__ . '/../../library/Scores', - ), - 'fallback_autoloader' => true - ) -)); - -// Create application, bootstrap, and run -$application = new Zend_Application( - APPLICATION_ENV, - APPLICATION_PATH . '/configs/application.ini' -); - -try { - $opts = new Zend_Console_Getopt( - //Options - array( - 'help|?' => "Aide.", - 'cron' => "Mode automatique", - 'manuel' => "Mode manuel", - ) - ); - $opts->parse(); -} catch (Zend_Console_Getopt_Exception $e) { - echo $e->getUsageMessage(); - exit; -} - -//Usage -if(isset($opts->help)) -{ - echo $opts->getUsageMessage(); - exit; -} - -$config = new Zend_Config($application->getOptions()); -try { - $db = Zend_Db::factory($config->profil->db->metier); -} catch ( Exception $e ) { - exit ( $e->getMessage() ); -} - -$keys = array( - 'dateCrea_ent' => array( - 'dateCrea_ent>17000101', - 'dateCrea_ent array( - 'dateCrea_etab>17000101', - 'dateCrea_etab array(), - 'nbPart' => array(), - 'age_etab' => array(), - 'age_entrep' => array(), - 'nbEtab' => array(), - 'eff_entrep' => array(), - 'eff_etab' => array(), - 'capital' => array(), - 'dateImmat' => array( - 'dateImmat>17000101', - 'dateImmat array(), - 'bilCloture' => array(), - 'bilEE' => array(), - 'bilFL' => array(), - 'bilFK' => array(), - 'bilFR' => array(), - 'bilGF' => array(), - 'bilGP' => array(), - 'bilGW' => array(), - 'bilHD' => array(), - 'bilHH' => array(), - 'bilHL' => array(), - 'bilHM' => array(), - 'bilHN' => array(), - 'bilYP' => array(), -); - -$sql = 'TRUNCATE TABLE ciblage.fields_minmax'; -if ( !$db->query($sql) ) { - die ('Impossible de vider la table fields_minmax'); -} - -foreach($keys as $key => $where) { - - if ($opts->manuel) echo $key.PHP_EOL; - - //Lecture - $sql = 'SELECT MIN('.$key.') AS min, MAX('.$key.') AS max FROM jo.etablissements_act'; - if ( count($where)>0 ) { - $sql.= ' WHERE '; - $i = 0; - foreach ( $where as $w ) { - if ( $i>0 ) { - $sql.= ' AND '; - } - $sql.= $w; - $i++; - } - } - if ($opts->manuel) echo $sql.PHP_EOL; - try { - $stmt = $db->query($sql); - $result = $stmt->fetchObject(); - - //Insertion - $data = array( - 'cle' => $key, - 'min' => $result->min, - 'max' => $result->max, - ); - $db->insert('ciblage.fields_minmax', $data); - if ($opts->manuel) echo $key.' => min:'.$result->min.' max:'.$result->max."\n"; - } catch ( Zend_Db_Exception $e ) { - if ($opts->manuel) echo $key." => Erreur\n"; - } - -} -if ($opts->manuel) echo "Fin\n";