Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
// $Id$
/**
* A FeedsImportBatch is the actual content retrieved from a FeedsSource. On
* import, it is created on the fetching stage and passed through the parsing
* and processing stage where it is normalized and consumed.
*
* @see FeedsSource class
* @see FeedsFetcher class
*/
class FeedsImportBatch {
protected $url;
protected $file_path;
protected $raw;
protected $items;
protected $link;
/**
* Constructor.
*
* Either $url or $file_path must be given.
*/
public function __construct($url = NULL, $file_path = NULL) {
$this->url = $url;
$this->file_path = $file_path;
$this->items = array();
}
/**
* @return
* The raw content of the feed.
*/
public function getRaw() {
if (empty($this->raw)) {
// Prefer file.
if ($this->file_path) {
$this->raw = file_get_contents(realpath($this->file_path));
}
elseif ($this->url) {
feeds_include_library('http_request.inc', 'http_request');
$result = http_request_get($this->url);
if ($result->code != 200) {
throw new Exception(t('Download of @url failed with code !code.', array('@url' => $this->url, '!code' => $result->code)));
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
}
$this->raw = $result->data;
}
}
return $this->raw;
}
/**
* @return
* Path to the feed. This path is relative to Drupal's root directory.
* If the feed is not local, getFilePath downloads it to file directory.
*/
public function getFilePath() {
if (!isset($this->file_path)) {
$dest = file_destination(file_directory_path() .'/feeds/'. get_class($this) .'_'. md5($this->url) .'_'. time(), FILE_EXISTS_RENAME);
$this->file_path = file_save_data($this->getRaw(), $dest);
if($this->file_path === 0) {
throw new Exception(t('Cannot write content to %dest', array('%dest' => $dest)));
}
}
return $this->file_path;
}
/**
* @return
* URL to the document.
*/
public function getURL() {
if (!isset($this->url) && isset($this->file)) {
return $_GLOBALS['base_url'] .'/'. $this->file;
}
}
/**
* @return
* A string that is the feed's title.
*/
public function getTitle() {
return $this->title;
}
/**
* @return
* A string that is the feed's description.
*/
public function getDescription() {
return $this->description;
}
/**
* @return
* A string that is the link to the feed's site (not the actual URL of the
* feed). Falls back to URL if not available.
*/
public function getLink() {
return isset($this->link) ? $this->link : $this->getURL();
}
/**
* @return
* Next available item or NULL if there is none. Every returned item is
* removed from the internal array.
*/
public function shiftItem() {
return array_shift($this->items);
}
/**
* Set title.
*/
public function setTitle($title) {
$this->title = $title;
}
/**
* Set description.
*/
public function setDescription($description) {
$this->description = $description;
}
/**
* Set link.
*/
public function setLink($link) {
$this->link = $link;
}
/**
* Set items.
*/
public function setItems($items) {
$this->items = $items;
}
/**
* Add an item.
*/
public function addItem($item) {
$this->items[] = $item;
}
}