/DemiBlog/DynamicPage.class.php
PHP | 137 lines | 94 code | 23 blank | 20 comment | 22 complexity | 74f0c7647a00440c520354bac6174fb7 MD5 | raw file
Possible License(s): GPL-2.0
- <?php
- /* demiblog³ - semantic cms
- * Copyright © 2006-2009 Toby A Inkster <mail@tobyinkster.co.uk>
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as published
- * by the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- require_once 'ConNeg.class.php';
- require_once 'Lens.class.php';
- require_once 'RequestHandler.interface.php';
- require_once 'SPARQL.class.php';
- class DynamicPage implements RequestHandler
- {
- public static function preliminary ($path)
- {
- }
- public static function handle_request ($path, $is_fallback=false)
- {
- if (demiblog_parse_size(ini_get('memory_limit')) < 50331648)
- throw new Exception("Need to raise PHP memory_limit to at least 48M.");
- $u = demiblog_config('uri');
-
- if ($is_fallback)
- {
- $uri = ($_SERVER['HTTPS']?'https://':'http://')
- . demiblog_config('authority')
- . $path;
- $uri = self::resolve($uri);
-
- return self::description($uri, $_GET['format'], $is_fallback);
- }
-
- elseif ($path == $u['describe'])
- {
- return self::description($_GET['uri'], $_GET['format'], $is_fallback);
- }
- }
- public static function description ($uri, $format=null, $is_fallback=false)
- {
- $lens = Lens::best_lens_for_uri($uri);
- $lens_klass = $lens['klass'];
-
- $available = ConNeg::STANDARD_OFFERS();
- $extra_avail = call_user_func(array($lens['klass'], 'extra_serialisations'));
- if (!empty($extra_avail))
- $available .= ', ' . $extra_avail;
-
- $format = ConNeg::negotiate($available, $format, false);
- $serialisation = ConNeg::serialisation($format);
- $sparqler = SPARQL::instance();
-
- if ( $serialisation=='html' || method_exists($lens_klass, 'ser_'.$serialisation) )
- {
- $index = $sparqler->describe($uri,
- true,
- false
- );
-
- // No information found.
- if (empty($index) && $is_fallback)
- throw new HttpException($uri, 404);
- // Specific death request.
- if (isset($index[$uri][DEMIBLOG_RDF_DEATHPROP])
- && isset($index[$uri][DEMIBLOG_RDF_DEATHPROP][0])
- && isset($index[$uri][DEMIBLOG_RDF_DEATHPROP][0]['value']))
- throw new HttpException($uri, $index[$uri][DEMIBLOG_RDF_DEATHPROP][0]['value']);
-
- $lens_obj = new $lens_klass ($index, $uri, $uri);
- $doc = call_user_func(array($lens_obj, 'ser_'.$serialisation));
-
- header("Content-Type: $format");
- demiblog_print_exit($doc);
- }
- else
- {
- $index = $sparqler->describe($uri,
- true,
- true,
- RdfUtils::supports_quads($serialisation)
- );
-
- // No information found.
- if (empty($index) && $is_fallback)
- throw new HttpException($uri, 404);
- // Specific death request.
- if (isset($index[$uri][DEMIBLOG_RDF_DEATHPROP])
- && isset($index[$uri][DEMIBLOG_RDF_DEATHPROP][0])
- && isset($index[$uri][DEMIBLOG_RDF_DEATHPROP][0]['value']))
- throw new HttpException($uri, $index[$uri][DEMIBLOG_RDF_DEATHPROP][0]['value']);
-
- $conf = array( 'ns' => RdfUtils::$suggested_prefixes );
- $ser = RdfUtils::get_serialiser($serialisation, $conf);
- $doc = $ser->getSerializedIndex($index);
- if ($serialisation == 'js')
- $doc = RdfUtils::apply_json_callback($doc, $_REQUEST['callback']);
-
- header("Content-Type: $format");
- demiblog_print_exit($doc."\n");
- }
- }
-
- public static function resolve ($uri)
- {
- $sparqler = SPARQL::instance();
- $result = $sparqler->query(
- sprintf('SELECT ?concept WHERE {?concept <%s> <%s>}',
- DEMIBLOG_RDF_CANONPROP, $uri));
-
- if ($result
- && $result['result']
- && $result['result']['rows']
- && $result['result']['rows'][0])
- {
- return $result['result']['rows'][0]['concept'];
- }
-
- return str_replace('*', '#', $uri);
- }
- }