PageRenderTime 75ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/src/php/subsets/read.php

https://bitbucket.org/silverasm/wordseer
PHP | 482 lines | 418 code | 27 blank | 37 comment | 34 complexity | afd9044c3ab54a6b60e507d27c1e1344 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-3.0, BSD-3-Clause
  1. <?php
  2. /* Copyright 2012 Aditi Muralidharan. See the file "LICENSE" for the full license governing this code. */
  3. /** read.php
  4. Called by subsets.js in service of all the main .php pages.
  5. Returns the contents of subsets
  6. And lists all the subsets made by a user
  7. **/
  8. /** Returns the contents of the collection with the given ID
  9. @param {Number} subsetID The ID of the collection to list.
  10. @return {Array} The contents of the subject, an associative array with the
  11. following fields:
  12. - date: The creation date of the collection
  13. - text: The name of the collection
  14. - type: The type of the collection
  15. - id: The id of the collection
  16. - words: If this is a collection of type 'word', a space-separated list of
  17. the words in this collection
  18. - ids: If this is not a collection of words, an array of arrays listing the
  19. item ID's in the collection. The list has the format [{id: <id>}, ...].
  20. */
  21. function listSubsetContents($subsetID){
  22. $contents = array();
  23. $sql = "SELECT * from working_set WHERE id = $subsetID;";
  24. $result = mysql_query($sql);
  25. $row = mysql_fetch_assoc($result);
  26. $type = $row['type'];
  27. $name = $row['name'];
  28. $date = $row['date'];
  29. $contents['text'] = $name;
  30. $contents['id'] = $subsetID;
  31. $contents['date'] = $date;
  32. $contents['type'] = $type;
  33. if($type == 'word'){// Read contents of a word set
  34. $sql = "SELECT DISTINCT word
  35. FROM working_set_contents, word
  36. WHERE working_set_id = $subsetID
  37. AND item_table_name = 'word'
  38. AND word.id = item_id;";
  39. $result = mysql_query($sql) or die("
  40. read.php l.23<br>
  41. <b>A fatal MySQL error occured</b>.
  42. <br/> Query: " . $sql . "
  43. <br/> Error: (" . mysql_errno() . ") " . mysql_error());
  44. $words = '';
  45. while($row = mysql_fetch_assoc($result)){
  46. $words = $words.' '.$row['word'];
  47. }
  48. $contents['words'] = $words;
  49. }
  50. else {
  51. $sql = "SELECT DISTINCT `item_id`
  52. FROM `working_set_contents`
  53. WHERE `working_set_id` = $subsetID
  54. AND `item_table_name`='".mysql_real_escape_string($type)."'";
  55. $result = mysql_query($sql) or die("
  56. read.php l.37<br/>
  57. <b>A fatal MySQL error occured</b>.
  58. <br/> Query: " . $sql . "
  59. <br/> Error: (" . mysql_errno() . ") " . mysql_error());
  60. $unitIDs = array();
  61. while($row = mysql_fetch_assoc($result)){
  62. array_push($unitIDs, array("id"=>$row['item_id']));
  63. }
  64. $contents['ids'] = $unitIDs;
  65. }
  66. return $contents;
  67. }
  68. function getAllChildIDs($id) {
  69. $ids = array();
  70. $sql = "SELECT id from working_set where parent = $id;";
  71. $result = mysql_query($sql);
  72. while ($row = mysql_fetch_assoc($result)) {
  73. array_push($ids, $row['id']);
  74. $grandchildren = getAllChildIDs($row['id']);
  75. foreach ($grandchildren as $grandchild) {
  76. array_push($ids, $grandchild);
  77. }
  78. }
  79. return $ids;
  80. }
  81. function getAllParentIDs($id) {
  82. $ids = array();
  83. $sql = "SELECT parent from working_set where id = $id;";
  84. $result = mysql_query($sql);
  85. while ($row = mysql_fetch_assoc($result)) {
  86. array_push($ids, $row['parent']);
  87. $grandchildren = getAllParentIDs($row['parent']);
  88. foreach ($grandchildren as $grandchild) {
  89. array_push($ids, $grandchild);
  90. }
  91. }
  92. return $ids;
  93. }
  94. function addHighlightContents($subsetID){
  95. $contents = array();
  96. $sql = "SELECT highlight.document_id, start_index, end_index, end, start, full, title, date, item_id from working_set_contents, highlight, document, author_xref_document as axn, author
  97. WHERE item_table_name = 'highlight'
  98. AND highlight.id = item_id
  99. AND axn.author_id = author.id
  100. AND axn.document_id = document.id
  101. AND highlight.document_id = document.id
  102. AND working_set_id = ".$subsetID.";";
  103. $result = mysql_query($sql) or die("<b>A fatal MySQL error occured</b>.
  104. <br/> Query: " . $sql . "
  105. <br/> Error: (" . mysql_errno() . ") " . mysql_error());
  106. while($row = mysql_fetch_array($result)){
  107. //item metadata
  108. $highlightInfo = array();
  109. $highlightInfo['id'] = $row['item_id'];
  110. $highlightInfo['text'] = getHighlightText($row['item_id'],
  111. $row['start'],
  112. $row['start_index'],
  113. $row['end'],
  114. $row['end_index']);
  115. $highlightInfo['type'] = "highlight";
  116. // item notes and tags
  117. $highlightInfo['notes'] = getHighlightNotes($row['item_id']);
  118. $highlightInfo['tags'] = getHighlightTags($row['item_id']);
  119. // document metadata
  120. $highlightInfo['title'] = $row['title'];
  121. $highlightInfo['date'] = $row['date'];
  122. $highlightInfo['author'] = $row['full'];
  123. $highlightInfo['document_id'] = $row['document_id'];
  124. array_push($contents, $highlightInfo);
  125. }
  126. return $contents;
  127. }
  128. function getHighlightText($highlightID, $start, $startIndex, $end, $endIndex){
  129. $sql = "SELECT surface FROM sentence_xref_word
  130. WHERE (sentence_id > ".$start." AND sentence_id < ".$end.")
  131. OR (sentence_id = ".$start." AND position >= ".$startIndex.")
  132. OR (sentence_id = ".$end." AND position <= ".$endIndex.")
  133. ORDER BY sentence_id asc, position asc;";
  134. $result = mysql_query($sql) or die("<b>A fatal MySQL error occured</b>.
  135. <br/> Query: " . $sql . "
  136. <br/> Error: (" . mysql_errno() . ") " . mysql_error());
  137. $text = "";
  138. while($row = mysql_fetch_array($result)){
  139. if(!strstr(".,?!@#$%^&*:()_llre-+t='\"`s", $row['surface']) && !(substr($row['surface'], 0, 1) === "'") && !strstr($row['surface'], "'")){
  140. $text = $text.' '; // don't put spaces in front of punctuation
  141. }
  142. $text = $text.replaceWeirdCharacters($row['surface']);
  143. }
  144. return $text;
  145. }
  146. function addDocumentContents($subsetID){
  147. $contents = array();
  148. $sql = "SELECT full, title, date, item_id
  149. FROM working_set_contents,
  150. document,
  151. author_xref_document as axn,
  152. author
  153. WHERE item_table_name = 'document'
  154. AND document.id = item_id
  155. AND axn.document_id = document.id
  156. AND axn.author_id = author.id
  157. AND working_set_id = ".$subsetID.";";
  158. $result = mysql_query($sql) or die("<b>A fatal MySQL error occured</b>.
  159. <br/> Query: " . $sql . "
  160. <br/> Error: (" . mysql_errno() . ") " . mysql_error());
  161. while($row = mysql_fetch_array($result)){
  162. // item info
  163. $documentInfo['id'] = $row['item_id'];
  164. $documentInfo['type'] = "document";
  165. // notes and tags
  166. $documentInfo['notes'] = getItemNotes($row['item_id'], "document");
  167. $documentInfo['tags'] = getItemTags($row['item_id'], "document");
  168. // document metadata
  169. $documentInfo['title'] = $row['title'];
  170. $documentInfo['date'] = $row['date'];
  171. $documentInfo['author'] = $row['full'];
  172. $documentInfo['document_id'] = $row['item_id'];
  173. array_push($contents, $documentInfo);
  174. }
  175. return $contents;
  176. }
  177. function addSentenceContents($subsetID){
  178. $contents = array();
  179. $sql = "SELECT sentence.document_id, sentence, full, title, date, item_id
  180. FROM
  181. working_set_contents,
  182. sentence,
  183. document,
  184. author_xref_document as axn,
  185. author
  186. WHERE item_table_name = 'sentence'
  187. AND sentence.id = item_id
  188. AND sentence.document_id = document.id
  189. AND axn.document_id = document.id
  190. AND axn.author_id = author.id
  191. AND working_set_id = ".$subsetID.";";
  192. $result = mysql_query($sql) or die("<b>A fatal MySQL error occured</b>.
  193. <br/> Query: " . $sql . "
  194. <br/> Error: (" . mysql_errno() . ") " . mysql_error());
  195. while($row = mysql_fetch_array($result)){
  196. //item metadata
  197. $sentenceInfo = array();
  198. $sentenceInfo['id'] = $row['item_id'];
  199. $sentenceInfo['sentence'] = $row['sentence'];
  200. $sentenceInfo['type'] = "sentence";
  201. // item notes and tags
  202. $sentenceInfo['notes'] = getItemNotes($row['item_id'], "sentence");
  203. $sentenceInfo['tags'] = getItemTags($row['item_id'], "sentence");
  204. // document metadata
  205. $sentenceInfo['title'] = $row['title'];
  206. $sentenceInfo['date'] = $row['date'];
  207. $sentenceInfo['author'] = $row['full'];
  208. $sentenceInfo['document_id'] = $row['document_id'];
  209. array_push($contents, $sentenceInfo);
  210. }
  211. return $contents;
  212. }
  213. function addWordContents($subsetID, $contents){
  214. $contents = array();
  215. $sql = "SELECT word, pos, item_id
  216. FROM
  217. working_set_contents,
  218. word
  219. WHERE item_table_name = 'word'
  220. AND word.id = item_id
  221. AND working_set_id = ".$subsetID.";";
  222. $result = mysql_query($sql) or die("<b>A fatal MySQL error occured</b>.
  223. <br/> Query: " . $sql . "
  224. <br/> Error: (" . mysql_errno() . ") " . mysql_error());
  225. while($row = mysql_fetch_array($result)){
  226. //item metadata
  227. $wordInfo = array();
  228. $wordInfo['id'] = $row['item_id'];
  229. $wordInfo['word'] = $row['word'];
  230. $wordInfo['pos'] = $row['pos'];
  231. $wordInfo['type'] = "word";
  232. // item notes and tags
  233. $wordInfo['notes'] = getItemNotes($row['item_id'], "word");
  234. $wordInfo['tags'] = getItemTags($row['item_id'], "word");
  235. array_push($contents, $wordInfo);
  236. }
  237. return $contents;
  238. }
  239. function getHighlightNotes($id){
  240. $sql = "SELECT * from highlight_xref_note, note
  241. WHERE note.id = note_id
  242. AND highlight_id = ".$id.";";
  243. $result = mysql_query($sql) or die("<b>A fatal MySQL error occured</b>.
  244. <br/> Query: " . $sql . "
  245. <br/> Error: (" . mysql_errno() . ") " . mysql_error());
  246. return extractNotes($result);
  247. }
  248. function getItemNotes($id, $tableName){
  249. $sql = "SELECT * from item_notes_and_tags, note
  250. WHERE annotation_table_name = 'note'
  251. AND note.id = annotation_id
  252. AND item_table_name = '".$tableName."'
  253. AND item_id = ".$id.";";
  254. $result = mysql_query($sql) or die("<b>A fatal MySQL error occured</b>.
  255. <br/> Query: " . $sql . "
  256. <br/> Error: (" . mysql_errno() . ") " . mysql_error());
  257. return extractNotes($result);
  258. }
  259. function extractNotes($result){
  260. $notes = array();
  261. while($row = mysql_fetch_array($result)){
  262. $notesInfo = array();
  263. $notesInfo['text'] = $row['text'];
  264. $notesInfo['user'] = $row['user'];
  265. $notesInfo['id'] = $row['id'];
  266. $notesInfo['date'] = $row['date'];
  267. array_push($notes, $notesInfo);
  268. }
  269. return $notes;
  270. }
  271. function getHighlightTags($id){
  272. $sql = "SELECT * from highlight_xref_tag, tag
  273. WHERE tag.id = tag_id
  274. AND highlight_id = ".$id.";";
  275. $result = mysql_query($sql) or die("<b>A fatal MySQL error occured</b>.
  276. <br/> Query: " . $sql . "
  277. <br/> Error: (" . mysql_errno() . ") " . mysql_error());
  278. return extractTags($result);
  279. }
  280. function getItemTags($id, $tableName){
  281. $sql = "SELECT * from item_notes_and_tags, tag
  282. WHERE annotation_table_name = 'tag'
  283. AND tag.id = annotation_id
  284. AND item_table_name = '".$tableName."'
  285. AND item_id = ".$id.";";
  286. $result = mysql_query($sql) or die("<b>A fatal MySQL error occured</b>.
  287. <br/> Query: " . $sql . "
  288. <br/> Error: (" . mysql_errno() . ") " . mysql_error());
  289. return extractTags($result);
  290. }
  291. function extractTags($result){
  292. $tags = array();
  293. while($row = mysql_fetch_array($result)){
  294. $tagsInfo = array();
  295. $tagsInfo['tag'] = $row['name'];
  296. $tagsInfo['user'] = $row['user'];
  297. $tagsInfo['id'] = $row['id'];
  298. array_push($tags, $tagsInfo);
  299. }
  300. return $tags;
  301. }
  302. function getCollectionID($collectionName){
  303. $q = "SELECT id from working_set where name = '".mysql_escape_string($collectionName)."';";
  304. $result = mysql_query($q) or die("<b>A fatal MySQL error occured</b>.
  305. <br/> Query: " . $q . "
  306. <br/> Error: (" . mysql_errno() . ") " . mysql_error());
  307. if(mysql_num_rows($result) > 0){
  308. $row = mysql_fetch_array($result);
  309. return $row['id'];
  310. }else{
  311. return -1;
  312. }
  313. }
  314. function getDocumentIDsInCollection($collectionID){
  315. $type = getCollectionType($collectionID);
  316. $property_name = "";
  317. if ($type == "word") {
  318. $property_name = "word_set";
  319. } else if ($type == "document") {
  320. $property_name = "document_set";
  321. } if ($type == "sentence") {
  322. $property_name = "sentence_set";
  323. }
  324. $sql = "SELECT distinct document_id
  325. FROM metadata
  326. WHERE property_name = '$property_name'
  327. AND value = '$collectionID';";
  328. $result = mysql_query($sql) or die(mysql_error()."
  329. <br> on query $sql
  330. <br> at subsets/read.php line 316");
  331. $document_ids = array();
  332. while ($row = mysql_fetch_array($result)) {
  333. array_push($document_ids, $row['document_id']);
  334. }
  335. return $document_ids;
  336. }
  337. function getCollectionType($collectionID) {
  338. $sql = "SELECT * from working_set where id = $collectionID";
  339. $result = mysql_query($sql) or die ("SQL error
  340. <br>".mysql_error()."
  341. <br> on read.php l. 318
  342. <br> on query:
  343. <br> ".$sql);
  344. $row = mysql_fetch_assoc($result);
  345. return $row['type'];
  346. }
  347. /** Returns an array of the sentence ID's that are contained in this collection,
  348. both indirectly (through their parent units) and directly (in the case of
  349. sentence collections). Consults the GET parameter document_id to determine
  350. whether the results should be restricted to a particular document.
  351. @return {Array} A list of sentence ID's in this collection.
  352. */
  353. function getSentenceIDsInCollection($collectionID) {
  354. $table_identifier = 'filtered_sent_ids';
  355. $insertion_fields = '(id, document_id)';
  356. $field_identifier = "DISTINCT unit_id, document_id ";
  357. $query_id_where = '';
  358. global $num_filter_conditions;
  359. global $cache_results;
  360. global $query_id;
  361. if ($cache_results || $query_id) {
  362. $table_identifier = 'cached_filtered_sent_ids';
  363. $insertion_fields = '(id, document_id, query_id)';
  364. $field_identifier = "DISTINCT unit_id, document_id, $query_id";
  365. $query_id_where = " AND query_id = $query_id ";
  366. }
  367. if (!$query_id || $cache_results) {
  368. $type = getCollectionType($collectionID);
  369. $property_name = "";
  370. if ($type == "word") {
  371. $property_name = "word_set";
  372. } else if ($type == "document") {
  373. $property_name = "document_set";
  374. } if ($type == "sentence") {
  375. $property_name = "sentence_set";
  376. }
  377. $sql = "";
  378. if ($property_name == "word_set" ||
  379. $property_name == "sentence_set") {
  380. $sql = "INSERT IGNORE into $table_identifier $insertion_fields
  381. SELECT $field_identifier from metadata
  382. WHERE property_name = '$property_name'
  383. AND value = '$collectionID';";
  384. } else {
  385. $field_identifier = str_replace("unit_id", "sentence.id",
  386. $field_identifier);
  387. $field_identifier = str_replace("document_id",
  388. "sentence.document_id",
  389. $field_identifier);
  390. $sql = "INSERT IGNORE into $table_identifier $insertion_fields
  391. SELECT $field_identifier
  392. FROM metadata, sentence
  393. WHERE property_name = 'document_set'
  394. AND value = '$collectionID'
  395. AND sentence.document_id = unit_id;";
  396. }
  397. $result = mysql_query($sql) or die(mysql_error()."
  398. <br> on query $sql
  399. <br> at subsets/read.php line 316");
  400. $num_filter_conditions += 1;
  401. updateSentenceFilterTable();
  402. }
  403. if (!cache_results) {
  404. $sql = "SELECT * from $table_identifier $query_id_where;";
  405. $result = mysql_query($sql);
  406. while ($row = mysql_fetch_assoc($result)) {
  407. array_push($sentence_ids, $row['id']);
  408. }
  409. }
  410. return $sentence_ids;
  411. }
  412. function listCollections($username, $type, $parent){
  413. $sql = "SELECT id, name, date FROM working_set
  414. WHERE parent = '$parent'
  415. AND type = '$type'
  416. AND username = '$username';";
  417. $result = mysql_query($sql) or die("
  418. read.php line 304 <br>
  419. <b>A fatal MySQL error occured</b>.
  420. <br/> Query: " . $sql . "
  421. <br/> Error: (" . mysql_errno() . ") " . mysql_error());
  422. $subsets = array();
  423. while($row = mysql_fetch_array($result)){
  424. $subsetInfo = listSubsetContents($row['id']);
  425. $subsetInfo['children'] = listCollections($username, $type, $row['id']);
  426. array_push($subsets, $subsetInfo);
  427. }
  428. return $subsets;
  429. }
  430. function listCollectionsFlat($username, $type){
  431. $sql = 'SELECT id, name, date FROM working_set
  432. WHERE type = "'.$type.'"
  433. AND username = "'.$username.'"
  434. ORDER BY name asc;';
  435. $result = mysql_query($sql) or die("
  436. read.php line 304 <br>
  437. <b>A fatal MySQL error occured</b>.
  438. <br/> Query: " . $sql . "
  439. <br/> Error: (" . mysql_errno() . ") " . mysql_error());
  440. $subsets = array();
  441. if($type == 'document'){
  442. $subsetInfo = array();
  443. $subsetInfo['text'] = "all";
  444. $subsetInfo['date'] = "";
  445. $subsetInfo['id'] = 0;
  446. array_push($subsets, $subsetInfo);
  447. }
  448. while($row = mysql_fetch_array($result)){
  449. $subsetInfo = listSubsetContents($row['id']);
  450. array_push($subsets, $subsetInfo);
  451. }
  452. return $subsets;
  453. }
  454. ?>