PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/extensions.ext/plugins/RepositoryservicesController.php

https://code.google.com/p/ontowiki/
PHP | 160 lines | 117 code | 11 blank | 32 comment | 10 complexity | ba9f3f06b407f8dcce80ff7a7f97cc9d MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. <?php
  2. require_once 'Zend/Controller/Action.php';
  3. require_once 'OntoWiki/Controller/Component.php';
  4. require_once 'OntoWiki/Toolbar.php';
  5. require_once 'Erfurt/Sparql/SimpleQuery.php';
  6. require_once 'Zend/Http/Client.php';
  7. require_once 'Zend/Json/Encoder.php';
  8. require_once 'Zend/Json/Decoder.php';
  9. /**
  10. * OntoWiki index controller.
  11. *
  12. * @category OntoWiki
  13. * @package OntoWiki_extensions_components_repository
  14. * @author Feng Qiu <qiu_feng39@hotmail.com>
  15. * @copyright Copyright (c) 2008, {@link http://aksw.org AKSW}
  16. * @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
  17. * @version $Id: ServiceController.php 3031 2009-05-05 17:31:06Z norman.heino $
  18. */
  19. class RepositoryservicesController extends OntoWiki_Controller_Component
  20. {
  21. /**
  22. * Wait for the request from pluginclient and send the information
  23. * about the tags
  24. */
  25. public function tagsAction()
  26. {
  27. $repository_base_url = $this->_privateConfig->repository->graphiri;
  28. $tag_base_url = $this->_privateConfig->repository->tag_base_url;
  29. // service controller needs no view renderer
  30. $this->_helper->viewRenderer->setNoRender();
  31. // disable layout for Ajax requests
  32. $this->_helper->layout()->disableLayout();
  33. $store = OntoWiki::getInstance()->erfurt->getStore();
  34. $response = $this->getResponse();
  35. // The parameters
  36. $num_of_tags = $this->_request->getParam('count', 10);
  37. $class = $this->_request->getParam('class', '');
  38. $selected_tags = $this->_request->getParam('tags', '');
  39. if($selected_tags!=''){
  40. $selected_tags = Zend_Json::decode($selected_tags);
  41. }
  42. else{
  43. $selected_tags = array();
  44. }
  45. require_once 'Erfurt/Sparql/SimpleQuery.php';
  46. if(count($selected_tags)==0){
  47. $query = Erfurt_Sparql_SimpleQuery::initWithString(
  48. 'SELECT DISTINCT ?tag
  49. FROM <' . $repository_base_url . '>
  50. WHERE {
  51. ?node <'. $tag_base_url .'> ?tag.
  52. }'
  53. );
  54. $tags = $store->sparqlQuery($query);
  55. }
  56. else{
  57. $tags = array();
  58. for($i=0;$i<count($selected_tags);$i++){
  59. $a_selected_tag = $selected_tags[$i];
  60. $query = Erfurt_Sparql_SimpleQuery::initWithString(
  61. "SELECT DISTINCT ?tag
  62. FROM <".$repository_base_url.">
  63. WHERE {
  64. ?node <" . $tag_base_url . "> ?tag.
  65. ?node <" . $tag_base_url . "> \"$a_selected_tag\"^^xsd:string.
  66. }"
  67. );
  68. $tags = array_merge($store->sparqlQuery($query),$tags);
  69. $tags = $this->unique_tags($tags);
  70. }
  71. }
  72. //echo "Tags:".print_r($tags)."<br/><br/>";
  73. $tag_with_frequence = array();
  74. $tags_results = array();
  75. if(count($tags)>0){
  76. $bigger = (count($tags)>$num_of_tags ? $num_of_tags:count($tags));
  77. for($i=0; $i<$bigger; $i++){
  78. $a_tag = $tags[$i];
  79. $tag_name = $a_tag['tag'];
  80. $where = "WHERE {
  81. ?node <". $tag_base_url ."> \"".$tag_name."\"^^xsd:string.
  82. }";
  83. $count = $store->countWhereMatches($repository_base_url,$where,"?node");
  84. $tag_with_frequence[$tag_name] = $count;
  85. }
  86. ksort($tag_with_frequence);
  87. $tags_results = $this->sort_tags($tag_with_frequence);
  88. //echo"Tags_results".print_r($tags_results)."<br/><br/>";
  89. }
  90. // send the response
  91. $response->setHeader('Content-Type', 'application/json');
  92. $response->setBody(Zend_Json::encode($tags_results));
  93. $response->sendResponse();
  94. exit;
  95. }
  96. /**
  97. * A function to get the weight
  98. *
  99. * @param array $tags_with_frequence
  100. */
  101. public function sort_tags($in_alphabet){
  102. //The parameters
  103. $count = $this->_request->getParam('count', 10);
  104. $class = $this->_request->getParam('class', '');
  105. $tags = $this->_request->getParam('tags', '');
  106. if($tags != ''){
  107. $tags = Zend_Json::decode($tags);
  108. }
  109. else{
  110. $tags = array();
  111. }
  112. $in_frequence = $in_alphabet;
  113. arsort($in_frequence);
  114. $tags_results = array();
  115. $a_in_alphabet = current($in_alphabet);
  116. while(list($a,$b) = each($in_alphabet)){
  117. $weight = $count-1;
  118. reset($in_frequence);
  119. while(list($c, $d) = each($in_frequence)) {
  120. if($a == $c){
  121. $tags_results[] = array(
  122. 'tag' => $a,
  123. 'weight' => $weight,
  124. 'frequence' => $b);
  125. }
  126. $weight--;
  127. }
  128. }
  129. return $tags_results;
  130. }
  131. /**
  132. * Unique the tags in array, to save the time
  133. *
  134. * @param unknown_type $tags
  135. * @return unknown
  136. */
  137. public function unique_tags($tags){
  138. $temp_tags = array();
  139. $results_tags = array();
  140. for($i=0;$i<count($tags);$i++){
  141. $temp_tags[] = $tags[$i]['tag'];
  142. }
  143. $temp_tags = array_unique($temp_tags);
  144. foreach($temp_tags as $a_tag){
  145. $results_tags[] = array('tag' => $a_tag);
  146. }
  147. return $results_tags;
  148. }
  149. }