'blue.png', 'enc_yes' => 'blue_plus.png', 'enc_no' => 'blue_minus.png', 'dec_perc' => 'red.png', 'dec_yes' => 'red_plus.png', 'dec_no' => 'red_minus.png' ); define('IMG_HEIGHT', '10'); define('IMG_MAX_WIDTH', '50'); function shorten_url($url, $maxlen = 40, $target='_blank') { if (strlen($url) > $maxlen) { $n1 = floor(($maxlen-3)*(3/4)); $n2 = floor(($maxlen-3)*(1/4)); $x = substr($url, 0, $n1).'...'.substr($url, -1, $n2); } else { $x = $url; } $r = ''; return $r; } class OMDB { var $dbconn; var $notes; var $notecount; var $notesrmap; function OMDB() { } function Query($sql) { if (!isset($this->dbconn)) { $this->dbconn = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS); mysql_select_db(MYSQL_DB, $this->dbconn); } if (OMDB_DEBUG) echo "
SQL: $sql

"; return mysql_query($sql, $this->dbconn); } /* * Straightforward (foot)notes */ function ResetNotes() { $this->notes = ''; $this->notecount = 0; $this->notesrmap = array(); } function AddNote($txt) { if ($txt == '') return false; if (!($n = @$this->notesrmap[$txt])) { $n = ++$this->notecount; $this->notesrmap[$txt] = $n; $this->notes .= $n.'. '.$txt.'
'; } return $n; } function AddNoteURL($url) { if ($url == '') return false; if (!($n = @$this->notesrmap[$url])) { $n = ++$this->notecount; $this->notesrmap[$url] = $n; $this->notes .= $n.'. '.shorten_url($url).'
'; } return $n; } /* * Getting data */ function GetQueryIntoArray($qry) { $res = $this->Query($qry); $r = array(); while ($row = mysql_fetch_array($res)) array_push($r, $row); return $r; } function GetCDs() { return $this->GetQueryIntoArray('SELECT * FROM om_cds ORDER BY LOWER(cd_name);'); } function GetCD($cd_id) { $t = $this->GetQueryIntoArray('SELECT * FROM om_cds WHERE cd_id='.((int) $cd_id)); if (sizeof($t) == 0) return false; return $t[0]; } function GetDefs($cd_id) { return $this->GetQueryIntoArray('SELECT * FROM om_defs WHERE cd_id='.((int) $cd_id).' ORDER BY LOWER(def_name)'); } function GetCass($cd_id = -1) { if ($cd_id == -1) { return $this->GetQueryIntoArray('SELECT * FROM om_cass ORDER BY LOWER(cas_name)'); } else { return $this->GetQueryIntoArray('SELECT c.*, cc.refurl, cc.demourl FROM om_cass c INNER JOIN om_cd_cas cc ON c.cas_id = cc.cas_id WHERE cd_id = '.((int) $cd_id).' ORDER BY LOWER(cas_name)'); } } function CreateCasEmptyAndFirstRow(&$empty_row, &$first_row, &$cass_col_index, $cd_id = -1) { $cass = $this->GetCass($cd_id); $empty_row = array(''); $first_row = array(''); $cass_col_index = array(); foreach($cass as $cas) { array_push($first_row, array('cas_name' => $cas['cas_name'], 'cas_id' => $cas['cas_id'], 'refurl' => stripslashes(@$cas['refurl']), 'demourl' => stripslashes(@$cas['demourl']) )); array_push($empty_row, ''); $cass_col_index[$cas['cas_id']] = sizeof($empty_row) - 1; } } function GetCDsWithSupport() { //Since MySQL has no crosstab capabilities, we DIO (Do It Ourselves). $this->CreateCasEmptyAndFirstRow($empty_row, $first_row, $cass_col_index); $res = $this->Query(' select c.cd_name, c.cd_id, cc.cas_id, cc.cnt_tot, cc.cnt_sup_enc, cc.cnt_sup_dec from om_cds c left join om_cd_cas_with_count cc on c.cd_id = cc.cd_id ORDER BY LOWER(cd_name), cd_id; '); $rettable = array($first_row); $rw = $empty_row; $cd_id = -1; while ($row = mysql_fetch_array($res)) { if ($cd_id == -1 || $cd_id != $row['cd_id']) { if ($cd_id != -1) array_push($rettable, $rw); $rw = $empty_row; $rw[0] = array('cd_name' => $row['cd_name'], 'cd_id' => $row['cd_id'] ); $cd_id = $row['cd_id']; } if ($row['cnt_tot'] != 0) { $rw[$cass_col_index[$row['cas_id']]] = $row; } } array_push($rettable, $rw); return $rettable; } function GetDefsWithSupport($cd_id) { //Again, we do the crosstab ourselves. $this->CreateCasEmptyAndFirstRow($empty_row, $first_row, $cass_col_index, $cd_id); $res = $this->Query(' select d.def_id, d.def_name, cc.cas_id, dc.support_enc, dc.support_dec, dc.note from om_defs d left join om_cd_cas cc on d.cd_id = cc.cd_id left join om_def_cas dc on d.def_id = dc.def_id and cc.cas_id = dc.cas_id where d.cd_id='.((int) $cd_id).' order by lower(def_name); '); $rettable = array($first_row); $rw = $empty_row; $def_id = -1; while ($row = mysql_fetch_array($res)) { if ($def_id == -1 || $def_id != $row['def_id']) { if ($def_id != -1) array_push($rettable, $rw); $rw = $empty_row; $rw[0] = array( 'def_name' => $row['def_name'], 'def_id' => $row['def_id'] ); $def_id = $row['def_id']; } if ($row['cas_id'] == '') continue; //This is the no-support-at-all case $rw[$cass_col_index[$row['cas_id']]] = array('enc' => $row['support_enc'], 'dec' => $row['support_dec'], 'note' => @$row['note']); } array_push($rettable, $rw); return $rettable; } /* * Getting formatting data */ function PrintSupportImgs($tot, $enc, $dec) { global $IMGS_SUP; $r = ''; $tot = (int) $tot; if ($tot == 0) return ''; $fracs = array('enc' => round(100*($enc / $tot)), 'dec' => round(100*($dec / $tot))); foreach($fracs as $m => $v) { $r .= ''.$v.'%'; $r .= $v.'%'; $r .= '
'; } return $r; } function PrintCDsWithSupport($show_admin = IS_ADMIN) { $data = $this->GetCdsWithSupport(); $r = ''."\n"; foreach($data as $i => $rw) { $r .= ' $c) { if ($i != 0 && $j == 0) { //First column, print link $cd_id = (int) $c['cd_id']; $r .= ''; } else if ($i == 0) { //First row, print names of CASs $r .= ""; } else if ($c == '') { $r .= ''; } else if ($c != '') { //Other columns, is support fraction -> draw some sort of graph $r .= ''; } } if ($show_admin && $i != 0) $r .= ''; $r .= ''."\n"; } $r .= '
'.$c['cd_name'].'".(is_array($c) ? $c['cas_name'] : $c)."'; $r .= $this->PrintSupportImgs($c['cnt_tot'], $c['cnt_sup_enc'], $c['cnt_sup_dec']); $r .= 'Add CAS...
'."\n"; return $r; } function PrintDefsWithSupport($cd_id, $showadmin = false) { global $IMGS_SUP; $data = $this->GetDefsWithSupport($cd_id); $cd = $this->GetCD($cd_id); $firstrow = $data[0]; $this->ResetNotes(); $r = ''; if ($showadmin) $r .= '
'."\n"; $r .= ''."\n"; foreach($data as $i => $rw) { $r .= ' $c) { if ($i == 0 && $j == 0) { $r .= '"; } else if ($j == 0) { //First column, just name $r .= ""; $def_id = $c['def_id']; } else if ($i == 0) { //First row, just name + desc $r .= '"; } else if ($c == '') { $r .= ''; } else if ($c != '') { //Other columns, is support fraction -> draw plusses and minusses $r .= ''; } } $r .= ''."\n"; } $r .= '
'.$cd['cd_name']."".$c['def_name']."
'.$c['cas_name']; /* ref url */ if ($showadmin) { $r .= ''; $r .= '
r: '; } else { if (is_array($c) && isset($c['refurl'])) { $n = $this->AddNoteURL($c['refurl']); if ($n) $r .= ''.$n.''; } } $r .= '
'; /* demo url */ if ($showadmin) { $r .= ''; $r .= '
r: '; } else { if (is_array($c) && isset($c['demourl']) && $c['demourl'] != '') { $r .= ''; } } $r .= '
enc/dec
'; $r .= "
'; foreach ($c as $m => $v) { if ($m == 'note') { if ($showadmin) { $v = stripslashes($v); $r .= '
'; $r .= ''; $r .= ''; } $n = $this->AddNote($v); if ($n) $r .= ''.$n.''; } else { if ($showadmin) { $r .= ''; } else { $r .= ''; } } $r .= ' '; } $r .= '
'."\n"; if ($showadmin) { $r .= ''; $r .= '
'."\n"; } return $r; } function PrintEncDecNote() { return '
'. 'Encoding is from OpenMath to the CAS;
'. 'Decoding is from the CAS to OpenMath.'. '
'; } function PrintNotes() { $r = ''; if ($this->notes != '') { $r .= '
References / Notes:
'.$this->notes.'
'; } return $r; } /* * Inserting new data */ function CreateOrGetCDID($name, $desc) { $name = addslashes($name); $desc = addslashes($desc); $res = $this->Query('SELECT cd_id FROM om_cds WHERE cd_name=\''.$name.'\';'); if ($row = mysql_fetch_array($res)) { return $row['cd_id']; } $res = $this->Query('INSERT INTO om_cds(cd_name, description) VALUES(\''.$name.'\',\''.$desc.'\');'); $id = mysql_insert_id($this->dbconn); if ($id == 0) die('insert_id of om_cds is zero...'); return $id; } function RemoveDef($def_id) { $res = $this->Query('DELETE FROM om_defs WHERE def_id='.((int) $def_id)); return ( mysql_affected_rows($this->dbconn) == 1 ); } function AddDef($cd_id, $def_name) { $res = $this->Query('INSERT INTO om_defs(cd_id, def_name) VALUES('.((int) $cd_id).',\''.addslashes($def_name).'\');'); $id = mysql_insert_id($this->dbconn); if ($id == 0) die('insert_id of om_defs is zero...'); return $id; } function SetDefs($cd_id, $newnames) { sort($newnames); $retids = array(); //fetch existing definitions $oldrows = $this->GetDefs($cd_id); //traverse the two arrays simultaneously $i = $j = 0; while ($i < sizeof($newnames) && $j < sizeof($oldrows)) { $x = strtolower($newnames[$i]); $y = strtolower($oldrows[$j]['def_name']); if ($x == $y) { ++$i; ++$j; } else if ($x < $y) { $id = $this->AddDef($cd_id, $newnames[$i]); $retids[$newnames[$i]] = $id; ++$i; } else if ($x > $y) { $this->RemoveDef($oldrows[$j]['def_id']); ++$j; } else { die('This should never happen'); } } while ($j < sizeof($oldrows)) { $this->RemoveDef($oldrows[$j]['def_id']); ++$j; } while ($i < sizeof($newnames)) { $id = $this->AddDef($cd_id, $newnames[$i]); $retids[$newnames[$i]] = $id; ++$i; } return $retids; } function AddCDCas($cd_id, $cas_id, $std_enc, $std_dec, $refurl = '') { $refurl = trim($refurl); if ($refurl != '' && !preg_match('#^[a-z]+\://.*#', $refurl)) $refurl = 'http://'.$refurl; $refurl = addslashes($refurl); $res = $this->Query('SELECT om_cd_cas_id FROM om_cd_cas WHERE cd_id='.((int) $cd_id).' AND cas_id='.((int) $cas_id)); if ($row = mysql_fetch_array($res)) { $id = $row['om_cd_cas_id']; } else { $this->Query('INSERT INTO om_cd_cas(cd_id, cas_id, refurl) VALUES('.((int) $cd_id).','.((int) $cas_id).',\''.$refurl.'\')'); $id = mysql_insert_id($this->dbconn); } if ($id == 0) die('insert_id of om_cd_cas is zero...'); if ($std_enc || $std_dec) { $this->RemvDefsCas($cd_id, $cas_id); $this->Query('INSERT INTO om_def_cas(def_id, cas_id, support_enc, support_dec) SELECT def_id, '.((int) $cas_id).', '.((int) $std_enc).', '.((int) $std_dec).' FROM om_defs WHERE cd_id = '.((int) $cd_id).' ;'); } return $id; } function RemvCDCas($cd_id, $cas_id) { $b = $this->RemvDefsCas($cd_id, $cas_id); $this->Query('DELETE FROM om_cd_cas WHERE cas_id='.((int) $cas_id).' AND cd_id='.((int) $cd_id).';'); return ( $b && (mysql_affected_rows($this->dbconn) != 0) ); } function RemvDefsCas($cd_id, $cas_id) { $res = $this->Query('DELETE FROM om_def_cas WHERE cas_id='.((int) $cas_id).' AND def_id IN ( SELECT def_id FROM om_defs WHERE cd_id='.((int) $cd_id).');'); return (mysql_affected_rows($this->dbconn) != 0); } function SetCDCasSupport($cd_id, $cas_id, $support_enc = false, $support_dec = false) { foreach( array('support_enc', 'support_dec') as $var ) $$var = ($$var ? 1 : 0); $cas_id = (int) $cas_id; $cd_id = (int) $cd_id; $this->AddCDCas($cd_id, $cas_id); $this->Query(' DELETE FROM om_def_cas WHERE def_id IN (SELECT def_id FROM om_defs WHERE cd_id='.$cd_id.') '); $this->Query(' INSERT INTO om_def_cas(def_id, cas_id, support_enc, support_dec) SELECT def_id, '.$cas_id.', '.$support_enc.','.$support_dec.' FROM om_defs WHERE cd_id='.$cd_id.' '); } function SetDefCasSupport($def_id, $cas_id, $support_enc = false, $support_dec = false) { foreach( array('support_enc', 'support_dec') as $var ) $$var = ($$var ? 1 : 0); $def_id = (int) $def_id; $cas_id = (int) $cas_id; $res = $this->Query('UPDATE om_def_cas SET support_enc='.$support_enc.', support_dec='.$support_dec.' WHERE def_id='.$def_id.' and cas_id='.$cas_id); if (mysql_affected_rows($this->dbconn) > 0) return true; $res = $this->Query('INSERT INTO om_def_cas(def_id, cas_id, support_enc, support_dec) VALUES('.$def_id.','.$cas_id.','.$support_enc.','.$support_dec.')'); if (mysql_affected_rows($this->dbconn) > 0) return true; return false; } function AddDefCasSupport($def_id, $cas_id, $which) { $def_id = (int) $def_id; $cas_id = (int) $cas_id; if ($which != 'enc' && $which != 'dec') return false; $res = $this->Query('UPDATE om_def_cas SET support_'.$which.'=1 WHERE def_id='.$def_id.' and cas_id='.$cas_id); if (mysql_affected_rows($this->dbconn) > 0) return true; $res = $this->Query('INSERT INTO om_def_cas(def_id, cas_id, support_'.$which.') VALUES('.$def_id.','.$cas_id.',1)'); return (mysql_affected_rows($this->dbconn) > 0); } function RemvDefCasSupport($def_id, $cas_id, $which) { $def_id = (int) $def_id; $cas_id = (int) $cas_id; if ($which != 'enc' && $which != 'dec') return false; $res = $this->Query('UPDATE om_def_cas SET support_'.$which.'=0 WHERE def_id='.$def_id.' and cas_id='.$cas_id); return (mysql_affected_rows($this->dbconn) > 0) ; } function SetDefCasNote($def_id, $cas_id, $note) { $note = addslashes($note); $def_id = (int) $def_id; $cas_id = (int) $cas_id; $res = $this->Query('UPDATE om_def_cas SET note=\''.$note.'\' WHERE def_id='.$def_id.' and cas_id='.$cas_id); return (mysql_affected_rows($this->dbconn) > 0); } function SetCDCasURL($cd_id, $cas_id, $tp, $v) { $v = addslashes($v); $cd_id = (int) $cd_id; $cas_id = (int) $cas_id; if (!in_array($tp, array('demourl', 'refurl'))) die('Foei!!!'); $res = $this->Query('UPDATE om_cd_cas SET '.$tp.'=\''.$v.'\' WHERE cd_id='.$cd_id.' and cas_id='.$cas_id); return (mysql_affected_rows($this->dbconn) > 0); } /* * Print Stuff for Admin */ function PrintAddCasForm($cd_id) { $CD = $this->GetCD($cd_id); $r = "

Add support for ".($CD['cd_name'])."

\n"; $r .= "
\n"; $r .= "
\n"; // CAS $cass = $this->GetQueryIntoArray(' select c.*, ccc.cd_id, ccc.cnt_tot, ccc.cnt_sup_enc, ccc.cnt_sup_dec from om_cass c left join (select * from om_cd_cas_with_count where cd_id = '.((int) $cd_id).') as ccc on c.cas_id = ccc.cas_id order by cas_name; '); $r .= " CAS
\n"; foreach ($cass as $cas) { $supported = ($cas['cnt_tot'] != ''); $r .= " \n"; } $r .= "
"; $r .= $cas['cas_name']; $r .= ""; //$r .= ''; $r .= ''; $r .= ""; if ($supported) { $r .= $this->PrintSupportImgs($cas['cnt_tot'], $cas['cnt_sup_enc'], $cas['cnt_sup_dec']); } $r .= "
\n"; $r .= "
\n"; // SUPPORT $r .= " Support (default for new)
\n"; foreach (array( 'enc' => 'Support encoding', 'dec' => 'Support decoding') as $s => $t) { $r .= " \n"; } $r .= "
$t


\n"; // REFERENCE URLS $r .= " Reference URL (optional)
\n"; $r .= " \n"; $r .= '
'; $r .= ''; $r .= '
'."\n"; return $r; } function PrintClickPath($ar) { $delim = ' » '; $r = '
'; foreach ($ar as $k => $v) { if ($v != '') $r .= ''; $r .= $k; if ($v != '') $r .= ''; $r .= $delim; } $r = substr($r, 0, strlen($r) - strlen($delim)); $r .= '
'; return $r; } } ?>