Newer
Older
Alex Barth
committed
/**
* Checks whether a library is present.
*
* @param $file
* The filename to load from.
* @param $library
* 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) {
if (module_exists('libraries') && file_exists(libraries_get_path($library) . "/$file")) {
Alex Barth
committed
return TRUE;
}
elseif (file_exists(DRUPAL_ROOT . '/' . drupal_get_path('module', 'feeds') . "/libraries/$file")) {
Alex Barth
committed
return TRUE;
}
elseif ($library_dir = variable_get('feeds_library_dir', FALSE)) {
if (file_exists("$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;
}
}
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
/**
* @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
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
/**
* 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
);
}
}
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 = feeds_get_feed_nid($entity_id, $entity_type);
if ($feed_nid === FALSE) {
return NULL;
}
return $feed_nid;
}
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
/**
* 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'];
}
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
/**
* Implements hook_form_FORM_ID_alter().
*/
function node_form_feedsentityprocessor_feeds_form_alter(&$form, &$form_state) {
if ($form['#configurable']->entityType() == 'node') {
unset($form['values']['title']);
$form['values']['author']['#required'] = FALSE;
$form['values']['author']['#autocomplete_path'] = 'user/autocomplete';
array_unshift($form['#validate'], 'node_form_feedsentityprocessor_feeds_form_validate');
if (is_numeric($form['values']['author']['#default_value']) &&
$account = user_load($form['values']['author']['#default_value'])) {
$form['values']['author']['#default_value'] = $account->name;
}
}
}
/**
* Validation callback for node_form_feedsentityprocessor_feeds_form_alter().
*/
function node_form_feedsentityprocessor_feeds_form_validate(&$form, &$form_state) {
if (empty($form_state['values']['values']['author'])) {
form_set_value($form['values']['author'], 0, $form_state);
}
else {
$account = user_load_by_name($form_state['values']['values']['author']);
if ($account) {
form_set_value($form['values']['author'], $account->uid, $form_state);
}
}
}
/**
* Implements hook_feeds_processor_targets_alter().
*/
function node_feeds_processor_targets_alter(&$targets, $entity_type, $bundle) {
if ($entity_type == 'node') {
$targets['nid']['name'] = t('Node id');
$targets['nid']['description'] = t('The nid of the node. NOTE: use this feature with care, node ids are usually assigned by Drupal.');
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function user_form_feedsentityprocessor_feeds_form_alter(&$form, &$form_state) {
if ($form['#configurable']->entityType() == 'user') {
unset($form['values']['name']);
$form['values']['mail']['#required'] = FALSE;
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function taxonomy_form_feedsentityprocessor_feeds_form_alter(&$form, &$form_state) {
if ($form['#configurable']->entityType() == 'taxonomy_term') {
unset($form['values']['name']);
if (empty($form['values']['weight']['#default_value'])) {
$form['values']['weight']['#default_value']= '';
}
array_unshift($form['#validate'], 'taxonomy_form_feedsentityprocessor_feeds_form_validate');
unset($form['values']['parent']);
$form['values']['machine_name'] = $form['values']['vocabulary'];
$form['values']['vocabulary']['#access'] = FALSE;
}
elseif ($form['#configurable']->entityType() == 'taxonomy_vocabulary') {
unset($form['values']['name']);
unset($form['values']['machine_name']);
unset($form['values']['vid']);
}
}
/**
* Validation callback for taxonomy_form_feedsentityprocessor_feeds_form_alter().
*/
function taxonomy_form_feedsentityprocessor_feeds_form_validate(&$form, &$form_state) {
if (empty($form_state['values']['values']['weight'])) {
form_set_value($form['values']['weight'], 0, $form_state);
}
form_set_value($form['values']['vocabulary'], $form_state['values']['values']['machine_name'], $form_state);
}