/** * Test cases for _loader() and _unloader() */ public function testForFunctionsLoaderAndUnloader() { global $lc_autoload; // 1. _loader('i18n_helper'); _loader('pager_helper'); $this->assertEqual($lc_autoload, array(HELPER . 'i18n_helper.php', HELPER . 'pager_helper.php')); // 2. _unloader('session_helper'); $this->assertEqual($lc_autoload, array(HELPER . 'i18n_helper.php', HELPER . 'pager_helper.php')); // 3. _loader('session_helper'); _loader('validation_helper'); _loader('auth_helper'); _loader('security_helper'); _loader('form_helper'); _loader('file_helper'); $this->assertEqual($lc_autoload, array(HELPER . 'i18n_helper.php', HELPER . 'pager_helper.php', HELPER . 'session_helper.php', HELPER . 'validation_helper.php', HELPER . 'auth_helper.php', HELPER . 'security_helper.php', HELPER . 'form_helper.php', HELPER . 'file_helper.php')); // 4. _unloader('file_helper'); _unloader('pager_helper'); $this->assertEqual($lc_autoload, array(HELPER . 'i18n_helper.php', HELPER . 'session_helper.php', HELPER . 'validation_helper.php', HELPER . 'auth_helper.php', HELPER . 'security_helper.php', HELPER . 'form_helper.php')); }
/** * extracts a list of URLs, in order to pre-fetch them (wget, curl, jmeter, ...) * and generate the cache on the server (memcache, filecache) and avoid cpu boost after system restart * * @param string $task * @param string $args * @return void * @author Guglielmo Celata */ function run_opp_urls_to_cache($task, $args) { static $loaded; // load application context if (!$loaded) { _loader(); } $site_url = sfConfig::get('sf_site_url', 'op_openparlamento'); $urls = array("/", "/attiDisegni", "/attiDecretiLegge", "/attiDecretiLegislativi", "/attiNonLegislativi/data_pres/desc", "/votazioni/data/desc", "/parlamentari/camera/nome/asc", "/parlamentari/senato/nome/asc", "/argomenti", "/community", "/blog", "/progetto", "/static/chisiamo", "/contatti", "/sottoscrizioni_pro", "/static/rssxml", "/static/regolamento", "/static/condizioni", "/static/informativa", "/static/inizia", "/faq"); foreach (array('attiDisegni', 'attiDecretiLegge', 'attiDecretiLegislativi', 'attiNonLegislativi', 'votazioni/data/desc') as $page) { $links = getInnerLinksForPage("http://" . $site_url . "/" . $page); foreach ($links as $link) { if (preg_match("/singolo_atto/", $link['href']) || preg_match("/votazione/", $link['href'])) { $urls[] = $link['href']; } } } foreach ($urls as $url) { echo "http://" . $site_url . $url . "\n"; } }
/** * Prepara uno o più atti, specificati dall'id nell'elenco argomenti per un test * Vengono rimossi tutti i dati accessori: * firme, assrgnazioni in commissione, documenti, tagging, esiti, interventi * Sono resettati il titolo e l'md5 * * Necessario per pulire un oggetto prima di un test di upgrade */ function run_opp_prepare_ddl_for_test($task, $args, $options) { static $loaded; // load application context if (!$loaded) { _loader(); } $dry_run = false; if (array_key_exists('dry-run', $options)) { $dry_run = true; } $con = Propel::getConnection(OppAttoPeer::DATABASE_NAME); echo "memory usage: " . memory_get_usage() . "\n"; $start_time = time(); try { $atti_rs = OppAttoPeer::getRSFromIDArray($args, $con); } catch (Exception $e) { throw new Exception("Specificare degli ID validi. \n" . $e); } $n_atti = $atti_rs->getRecordCount(); $cnt = 0; while ($atti_rs->next()) { $a = $atti_rs->getRow(); $atto_id = $a['id']; print "atto: {$atto_id}\n"; $atto = OppAttoPeer::retrieveByPK($atto_id); $atto->setTitolo(""); $atto->setMd5(""); $atto->save(); print " titolo e md5 annullat1\n"; # firme $items = $atto->getOppCaricaHasAttos(null, $con); $nitems = count($items); foreach ($items as $cnt => $item) { $item->delete($con); } print " {$nitems} firme rimosse\n"; # assegnazioni $items = $atto->getOppAttoHasSedes(null, $con); $nitems = count($items); foreach ($items as $cnt => $item) { $item->delete($con); } print " {$nitems} assegnazioni in commissione rimosse\n"; # tag (sf_tagging) $items = $atto->getTagsAsTaggingObjects(null, $con); $nitems = count($items); foreach ($items as $cnt => $item) { $item->delete($con); } print " {$nitems} tagging dell'atto rimossi\n"; # documenti $items = $atto->getOppDocumentos(null, $con); $nitems = count($items); foreach ($items as $cnt => $item) { $item->delete($con); } print " {$nitems} documenti allegati rimossi\n"; # iter $last_iter = $atto->getLastIter(); $last_iter->delete(); # relazioni $items = $atto->getRelazioni(); $nitems = count($items); foreach ($items as $cnt => $item) { $item->delete($con); } print " {$nitems} relazioni rimosse\n"; # esito sedute $items = $atto->getOppEsitoSedutas(); $nitems = count($items); foreach ($items as $cnt => $item) { $item->delete($con); } print " {$nitems} esiti rimossi\n"; # interventi $items = $atto->getOppInterventos(); $nitems = count($items); foreach ($items as $cnt => $item) { $item->delete($con); } print " {$nitems} interventi rimossi\n"; print "\n"; } $msg = sprintf("fine task\n"); echo pakeColor::colorize($msg, array('fg' => 'cyan', 'bold' => true)); $msg = sprintf(" [%4d sec] [%10d bytes]\n", time() - $start_time, memory_get_usage()); echo pakeColor::colorize($msg, array('fg' => 'red', 'bold' => false)); }
/** * Calcola o ri-calcola la rilevanza degli argomenti, come somma della rilevanza degli atti taggati * Si può specificare il una data fino alla quale calcolare la rilevanza * Se sono passati degli ID (argomenti), sono interpretati come ID di argomenti e il calcolo è fatto solo per loro */ function run_opp_build_cache_tags($task, $args, $options) { static $loaded; // load application context if (!$loaded) { _loader(); } echo "memory usage: " . memory_get_usage() . "\n"; $data = ''; $verbose = false; $offset = null; $limit = null; $start_time = time(); if (array_key_exists('data', $options)) { $data = $options['data']; } if (array_key_exists('verbose', $options)) { $verbose = true; } if (array_key_exists('offset', $options)) { $offset = $options['offset']; } if (array_key_exists('limit', $options)) { $limit = $options['limit']; } if ($data != '') { $legislatura_corrente = OppLegislaturaPeer::getCurrent($data); $data_lookup = $data; } else { $legislatura_corrente = OppLegislaturaPeer::getCurrent(); $data = date('Y-m-d'); $data_lookup = OppTagHistoryCachePeer::fetchLastData(); } $msg = sprintf("calcolo rilevanza tag - fino a: %10s\n", $data); echo pakeColor::colorize($msg, array('fg' => 'cyan', 'bold' => true)); if (count($args) > 0) { $tags_ids = $args; } else { $tags_ids = TaggingPeer::getActiveTagsIds('OppAtto'); } $n_tags = count($tags_ids); echo "memory usage: " . memory_get_usage() . "\n"; foreach ($tags_ids as $cnt => $tag_id) { printf("%5d/%6d) %40s %6d ... ", $cnt + 1, $n_tags, TagPeer::retrieveByPK($tag_id), $tag_id); $indice = OppIndiceRilevanzaPeer::calcola_rilevanza_tag($tag_id, $data, $verbose); // inserimento o aggiornamento del valore in opp_tag_history_cache $cache_record = OppTagHistoryCachePeer::retrieveByDataChiTipoChiId($data_lookup, 'S', $tag_id); if (!$cache_record) { $cache_record = new OppTagHistoryCache(); } $cache_record->setLegislatura($legislatura_corrente); $cache_record->setChiTipo('S'); $cache_record->setChiId($tag_id); $cache_record->setIndice($indice); $cache_record->setData($data); $cache_record->setUpdatedAt(date('Y-m-d H:i')); // forza riscrittura updated_at, per tenere traccia esecuzioni $cache_record->save(); unset($cache_record); $msg = sprintf("%7.2f", $indice); echo pakeColor::colorize($msg, array('fg' => 'cyan', 'bold' => true)); $msg = sprintf(" [%4d sec] [%10d bytes]\n", time() - $start_time, memory_get_usage()); echo pakeColor::colorize($msg, array('fg' => 'red', 'bold' => false)); } $msg = sprintf("%d tag elaborati\n", $cnt + 1); echo pakeColor::colorize($msg, array('fg' => 'cyan', 'bold' => true)); }
/** * Genera i files di testo degli atti * ATTO_ID_i.txt - i num progressivo del testo */ function run_stlab_genera_testi_atti($task, $args, $options) { static $loaded; // load application context if (!$loaded) { _loader(); } $files_path = sfConfig::get('sf_data_dir') . DIRECTORY_SEPARATOR . "stlab"; if (array_key_exists('files_path', $options)) { $files_path = strtolower($options['files_path']); } $verbose = false; $offset = null; $limit = null; $act_types = array(); if (array_key_exists('verbose', $options)) { $verbose = true; } if (array_key_exists('offset', $options)) { $offset = $options['offset']; } if (array_key_exists('limit', $options)) { $limit = $options['limit']; } if (array_key_exists('types', $options)) { $act_types = explode(",", $options['types']); } echo "memory usage: " . memory_get_usage() . "\n"; $start_time = time(); $msg = sprintf("generazione files dei testi degli atti\n"); echo pakeColor::colorize($msg, array('fg' => 'cyan', 'bold' => true)); // creazione del file di archivio $zip = new ZipArchive(); $zip_file_name = $files_path . DIRECTORY_SEPARATOR . "testi.zip"; if (!file_exists($zip_file_name)) { $res = $zip->open($zip_file_name, ZIPARCHIVE::CREATE); } else { $res = $zip->open($zip_file_name); } if ($res !== TRUE) { throw new Exception("Impossibile creare l'archivio testi.zip: " . $res); } // set dei limiti $c = new Criteria(); if (!is_null($limit)) { $c->setLimit($limit); } if (!is_null($offset)) { $c->setOffset($offset); } // estrazione documenti $c->addJoin(OppDocumentoPeer::ATTO_ID, OppAttoPeer::ID); if (count($act_types)) { $c->add(OppAttoPeer::TIPO_ATTO_ID, $act_types, Criteria::IN); } $docs = OppDocumentoPeer::doSelect($c); $n_docs = count($docs); foreach ($docs as $cnt => $doc) { printf("%5d/%5d: ", $c->getOffset() + $cnt + 1, $c->getOffset() + $n_docs); // definizione nome nell'archivio (attoID_docID) $file_name = $doc->getAttoId() . "_" . $doc->getId() . ".txt"; // aggiunta testo all'archivio $zip->addFromString($file_name, $doc->getTesto()); printf(" %d_%d ok (%d)\n", $doc->getAttoId(), $doc->getId(), memory_get_usage()); } // chiusura archivio e scrittura su file system $zip->close(); $msg = sprintf("%d atti elaborati\n", $cnt); echo pakeColor::colorize($msg, array('fg' => 'cyan', 'bold' => true)); $msg = sprintf(" [%4d sec] [%10d bytes]\n", time() - $start_time, memory_get_usage()); echo pakeColor::colorize($msg, array('fg' => 'red', 'bold' => false)); }
/** * Genera i files di testo degli atti * $prefix/$atto_id */ function run_nltk_genera_files($task, $args, $options) { static $loaded; // load application context if (!$loaded) { _loader(); } $files_path = sfConfig::get('sf_data_dir') . DIRECTORY_SEPARATOR . "nltk"; $act_types = array(); $prefix = null; // usato per differenziare training, test e dev, è aggiunto prima dell'id $offset = null; $limit = null; if (array_key_exists('offset', $options)) { $offset = $options['offset']; } if (array_key_exists('limit', $options)) { $limit = $options['limit']; } if (array_key_exists('path', $options)) { $files_path = strtolower($options['path']); } if (array_key_exists('types', $options)) { $act_types = explode(",", $options['types']); } if (array_key_exists('prefix', $options)) { $prefix = strtolower($options['prefix']); } if (count($args) > 0) { try { $atti_rs = OppAttoPeer::getRSFromIDArray($args); } catch (Exception $e) { throw new Exception("Specificare degli ID validi. \n" . $e); } } else { $atti_rs = OppAttoPeer::getAttiTipoRS($act_types, $offset, $limit); } echo "memory usage: " . memory_get_usage() . "\n"; $start_time = time(); $msg = sprintf("generazione files dei testi degli atti\n"); echo pakeColor::colorize($msg, array('fg' => 'cyan', 'bold' => true)); // loop principale $n_atti = $atti_rs->getRecordCount(); $cnt = 0; while ($atti_rs->next()) { $a = $atti_rs->getRow(); $atto_id = $a['id']; $tipo_atto_id = $a['tipo_atto_id']; $atto = OppAttoPeer::retrieveByPK($atto_id); if ($n_docs = $atto->countOppDocumentos()) { $docs = $atto->getOppDocumentos(); // path di tutti i testi dei doc relativi all'atto $atto_txt = ""; foreach ($docs as $doc) { $atto_txt .= $doc->getTesto(); } unset($docs); $fh = fopen($files_path . "/" . $atto_id, 'w'); fwrite($fh, $atto_txt); fclose($fh); } printf(" %d %d docs - ok (%d)\n", $atto_id, $n_docs, memory_get_usage()); unset($atto); $cnt++; } $msg = sprintf("%d atti elaborati\n", $cnt); echo pakeColor::colorize($msg, array('fg' => 'cyan', 'bold' => true)); $msg = sprintf(" [%4d sec] [%10d bytes]\n", time() - $start_time, memory_get_usage()); echo pakeColor::colorize($msg, array('fg' => 'red', 'bold' => false)); }
/** * Calcola e mostra lo storico dell'interesse dei politici su determinati argomenti * Si deve specificare gli ID dei politici e l'ID degli argomenti */ function run_opp_get_storico_interesse($task, $args, $options) { static $loaded; // load application context if (!$loaded) { _loader(); } echo "memory usage: " . memory_get_usage() . "\n"; $start_time = time(); // parameteri obbligatori if (array_key_exists('tags', $options)) { $tags = $options['tags']; $tags_ids = split(",", $tags); } else { throw new Exception("E' obbligatorio specificare i tags, ad esempio: 845,3487,123"); } // specificare la data fino alla quale estrarre lo storico $data = ''; if (array_key_exists('data', $options)) { // verifica che si tratti di una data (evita sql-injection) $data = date('Y-m-d', strtotime($options['data'])); $data_condition = " data < {$data} "; } else { $data_condition = null; } $msg = sprintf("calcolo storico interesse su tag: %s\n", $tags); echo pakeColor::colorize($msg, array('fg' => 'cyan', 'bold' => true)); if (count($args) > 0) { try { $parlamentari_rs = OppCaricaPeer::getRSFromIDArray($args); } catch (Exception $e) { throw new Exception("Specificare dei carica_id validi. \n" . $e); } } else { throw new Exception("Specificare almeno un carica_id valido. \n" . $e); } echo "memory usage: " . memory_get_usage() . "\n"; $cnt = 0; while ($parlamentari_rs->next()) { $cnt++; $p = $parlamentari_rs->getRow(); $msg = sprintf("%d %s %s\n", $cnt, $p['tipo_carica_id'] == '1' ? 'On.' : 'Sen.', ucfirst($p['nome']) . ' ' . strtoupper($p['cognome'])); echo pakeColor::colorize($msg, array('fg' => 'cyan', 'bold' => true)); $storico = OppCaricaPeer::getStoricoInteressePoliticoArgomenti($p['id'], $tags_ids, $data_condition); foreach ($storico as $key => $value) { $msg = sprintf("\t %s => %7.2f\n", $key, round($value, 2)); echo pakeColor::colorize($msg, array('fg' => 'cyan', 'bold' => true)); } } $msg = sprintf("%d parlamentari elaborati\n", $cnt); echo pakeColor::colorize($msg, array('fg' => 'cyan', 'bold' => true)); }
if ($dbEngine === 'mysql') { $dbEngine = 'mysqli'; } require HELPER . 'db_helper.' . $dbEngine . '.php'; if (db_host($lc_defaultDbSource) && db_user($lc_defaultDbSource) && db_name($lc_defaultDbSource)) { # Start DB connection db_connect($lc_defaultDbSource); } } _loader('session_helper', HELPER); _loader('i18n_helper', HELPER); _loader('validation_helper', HELPER); _loader('auth_helper', HELPER); _loader('pager_helper', HELPER); _loader('form_helper', HELPER); _loader('file_helper', HELPER); if (file_exists(INC . 'autoload.php')) { require INC . 'autoload.php'; } # Session helper (unloadable from /inc/autoload.php) if ($file = _i('helpers/session_helper.php', false)) { include $file; } if ($moduleSession = _readyloader('session_helper')) { require $moduleSession; __session_init(); } _unloader('session_helper', HELPER); # Translation helper (unloadable from /inc/autoload.php) if ($moduleI18n = _readyloader('i18n_helper')) { require $moduleI18n;