0)$cshost="host=$dbhost"; $csname=""; if(strlen($dbname)>0)$csname="dbname=$dbname"; $csuser=""; if(strlen($dbuser)>0)$csuser="user=$dbuser"; $cspass=""; if(strlen($dbpass)>0)$cspass="password=$dbpass"; $con = pg_connect("host=$dbhost dbname=$dbname user=$dbuser password=$dbpass"); /* roman numerals */ $roman_numerals = array ( "1"=>"i", "2"=>"ii", "3"=>"iii", "4"=>"iv", "5"=>"v", "6"=>"vi", "7"=>"vii", "8"=>"viii", "9"=>"ix", "10"=>"x", "11"=>"xi", "12"=>"xii", "13"=>"xiii", "14"=>"xiv", "15"=>"xv", "16"=>"xvi", "17"=>"xvii", "18"=>"xviii", "19"=>"xix", "20"=>"xx" ); /* get the meta items for the output page * including; * - dynamic index template */ $qry="select x_current, dynamicindex, pagesize from html;"; $res=pg_exec($con,$qry); if(pg_numrows($res)>0){ /* retrieve the first (and only?) row only */ $row=pg_fetch_array($res,0); /* the template file to fill out */ $templ_file=$row["dynamicindex"]; $text_color=$row["current"]; $page_size=strlen(trim($row["pagesize"])); } else{ /* There is no html table information, * Now would be a good time to panic. */ echo "No HTML data table access"; exit; } $results=""; $indexterm="Unknown"; /* Fold case for the eid parameter and flag when no value for eid is passed * For testing, if there is no entity id provided use the royal society * of victoria: A000804 */ if(!empty($EID))$eid=$EID; $eid_empty_flag=false; if(empty($eid)){ $eid_empty_flag=true; //$eid="A000804"; $eid="UNKNOWN"; } /* force entity identifier to upper case */ $eid=strtoupper($eid); /* get index concept heading for given eid */ $qry="select display, indexterm from tblindexterms where eid='$eid';"; $res=pg_exec($con,$qry); $num_rows = pg_numrows($res); if($num_rows > 0){ /* fetch the first row */ $row=pg_fetch_array($res,0); /* Attempt to use the display field, defaulting to indexterm if the display field has not been used. */ $tmp=trim($row["display"]); if(strlen($tmp)<1){ $tmp=trim($row["indexterm"]); } $indexterm=$tmp; } else{ /* Could not find the entity within the indexed terms */ $indexterm="UNKNOWN"; } /* each entry in the generated index will have the following * structure; * * tblindexterms.display|indexterm * Chapter # - chaptertitle * sectiontitle, [(subdisplay|subindexterm)] pg * */ /* Query for Postgresql ver. 7.1 */ $qry="select distinct on (ty.display, tp.webpageno, tx.indterm) ty.display, "; $qry.="ty.subdisplay, ty.indexterm, ty.subindexterm, "; $qry.="tx.indterm, tc.chapter, tc.chaptertitle, "; $qry.="tp.sectitle, tp.webpageno, tp.paraid from tblchapter as tc, "; $qry.="tblpara as tp, (select tblindex.it_id as indterm, parafrom as p1, "; $qry.="parato as p2 from tblindex where tblindex.it_id IN "; $qry.="(select DISTINCT a.it_id from tblindexterms as a "; $qry.="where eid='$eid' or subeid='$eid')) as tx, "; $qry.="tblindexterms as ty where tc.chapter=tp.chapter and "; $qry.="tx.indterm=ty.it_id and tp.paraid = tx.p1 "; $qry.="order by ty.display, tp.webpageno, tx.indterm;"; /* get the index contents */ $res=pg_exec($con,$qry); $num_rows = pg_numrows($res); /* set initial values for results * processing variables */ $last_term=""; // the last index term seen $cur_term=""; // the curent index term $last_chap=""; // the last chapter seen $cur_chap=""; // the current chapter $last_sec=""; // the last section seen $cur_sec=""; // the current section $str=""; /* for each index reference */ for($i=0;$i<$num_rows;$i++){ /* fetch next index reference */ $row=pg_fetch_array($res,$i); /* Set current index term: * pick indexterms.display in preference * to indexterms.indexterms, otherwise use * nonbreaking whitespace as current index term */ if(strlen($row["display"])>0){ $cur_term=$row["display"]; } elseif(strlen(trim($row["indexterm"]))>0){ $cur_term=$row["indexterm"]; } else{ $cur_term=" "; } /* use first (verbose) term as _the_ term NB - now explicitly querying for correct display term to avoid using a subterm as the indexterm. if(!strcmp($indexterm, "Unknown") && strcmp($cur_term, " ") ){ $indexterm=$cur_term; } */ /* Set current chapter, fallback * to non breaking whitespace */ if(strlen(trim($row["chapter"]))>0){ $cur_chap=$row["chapter"]; if($cur_chap < 0){ $cur_chap=" "; } } else{ $cur_chap=" "; } /* set chapter title if available, * otherwise set to empty string */ if(strlen(trim($row["chaptertitle"]))>0){ $chapter_title = $row["chaptertitle"]; } else{ $chapter_title = ""; } /* set section title if available */ if(strlen(trim($row["sectitle"]))>0){ $cur_sec=$row["sectitle"]; } else{ $cur_sec=" "; } /* test for a change in terms, chapters or sections */ $new_sec=false; // flag start of new section $new_chap=false; // flag start of new chapter $new_term=false; // flag start of new term // Problem: // New section detection must go further than // comparing text of this section with the last // section title as the case where two chapters // in which no section titles exist in rowset // if(strcmp($cur_sec, $last_sec)){ $last_sec=$cur_sec; $new_sec=true; } // Solution: // A new chapter and term imply a new section // if(strcmp($cur_chap, $last_chap)){ $last_chap=$cur_chap; $new_chap=true; $new_sec=true; } if(strcmp($cur_term, $last_term)){ $last_term=$cur_term; $new_term=true; $new_sec=true; } /* closing off last definition list only if * this is not the first iteration */ if($i > 0){ /* close section off */ if($new_sec || $new_chap || $new_term){ $str.="\n"; } /* close chapter off */ if($new_chap || $new_term){ $str.="\n"; } /* close term off */ if($new_term){ $str.=""; } } /* starting new defintion list parts */ /* start of new term */ if($new_term){ $str.="