FeedsParser.inc

  1. cis7 sites/all/modules/ulmus/feeds/plugins/FeedsParser.inc
  2. cle7 sites/all/modules/ulmus/feeds/plugins/FeedsParser.inc
  3. ecd7 sites/all/modules/ulmus/feeds/plugins/FeedsParser.inc
  4. elmsmedia7 sites/all/modules/ulmus/feeds/plugins/FeedsParser.inc
  5. harmony7 sites/all/modules/ulmus/feeds/plugins/FeedsParser.inc
  6. icor7 sites/all/modules/ulmus/feeds/plugins/FeedsParser.inc
  7. meedjum_blog7 sites/all/modules/ulmus/feeds/plugins/FeedsParser.inc
  8. mooc7 sites/all/modules/ulmus/feeds/plugins/FeedsParser.inc

Contains FeedsParser and related classes.

Functions

Namesort descending Description
feeds_to_unixtime Converts to UNIX time.

Classes

Namesort descending Description
FeedsDateTime Extend PHP DateTime class with granularity handling, merge functionality and slightly more flexible initialization parameters.
FeedsDateTimeElement Defines a date element of a parsed result (including ranges, repeat).
FeedsElement Defines an element of a parsed result. Such an element can be a simple type, a complex type (derived from FeedsElement) or an array of either.
FeedsEnclosure Enclosure element, can be part of the result array.
FeedsGeoTermElement A geo term element.
FeedsParser Abstract class, defines interface for parsers.
FeedsParserResult A result of a parsing stage.
FeedsTermElement Encapsulates a taxonomy style term object.

File

sites/all/modules/ulmus/feeds/plugins/FeedsParser.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Contains FeedsParser and related classes.
  5. */
  6. /**
  7. * A result of a parsing stage.
  8. */
  9. class FeedsParserResult extends FeedsResult {
  10. public $title;
  11. public $description;
  12. public $link;
  13. public $items;
  14. public $current_item;
  15. /**
  16. * Constructor.
  17. */
  18. public function __construct($items = array()) {
  19. $this->title = '';
  20. $this->description = '';
  21. $this->link = '';
  22. $this->items = $items;
  23. }
  24. /**
  25. * @todo Move to a nextItem() based approach, not consuming the item array.
  26. * Can only be done once we don't cache the entire batch object between page
  27. * loads for batching anymore.
  28. *
  29. * @return
  30. * Next available item or NULL if there is none. Every returned item is
  31. * removed from the internal array.
  32. */
  33. public function shiftItem() {
  34. $this->current_item = array_shift($this->items);
  35. return $this->current_item;
  36. }
  37. /**
  38. * @return
  39. * Current result item.
  40. */
  41. public function currentItem() {
  42. return empty($this->current_item) ? NULL : $this->current_item;
  43. }
  44. }
  45. /**
  46. * Abstract class, defines interface for parsers.
  47. */
  48. abstract class FeedsParser extends FeedsPlugin {
  49. /**
  50. * Parse content fetched by fetcher.
  51. *
  52. * Extending classes must implement this method.
  53. *
  54. * @param FeedsSource $source
  55. * Source information.
  56. * @param $fetcher_result
  57. * FeedsFetcherResult returned by fetcher.
  58. */
  59. public abstract function parse(FeedsSource $source, FeedsFetcherResult $fetcher_result);
  60. /**
  61. * Clear all caches for results for given source.
  62. *
  63. * @param FeedsSource $source
  64. * Source information for this expiry. Implementers can choose to only clear
  65. * caches pertaining to this source.
  66. */
  67. public function clear(FeedsSource $source) {}
  68. /**
  69. * Declare the possible mapping sources that this parser produces.
  70. *
  71. * @ingroup mappingapi
  72. *
  73. * @return
  74. * An array of mapping sources, or FALSE if the sources can be defined by
  75. * typing a value in a text field.
  76. *
  77. * Example:
  78. * @code
  79. * array(
  80. * 'title' => t('Title'),
  81. * 'created' => t('Published date'),
  82. * 'url' => t('Feed item URL'),
  83. * 'guid' => t('Feed item GUID'),
  84. * )
  85. * @endcode
  86. */
  87. public function getMappingSources() {
  88. self::loadMappers();
  89. $sources = array();
  90. $content_type = feeds_importer($this->id)->config['content_type'];
  91. drupal_alter('feeds_parser_sources', $sources, $content_type);
  92. if (!feeds_importer($this->id)->config['content_type']) {
  93. return $sources;
  94. }
  95. $sources['parent:uid'] = array(
  96. 'name' => t('Feed node: User ID'),
  97. 'description' => t('The feed node author uid.'),
  98. );
  99. $sources['parent:nid'] = array(
  100. 'name' => t('Feed node: Node ID'),
  101. 'description' => t('The feed node nid.'),
  102. );
  103. return $sources;
  104. }
  105. /**
  106. * Get an element identified by $element_key of the given item.
  107. * The element key corresponds to the values in the array returned by
  108. * FeedsParser::getMappingSources().
  109. *
  110. * This method is invoked from FeedsProcessor::map() when a concrete item is
  111. * processed.
  112. *
  113. * @ingroup mappingapi
  114. *
  115. * @param $batch
  116. * FeedsImportBatch object containing the sources to be mapped from.
  117. * @param $element_key
  118. * The key identifying the element that should be retrieved from $source
  119. *
  120. * @return
  121. * The source element from $item identified by $element_key.
  122. *
  123. * @see FeedsProcessor::map()
  124. * @see FeedsCSVParser::getSourceElement()
  125. */
  126. public function getSourceElement(FeedsSource $source, FeedsParserResult $result, $element_key) {
  127. switch ($element_key) {
  128. case 'parent:uid':
  129. if ($source->feed_nid && $node = node_load($source->feed_nid)) {
  130. return $node->uid;
  131. }
  132. break;
  133. case 'parent:nid':
  134. return $source->feed_nid;
  135. }
  136. $item = $result->currentItem();
  137. return isset($item[$element_key]) ? $item[$element_key] : '';
  138. }
  139. }
  140. /**
  141. * Defines an element of a parsed result. Such an element can be a simple type,
  142. * a complex type (derived from FeedsElement) or an array of either.
  143. *
  144. * @see FeedsEnclosure
  145. */
  146. class FeedsElement {
  147. // The standard value of this element. This value can contain be a simple type,
  148. // a FeedsElement or an array of either.
  149. protected $value;
  150. /**
  151. * Constructor.
  152. */
  153. public function __construct($value) {
  154. $this->value = $value;
  155. }
  156. /**
  157. * @todo Make value public and deprecate use of getValue().
  158. *
  159. * @return
  160. * Value of this FeedsElement represented as a scalar.
  161. */
  162. public function getValue() {
  163. return $this->value;
  164. }
  165. /**
  166. * Magic method __toString() for printing and string conversion of this
  167. * object.
  168. *
  169. * @return
  170. * A string representation of this element.
  171. */
  172. public function __toString() {
  173. if (is_array($this->value)) {
  174. return 'Array';
  175. }
  176. if (is_object($this->value)) {
  177. return 'Object';
  178. }
  179. return (string) $this->getValue();
  180. }
  181. }
  182. /**
  183. * Encapsulates a taxonomy style term object.
  184. *
  185. * Objects of this class can be turned into a taxonomy term style arrays by
  186. * casting them.
  187. *
  188. * @code
  189. * $term_object = new FeedsTermElement($term_array);
  190. * $term_array = (array)$term_object;
  191. * @endcode
  192. */
  193. class FeedsTermElement extends FeedsElement {
  194. public $tid, $vid, $name;
  195. /**
  196. * @param $term
  197. * An array or a stdClass object that is a Drupal taxonomy term.
  198. */
  199. public function __construct($term) {
  200. if (is_array($term)) {
  201. parent::__construct($term['name']);
  202. foreach ($this as $key => $value) {
  203. $this->$key = isset($term[$key]) ? $term[$key] : NULL;
  204. }
  205. }
  206. elseif (is_object($term)) {
  207. parent::__construct($term->name);
  208. foreach ($this as $key => $value) {
  209. $this->$key = isset($term->$key) ? $term->$key : NULL;
  210. }
  211. }
  212. }
  213. /**
  214. * Use $name as $value.
  215. */
  216. public function getValue() {
  217. return $this->name;
  218. }
  219. }
  220. /**
  221. * A geo term element.
  222. */
  223. class FeedsGeoTermElement extends FeedsTermElement {
  224. public $lat, $lon, $bound_top, $bound_right, $bound_bottom, $bound_left, $geometry;
  225. /**
  226. * @param $term
  227. * An array or a stdClass object that is a Drupal taxonomy term. Can include
  228. * geo extensions.
  229. */
  230. public function __construct($term) {
  231. parent::__construct($term);
  232. }
  233. }
  234. /**
  235. * Enclosure element, can be part of the result array.
  236. */
  237. class FeedsEnclosure extends FeedsElement {
  238. protected $mime_type;
  239. /**
  240. * Constructor, requires MIME type.
  241. *
  242. * @param $value
  243. * A path to a local file or a URL to a remote document.
  244. * @param $mimetype
  245. * The mime type of the resource.
  246. */
  247. public function __construct($value, $mime_type) {
  248. parent::__construct($value);
  249. $this->mime_type = $mime_type;
  250. }
  251. /**
  252. * @return
  253. * MIME type of return value of getValue().
  254. */
  255. public function getMIMEType() {
  256. return $this->mime_type;
  257. }
  258. /**
  259. * Use this method instead of FeedsElement::getValue() when fetching the file
  260. * from the URL.
  261. *
  262. * @return
  263. * Value with encoded space characters to safely fetch the file from the URL.
  264. *
  265. * @see FeedsElement::getValue()
  266. */
  267. public function getUrlEncodedValue() {
  268. return str_replace(' ', '%20', $this->getValue());
  269. }
  270. /**
  271. * Use this method instead of FeedsElement::getValue() to get the file name
  272. * transformed for better local saving (underscores instead of spaces)
  273. *
  274. * @return
  275. * Value with space characters changed to underscores.
  276. *
  277. * @see FeedsElement::getValue()
  278. */
  279. public function getLocalValue() {
  280. return str_replace(' ', '_', $this->getValue());
  281. }
  282. /**
  283. * @return
  284. * The content of the referenced resource.
  285. */
  286. public function getContent() {
  287. feeds_include_library('http_request.inc', 'http_request');
  288. $result = http_request_get($this->getUrlEncodedValue());
  289. if ($result->code != 200) {
  290. throw new Exception(t('Download of @url failed with code !code.', array('@url' => $this->getUrlEncodedValue(), '!code' => $result->code)));
  291. }
  292. return $result->data;
  293. }
  294. /**
  295. * Get a Drupal file object of the enclosed resource, download if necessary.
  296. *
  297. * @param $destination
  298. * The path or uri specifying the target directory in which the file is
  299. * expected. Don't use trailing slashes unless it's a streamwrapper scheme.
  300. *
  301. * @return
  302. * A Drupal temporary file object of the enclosed resource.
  303. *
  304. * @throws Exception
  305. * If file object could not be created.
  306. */
  307. public function getFile($destination) {
  308. if ($this->getValue()) {
  309. // Prepare destination directory.
  310. file_prepare_directory($destination, FILE_MODIFY_PERMISSIONS | FILE_CREATE_DIRECTORY);
  311. // Copy or save file depending on whether it is remote or local.
  312. if (drupal_realpath($this->getValue())) {
  313. $file = new stdClass();
  314. $file->uid = 0;
  315. $file->uri = $this->getValue();
  316. $file->filemime = $this->mime_type;
  317. $file->filename = basename($file->uri);
  318. if (dirname($file->uri) != $destination) {
  319. $file = file_copy($file, $destination);
  320. }
  321. else {
  322. // If file is not to be copied, check whether file already exists,
  323. // as file_save() won't do that for us (compare file_copy() and
  324. // file_save())
  325. $existing_files = file_load_multiple(array(), array('uri' => $file->uri));
  326. if (count($existing_files)) {
  327. $existing = reset($existing_files);
  328. $file->fid = $existing->fid;
  329. $file->filename = $existing->filename;
  330. }
  331. file_save($file);
  332. }
  333. }
  334. else {
  335. $filename = basename($this->getLocalValue());
  336. if (module_exists('transliteration')) {
  337. require_once drupal_get_path('module', 'transliteration') . '/transliteration.inc';
  338. $filename = transliteration_clean_filename($filename);
  339. }
  340. if (file_uri_target($destination)) {
  341. $destination = trim($destination, '/') . '/';
  342. }
  343. try {
  344. $file = file_save_data($this->getContent(), $destination . $filename);
  345. }
  346. catch (Exception $e) {
  347. watchdog_exception('Feeds', $e, nl2br(check_plain($e)));
  348. }
  349. }
  350. // We couldn't make sense of this enclosure, throw an exception.
  351. if (!$file) {
  352. throw new Exception(t('Invalid enclosure %enclosure', array('%enclosure' => $this->getValue())));
  353. }
  354. }
  355. return $file;
  356. }
  357. }
  358. /**
  359. * Defines a date element of a parsed result (including ranges, repeat).
  360. */
  361. class FeedsDateTimeElement extends FeedsElement {
  362. // Start date and end date.
  363. public $start;
  364. public $end;
  365. /**
  366. * Constructor.
  367. *
  368. * @param $start
  369. * A FeedsDateTime object or a date as accepted by FeedsDateTime.
  370. * @param $end
  371. * A FeedsDateTime object or a date as accepted by FeedsDateTime.
  372. * @param $tz
  373. * A PHP DateTimeZone object.
  374. */
  375. public function __construct($start = NULL, $end = NULL, $tz = NULL) {
  376. $this->start = (!isset($start) || ($start instanceof FeedsDateTime)) ? $start : new FeedsDateTime($start, $tz);
  377. $this->end = (!isset($end) || ($end instanceof FeedsDateTime)) ? $end : new FeedsDateTime($end, $tz);
  378. }
  379. /**
  380. * Override FeedsElement::getValue().
  381. *
  382. * @return
  383. * The UNIX timestamp of this object's start date. Return value is
  384. * technically a string but will only contain numeric values.
  385. */
  386. public function getValue() {
  387. if ($this->start) {
  388. return $this->start->format('U');
  389. }
  390. return '0';
  391. }
  392. /**
  393. * Merge this field with another. Most stuff goes down when merging the two
  394. * sub-dates.
  395. *
  396. * @see FeedsDateTime
  397. */
  398. public function merge(FeedsDateTimeElement $other) {
  399. $this2 = clone $this;
  400. if ($this->start && $other->start) {
  401. $this2->start = $this->start->merge($other->start);
  402. }
  403. elseif ($other->start) {
  404. $this2->start = clone $other->start;
  405. }
  406. elseif ($this->start) {
  407. $this2->start = clone $this->start;
  408. }
  409. if ($this->end && $other->end) {
  410. $this2->end = $this->end->merge($other->end);
  411. }
  412. elseif ($other->end) {
  413. $this2->end = clone $other->end;
  414. }
  415. elseif ($this->end) {
  416. $this2->end = clone $this->end;
  417. }
  418. return $this2;
  419. }
  420. /**
  421. * Helper method for buildDateField(). Build a FeedsDateTimeElement object
  422. * from a standard formatted node.
  423. */
  424. protected static function readDateField($entity, $field_name) {
  425. $ret = new FeedsDateTimeElement();
  426. if (isset($entity->{$field_name}['und'][0]['date']) && $entity->{$field_name}['und'][0]['date'] instanceof FeedsDateTime) {
  427. $ret->start = $entity->{$field_name}['und'][0]['date'];
  428. }
  429. if (isset($entity->{$field_name}['und'][0]['date2']) && $entity->{$field_name}['und'][0]['date2'] instanceof FeedsDateTime) {
  430. $ret->end = $entity->{$field_name}['und'][0]['date2'];
  431. }
  432. return $ret;
  433. }
  434. /**
  435. * Build a entity's date field from our object.
  436. *
  437. * @param $entity
  438. * The entity to build the date field on.
  439. * @param $field_name
  440. * The name of the field to build.
  441. */
  442. public function buildDateField($entity, $field_name) {
  443. $info = field_info_field($field_name);
  444. $oldfield = FeedsDateTimeElement::readDateField($entity, $field_name);
  445. // Merge with any preexisting objects on the field; we take precedence.
  446. $oldfield = $this->merge($oldfield);
  447. $use_start = $oldfield->start;
  448. $use_end = $oldfield->end;
  449. // Set timezone if not already in the FeedsDateTime object
  450. $to_tz = date_get_timezone($info['settings']['tz_handling'], date_default_timezone());
  451. $temp = new FeedsDateTime(NULL, new DateTimeZone($to_tz));
  452. $db_tz = '';
  453. if ($use_start) {
  454. $use_start = $use_start->merge($temp);
  455. if (!date_timezone_is_valid($use_start->getTimezone()->getName())) {
  456. $use_start->setTimezone(new DateTimeZone("UTC"));
  457. }
  458. $db_tz = date_get_timezone_db($info['settings']['tz_handling'], $use_start->getTimezone()->getName());
  459. }
  460. if ($use_end) {
  461. $use_end = $use_end->merge($temp);
  462. if (!date_timezone_is_valid($use_end->getTimezone()->getName())) {
  463. $use_end->setTimezone(new DateTimeZone("UTC"));
  464. }
  465. if (!$db_tz) {
  466. $db_tz = date_get_timezone_db($info['settings']['tz_handling'], $use_end->getTimezone()->getName());
  467. }
  468. }
  469. if (!$db_tz) {
  470. return;
  471. }
  472. $db_tz = new DateTimeZone($db_tz);
  473. if (!isset($entity->{$field_name})) {
  474. $entity->{$field_name} = array('und' => array());
  475. }
  476. if ($use_start) {
  477. $entity->{$field_name}['und'][0]['timezone'] = $use_start->getTimezone()->getName();
  478. $entity->{$field_name}['und'][0]['offset'] = $use_start->getOffset();
  479. $use_start->setTimezone($db_tz);
  480. $entity->{$field_name}['und'][0]['date'] = $use_start;
  481. /**
  482. * @todo the date_type_format line could be simplified based upon a patch
  483. * DO issue #259308 could affect this, follow up on at some point.
  484. * Without this, all granularity info is lost.
  485. * $use_start->format(date_type_format($field['type'], $use_start->granularity));
  486. */
  487. $entity->{$field_name}['und'][0]['value'] = $use_start->format(date_type_format($info['type']));
  488. }
  489. if ($use_end) {
  490. // Don't ever use end to set timezone (for now)
  491. $entity->{$field_name}['und'][0]['offset2'] = $use_end->getOffset();
  492. $use_end->setTimezone($db_tz);
  493. $entity->{$field_name}['und'][0]['date2'] = $use_end;
  494. $entity->{$field_name}['und'][0]['value2'] = $use_end->format(date_type_format($info['type']));
  495. }
  496. }
  497. }
  498. /**
  499. * Extend PHP DateTime class with granularity handling, merge functionality and
  500. * slightly more flexible initialization parameters.
  501. *
  502. * This class is a Drupal independent extension of the >= PHP 5.2 DateTime
  503. * class.
  504. *
  505. * @see FeedsDateTimeElement
  506. */
  507. class FeedsDateTime extends DateTime {
  508. public $granularity = array();
  509. protected static $allgranularity = array('year', 'month', 'day', 'hour', 'minute', 'second', 'zone');
  510. private $_serialized_time;
  511. private $_serialized_timezone;
  512. /**
  513. * Helper function to prepare the object during serialization.
  514. *
  515. * We are extending a core class and core classes cannot be serialized.
  516. *
  517. * Ref: http://bugs.php.net/41334, http://bugs.php.net/39821
  518. */
  519. public function __sleep() {
  520. $this->_serialized_time = $this->format('c');
  521. $this->_serialized_timezone = $this->getTimezone()->getName();
  522. return array('_serialized_time', '_serialized_timezone');
  523. }
  524. /**
  525. * Upon unserializing, we must re-build ourselves using local variables.
  526. */
  527. public function __wakeup() {
  528. $this->__construct($this->_serialized_time, new DateTimeZone($this->_serialized_timezone));
  529. }
  530. /**
  531. * Overridden constructor.
  532. *
  533. * @param $time
  534. * time string, flexible format including timestamp. Invalid formats will
  535. * fall back to 'now'.
  536. * @param $tz
  537. * PHP DateTimeZone object, NULL allowed
  538. */
  539. public function __construct($time = '', $tz = NULL) {
  540. // Assume UNIX timestamp if numeric.
  541. if (is_numeric($time)) {
  542. // Make sure it's not a simple year
  543. if ((is_string($time) && strlen($time) > 4) || is_int($time)) {
  544. $time = "@" . $time;
  545. }
  546. }
  547. // PHP < 5.3 doesn't like the GMT- notation for parsing timezones.
  548. $time = str_replace("GMT-", "-", $time);
  549. $time = str_replace("GMT+", "+", $time);
  550. // Some PHP 5.2 version's DateTime class chokes on invalid dates.
  551. if (!strtotime($time)) {
  552. $time = 'now';
  553. }
  554. // Create and set time zone separately, PHP 5.2.6 does not respect time zone
  555. // argument in __construct().
  556. parent::__construct($time);
  557. $tz = $tz ? $tz : new DateTimeZone("UTC");
  558. $this->setTimeZone($tz);
  559. // Verify that timezone has not been specified as an offset.
  560. if (!preg_match('/[a-zA-Z]/', $this->getTimezone()->getName())) {
  561. $this->setTimezone(new DateTimeZone("UTC"));
  562. }
  563. // Finally set granularity.
  564. $this->setGranularityFromTime($time, $tz);
  565. }
  566. /**
  567. * This function will keep this object's values by default.
  568. */
  569. public function merge(FeedsDateTime $other) {
  570. $other_tz = $other->getTimezone();
  571. $this_tz = $this->getTimezone();
  572. // Figure out which timezone to use for combination.
  573. $use_tz = ($this->hasGranularity('zone') || !$other->hasGranularity('zone')) ? $this_tz : $other_tz;
  574. $this2 = clone $this;
  575. $this2->setTimezone($use_tz);
  576. $other->setTimezone($use_tz);
  577. $val = $this2->toArray();
  578. $otherval = $other->toArray();
  579. foreach (self::$allgranularity as $g) {
  580. if ($other->hasGranularity($g) && !$this2->hasGranularity($g)) {
  581. // The other class has a property we don't; steal it.
  582. $this2->addGranularity($g);
  583. $val[$g] = $otherval[$g];
  584. }
  585. }
  586. $other->setTimezone($other_tz);
  587. $this2->setDate($val['year'], $val['month'], $val['day']);
  588. $this2->setTime($val['hour'], $val['minute'], $val['second']);
  589. return $this2;
  590. }
  591. /**
  592. * Overrides default DateTime function. Only changes output values if
  593. * actually had time granularity. This should be used as a "converter" for
  594. * output, to switch tzs.
  595. *
  596. * In order to set a timezone for a datetime that doesn't have such
  597. * granularity, merge() it with one that does.
  598. */
  599. public function setTimezone($tz, $force = FALSE) {
  600. // PHP 5.2.6 has a fatal error when setting a date's timezone to itself.
  601. // http://bugs.php.net/bug.php?id=45038
  602. if (version_compare(PHP_VERSION, '5.2.7', '<') && $tz == $this->getTimezone()) {
  603. $tz = new DateTimeZone($tz->getName());
  604. }
  605. if (!$this->hasTime() || !$this->hasGranularity('zone') || $force) {
  606. // this has no time or timezone granularity, so timezone doesn't mean much
  607. // We set the timezone using the method, which will change the day/hour, but then we switch back
  608. $arr = $this->toArray();
  609. parent::setTimezone($tz);
  610. $this->setDate($arr['year'], $arr['month'], $arr['day']);
  611. $this->setTime($arr['hour'], $arr['minute'], $arr['second']);
  612. return;
  613. }
  614. parent::setTimezone($tz);
  615. }
  616. /**
  617. * Safely adds a granularity entry to the array.
  618. */
  619. public function addGranularity($g) {
  620. $this->granularity[] = $g;
  621. $this->granularity = array_unique($this->granularity);
  622. }
  623. /**
  624. * Removes a granularity entry from the array.
  625. */
  626. public function removeGranularity($g) {
  627. if ($key = array_search($g, $this->granularity)) {
  628. unset($this->granularity[$key]);
  629. }
  630. }
  631. /**
  632. * Checks granularity array for a given entry.
  633. */
  634. public function hasGranularity($g) {
  635. return in_array($g, $this->granularity);
  636. }
  637. /**
  638. * Returns whether this object has time set. Used primarily for timezone
  639. * conversion and fomratting.
  640. *
  641. * @todo currently very simplistic, but effective, see usage
  642. */
  643. public function hasTime() {
  644. return $this->hasGranularity('hour');
  645. }
  646. /**
  647. * Protected function to find the granularity given by the arguments to the
  648. * constructor.
  649. */
  650. protected function setGranularityFromTime($time, $tz) {
  651. $this->granularity = array();
  652. $temp = date_parse($time);
  653. // This PHP method currently doesn't have resolution down to seconds, so if
  654. // there is some time, all will be set.
  655. foreach (self::$allgranularity AS $g) {
  656. if ((isset($temp[$g]) && is_numeric($temp[$g])) || ($g == 'zone' && (isset($temp['zone_type']) && $temp['zone_type'] > 0))) {
  657. $this->granularity[] = $g;
  658. }
  659. }
  660. if ($tz) {
  661. $this->addGranularity('zone');
  662. }
  663. }
  664. /**
  665. * Helper to return all standard date parts in an array.
  666. */
  667. protected function toArray() {
  668. return array('year' => $this->format('Y'), 'month' => $this->format('m'), 'day' => $this->format('d'), 'hour' => $this->format('H'), 'minute' => $this->format('i'), 'second' => $this->format('s'), 'zone' => $this->format('e'));
  669. }
  670. }
  671. /**
  672. * Converts to UNIX time.
  673. *
  674. * @param $date
  675. * A date that is either a string, a FeedsDateTimeElement or a UNIX timestamp.
  676. * @param $default_value
  677. * A default UNIX timestamp to return if $date could not be parsed.
  678. *
  679. * @return
  680. * $date as UNIX time if conversion was successful, $dfeault_value otherwise.
  681. */
  682. function feeds_to_unixtime($date, $default_value) {
  683. if (is_numeric($date)) {
  684. return $date;
  685. }
  686. elseif (is_string($date) && !empty($date)) {
  687. $date = new FeedsDateTimeElement($date);
  688. return $date->getValue();
  689. }
  690. elseif ($date instanceof FeedsDateTimeElement) {
  691. return $date->getValue();
  692. }
  693. return $default_value;
  694. }
Error | ELMSLN API

Error

×

Error message

  • Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/elmsln_community/api.elmsln.org/includes/common.inc:2791) in drupal_send_headers() (line 1499 of /var/www/html/elmsln_community/api.elmsln.org/includes/bootstrap.inc).
  • Error: Call to undefined function apc_delete() in DrupalAPCCache->clear() (line 289 of /var/www/html/elmsln_community/api.elmsln.org/sites/all/modules/apc/drupal_apc_cache.inc).
The website encountered an unexpected error. Please try again later.