FeedsParser.inc 26 KB

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