PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/src/php/grammaticalsearch/get-search-results.php

https://bitbucket.org/silverasm/wordseer
PHP | 689 lines | 666 code | 9 blank | 14 comment | 35 complexity | 8cf9a18150cf5c80e53837813bd91662 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. include_once "../util.php";
  4. include_once '../document/get-metadata.php';
  5. //Query parameters
  6. $wordseer_instance = getGetParam('instance');
  7. $gov = '';
  8. $dep = '';
  9. $relation = '';
  10. $govtype = 'word';
  11. $deptype = 'word';
  12. $searches = decodeGetJson('search');
  13. if (count($searches) <= 1) {
  14. $gov = getGetParam('gov');
  15. $govtype = getGetParam('govtype');
  16. $dep = getGetParam('dep');
  17. $deptype = getGetParam('deptype');
  18. $relation = getGetParam('relation');
  19. }
  20. $collection = getGetParam('collection');
  21. $statistics = getGetParam('statistics');
  22. $phrasess = decodeGetJson('phrases');
  23. $metadata = decodeGetJson('metadata');
  24. $timing = getGetParam('timing');
  25. $path = '../../../instances/'.$wordseer_instance.'/config.php';
  26. include_once $path;
  27. include_once '../subsets/read.php';
  28. // A variable that controls whether the results of searching (as opposed to
  29. // earlier steps like metadata filtering) are cached. Useful in cases such as
  30. // the column vis or the word frequencies where there can be multiple searches
  31. // over the same base filters.
  32. global $dont_cache_search_results;
  33. $dont_cache_search_results = false;
  34. if (strstr($_SERVER['REQUEST_URI'], 'get-search-results.php')) {
  35. dispatch($gov, $govtype, $dep, $deptype,
  36. $relation, $collection, $statistics, $metadata, $phrases);
  37. }
  38. function dispatch($gov, $govtype, $dep, $deptype, $relation,
  39. $collection, $statistics, $metadata, $phrases){
  40. global $wordseer_instance;
  41. if($wordseer_instance){
  42. $timing = getGetParam('timing');
  43. $t1 = time();
  44. if($relation == ""){
  45. if(!$statistics){
  46. $results = getSearchResults($gov, $govtype, $collection,
  47. $metadata, $phrases);
  48. }else{
  49. $results = array('statistics'=>array(), 'sentences'=>array());
  50. }
  51. }else{
  52. $results = getDependencySearchResults($gov, $govtype, $dep,
  53. $deptype, $relation, $collection, $statistics, $metadata, $phrases);
  54. }
  55. $t2 = time();
  56. if ($timing) {
  57. echo "
  58. Total: ".($t2 - $t1)."s<br><br>
  59. ";
  60. }
  61. echo json_encode($results);
  62. $t3 = time();
  63. if ($timing) {
  64. echo '<br><br> Time to render JSON:'.($t3-$t2).'s<br>';
  65. }
  66. }
  67. }
  68. function getSearchResults($gov, $govtype, $collection, $metadata, $phrases) {
  69. $timing = getGetParam('timing');
  70. $all = true;
  71. // Get the ID's of the sentences that match the query, along with the match
  72. // positions.
  73. $results = getSentenceSearchResults($gov, $govtype,
  74. $collection, $metadata, $phrases);
  75. $sentenceIDs = array();
  76. $sentenceInfo = array();
  77. $sentences = array();
  78. $total = 0;
  79. if ($results != 'all') {
  80. $all = false;
  81. $sql = 'SELECT FOUND_ROWS() as total;';
  82. $r = mysql_fetch_assoc(mysql_query($sql));
  83. $total = $r['total'];
  84. while($row = mysql_fetch_assoc($results)){
  85. $sentence_id = $row['sentence_id'];
  86. array_push($sentenceIDs, $sentence_id);
  87. if (!array_key_exists($sentence_id, $sentenceInfo)) {
  88. $sentenceInfo[$sentence_id] = array(
  89. 'id'=> $sentence_id,
  90. 'document_id'=> $row['document_id'],
  91. 'sentence'=>array(
  92. 'words'=>array(),
  93. 'dep_index'=> -1,
  94. 'gov_index'=> array(),
  95. )
  96. );
  97. }
  98. array_push(
  99. $sentenceInfo[$sentence_id]['sentence']['gov_index'],
  100. $row['position']);
  101. }
  102. $sentence_id_string = join(", ", $sentenceIDs);
  103. // Get the sentence details: words and metadata.
  104. if(getGetParam("onlyMetadata") != "true"){
  105. $sentenceInfo = populateSentenceInfo($sentence_id_string, $sentenceInfo);
  106. foreach ($sentenceIDs as $sentence_id) {
  107. array_push($sentences, $sentenceInfo[$sentence_id]);
  108. }
  109. }
  110. }
  111. $metadata = array();
  112. if (getGetParam("onlyMetadata") == "true") {
  113. $metadata = getMetadataTreeFromSentenceIDs($sentenceIDs, $all);
  114. }
  115. $data = array(
  116. 'statistics'=>array(),
  117. 'sentences'=>$sentences,
  118. 'total'=>$total,
  119. 'metadata'=>$metadata
  120. );
  121. return $data;
  122. }
  123. function getSentenceSearchResults($gov, $govtype, $collection, $metadata,
  124. $phrases) {
  125. $timing = getGetParam('timing');
  126. global $num_filter_conditions;
  127. global $num_search_conditions;
  128. if ($timing) {
  129. echo "<br>Starting search filter with $num_filter_conditions
  130. conditions <br>";
  131. }
  132. $table_identifier = 'filtered_sent_ids';
  133. $insertion_fields = '(id, document_id, num_matched, num_searches_matched)';
  134. $field_identifier = "DISTINCT id, document_id, 0, 1";
  135. $destination_table = $table_identifier;
  136. $query_id_where = '';
  137. global $cache_results;
  138. global $query_id;
  139. global $dont_cache_search_results;
  140. if (($cache_results || $query_id) && !$dont_cache_search_results) {
  141. $table_identifier = 'cached_filtered_sent_ids';
  142. $insertion_fields =
  143. '(id, document_id, query_id, num_matched, num_searches_matched)';
  144. $field_identifier = "DISTINCT id, document_id, $query_id, 0, 1";
  145. $query_id_where = " AND query_id = $query_id ";
  146. }
  147. // Apply metadata, collection and phrase filters, and get the list of
  148. //sentence ids that match those filters.
  149. $filter_clause_active = false;
  150. $filtered_sentence_ids = getSentenceIDsForFilters($metadata,
  151. $collection, $phrases); // in get-metadata.php
  152. if ($filtered_sentence_ids != "all") {
  153. $filter_clause_active = true;
  154. if ($dont_cache_search_results && $query_id) {
  155. $num_filter_conditions += 1;
  156. // transfer the permanent cached results into an in-memory
  157. // table before proceeding to add the filtered sentences.
  158. $sql = "INSERT INTO filtered_sent_ids (id, document_id, num_matched)
  159. SELECT id, document_id, $num_filter_conditions
  160. FROM cached_filtered_sent_ids
  161. WHERE query_id = $query_id";
  162. mysql_query($sql) or die (mysql_error()."
  163. On <br> $sql <br> get-search-result.php l.146");
  164. }
  165. }
  166. global $searches;
  167. $search_clause_active = false;
  168. $word_where_clause = "";
  169. if ($gov || count($searches) > 0) {
  170. $search_clause_active = true;
  171. // Get the word id's corresponding to the query terms.
  172. $word_id_string = "";
  173. $exact_phrase = false;
  174. if ((strstr($gov, " ") || strstr($gov, "+")) && !strstr($gov, ", ")) {
  175. $exact_phrase = true;
  176. }
  177. if ($govtype == 'word') {
  178. $word_id_string = wordIDList($gov);
  179. } else if ($govtype == 'word-set') {
  180. $word_id_string = getWordIDsFromWordSet($gov);
  181. }
  182. if ($exact_phrase) {
  183. if (!(strstr($gov, "\"")|| strstr($gov, "+"))) {
  184. $query = "\"$gov\"";
  185. } else {
  186. $query = $gov;
  187. }
  188. $sentence_where_clause =
  189. " match(sentence) AGAINST('$query' IN BOOLEAN MODE) ";
  190. }
  191. if (strlen($word_id_string) > 0) {
  192. $word_where_clause = " word_id in ($word_id_string) ";
  193. } else {
  194. $word_where_clause = " word_id in (-1) ";
  195. }
  196. global $num_search_conditions;
  197. $num_search_conditions += 1;
  198. if (!$query_id || $cache_results || $dont_cache_search_results) {
  199. // Filter the sentence ID's stored in the temporary table
  200. // filtered_sent_ids to keep only the ID's that match this search.
  201. if ($exact_phrase) {
  202. $sql = "INSERT INTO $table_identifier $insertion_fields
  203. SELECT $field_identifier
  204. from sentence WHERE $sentence_where_clause
  205. ON DUPLICATE KEY
  206. UPDATE num_searches_matched = num_searches_matched +1 ;";
  207. } else {
  208. $field_identifier = str_replace(' id', ' sentence_id', $field_identifier);
  209. $sql = "INSERT INTO $table_identifier $insertion_fields
  210. SELECT $field_identifier
  211. from sentence_xref_word WHERE
  212. $word_where_clause
  213. ON DUPLICATE KEY
  214. UPDATE num_searches_matched = num_searches_matched + 1;";
  215. }
  216. if ($timing) {
  217. echo $sql."
  218. <br> With $num_filter_conditions conditions <br>";
  219. }
  220. mysql_query($sql) or die (mysql_error()."
  221. On <br> $sql <br> get-search-result.php l.143");
  222. if ($dont_cache_search_results && $query_id) {
  223. updateTemporarySentenceFilterTable();
  224. } else {
  225. updateSentenceFilterTable();
  226. }
  227. }
  228. }
  229. if (!$cache_results) {
  230. if ($search_clause_active || $filter_clause_active) {
  231. $t1 = time();
  232. $sql = "";
  233. if ($search_clause_active) {
  234. $sql = "SELECT
  235. SQL_CALC_FOUND_ROWS
  236. DISTINCT sentence_id, sentence_xref_word.document_id, position
  237. FROM sentence_xref_word, $table_identifier
  238. WHERE sentence_id = $table_identifier.id
  239. AND $word_where_clause
  240. $query_id_where
  241. LIMIT 1000;";
  242. } else if ($filter_clause_active) {
  243. $sql = "SELECT SQL_CALC_FOUND_ROWS
  244. DISTINCT id as sentence_id, -1 as position, document_id
  245. FROM $table_identifier
  246. WHERE true
  247. $query_id_where
  248. LIMIT 1000;";
  249. }
  250. $results = mysql_query($sql) or die(mysql_error()."
  251. <br> On query
  252. <br> $sql
  253. <br> at get-search-results.php line 98");
  254. $t2 = time();
  255. if ($timing) {
  256. echo "Time to get search results: ".($t2 - $t1)."s<br><br>";
  257. }
  258. return $results;
  259. } else {
  260. return "all";
  261. }
  262. }
  263. }
  264. function getDependencySentenceResults ($g, $gt, $d, $dt, $r,
  265. $collection, $stats, $metadata, $phrases) {
  266. $search_clause_active = false;
  267. $relation = relationshipIDList($r);
  268. global $searches;
  269. if ($g != '' || $d != '' || strlen($relation) != 0 || count($searches) > 0) {
  270. $search_clause_active = true;
  271. if ($gt == "word") {
  272. $gov = wordIDList($g);
  273. } else if($gt == 'word-set'){
  274. $gov = getWordIDsFromWordSet($g);
  275. }
  276. if ($dt == "word") {
  277. $dep = wordIDList($d);
  278. } else if($dt == 'word-set'){
  279. $dep = getWordIDsFromWordSet($d);
  280. }
  281. }
  282. if ($search_clause_active) {
  283. $table_identifier = 'filtered_sent_ids';
  284. $insertion_fields = '(id, document_id)';
  285. $field_identifier = "DISTINCT sentence_id, document_id ";
  286. $query_id_where = '';
  287. global $cache_results;
  288. global $num_filter_conditions;
  289. global $query_id;
  290. global $dont_cache_search_results;
  291. if ($cache_results || $query_id && !$dont_cache_search_results) {
  292. $table_identifier = 'cached_filtered_sent_ids';
  293. $insertion_fields = '(id, document_id, query_id)';
  294. $field_identifier = "DISTINCT sentence_id, document_id, $query_id";
  295. $query_id_where = " AND query_id = $query_id ";
  296. }
  297. $withinSentence = false;
  298. $within = "";
  299. $filtered_sentence_ids = getSentenceIDsForFilters($metadata,
  300. $collection, $phrases);
  301. $filter_clause_active = ($filtered_sentence_ids != "all");
  302. if ($filter_clause_active) {
  303. array_push($filtered_sentence_ids, "-2");
  304. $withinSentence = "table";
  305. $within = $table_identifier;
  306. if ($dont_cache_search_results && $query_id) {
  307. // transfer the permanent cached results into an in-memory
  308. // table before proceeding to add the filtered sentences.
  309. $num_filter_conditions += 1;
  310. $sql = "INSERT INTO filtered_sent_ids
  311. (id, document_id, num_matched)
  312. SELECT id, document_id, $num_filter_conditions
  313. FROM cached_filtered_sent_ids
  314. WHERE query_id = $query_id";
  315. mysql_query($sql) or die (mysql_error()."
  316. On <br> $sql <br> get-search-result.php l.184");
  317. }
  318. }
  319. $withinDocument = false;
  320. $dependency_id_result = getDependencyIDs($gov,
  321. $dep,
  322. $relation,
  323. $withinDocument,
  324. $withinSentence,
  325. $within,
  326. $start,
  327. $limit);
  328. global $cache_results;
  329. global $query_id;
  330. if (!$cache_results) {
  331. mysql_data_seek($dependency_id_result, 0);
  332. return $dependency_id_result;
  333. }
  334. } else {
  335. return 'all';
  336. }
  337. }
  338. function getDependencySearchResults($g, $gt, $d, $dt, $r, $collection, $stats,
  339. $metadata, $phrases){
  340. global $timing;
  341. if ($gt == "word") {
  342. $gov = wordIDList($g);
  343. } else if($gt == 'word-set'){
  344. $gov = getWordIDsFromWordSet($g);
  345. }
  346. if ($dt == "word") {
  347. $dep = wordIDList($d);
  348. } else if($dt == 'word-set'){
  349. $dep = getWordIDsFromWordSet($d);
  350. }
  351. if ($timing != 0) {
  352. echo "<br> dep: $d <br>";
  353. }
  354. $relation = relationshipIDList($r);
  355. $withinSentence = false;
  356. $within = "";
  357. $filtered_sentence_ids = getSentenceIDsForFilters($metadata,
  358. $collection, $phrases);
  359. if($filtered_sentence_ids != "all"){
  360. array_push($filtered_sentence_ids, "-2");
  361. $withinSentence = true;
  362. $within = join(", ", $filtered_sentence_ids);
  363. }
  364. $withinDocument = false;
  365. $statistics = array();
  366. $total = 0;
  367. $sentenceIDs = array();
  368. $sentenceInfo = array();
  369. $sentences = array();
  370. if ($stats) { // Used by the bar chart module to get counts.
  371. $statistics = getStatistics($gov,
  372. $dep,
  373. $relation,
  374. $withinDocument,
  375. $withinSentence,
  376. $within);
  377. $total = $statistics['gov']['value'];
  378. } else { // Used by the search modules.
  379. $t1 = time();
  380. $dependency_id_result = getDependencyIDs($gov,
  381. $dep,
  382. $relation,
  383. $withinDocument,
  384. $withinSentence,
  385. $within,
  386. $start,
  387. $limit);
  388. $sql = 'SELECT FOUND_ROWS() as total;';
  389. $r = mysql_fetch_assoc(mysql_query($sql));
  390. $total = $r['total'];
  391. while ($row = mysql_fetch_assoc($dependency_id_result)) {
  392. $sentence_id = $row['sentence_id'];
  393. array_push($sentenceIDs, $sentence_id);
  394. if (!array_key_exists($sentence_id, $sentenceInfo)) {
  395. $sentenceInfo[$sentence_id] = array(
  396. 'id'=> $sentence_id,
  397. 'document_id'=> $row['document_id'],
  398. 'sentence'=>array(
  399. 'words'=>array(),
  400. 'dep_index'=> -1,
  401. 'gov_index'=> array(),
  402. )
  403. );
  404. }
  405. array_push(
  406. $sentenceInfo[$sentence_id]['sentence']['gov_index'],
  407. $row['gov_index']);
  408. $sentenceInfo[$sentence_id]['sentence']['dep_index'] =
  409. $row['dep_index'];
  410. }
  411. $sentence_id_string = join(", ", $sentenceIDs);
  412. $sentenceInfo = populateSentenceInfo($sentence_id_string, $sentenceInfo);
  413. foreach ($sentenceIDs as $id) {
  414. array_push($sentences, $sentenceInfo[$id]);
  415. }
  416. $t2 = time();
  417. //echo "Time to get dependency search results: ".($t2-$t1)."s<br><br>";
  418. }
  419. $metadata = getMetadataTreeFromSentenceIDs($sentenceIDs, false);
  420. $results = array('sentences'=>$sentences,
  421. 'statistics'=>$statistics,
  422. 'total'=>$total,
  423. 'metadata'=>$metadata);
  424. return $results;
  425. }
  426. function populateSentenceInfo($sentence_id_string, $sentenceInfo) {
  427. $timing = getGetParam('timing');
  428. $t1 = time();
  429. // Get the words in the sentences.
  430. $table_identifier = 'filtered_sent_ids';
  431. $insertion_fields = '(id, document_id)';
  432. $field_identifier = "DISTINCT id, document_id ";
  433. $destination_table = $table_identifier;
  434. $query_id_where = '';
  435. global $cache_results;
  436. global $query_id;
  437. global $dont_cache_search_results;
  438. if (($cache_results || $query_id) && !$dont_cache_search_results) {
  439. $table_identifier = 'cached_filtered_sent_ids';
  440. $insertion_fields = '(id, document_id, query_id)';
  441. $field_identifier = "DISTINCT id, document_id, $query_id";
  442. $query_id_where = " AND query_id = $query_id ";
  443. }
  444. $sql = "SELECT * FROM
  445. $table_identifier, sentence
  446. WHERE sentence.id = $table_identifier.id $query_id_where;";
  447. //$t2 = time();
  448. // if ($timing) {
  449. // echo "
  450. // Time to query for words in sentences: ".($t2-$t1)."s
  451. // <br>";
  452. // }
  453. if ($timing) {
  454. echo "--- Final words query ---
  455. <br> $sql
  456. <br>";
  457. }
  458. $t2 = time();
  459. $result = mysql_query($sql);
  460. while($row = mysql_fetch_assoc($result)){
  461. // $word = array(
  462. // 'word'=>replaceWeirdCharacters($row['surface']),
  463. // 'word_id'=>$row['word_id']
  464. // );
  465. // array_push(
  466. // $sentenceInfo[$row['sentence_id']]['sentence']['words'],
  467. // $word);
  468. $sentenceInfo[$row['id']]['sentence']['words'] = $row["html"];
  469. $sentenceInfo[$row['id']]['sentence']['text'] = $row['sentence'];
  470. }
  471. $t3 = time();
  472. if ($timing) {
  473. echo "
  474. <br><br>
  475. Time to assemble words in sentences: ".($t3-$t2)."s
  476. <br>";
  477. }
  478. // Get the metadata fields associated with the sentences.
  479. $info = getMetadataInformationForSentences(false);
  480. foreach ($info['sentences'] as $row) {
  481. $sentence_id = $row['sentence_id'];
  482. if ($sentenceInfo[$sentence_id]) {
  483. foreach (array_keys($row) as $identifier) {
  484. if ($identifier != "document_id" && $identifier != "words") {
  485. $components = explode("__", $identifier);
  486. $property = $components[1];
  487. $value = $row[$identifier];
  488. if (!array_key_exists($property,
  489. $sentenceInfo[$sentence_id])) {
  490. $sentenceInfo[$sentence_id][$property] = "";
  491. }
  492. if (!strstr($sentenceInfo[$sentence_id][$property],
  493. $value." ")) {
  494. $sentenceInfo[$sentence_id][$property] =
  495. $sentenceInfo[$sentence_id][$property].$value." ";
  496. }
  497. }
  498. }
  499. }
  500. }
  501. $t4 = time();
  502. if ($timing) {
  503. echo "
  504. <br><br>
  505. Time to get metadata details for sentences: ".($t4-$t3)."s
  506. <br>";
  507. }
  508. return $sentenceInfo;
  509. }
  510. function getStatistics($gov, $dep, $relation, $withinDocument, $withinSentence,
  511. $within){
  512. $tablenames = "dependency_xref_sentence ";
  513. $where = "";
  514. if($withinSentence && strlen($within)>0){
  515. $where = "AND sentence_id in (".$within.")";
  516. }else if ($withinDocument && strlen($within)>0){
  517. $tablenames = "dependency_xref_sentence, sentence ";
  518. $where = " AND sentence.id = sentence_id
  519. AND document_id in (".$within.")";
  520. }
  521. $r = strlen($relation)>0;
  522. $g = strlen($gov)>0;
  523. $d = strlen($dep)>0;
  524. $rel_w = "";
  525. $gov_w ="";
  526. $dep_w = "";
  527. if($r){
  528. $rel_w = "relation_id IN (".$relation.")";
  529. }
  530. if($g){
  531. $gov_w = "gov_id IN (".$gov.") ";
  532. }
  533. if($d){
  534. $dep_w = "dep_id IN (".$dep.") ";
  535. }
  536. $statistics_query = "SELECT *, COUNT(sentence_id) as value
  537. FROM ".$tablenames." WHERE ";
  538. if($r || $g || $d ){
  539. if($r && $g && $d){
  540. $statistics_query = "$statistics_query $rel_w AND $gov_w AND $dep_w";
  541. }
  542. else if($r && $g){
  543. $statistics_query = "$statistics_query $rel_w AND $gov_w";
  544. }
  545. else if($r && $d){
  546. $statistics_query = "$statistics_query $rel_w AND $dep_w";
  547. }
  548. else if($g && $d){
  549. $statistics_query = $statistics_query."((".$gov_w." AND ".$dep_w.") OR ";
  550. $statistics_query = $statistics_query." (dep_id IN (".$gov.") AND gov_id IN (".$dep."))) ";
  551. }
  552. else if ($r){
  553. $statistics_query = $statistics_query.$rel_w;
  554. }
  555. else if ($g){
  556. $statistics_query = $statistics_query." (".$gov_w;
  557. $statistics_query = $statistics_query." OR dep_id IN (".$gov.")) ";
  558. }
  559. else{
  560. $statistics_query = $statistics_query." (".$dep_w;
  561. $statistics_query = $statistics_query." OR gov_id IN (".$dep.")) ";
  562. }
  563. $statistics_query = $statistics_query.$where;
  564. }
  565. else if($withinSentence){
  566. $statistics_query = "SELECT *, COUNT(sentence_id) FROM ".$tablenames.$where;
  567. }
  568. $relationship_query = $statistics_query."
  569. GROUP BY relation_id, gov_id, dep_id ORDER BY value desc;";
  570. $gov_query = $statistics_query."
  571. GROUP BY gov_id, dep_id, relation_id ORDER BY value desc;";
  572. $dep_query = $statistics_query."
  573. GROUP BY dep_id, gov_id, relation_id ORDER BY value desc;";
  574. $rel_result = mysql_query($relationship_query) or die (
  575. "get-search-results.php 184
  576. <br> $relationship_query
  577. <br> mysql error <br>".mysql_error());
  578. $gov_result = mysql_query($gov_query) or die (
  579. "get-search-results.php 185
  580. <br> $gov_query
  581. </br> mysql error <br>".mysql_error());
  582. $dep_result = mysql_query($dep_query) or die (
  583. "get-search-results.php 186
  584. <br> $dep_query
  585. <br> mysql error
  586. <br>".mysql_error());
  587. $dep_statistics = countStatistics($dep_result, 'dep_id', 'gov_id',
  588. 'relation_id');
  589. $rel_statistics = countStatistics($rel_result, 'relation_id', 'gov_id',
  590. 'dep_id');
  591. $gov_statistics = countStatistics($gov_result, 'gov_id', 'dep_id',
  592. 'relation_id');
  593. return array("relationship"=>$rel_statistics,
  594. "gov"=>$gov_statistics,
  595. "dep"=>$dep_statistics);
  596. }
  597. function countStatistics($statistics, $category, $subCategory, $subSubCategory){
  598. $counter = 0;
  599. $counted = array();
  600. $maxVal = 0;
  601. $total = 0;
  602. $data = array();
  603. while($row = mysql_fetch_assoc($statistics)){
  604. $cat = array_key_exists($category, $row) ? getWord($row[$category])
  605. : null;
  606. $subCat = array_key_exists($subCategory, $row) ? getWord($row[$subCategory]):null;
  607. $subSubCat = array_key_exists($subSubCategory, $row)?getWord($row[$subSubCategory]):null;
  608. $value = intval($row['value']);
  609. if($cat != null){
  610. if(!array_key_exists($cat, $data)){
  611. $data[$cat] = array("children"=>array(), "childMax"=>0,"value"=>0, "name"=>$cat);
  612. }
  613. if($subCat != null){
  614. if(!array_key_exists($subCat, $data[$cat]['children'])){
  615. $data[$cat]['children'][$subCat] = array("children"=>array(), "childMax"=>0, "value"=>0, "name"=>$subCat);
  616. }
  617. if($subSubCat != null){
  618. $data[$cat]['children'][$subCat]['children'][$subSubCat] = array("value"=>$value, "name"=>$subSubCat);
  619. if($value > $data[$cat]['children'][$subCat]['childMax']){
  620. $data[$cat]['children'][$subCat]['childMax'] = $value;
  621. }
  622. }
  623. $data[$cat]['children'][$subCat]['value'] += $value;
  624. if($data[$cat]['children'][$subCat]['value'] > $data[$cat]['childMax']){
  625. $data[$cat]['childMax'] = $data[$cat]['children'][$subCat]['value'];
  626. }
  627. }
  628. $data[$cat]['value'] += $value;
  629. if($data[$cat]['value'] > $maxVal){
  630. $maxVal = $data[$cat]['value'];
  631. }
  632. }
  633. $total += $value;
  634. }
  635. $sortedData = array();
  636. uasort($data, 'compareValues');
  637. foreach($data as $c){
  638. uasort($c['children'], 'compareValues');
  639. $sortedCategory = array();
  640. foreach($c['children'] as $sc){
  641. $sortedSubSubCategory = array();
  642. uasort($sc['children'], 'compareValues');
  643. foreach($sc['children'] as $ssc){
  644. array_push($sortedSubSubCategory, $ssc);
  645. }
  646. $sc['children'] = $sortedSubSubCategory;
  647. array_push($sortedCategory, $sc);
  648. }
  649. $c['children'] = $sortedCategory;
  650. array_push($sortedData, $c);
  651. }
  652. return array("value"=>$total, "childMax"=>$maxVal, "children"=>$sortedData);
  653. }
  654. function compareValues($a, $b){
  655. if($a['value'] > $b['value']){
  656. return -1;
  657. }else if($a['value'] == $b['value']){
  658. return 0;
  659. }else{
  660. return 1;
  661. }
  662. }
  663. ?>