Newer
Older
/**
* Helper function for feeds_get_importer_id() and feeds_enabled_importers().
*/
function _feeds_importer_digest() {
$importers = &drupal_static(__FUNCTION__);
if ($importers === NULL) {
if ($cache = cache_get(__FUNCTION__)) {
$importers = $cache->data;
}
else {
$importers = array();
foreach (feeds_importer_load_all() as $importer) {
$importers[$importer->id] = isset($importer->config['content_type']) ? $importer->config['content_type'] : '';
Alex Barth
committed
}
cache_set(__FUNCTION__, $importers);
Alex Barth
committed
}
return $importers;
}
/**
* Resets importer caches. Call when enabling/disabling importers.
*/
function feeds_cache_clear($rebuild_menu = TRUE) {
cache_clear_all('_feeds_importer_digest', 'cache');
drupal_static_reset('_feeds_importer_digest');
cache_clear_all('plugins:feeds:plugins', 'cache');
ctools_include('export');
ctools_export_load_object_reset('feeds_importer');
if ($rebuild_menu) {
menu_rebuild();
}
}
* Exports a FeedsImporter configuration to code.
Alex Barth
committed
function feeds_export($importer_id, $indent = '') {
ctools_include('export');
$result = ctools_export_load_object('feeds_importer', 'names', array('id' => $importer_id));
if (isset($result[$importer_id])) {
return ctools_export_object('feeds_importer', $result[$importer_id], $indent);
}
}
* Logs to a file like /tmp/feeds_my_domain_org.log in temporary directory.
if (variable_get('feeds_debug', FALSE)) {
}
$filename = trim(str_replace('/', '_', $_SERVER['HTTP_HOST'] . base_path()), '_');
$handle = fopen("temporary://feeds_$filename.log", 'a');
fwrite($handle, gmdate('c') . "\t$msg\n");
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
/**
* Writes to feeds log.
*/
function feeds_log($importer_id, $feed_nid, $type, $message, $variables = array(), $severity = WATCHDOG_NOTICE) {
if ($severity < WATCHDOG_NOTICE) {
$error = &drupal_static('feeds_log_error', FALSE);
$error = TRUE;
}
db_insert('feeds_log')
->fields(array(
'id' => $importer_id,
'feed_nid' => $feed_nid,
'log_time' => time(),
'request_time' => REQUEST_TIME,
'type' => $type,
'message' => $message,
'variables' => serialize($variables),
'severity' => $severity,
))
->execute();
}
/**
* Loads an item info object.
*
* Example usage:
*
* $info = feeds_item_info_load('node', $node->nid);
*/
function feeds_item_info_load($entity_type, $entity_id) {
return db_select('feeds_item')
->fields('feeds_item')
->condition('entity_type', $entity_type)
->condition('entity_id', $entity_id)
->execute()
->fetchObject();
}
/**
* Inserts an item info object into the feeds_item table.
*/
function feeds_item_info_insert($entity, $entity_id) {
if (isset($entity->feeds_item)) {
$entity->feeds_item->entity_id = $entity_id;
drupal_write_record('feeds_item', $entity->feeds_item);
}
}
/**
* Inserts or updates an item info object in the feeds_item table.
function feeds_item_info_save($entity, $entity_id) {
if (isset($entity->feeds_item)) {
$entity->feeds_item->entity_id = $entity_id;
if (feeds_item_info_load($entity->feeds_item->entity_type, $entity_id)) {
drupal_write_record('feeds_item', $entity->feeds_item, array('entity_type', 'entity_id'));
}
else {
feeds_item_info_insert($entity, $entity_id);
}
Alex Barth
committed
/**
Alex Barth
committed
*/
/**
* @defgroup instantiators Instantiators
* @{
*/
/**
* Gets an importer instance.
Alex Barth
committed
*
* @param $id
* The unique id of the importer object.
*
* @return
* A FeedsImporter object or an object of a class defined by the Drupal
* variable 'feeds_importer_class'. There is only one importer object
* per $id system-wide.
*/
function feeds_importer($id) {
return FeedsConfigurable::instance(variable_get('feeds_importer_class', 'FeedsImporter'), $id);
}
/**
* Gets an instance of a source object.
Alex Barth
committed
*
* @param $importer_id
* A FeedsImporter id.
Alex Barth
committed
* @param $feed_nid
* The node id of a feed node if the source is attached to a feed node.
*
* @return
* A FeedsSource object or an object of a class defiend by the Drupal
* variable 'source_class'.
*/
function feeds_source($importer_id, $feed_nid = 0) {
return FeedsSource::instance($importer_id, $feed_nid);
Alex Barth
committed
}
* Gets an instance of a class for a given plugin and id.
Alex Barth
committed
*
* @param string $plugin
Alex Barth
committed
* A string that is the key of the plugin to load.
* @param string $id
Alex Barth
committed
* A string that is the id of the object.
*
* @return FeedsPlugin
Alex Barth
committed
* A FeedsPlugin object.
Alex Barth
committed
function feeds_plugin($plugin, $id) {
Alex Barth
committed
ctools_include('plugins');
Alex Barth
committed
if ($class = ctools_plugin_load_class('feeds', 'plugins', $plugin, 'handler')) {
return FeedsPlugin::instance($class, $id, ctools_get_plugins('feeds', 'plugins', $plugin));
$args = array('%plugin' => $plugin, '@id' => $id);
if (user_access('administer feeds')) {
if (module_exists('feeds_ui')) {
$args['@link'] = url('admin/structure/feeds/' . $id);
drupal_set_message(t('Missing Feeds plugin %plugin. See <a href="@link">@id</a>. Check whether all required libraries and modules are installed properly.', $args), 'warning', FALSE);
}
else {
drupal_set_message(t('Missing Feeds plugin %plugin used by the importer "@id". Check whether all required libraries and modules are installed properly. Enable the Feeds Admin UI module to check the importer\'s settings.', $args), 'warning', FALSE);
}
}
else {
Chris Leppanen
committed
drupal_set_message(t('Missing Feeds plugin %plugin. Please contact your site administrator.', $args), 'warning', FALSE);
$class = ctools_plugin_load_class('feeds', 'plugins', 'FeedsMissingPlugin', 'handler');
return FeedsPlugin::instance($class, $id);
Alex Barth
committed
/**
Alex Barth
committed
*/
/**
* @defgroup include Funtions for loading libraries
* @{
*/
/**
* Includes a library file.
Alex Barth
committed
*
* @param string $file
Alex Barth
committed
* The filename to load from.
* @param string $library
Alex Barth
committed
* The name of the library. If libraries module is installed,
* feeds_include_library() will look for libraries with this name managed by
* libraries module.
*/
function feeds_include_library($file, $library) {
static $included = array();
$key = $library . '/' . $file;
if (!isset($included[$key])) {
$included[$key] = FALSE;
$library_dir = variable_get('feeds_library_dir', FALSE);
$feeds_library_path = DRUPAL_ROOT . '/' . drupal_get_path('module', 'feeds') . "/libraries/$file";
$libraries_path = module_exists('libraries') ? libraries_get_path($library) : FALSE;
// Try first whether libraries module is present and load the file from
// there. If this fails, require the library from the local path.
if ($libraries_path && is_file("$libraries_path/$file")) {
require "$libraries_path/$file";
$included[$key] = TRUE;
}
elseif (is_file(DRUPAL_ROOT . '/sites/all/libraries/' . $key)) {
require DRUPAL_ROOT . '/sites/all/libraries/' . $key;
$included[$key] = TRUE;
Alex Barth
committed
}
elseif ($library_dir && is_file($library_dir . '/' . $key)) {
require $library_dir . '/' . $key;
$included[$key] = TRUE;
elseif (is_file($feeds_library_path)) {
Alex Barth
committed
// @todo: Throws "Deprecated function: Assigning the return value of new
// by reference is deprecated."
$included[$key] = TRUE;
Alex Barth
committed
}
Chris Leppanen
committed
return $included[$key];
Alex Barth
committed
/**
* Checks whether a library is present.
*
* @param string $file
Alex Barth
committed
* The filename to load from.
* @param string $library
Alex Barth
committed
* The name of the library. If libraries module is installed,
* feeds_library_exists() will look for libraries with this name managed by
* libraries module.
*/
function feeds_library_exists($file, $library) {
$path = module_exists('libraries') ? libraries_get_path($library) : FALSE;
if ($path && is_file($path . '/' . $file)) {
return TRUE;
}
elseif (is_file(DRUPAL_ROOT . "/sites/all/libraries/$library/$file")) {
Alex Barth
committed
return TRUE;
}
elseif (is_file(DRUPAL_ROOT . '/' . drupal_get_path('module', 'feeds') . "/libraries/$file")) {
Alex Barth
committed
return TRUE;
}
elseif ($library_dir = variable_get('feeds_library_dir', FALSE)) {
if (is_file("$library_dir/$library/$file")) {
return TRUE;
}
}
Alex Barth
committed
return FALSE;
}
/**
* Checks whether simplepie exists.
*/
function feeds_simplepie_exists() {
return (
feeds_library_exists('autoloader.php', 'simplepie') ||
feeds_library_exists('simplepie.compiled.php', 'simplepie') ||
feeds_library_exists('simplepie.mini.php', 'simplepie') ||
feeds_library_exists('simplepie.inc', 'simplepie')
);
}
/**
* Includes the simplepie library.
*/
function feeds_include_simplepie() {
$files = array(
'autoloader.php',
'simplepie.mini.php',
'simplepie.compiled.php',
'simplepie.inc',
);
foreach ($files as $file) {
if (feeds_include_library($file, 'simplepie')) {
return TRUE;
}
}
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
/**
* @deprecated
*
* Simplified drupal_alter().
*
* - None of that 'multiple parameters by ref' crazyness.
* - Don't use module_implements() to allow hot including on behalf
* implementations (see mappers/).
*
* @todo This needs to be removed and drupal_alter() used. This is crazy dumb.
*/
function feeds_alter($type, &$data) {
$args = array(&$data);
$additional_args = func_get_args();
array_shift($additional_args);
array_shift($additional_args);
$args = array_merge($args, $additional_args);
$hook = $type . '_alter';
foreach (module_list() as $module) {
if (module_hook($module, $hook)) {
call_user_func_array($module . '_' . $hook, $args);
}
}
}
Alex Barth
committed
/**
Alex Barth
committed
*/
/**
* Copy of valid_url() that supports the webcal scheme.
*
* @see valid_url().
*
Chris Leppanen
committed
* @todo Replace with valid_url() when http://drupal.org/node/295021 is fixed.
*/
function feeds_valid_url($url, $absolute = FALSE) {
if ($absolute) {
return (bool) preg_match("
/^ # Start at the beginning of the text
(?:ftp|https?|feed|webcal):\/\/ # Look for ftp, http, https, feed or webcal schemes
(?: # Userinfo (optional) which is typically
(?:(?:[\w\.\-\+!$&'\(\)*\+,;=]|%[0-9a-f]{2})+:)* # a username or a username and password
(?:[\w\.\-\+%!$&'\(\)*\+,;=]|%[0-9a-f]{2})+@ # combination
)?
(?:
(?:[a-z0-9\-\.]|%[0-9a-f]{2})+ # A domain name or a IPv4 address
|(?:\[(?:[0-9a-f]{0,4}:)*(?:[0-9a-f]{0,4})\]) # or a well formed IPv6 address
)
(?::[0-9]+)? # Server port number (optional)
(?:[\/|\?]
Chris Leppanen
committed
(?:[|\w#!:\.\?\+=&@$'~*,;\/\(\)\[\]\-]|%[0-9a-f]{2}) # The path and query (optional)
*)?
$/xi", $url);
}
else {
return (bool) preg_match("/^(?:[\w#!:\.\?\+=&@$'~*,;\/\(\)\[\]\-]|%[0-9a-f]{2})+$/i", $url);
}
}
elliotttf
committed
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
/**
* Registers a feed subscription job for execution on feeds_exit().
*
* @param array $job
* Information about a new job to queue; or if set to NULL (default), leaves
* the current queued jobs unchanged.
*
* @return
* An array of subscribe jobs to process.
*
* @see feeds_exit()
* @see feeds_get_subscription_jobs()
*/
function feeds_set_subscription_job(array $job = NULL) {
$jobs = &drupal_static(__FUNCTION__, array());
if (isset($job)) {
$jobs[] = $job;
}
return $jobs;
}
/**
* Returns the list of queued jobs to be run.
*
* @return
* An array of subscribe jobs to process.
*
* @see feeds_set_subscription_job()
*/
function feeds_get_subscription_jobs() {
return feeds_set_subscription_job();
}
Chris Leppanen
committed
/**
* Implements hook_entity_property_info_alter().
*/
function feeds_entity_property_info_alter(&$info) {
foreach ($info as $entity_type => $entity_info) {
Chris Leppanen
committed
$info[$entity_type]['properties']['feed_nid'] = array(
'label' => 'Feed NID',
'type' => 'integer',
'description' => t('Nid of the Feed Node that imported this entity.'),
Chris Leppanen
committed
'getter callback' => 'feeds_get_feed_nid_entity_callback',
Chris Leppanen
committed
);
$info[$entity_type]['properties']['feed_node'] = array(
'label' => 'Feed node',
'type' => 'node',
'description' => t('Feed Node that imported this entity.'),
'getter callback' => 'feeds_get_feed_nid_entity_callback',
);
Chris Leppanen
committed
}
}
Chris Leppanen
committed
/**
* Gets the feed_nid for an entity for use in entity metadata.
*/
function feeds_get_feed_nid_entity_callback($entity, array $options, $name, $entity_type) {
list($entity_id, , ) = entity_extract_ids($entity_type, $entity);
$feed_nid = NULL;
if ($entity_id) {
$feed_nid = feeds_get_feed_nid($entity_id, $entity_type);
if ($feed_nid === FALSE) {
return NULL;
}
}
// If the entity has no ID (yet) try read the feed nid from the object
// directly.
elseif (isset($entity->feeds_item->feed_nid)) {
$feed_nid = $entity->feeds_item->feed_nid;
Chris Leppanen
committed
}
return $feed_nid;
}
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
/**
* Implements hook_file_download().
*/
function feeds_file_download($uri) {
$id = db_query("SELECT id FROM {feeds_source} WHERE source = :uri", array(':uri' => $uri))->fetchField();
if (!$id) {
// File is not associated with a feed.
return;
}
// Get the file record based on the URI. If not in the database just return.
$files = file_load_multiple(array(), array('uri' => $uri));
foreach ($files as $item) {
// Since some database servers sometimes use a case-insensitive comparison
// by default, double check that the filename is an exact match.
if ($item->uri === $uri) {
$file = $item;
break;
}
}
if (!isset($file)) {
return;
}
// Check if this file belongs to Feeds.
$usage_list = file_usage_list($file);
if (!isset($usage_list['feeds'])) {
return;
}
if (!feeds_access('import', $id)) {
// User does not have permission to import this feed.
return -1;
}
// Return file headers.
return file_get_content_headers($file);
}
worldfallz
committed
/**
* Feeds API version.
*/
function feeds_api_version() {
$version = feeds_ctools_plugin_api('feeds', 'plugins');
return $version['version'];
}