Skip to content

Instantly share code, notes, and snippets.

@juzna
Created March 26, 2012 10:52
Show Gist options
  • Select an option

  • Save juzna/2204421 to your computer and use it in GitHub Desktop.

Select an option

Save juzna/2204421 to your computer and use it in GitHub Desktop.
UIR-ADR update
<?php
/**
* copy&paste'd from a system so it won't work by itself, need some fixes
*/
function akce_adr_update() {
@ob_end_flush(); ob_implicit_flush(true);
echo '<pre>';
$url = "http://forms.mpsv.cz/uir/view.jsp?D=Verze_42";
$br = new browser;
if(!$data = $br->request($url)) return ajax_ret(0, 'Nepodarilo se otevrit URL');
if(!$data = strcut($data, '<TABLE ', '</TABLE>')) return ajax_ret(0, 'Nenalezena tabulka s vypisem souboru');
if(!preg_match_all('|<TR>(.+)</TR>|Usm', $data, $match)) return ajax_ret(0, 'Nepodarilo se nalezt radky tabulky');
// Remove first three rows
$rows = $match[1];
array_splice($rows, 0, 3);
array_pop($rows); // And last one
$rows = array_reverse($rows);
// Read rows
$updates = 0;
foreach($rows as $k => $row) {
if(!preg_match('|<TD.+<a href\s*="(?<file>[^"]+)">.*</TD>\s*<TD.*>\s*(?<date>[0-9.]+)\s*</TD>\s*<TD.*>\s*(?<time>[0-9:]+)\s*</TD>|Usmi', $row, $match)) {
echo "Unable to parse row $k\n";
continue;
}
if(!startsWith($match['file'], '../uir/')) {
echo "Necekany odkaz - {$match['file']}\n";
continue;
}
$verze = (int) substr($match['file'], -9, 5);
$file = 'http://forms.mpsv.cz/' . substr($match['file'], 3);
list($d, $m, $y) = explode('.', $match['date']);
$date = date('Y-m-d H:i:s', strtotime("$y-$m-$d {$match['time']}"));
// Kontrola zda uz je v nasi databazi
$row2 = mfo(q("select * from `verze` where `ver_cislo`='$verze'", 'adresy'));
if($row2) {
if($row2->cas_uzav) continue; // Uz mame ulozenou a uzavrenou
else {
$this->adr_update($file); // Updatujeme
$updates++;
}
}
else {
// Not in DB, try previous version
$v2 = $verze - 1;
if(!mr("select count(*) from `verze` where `ver_cislo`='$v2'", 'adresy')) {
echo "Verze $verze neleze naimportovat, v databazi nam chybi $v2\n";
}
else {
$this->adr_update($file); // Updatujeme
$updates++;
}
}
}
// Final msg
if($updates) echo "<b>Bylo provedeno celkem $updates updatu</b>\n";
else echo "<b>Dneska zadne updaty</b>\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment