comparison search/search_function.php @ 0:b12c99b7c3f0

commit for previous development
author Zoe Hong <zhong@mpiwg-berlin.mpg.de>
date Mon, 19 Jan 2015 17:13:49 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:b12c99b7c3f0
1 <?php
2
3 include_once('../interface/Lib_mb_utf8.php');
4 include_once('../interface/config.php');
5
6 set_time_limit(0);
7 ini_set('memory_limit', '-1');
8
9 $link_mysql = mysql_connect($mysql_server, $mysql_user, $mysql_password);
10 mysql_query("SET NAMES utf8");
11
12 if (!$link_mysql) {
13 die('Could not connect: ' . mysql_error());
14 }
15
16 $db_selected = mysql_select_db($mysql_database, $link_mysql);
17 if (!$db_selected) {
18 die ('Can\'t use foo : ' . mysql_error());
19 }
20 function trimKeyword($keywords){
21 $token=strtok($keywords,",");
22 while($token!=false){
23 $token=trim($token);
24 $keywordArray[]=$token;
25 $token=strtok(",");
26 }
27 return $keywordArray;
28 }
29
30 function search($keywordArray){
31 $condition="";
32 foreach($keywordArray as $i=>$keyword){
33 if($i!=0){
34 $condition.=" OR ";
35 }
36 $condition.=" contents.content LIKE '%".$keyword."%' ";
37 }
38 //books.name AS book_name, books.level1, books.level2, books.period, contents.books_id, contents.line, contents.content
39 $query="SELECT books.level1 AS LEVEL1, books.level2 AS LEVEL2,
40 books.name AS Name, books.period AS PERIOD,
41 books.start_year AS 'TimeSpan:begin', books.end_year AS 'TimeSpan:end',
42 books.id AS BOOK_ID, contents.line AS PAGE, contents.content AS CONTENT,
43 info.volume AS VOLUME, info.author AS AUTHOR, info.edition AS EDITION
44 FROM contents
45 JOIN books ON contents.books_id = books.id
46 JOIN books_info info ON contents.books_id=info.books_id
47 WHERE ".$condition."
48 ORDER BY contents.books_id, contents.line";
49 //WHERE contents.content LIKE '%".$keyword."%'
50 $result = mysql_query($query);
51 //echo "result length: ".mysql_num_rows($result)."<br>";
52 while ($row = mysql_fetch_assoc($result)) { //Find the section(s) where the page belongs to
53 //$resultArray[$i]=$row;
54 $subQuery="SELECT id, name, start_page, end_page
55 FROM sections
56 WHERE books_id=".$row['BOOK_ID']." AND start_page<=".$row['PAGE']." AND end_page>=".$row['PAGE'];
57 $subResult=mysql_query($subQuery);
58 $sectionArray=array();
59 while($subRow=mysql_fetch_assoc($subResult)){
60 $sectionArray[]=$subRow;
61 }
62 $row['SECTION']=$sectionArray;
63 $resultArray[]=$row;
64 }
65 //echo "array length: ".sizeof($resultArray)."<br>";
66 return $resultArray;
67 }
68 $NO_TAG=0;
69 $LOCUST_TEMPLE=1;
70
71 function printTable($array,$keywordArray,$filename,$tag){ //print HTML
72 global $NO_TAG, $LOCUST_TEMPLE;
73 $header='<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
74 <html>
75 <head>
76 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
77 <link href="../search.css" type="text/css" rel="stylesheet"/>
78 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
79 <script src="../search.js" charset="utf-8"></script>
80 </head>
81 <body>';
82 $keywordStr="";
83 foreach($keywordArray as $i=>$keyword){
84 if($i!=0){
85 $keywordStr.=", ";
86 }
87 $keywordStr.=$keyword;
88 }
89
90
91 $fp=fopen("search_results/".$filename.".html","w");
92 fwrite($fp,$header);
93 //$str="<a href='http://".$_SERVER['HTTP_HOST']."/map/map.php?mode=1&file=".$filename.".csv&name=".$keywordStr."' target='_blank'>view the distribution on the map</a><br><br>";
94 $str="<a href='http://".$_SERVER['HTTP_HOST']."/map/get_coordinates_for_listed_books.php?file=".$filename.".csv&name=".$keywordStr."' target='_blank'>view the distribution on the map</a><br><br>";
95 fwrite($fp,$str);
96 $str.="<a href='http://".$_SERVER['HTTP_HOST']."/search/search_results/".$filename.".html' target='_blank'>html version</a><br><br>";
97 echo $str;
98 echo sizeof($array)." result(s) of \"".$keywordStr."\" ";
99 fwrite($fp,sizeof($array)." result(s) of \"".$keywordStr."\"<br>");
100 $str="<table>";
101 $str.="<tr>";
102 $str.="<td class='sequence'>#<td class='bookId'>book id<td class='bookName'>book name<td class='level1'>level1<td class='level2'>level2<td class='period'>period<td class='sectionName'>section info<td class='page'>page<td class='content'>content";
103 fwrite($fp,$str);
104 echo $str;
105 $i=1;
106 foreach($array as $row){
107 $str="<tr>";
108 $str.="<td>".$i;
109 $str.="<td>".$row['BOOK_ID'];
110 $str.="<td>".$row['Name'];
111 $str.="<td>".$row['LEVEL1'];
112 $str.="<td>".$row['LEVEL2'];
113 $str.="<td>".$row['PERIOD'];
114 $str.="<td>";
115 fwrite($fp,$str);
116 echo $str;
117 foreach($row['SECTION'] as $section){
118 $str="<div class='section'><a href='/interface/tagging_text.php?id=".$section['id']."' target='_blank'>".$section['name']."</a>p".$section['start_page']."-".$section['end_page']."</div>";
119 echo $str;
120 $str="<div class='section'><a href='/interface/tagging_text.php?id=".$section['id']."' target='_blank'>".$section['name']."</a>p".$section['start_page']."-".$section['end_page']."</div>";
121 fwrite($fp,$str);
122 }
123 $str="<td>".$row['PAGE'];
124 if($tag==$NO_TAG){
125 $str.="<td>".$row['CONTENT'];
126 }else if($tag==$LOCUST_TEMPLE){
127 $str.="<td>".findLocustTempleDescription($row['CONTENT'],$keywordArray,0);
128 }
129 fwrite($fp,$str);
130 echo $str;
131 $i++;
132 }
133
134 echo "</table>";
135 fwrite($fp,"</table></body></html>");
136 fclose($fp);
137 }
138
139 function findLocustTempleDescription($str,$keywordArray,$findUnknownTemple){
140 $result="";
141 $pattern[0]="/(.*)(";
142 foreach($keywordArray as $i=>$keyword){
143 $pattern[0].=$keyword;
144 if($i!=sizeof($keywordArray)-1){
145 $pattern[0].="|";
146 }
147 }
148 $pattern[0].=")(.*)/u";
149 //0=whole,1=text,2=locust temple,3=description
150 $pattern[1]="/(.*\s+)(\S{1,5}[廟寺祠])(.*)/u";
151 //0=whole,1=descripption,2=unknown temple,3=text
152 $success=preg_match($pattern[$findUnknownTemple],$str,$match); //note that preg_match only matches the last match!
153 if($findUnknownTemple==0){
154 if($success==1){//find string "locust temple"
155 $result=findLocustTempleDescription($match[1],$keywordArray,0);
156 $result.="<keyword>".$match[2]."</keyword>";
157 $result.=findLocustTempleDescription($match[3],$keywordArray,1);
158 return $result;
159 }else{//no string "locust temple"
160 return "<irrelevant>".$str."</irrelevant>";
161 }
162 }else{
163 if($success==1){//find the pattern "locust temple......unknown temple"
164 $result=findLocustTempleDescription($match[1],$keywordArray,1);
165 $result.="<irrelevant>".$match[2].$match[3]."</irrelevant>";
166 //$result.="<keyword>".$match[2]."</keyword>".$match[3];
167 return $result;
168 }else{//can't find the pattern
169 return $str;
170 }
171
172 }
173 }
174
175 function writeCsvFile($array,$fileName){
176 $columnNameArray=['BOOK_ID','LEVEL1','LEVEL2',
177 'Name','PERIOD','TimeSpan:begin','TimeSpan:end','PAGE','SECTION','CONTENT',
178 'Description'];
179 $fp=fopen("./csv_files/".$fileName.".csv","w");
180 fputcsv($fp,$columnNameArray);
181 foreach($array as $row){
182 $book=array();
183 $row['Description']='';
184 foreach($columnNameArray as $column){
185 $book[$column]=$row[$column];
186 }
187 $book['Name']="(".$row['PERIOD'].") ".$row['Name'];
188 $row['AUTHOR']=str_replace("(","(",$row['AUTHOR']);
189 $row['AUTHOR']=str_replace(")",") ",$row['AUTHOR']);
190 $book['Description']=$row['VOLUME']." ╱ ".$row['AUTHOR']." ╱ ".$row['EDITION'];
191 $book['SECTION']='';
192 foreach($row['SECTION'] as $idx=>$section){
193 $book['SECTION'].=$section['name']." ".$section['start_page']."-".$section['end_page'];
194 if($idx!=sizeof($row['SECTION'])){
195 $book['SECTION'].=" ╱ ";
196 }
197 }
198 fputcsv($fp,$book);
199 }
200 fclose($fp);
201 }
202
203 ?>
204