PageRenderTime 62ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/DemiBlog/DynamicPage.class.php

https://bitbucket.org/tobyink/php-demiblog3
PHP | 137 lines | 94 code | 23 blank | 20 comment | 22 complexity | 74f0c7647a00440c520354bac6174fb7 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /* demiblog³ - semantic cms
  3. * Copyright © 2006-2009 Toby A Inkster <mail@tobyinkster.co.uk>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as published
  7. * by the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. require_once 'ConNeg.class.php';
  19. require_once 'Lens.class.php';
  20. require_once 'RequestHandler.interface.php';
  21. require_once 'SPARQL.class.php';
  22. class DynamicPage implements RequestHandler
  23. {
  24. public static function preliminary ($path)
  25. {
  26. }
  27. public static function handle_request ($path, $is_fallback=false)
  28. {
  29. if (demiblog_parse_size(ini_get('memory_limit')) < 50331648)
  30. throw new Exception("Need to raise PHP memory_limit to at least 48M.");
  31. $u = demiblog_config('uri');
  32. if ($is_fallback)
  33. {
  34. $uri = ($_SERVER['HTTPS']?'https://':'http://')
  35. . demiblog_config('authority')
  36. . $path;
  37. $uri = self::resolve($uri);
  38. return self::description($uri, $_GET['format'], $is_fallback);
  39. }
  40. elseif ($path == $u['describe'])
  41. {
  42. return self::description($_GET['uri'], $_GET['format'], $is_fallback);
  43. }
  44. }
  45. public static function description ($uri, $format=null, $is_fallback=false)
  46. {
  47. $lens = Lens::best_lens_for_uri($uri);
  48. $lens_klass = $lens['klass'];
  49. $available = ConNeg::STANDARD_OFFERS();
  50. $extra_avail = call_user_func(array($lens['klass'], 'extra_serialisations'));
  51. if (!empty($extra_avail))
  52. $available .= ', ' . $extra_avail;
  53. $format = ConNeg::negotiate($available, $format, false);
  54. $serialisation = ConNeg::serialisation($format);
  55. $sparqler = SPARQL::instance();
  56. if ( $serialisation=='html' || method_exists($lens_klass, 'ser_'.$serialisation) )
  57. {
  58. $index = $sparqler->describe($uri,
  59. true,
  60. false
  61. );
  62. // No information found.
  63. if (empty($index) && $is_fallback)
  64. throw new HttpException($uri, 404);
  65. // Specific death request.
  66. if (isset($index[$uri][DEMIBLOG_RDF_DEATHPROP])
  67. && isset($index[$uri][DEMIBLOG_RDF_DEATHPROP][0])
  68. && isset($index[$uri][DEMIBLOG_RDF_DEATHPROP][0]['value']))
  69. throw new HttpException($uri, $index[$uri][DEMIBLOG_RDF_DEATHPROP][0]['value']);
  70. $lens_obj = new $lens_klass ($index, $uri, $uri);
  71. $doc = call_user_func(array($lens_obj, 'ser_'.$serialisation));
  72. header("Content-Type: $format");
  73. demiblog_print_exit($doc);
  74. }
  75. else
  76. {
  77. $index = $sparqler->describe($uri,
  78. true,
  79. true,
  80. RdfUtils::supports_quads($serialisation)
  81. );
  82. // No information found.
  83. if (empty($index) && $is_fallback)
  84. throw new HttpException($uri, 404);
  85. // Specific death request.
  86. if (isset($index[$uri][DEMIBLOG_RDF_DEATHPROP])
  87. && isset($index[$uri][DEMIBLOG_RDF_DEATHPROP][0])
  88. && isset($index[$uri][DEMIBLOG_RDF_DEATHPROP][0]['value']))
  89. throw new HttpException($uri, $index[$uri][DEMIBLOG_RDF_DEATHPROP][0]['value']);
  90. $conf = array( 'ns' => RdfUtils::$suggested_prefixes );
  91. $ser = RdfUtils::get_serialiser($serialisation, $conf);
  92. $doc = $ser->getSerializedIndex($index);
  93. if ($serialisation == 'js')
  94. $doc = RdfUtils::apply_json_callback($doc, $_REQUEST['callback']);
  95. header("Content-Type: $format");
  96. demiblog_print_exit($doc."\n");
  97. }
  98. }
  99. public static function resolve ($uri)
  100. {
  101. $sparqler = SPARQL::instance();
  102. $result = $sparqler->query(
  103. sprintf('SELECT ?concept WHERE {?concept <%s> <%s>}',
  104. DEMIBLOG_RDF_CANONPROP, $uri));
  105. if ($result
  106. && $result['result']
  107. && $result['result']['rows']
  108. && $result['result']['rows'][0])
  109. {
  110. return $result['result']['rows'][0]['concept'];
  111. }
  112. return str_replace('*', '#', $uri);
  113. }
  114. }