PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/ontowiki/src/extensions/modules/usage/usage.php

https://bitbucket.org/nihilus/ontowiki
PHP | 150 lines | 87 code | 29 blank | 34 comment | 7 complexity | 239966a0d7421834a44a5f80c3d9f4d5 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0
  1. <?php
  2. /**
  3. * This file is part of the {@link http://ontowiki.net OntoWiki} project.
  4. *
  5. * @category OntoWiki
  6. * @package OntoWiki_extensions_modules_usage
  7. * @copyright Copyright (c) 2008, {@link http://aksw.org AKSW}
  8. * @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
  9. * @version $Id: usage.php 4092 2009-08-19 22:20:53Z christian.wuerker $
  10. */
  11. /**
  12. * OntoWiki usage module
  13. *
  14. * Adds the "Usage as Property" box to the properties context
  15. *
  16. * @category OntoWiki
  17. * @package OntoWiki_extensions_modules_usage
  18. * @copyright Copyright (c) 2008, {@link http://aksw.org AKSW}
  19. * @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
  20. * @category extensions
  21. * @package modules
  22. * @author Norman Heino <norman.heino@gmail.com>
  23. * @author Sebastian Dietzold <dietzold@informatik.uni-leipzig.de>
  24. */
  25. class UsageModule extends OntoWiki_Module
  26. {
  27. /** @var array */
  28. protected $_instances = null;
  29. /** @var OntoWiki_Model */
  30. protected $_model = null;
  31. /** @var array */
  32. protected $_objects = null;
  33. /**
  34. * Constructor
  35. */
  36. public function init()
  37. {
  38. // instances (subjects)
  39. $query1 = new Erfurt_Sparql_SimpleQuery();
  40. $query1->setProloguePart('SELECT DISTINCT ?uri')
  41. ->setWherePart('WHERE {
  42. ?uri <' . (string) $this->_owApp->selectedResource . '> ?object.
  43. FILTER (isURI(?uri))
  44. }')
  45. ->setLimit(OW_SHOW_MAX);
  46. $this->_instances = $this->_owApp->selectedModel->sparqlQuery($query1);
  47. // objects
  48. $query2 = new Erfurt_Sparql_SimpleQuery();
  49. $query2->setProloguePart('SELECT DISTINCT ?uri')
  50. ->setWherePart('WHERE {
  51. ?subject <' . (string) $this->_owApp->selectedResource . '> ?uri.
  52. FILTER (isURI(?uri))
  53. }')
  54. ->setLimit(OW_SHOW_MAX);
  55. $this->_objects = $this->_owApp->selectedModel->sparqlQuery($query2);
  56. }
  57. public function shouldShow()
  58. {
  59. if (!empty($this->_instances) || !empty($this->_objects)) {
  60. return true;
  61. }
  62. return false;
  63. }
  64. public function getTitle()
  65. {
  66. $title = $this->view->_($this->title) . ' ('
  67. . count($this->_instances) . '/'
  68. . count($this->_objects) . ')';
  69. return $title;
  70. }
  71. public function getContents()
  72. {
  73. $url = new OntoWiki_Url(array('route' => 'properties'));
  74. if (!empty($this->_instances)) {
  75. $instances = array();
  76. $instancesTitleHelper = new OntoWiki_Model_TitleHelper($this->_owApp->selectedModel);
  77. $instancesTitleHelper->addResources($this->_instances, 'uri');
  78. foreach ($this->_instances as $instance) {
  79. $instanceUri = $instance['uri'];
  80. if (!array_key_exists($instanceUri, $instances)) {
  81. // URL
  82. $url->setParam('r', $instanceUri, true);
  83. $instances[$instanceUri] = array(
  84. 'uri' => $instanceUri,
  85. 'title' => $instancesTitleHelper->getTitle($instanceUri, $this->_lang),
  86. 'url' => (string) $url
  87. );
  88. }
  89. }
  90. $this->view->instances = $instances;
  91. }
  92. if (!empty($this->_objects)) {
  93. $objects = array();
  94. $objectTitleHelper = new OntoWiki_Model_TitleHelper($this->_owApp->selectedModel);
  95. $objectTitleHelper->addResources($this->_objects, 'uri');
  96. foreach ($this->_objects as $object) {
  97. $objectUri = $object['uri'];
  98. if (!array_key_exists($objectUri, $objects)) {
  99. // URL
  100. $url->setParam('r', $objectUri, true);
  101. $objects[$objectUri] = array(
  102. 'uri' => $objectUri,
  103. 'title' => $objectTitleHelper->getTitle($objectUri, $this->_lang),
  104. 'url' => (string) $url
  105. );
  106. }
  107. }
  108. $this->view->objects = $objects;
  109. }
  110. if (empty($this->_instances) and empty($this->_objects)) {
  111. $this->view->message = 'No matches.';
  112. }
  113. // render data into template
  114. return $this->render('usage');
  115. }
  116. public function getStateId() {
  117. $id = $this->_owApp->selectedModel
  118. . $this->_owApp->selectedResource;
  119. return $id;
  120. }
  121. }