| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
//////////////////////////////////////////////////////////////////////////////
|
| 5 |
// Menu callbacks for RDF export
|
| 6 |
|
| 7 |
function rdf_export_site() {
|
| 8 |
$data = rdf_query(url(NULL, array('absolute' => TRUE)));
|
| 9 |
rdf_export($data, 'site');
|
| 10 |
}
|
| 11 |
|
| 12 |
function rdf_export_node($node) {
|
| 13 |
return rdf_export_entity('node', $node->nid);
|
| 14 |
}
|
| 15 |
|
| 16 |
function rdf_export_user($account) {
|
| 17 |
return rdf_export_entity('user', $account->uid);
|
| 18 |
}
|
| 19 |
|
| 20 |
function rdf_export_entity($type, $id) {
|
| 21 |
$data = rdf_query(url($type . '/' . $id, array('absolute' => TRUE)));
|
| 22 |
rdf_export($data, implode('-', array($type, $id)));
|
| 23 |
}
|
| 24 |
|
| 25 |
/**
|
| 26 |
* @deprecated Replace with rdf_output().
|
| 27 |
*/
|
| 28 |
function rdf_export($data, $filename = 'export', $format = RDF_FORMAT, $options = array()) {
|
| 29 |
//module_load_include('inc', 'rdf_export', 'rdf_export');
|
| 30 |
|
| 31 |
$formats = rdf_get_formats();
|
| 32 |
$format = isset($formats[@$_GET['format']]) ? $_GET['format'] : $format;
|
| 33 |
$format = $formats[$format];
|
| 34 |
$filename = implode('.', array($filename, $format->file_ext));
|
| 35 |
|
| 36 |
if (!empty($options['log'])) {
|
| 37 |
watchdog('rdf', 'Exported RDF data file: %filename.', array('%filename' => $filename));
|
| 38 |
}
|
| 39 |
|
| 40 |
$output = rdf_serialize(is_string($data) ? $data() : $data, array('format' => $format->name));
|
| 41 |
|
| 42 |
header('Content-Disposition: inline; filename=' . $filename);
|
| 43 |
header('Content-Type: ' . $format->mime_type . '; charset=' . $format->encoding);
|
| 44 |
header('Content-Length: ' . strlen($output));
|
| 45 |
die($output);
|
| 46 |
}
|
| 47 |
|
| 48 |
function rdf_output_rss($filename, $format, $data, array $options = array()) {
|
| 49 |
if ($format == 'rdf+xml' && !isset($options['content_type'])) {
|
| 50 |
$options['content_type'] = 'application/rss+xml';
|
| 51 |
}
|
| 52 |
$options['base'] = isset($options['base']) ? $options['base'] : RDF_RSS_URI;
|
| 53 |
return rdf_output($filename, $format, $data, $options);
|
| 54 |
}
|
| 55 |
|
| 56 |
function rdf_output($filename, $format, $data, array $options = array()) {
|
| 57 |
global $base_url, $language;
|
| 58 |
|
| 59 |
// Figure out the serialization format and compose the filename
|
| 60 |
$formats = rdf_get_formats();
|
| 61 |
$format = !empty($format) ? $format : RDF_FORMAT;
|
| 62 |
$format = isset($formats[$format]) ? $formats[$format] : reset($formats); // TODO: auto-negotiation
|
| 63 |
$filename = !empty($filename) ? $filename : 'drupal';
|
| 64 |
$filename = implode('.', array($filename, $format->file_ext));
|
| 65 |
|
| 66 |
// Merge in the default options
|
| 67 |
$defaults = array(
|
| 68 |
'content_disposition' => 'inline',
|
| 69 |
'content_type' => $format->mime_type,
|
| 70 |
'content_type_charset' => $format->encoding,
|
| 71 |
);
|
| 72 |
$options = array_merge($defaults, $options);
|
| 73 |
|
| 74 |
// Developers: define this constant in your settings.php to make life easier:
|
| 75 |
if (defined('TRACE_TEXT_OUTPUT')) {
|
| 76 |
$options['content_type'] = 'text/plain';
|
| 77 |
}
|
| 78 |
|
| 79 |
// Serialize the RDF data
|
| 80 |
$content = rdf_serialize($data, array(
|
| 81 |
'format' => $format->name,
|
| 82 |
'language' => isset($options['language']) ? $options['language'] : $language->language,
|
| 83 |
'base' => isset($options['base']) ? $options['base'] : NULL,
|
| 84 |
));
|
| 85 |
|
| 86 |
// Compose HTTP response headers
|
| 87 |
$options['http_headers'] = !empty($options['http_headers']) ? $options['http_headers'] : array();
|
| 88 |
$options['http_headers'] = array_merge($options['http_headers'], array(
|
| 89 |
'Content-Disposition' => $options['content_disposition'] . '; filename=' . $filename,
|
| 90 |
'Content-Type' => $options['content_type'] . '; charset=' . $options['content_type_charset'],
|
| 91 |
'Content-Length' => strlen($content),
|
| 92 |
));
|
| 93 |
|
| 94 |
// Output HTTP response headers and content
|
| 95 |
if (!headers_sent()) {
|
| 96 |
if (isset($options['http_status'])) {
|
| 97 |
drupal_set_header('HTTP/1.1 ' . $options['http_status']);
|
| 98 |
}
|
| 99 |
foreach ($options['http_headers'] as $k => $vs) {
|
| 100 |
foreach (is_array($vs) ? $vs : array($vs) as $v) {
|
| 101 |
drupal_set_header($k . ': ' . $v);
|
| 102 |
}
|
| 103 |
}
|
| 104 |
}
|
| 105 |
print $content;
|
| 106 |
}
|
| 107 |
|
| 108 |
//////////////////////////////////////////////////////////////////////////////
|
| 109 |
// Menu callbacks for RDF feeds
|
| 110 |
|
| 111 |
function rdf_feed_callback($feed_id) {
|
| 112 |
$feed = rdf_get_feed_info($feed_id);
|
| 113 |
$feed->arguments = array_slice(func_get_args(), 1);
|
| 114 |
$feed->settings = rdf_get_feed_settings($feed_id);
|
| 115 |
|
| 116 |
$path = ($feed->path == variable_get('site_frontpage', 'node')) ? '' : $feed->path;
|
| 117 |
$path = $_GET['q']; // FIXME
|
| 118 |
$link = url($path, array('absolute' => TRUE));
|
| 119 |
|
| 120 |
module_load_include('inc', 'rdf', 'rdf.feed'); // rdf.feed.inc
|
| 121 |
$items = call_user_func_array($feed->callback, array_merge(array(&$feed), $feed->arguments));
|
| 122 |
|
| 123 |
if (is_null($items) || is_string($items) || is_int($items)) {
|
| 124 |
// Return to the Drupal menu handler
|
| 125 |
return $items;
|
| 126 |
}
|
| 127 |
else {
|
| 128 |
// Output RSS feed
|
| 129 |
$data = rdf_build_rss_feed($feed, $link, $items, $feed->settings);
|
| 130 |
rdf_output_rss('feed', $feed->settings['format'], $data);
|
| 131 |
exit;
|
| 132 |
}
|
| 133 |
}
|
| 134 |
|
| 135 |
/**
|
| 136 |
* Overrides node.module's RSS output.
|
| 137 |
*
|
| 138 |
* @see node_feed()
|
| 139 |
*/
|
| 140 |
function rdf_feed_node_frontpage(&$feed) {
|
| 141 |
$feed->settings['link'] = url('', array('absolute' => TRUE));
|
| 142 |
|
| 143 |
$result = db_query_range(db_rewrite_sql('SELECT n.nid FROM {node} n WHERE n.promote = 1 AND n.status = 1 ORDER BY n.created DESC'), 0, variable_get('feed_default_items', 10));
|
| 144 |
$items = array();
|
| 145 |
while ($item = db_fetch_object($result)) {
|
| 146 |
$items[] = $item->nid;
|
| 147 |
}
|
| 148 |
return new RDF_CallbackIterator('rdf_build_rss_feed_node', array(), $items, array($feed->settings));
|
| 149 |
}
|
| 150 |
|
| 151 |
/**
|
| 152 |
* Overrides taxonomy.module's RSS output.
|
| 153 |
*
|
| 154 |
* @see taxonomy_term_page()
|
| 155 |
*/
|
| 156 |
function rdf_feed_taxonomy_term(&$feed, $str_tids = '', $depth = 0, $op = 'page') {
|
| 157 |
// For anything but the RSS feed, we must delegate back to taxonomy.module:
|
| 158 |
if ($op != 'feed') {
|
| 159 |
module_load_include('inc', 'taxonomy', 'taxonomy.pages'); // taxonomy.pages.inc
|
| 160 |
return taxonomy_term_page($str_tids, $depth, $op);
|
| 161 |
}
|
| 162 |
|
| 163 |
$terms = taxonomy_terms_parse_string($str_tids);
|
| 164 |
if (($terms['operator'] == 'and' || $terms['operator'] == 'or') && !empty($terms['tids'])) {
|
| 165 |
// Rebuild the terms array to make sure it only contains terms the user has access to
|
| 166 |
$result = db_query(db_rewrite_sql('SELECT t.tid, t.name FROM {term_data} t WHERE t.tid IN (' . db_placeholders($terms['tids']) . ')', 't', 'tid'), $terms['tids']);
|
| 167 |
$terms['tids'] = $terms['names'] = array();
|
| 168 |
while ($term = db_fetch_object($result)) {
|
| 169 |
$terms['tids'][] = $term->tid;
|
| 170 |
$terms['names'][] = $term->name;
|
| 171 |
}
|
| 172 |
|
| 173 |
if (!empty($terms['tids'])) {
|
| 174 |
$path = 'taxonomy/term/' . $str_tids . (empty($depth) ? '' : '/' . $depth);
|
| 175 |
$feed->settings['link'] = url($path, array('absolute' => TRUE));
|
| 176 |
$feed->settings['title'] = variable_get('site_name', 'Drupal') . ' - ' . check_plain(implode(', ', $terms['names']));
|
| 177 |
// Only display the description if we have a single term:
|
| 178 |
$feed->settings['description'] = (count($tids) == 1 && ($term = taxonomy_get_term($tids[0]))) ? $term->description : NULL;
|
| 179 |
|
| 180 |
$result = taxonomy_select_nodes($terms['tids'], $terms['operator'], $depth, FALSE);
|
| 181 |
$items = array();
|
| 182 |
while ($item = db_fetch_object($result)) {
|
| 183 |
$items[] = $item->nid;
|
| 184 |
}
|
| 185 |
return new RDF_CallbackIterator('rdf_build_rss_feed_node', array(), $items, array($feed->settings));
|
| 186 |
}
|
| 187 |
}
|
| 188 |
|
| 189 |
return drupal_not_found();
|
| 190 |
}
|
| 191 |
|
| 192 |
/**
|
| 193 |
* Overrides blog.module's RSS output. Displays an RSS feed containing
|
| 194 |
* recent blog entries of a given user.
|
| 195 |
*
|
| 196 |
* @see blog_feed_user()
|
| 197 |
*/
|
| 198 |
function rdf_feed_blog_user(&$feed, $account) {
|
| 199 |
$feed->settings['title'] = t('!name\'s blog', array('!name' => $account->name));
|
| 200 |
$feed->settings['link'] = url('blog/' . $account->uid, array('absolute' => TRUE));
|
| 201 |
|
| 202 |
$result = db_query_range(db_rewrite_sql("SELECT n.nid, n.created FROM {node} n WHERE n.type = 'blog' AND n.uid = %d AND n.status = 1 ORDER BY n.created DESC"), $account->uid, 0, variable_get('feed_default_items', 10));
|
| 203 |
$items = array();
|
| 204 |
while ($item = db_fetch_object($result)) {
|
| 205 |
$items[] = $item->nid;
|
| 206 |
}
|
| 207 |
return new RDF_CallbackIterator('rdf_build_rss_feed_node', array(), $items, array($feed->settings));
|
| 208 |
}
|
| 209 |
|
| 210 |
/**
|
| 211 |
* Overrides blog.module's RSS output. Displays an RSS feed containing
|
| 212 |
* recent blog entries of all users.
|
| 213 |
*
|
| 214 |
* @see blog_feed_last()
|
| 215 |
*/
|
| 216 |
function rdf_feed_blog_last(&$feed) {
|
| 217 |
$feed->settings['title'] = t('@site_name blogs', array('@site_name' => variable_get('site_name', 'Drupal')));
|
| 218 |
$feed->settings['link'] = url('blog', array('absolute' => TRUE));
|
| 219 |
|
| 220 |
$result = db_query_range(db_rewrite_sql("SELECT n.nid, n.created FROM {node} n WHERE n.type = 'blog' AND n.status = 1 ORDER BY n.created DESC"), 0, variable_get('feed_default_items', 10));
|
| 221 |
$items = array();
|
| 222 |
while ($item = db_fetch_object($result)) {
|
| 223 |
$items[] = $item->nid;
|
| 224 |
}
|
| 225 |
return new RDF_CallbackIterator('rdf_build_rss_feed_node', array(), $items, array($feed->settings));
|
| 226 |
}
|
| 227 |
|
| 228 |
/**
|
| 229 |
* Overrides aggregator.module's RSS output. Generates an RSS 1.0 feed of
|
| 230 |
* aggregator items or categories.
|
| 231 |
*
|
| 232 |
* @see aggregator_page_rss()
|
| 233 |
*/
|
| 234 |
function rdf_feed_aggregator_rss(&$feed, $cid = NULL) {
|
| 235 |
if ($cid) {
|
| 236 |
$category = db_fetch_object(db_query('SELECT cid, title FROM {aggregator_category} WHERE cid = %d', $cid));
|
| 237 |
$result = db_query_range('SELECT i.*, f.title AS ftitle, f.link AS flink FROM {aggregator_category_item} c LEFT JOIN {aggregator_item} i ON c.iid = i.iid LEFT JOIN {aggregator_feed} f ON i.fid = f.fid WHERE cid = %d ORDER BY timestamp DESC, i.iid DESC', $category->cid, 0, variable_get('feed_default_items', 10));
|
| 238 |
}
|
| 239 |
else {
|
| 240 |
$category = NULL;
|
| 241 |
$result = db_query_range('SELECT i.*, f.title AS ftitle, f.link AS flink FROM {aggregator_item} i INNER JOIN {aggregator_feed} f ON i.fid = f.fid ORDER BY i.timestamp DESC, i.iid DESC', 0, variable_get('feed_default_items', 10));
|
| 242 |
}
|
| 243 |
|
| 244 |
$feed->settings['title'] = t('@site_name aggregator', array('@site_name' => ($site_name = variable_get('site_name', 'Drupal'))));
|
| 245 |
$feed->settings['link'] = url(empty($category) ? 'aggregator' : 'aggregator/categories/' . $category->cid, array('absolute' => TRUE));
|
| 246 |
$feed->settings['description'] = !empty($category) ?
|
| 247 |
t('@site_name - aggregated feeds in category @title', array('@site_name' => $site_name, '@title' => $category->title)) :
|
| 248 |
t('@site_name - aggregated feeds', array('@site_name' => $site_name));
|
| 249 |
|
| 250 |
$items = array();
|
| 251 |
while ($item = db_fetch_object($result)) {
|
| 252 |
$items[] = $item;
|
| 253 |
}
|
| 254 |
return new RDF_CallbackIterator('rdf_build_rss_feed_aggregator_item', array(), $items, array($category));
|
| 255 |
}
|
| 256 |
|
| 257 |
//////////////////////////////////////////////////////////////////////////////
|
| 258 |
// Menu callbacks
|
| 259 |
|
| 260 |
function rdf_schema_export_settings($prefix = NULL) {
|
| 261 |
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
|
| 262 |
return rdf_schema_import_settings();
|
| 263 |
}
|
| 264 |
|
| 265 |
$data = array();
|
| 266 |
$result = $prefix ?
|
| 267 |
db_query('SELECT name, value FROM {variable} WHERE name LIKE \'%s\' ORDER BY name, value', str_replace('*', '%', $prefix) .'%') :
|
| 268 |
db_query('SELECT name, value FROM {variable} ORDER BY name, value');
|
| 269 |
while ($variable = db_fetch_object($result)) {
|
| 270 |
$subject = url('rdf/variable/'. $variable->name, array('absolute' => TRUE));
|
| 271 |
|
| 272 |
if (strlen($variable->value) > 3000) {
|
| 273 |
// TODO: is this still needed?
|
| 274 |
continue; // HACK: the ARC Turtle parser currently chokes on literals longer than ~3000 characters
|
| 275 |
}
|
| 276 |
|
| 277 |
$data[$subject] = array(
|
| 278 |
rdf::type => array(rdf_uri('http://drupal.org/rdf/variable')),
|
| 279 |
rdfs::label => array($variable->name),
|
| 280 |
rdf::value => array(rdf_literal($variable->value, NULL, 'http://purl.org/php/serialized')), // FIXME
|
| 281 |
);
|
| 282 |
}
|
| 283 |
rdf_schema_export($data, 'settings', 'ntriples');
|
| 284 |
}
|
| 285 |
|
| 286 |
function rdf_schema_export_modules($prefix = NULL) {
|
| 287 |
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
|
| 288 |
return rdf_schema_import_modules();
|
| 289 |
}
|
| 290 |
|
| 291 |
$data = array();
|
| 292 |
$result = $prefix ?
|
| 293 |
db_query('SELECT name, status FROM {system} WHERE type = \'module\' AND name LIKE \'%s\' ORDER BY name, status', str_replace('*', '%', $prefix) .'%') :
|
| 294 |
db_query('SELECT name, status FROM {system} WHERE type = \'module\' ORDER BY name, status');
|
| 295 |
while ($module = db_fetch_object($result)) {
|
| 296 |
$subject = url('rdf/module/'. $module->name, array('absolute' => TRUE));
|
| 297 |
$data[$subject] = array(
|
| 298 |
rdf::type => array(rdf_uri('http://drupal.org/rdf/module')),
|
| 299 |
rdfs::label => array($module->name),
|
| 300 |
'http://drupal.org/rdf/terms#status' => array(rdf_literal($module->status, NULL, 'xsd:boolean')),
|
| 301 |
);
|
| 302 |
}
|
| 303 |
rdf_schema_export($data, 'modules', 'ntriples');
|
| 304 |
}
|
| 305 |
|
| 306 |
function rdf_schema_export($data, $filename = 'export', $format = RDF_FORMAT, $options = array()) {
|
| 307 |
$formats = rdf_get_formats();
|
| 308 |
$format = isset($formats[@$_GET['format']]) ? $_GET['format'] : $format;
|
| 309 |
$format = $formats[$format];
|
| 310 |
$filename = implode('.', array($filename, $format->file_ext));
|
| 311 |
|
| 312 |
$output = rdf_serialize(is_string($data) ? $data() : $data, array('format' => $format->name));
|
| 313 |
|
| 314 |
$format->mime_type = 'text/plain'; // FIXME
|
| 315 |
header('Content-Disposition: inline; filename='. $filename);
|
| 316 |
header('Content-Type: '. $format->mime_type .'; charset='. $format->encoding);
|
| 317 |
header('Content-Length: '. strlen($output));
|
| 318 |
die($output);
|
| 319 |
}
|
| 320 |
|
| 321 |
function rdf_schema_import_settings() {
|
| 322 |
$input = file_get_contents('php://input');
|
| 323 |
$input = rdf_normalize(rdf_unserialize($input, array('format' => 'ntriples')));
|
| 324 |
|
| 325 |
$config = array();
|
| 326 |
foreach ($input as $data) {
|
| 327 |
if ((string)$data[rdf::type][0] == 'http://drupal.org/rdf/variable') {
|
| 328 |
if (($name = $data[rdfs::label][0]) && isset($data[rdf::value])) {
|
| 329 |
$value = unserialize($data[rdf::value][0]->value);
|
| 330 |
$config[$name] = $value;
|
| 331 |
}
|
| 332 |
}
|
| 333 |
}
|
| 334 |
|
| 335 |
foreach ($config as $name => $value) {
|
| 336 |
variable_set($name, $value);
|
| 337 |
}
|
| 338 |
|
| 339 |
printf("OK (imported %d variables)\n", count($config));
|
| 340 |
}
|
| 341 |
|
| 342 |
function rdf_schema_import_modules() {
|
| 343 |
include_once './includes/install.inc';
|
| 344 |
|
| 345 |
$input = file_get_contents('php://input');
|
| 346 |
$input = rdf_normalize(rdf_unserialize($input, array('format' => 'ntriples')));
|
| 347 |
|
| 348 |
$modules = array();
|
| 349 |
foreach ($input as $data) {
|
| 350 |
if ((string)$data[rdf::type][0] == 'http://drupal.org/rdf/module') {
|
| 351 |
if (($name = $data[rdfs::label][0]) && isset($data['http://drupal.org/rdf/terms#status'])) {
|
| 352 |
$enabled = $data['http://drupal.org/rdf/terms#status'][0];
|
| 353 |
$enabled = !empty($enabled) && (bool)$enabled->value;
|
| 354 |
if ($enabled && drupal_get_installed_schema_version($name) == SCHEMA_UNINSTALLED) {
|
| 355 |
$modules['install'][] = $name;
|
| 356 |
}
|
| 357 |
else {
|
| 358 |
$modules[$enabled ? 'enable' : 'disable'][] = $name;
|
| 359 |
}
|
| 360 |
}
|
| 361 |
}
|
| 362 |
}
|
| 363 |
|
| 364 |
if (!empty($modules['enable'])) {
|
| 365 |
module_enable($modules['enable']);
|
| 366 |
}
|
| 367 |
|
| 368 |
if (!empty($modules['disable'])) {
|
| 369 |
module_disable($modules['disable']);
|
| 370 |
}
|
| 371 |
|
| 372 |
if (!empty($modules['install'])) {
|
| 373 |
foreach ($modules['install'] as $key => $module) {
|
| 374 |
if (!drupal_check_module($module)) {
|
| 375 |
unset($modules['install'][$key]);
|
| 376 |
}
|
| 377 |
}
|
| 378 |
drupal_install_modules($modules['install']);
|
| 379 |
}
|
| 380 |
|
| 381 |
drupal_clear_css_cache();
|
| 382 |
drupal_clear_js_cache();
|
| 383 |
|
| 384 |
// Notify locale module about module changes, so translations can be
|
| 385 |
// imported. This might start a batch, and only return to the redirect
|
| 386 |
// path after that.
|
| 387 |
module_invoke('locale', 'system_update', $modules['install']);
|
| 388 |
|
| 389 |
// Synchronize to catch any actions that were added or removed.
|
| 390 |
actions_synchronize();
|
| 391 |
|
| 392 |
printf("OK\n");
|
| 393 |
}
|