PageRenderTime 33ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 1ms

/extensions/resourcemodules/LinkinghereModule.php

https://code.google.com/p/ontowiki/
PHP | 134 lines | 90 code | 26 blank | 18 comment | 9 complexity | 4dd19df4b4d6d9b6acd86fd3d4f4b575 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * OntoWiki module รข&#x20AC;&#x201C; linkinhere
  4. *
  5. * Add instance properties to the list view
  6. *
  7. * @category OntoWiki
  8. * @package OntoWiki_extensions_modules_linkinghere
  9. * @author Norman Heino <norman.heino@gmail.com>
  10. * @copyright Copyright (c) 2008, {@link http://aksw.org AKSW}
  11. * @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
  12. * @version $Id: linkinghere.php 4092 2009-08-19 22:20:53Z christian.wuerker $
  13. */
  14. class LinkinghereModule extends OntoWiki_Module
  15. {
  16. /**
  17. * Constructor
  18. */
  19. public function init()
  20. {
  21. $query = new Erfurt_Sparql_SimpleQuery();
  22. $query->setProloguePart('SELECT DISTINCT ?uri')
  23. ->setWherePart('WHERE {
  24. ?subject ?uri <' . (string) $this->_owApp->selectedResource . '> .
  25. FILTER (isURI(?subject))
  26. }')
  27. ->setLimit(OW_SHOW_MAX);
  28. $this->predicates = $this->_owApp->selectedModel->sparqlQuery($query);
  29. }
  30. public function getTitle()
  31. {
  32. return "Instances linking here";
  33. }
  34. public function shouldShow()
  35. {
  36. // show only if there are predicates
  37. if ($this->predicates) {
  38. return true;
  39. } else {
  40. return false;
  41. }
  42. }
  43. public function getContents()
  44. {
  45. $titleHelper = new OntoWiki_Model_TitleHelper($this->_owApp->selectedModel);
  46. $query = new Erfurt_Sparql_SimpleQuery();
  47. $results = false;
  48. $predicates = $this->predicates;
  49. $properties = array();
  50. $instances = array();
  51. $url = new OntoWiki_Url(array('route' => 'properties'), array('r'));
  52. $titleHelper->addResources($predicates, 'uri');
  53. foreach ($predicates as $predicate) {
  54. $predicateUri = $predicate['uri'];
  55. $url->setParam('r', $predicateUri, true); // create properties url for the relation
  56. $properties[$predicateUri]['uri'] = $predicateUri;
  57. $properties[$predicateUri]['url'] = (string) $url;
  58. $properties[$predicateUri]['title'] = $titleHelper->getTitle($predicateUri, $this->_lang);
  59. $query->resetInstance()
  60. ->setProloguePart('SELECT DISTINCT ?uri')
  61. ->setWherePart('WHERE {
  62. ?uri <' . $predicateUri . '> <' . (string) $this->_owApp->selectedResource . '> .
  63. FILTER (isURI(?uri))
  64. }')
  65. ->setLimit(OW_SHOW_MAX + 1);
  66. if ($subjects = $this->_owApp->selectedModel->sparqlQuery($query)) {
  67. $results = true;
  68. // has_more is used for the dots
  69. if (count($subjects) > OW_SHOW_MAX) {
  70. $properties[$predicateUri]['has_more'] = true;
  71. $subjects = array_splice ( $subjects, 0, OW_SHOW_MAX);
  72. } else {
  73. $properties[$predicateUri]['has_more'] = false;
  74. }
  75. $subjectTitleHelper = new OntoWiki_Model_TitleHelper($this->_owApp->selectedModel);
  76. $subjectTitleHelper->addResources($subjects, 'uri');
  77. foreach ($subjects as $subject) {
  78. $subjectUri = $subject['uri'];
  79. $subject['title'] = $subjectTitleHelper->getTitle($subjectUri, $this->_lang);
  80. // set URL
  81. $url->setParam('r', $subjectUri, true);
  82. $subject['url'] = (string) $url;
  83. if (array_key_exists($predicateUri, $instances)) {
  84. if (!array_key_exists($subjectUri, $instances[$predicateUri])) {
  85. $instances[$predicateUri][$subjectUri] = $subject;
  86. }
  87. } else {
  88. $instances[$predicateUri] = array(
  89. $subjectUri => $subject
  90. );
  91. }
  92. }
  93. }
  94. }
  95. $this->view->resource = $this->_owApp->selectedResource;
  96. $this->view->properties = $properties;
  97. $this->view->instances = $instances;
  98. if (!$results) {
  99. $this->view->message = 'No matches.';
  100. }
  101. return $this->render('linkinghere');
  102. }
  103. public function getStateId() {
  104. $id = $this->_owApp->selectedModel->getModelIri()
  105. . $this->_owApp->selectedResource;
  106. return $id;
  107. }
  108. }