Open Journal Systems  3.3.0
bagit_fetch.php
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3 
46 {
47 
48  //{{{ Properties
49 
55  var $fileName;
56 
65  var $data;
66 
72  var $fileEncoding;
73 
74  //}}}
75 
76  //{{{ Public methods
77 
85  public function __construct($fileName, $fileEncoding='UTF-8')
86  {
87  $this->fileName = $fileName;
88  $this->fileEncoding = $fileEncoding;
89  $this->data = array();
90 
91  if (file_exists($this->fileName)) {
92  $this->read();
93  }
94  }
95 
101  public function getData()
102  {
103  return $this->data;
104  }
105 
111  public function read()
112  {
113  $lines = readLines($this->fileName, $this->fileEncoding);
114  $fetch = array();
115 
116  foreach ($lines as $line) {
117  $fields = preg_split('/\s+/', $line);
118  if (count($fields) == 3) {
119  array_push(
120  $fetch,
121  array('url' => $fields[0],
122  'length' => $fields[1],
123  'filename' => $fields[2])
124  );
125  }
126  }
127  $this->data = $fetch;
128  }
129 
135  public function write()
136  {
137  $lines = array();
138 
139  foreach ($this->data as $fetch) {
140  $data = array($fetch['url'], $fetch['length'], $fetch['filename']);
141  array_push($lines, join(' ', $data) . "\n");
142  }
143 
144  if (count($lines) == 0) {
145  if (file_exists($this->fileName)) {
146  unlink($this->fileName);
147  }
148  } else {
149  writeFileText($this->fileName, $this->fileEncoding, join('', $lines));
150  }
151  }
152 
158  public function clear()
159  {
160  $this->data = array();
161  file_put_contents($this->fileName, '');
162  }
163 
173  public function add($url, $filename)
174  {
175  array_push(
176  $this->data,
177  array('url' => $url, 'length' => '-', 'filename' => $filename)
178  );
179  $this->write();
180  }
181 
188  public function download()
189  {
190  $basedir = dirname($this->fileName);
191  foreach ($this->data as $fetch) {
192  $filename = $basedir . '/' . $fetch['filename'];
193  if (! file_exists($filename)) {
194  $this->_downloadFile($fetch['url'], $filename);
195  }
196  }
197  }
198 
199  //}}}
200 
201  //{{{ Private methods
202 
211  private function _downloadFile($url, $filename)
212  {
213  $dirname = dirname($filename);
214  if (! is_dir($dirname)) {
215  mkdir($dirname, 0777, true);
216  }
217  saveUrl($url, $filename);
218  }
219 
220  //}}}
221 
222 }
223 
224 
225 /*
226  * Local variables:
227  * tab-width: 4
228  * c-basic-offset: 4
229  * c-hanging-comment-ender-p: nil
230  * End:
231  */
232 
233 
234 ?>
BagItFetch\read
read()
Definition: bagit_fetch.php:120
BagItFetch\download
download()
Definition: bagit_fetch.php:197
BagItFetch\getData
getData()
Definition: bagit_fetch.php:110
BagItFetch\__construct
__construct($fileName, $fileEncoding='UTF-8')
Definition: bagit_fetch.php:94
BagItFetch
Definition: bagit_fetch.php:45
BagItFetch\add
add($url, $filename)
Definition: bagit_fetch.php:182
BagItFetch\$fileEncoding
$fileEncoding
Definition: bagit_fetch.php:81
BagItFetch\clear
clear()
Definition: bagit_fetch.php:167
Seboettg\Collection\count
count()
Definition: ArrayListTrait.php:253
BagItFetch\$data
$data
Definition: bagit_fetch.php:71
BagItFetch\$fileName
$fileName
Definition: bagit_fetch.php:58
BagItFetch\write
write()
Definition: bagit_fetch.php:144