1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Definition of FeedsSourceInterface and FeedsSource class.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Distinguish exceptions occuring when handling locks.
|
10
|
*/
|
11
|
class FeedsLockException extends Exception {}
|
12
|
|
13
|
/**
|
14
|
* Denote a import or clearing stage. Used for multi page processing.
|
15
|
*/
|
16
|
define('FEEDS_START', 'start_time');
|
17
|
define('FEEDS_FETCH', 'fetch');
|
18
|
define('FEEDS_PARSE', 'parse');
|
19
|
define('FEEDS_PROCESS', 'process');
|
20
|
define('FEEDS_PROCESS_CLEAR', 'process_clear');
|
21
|
define('FEEDS_PROCESS_EXPIRE', 'process_expire');
|
22
|
|
23
|
/**
|
24
|
* Declares an interface for a class that defines default values and form
|
25
|
* descriptions for a FeedSource.
|
26
|
*/
|
27
|
interface FeedsSourceInterface {
|
28
|
|
29
|
/**
|
30
|
* Crutch: for ease of use, we implement FeedsSourceInterface for every
|
31
|
* plugin, but then we need to have a handle which plugin actually implements
|
32
|
* a source.
|
33
|
*
|
34
|
* @see FeedsPlugin class.
|
35
|
*
|
36
|
* @return
|
37
|
* TRUE if a plugin handles source specific configuration, FALSE otherwise.
|
38
|
*/
|
39
|
public function hasSourceConfig();
|
40
|
|
41
|
/**
|
42
|
* Return an associative array of default values.
|
43
|
*/
|
44
|
public function sourceDefaults();
|
45
|
|
46
|
/**
|
47
|
* Return a Form API form array that defines a form configuring values. Keys
|
48
|
* correspond to the keys of the return value of sourceDefaults().
|
49
|
*/
|
50
|
public function sourceForm($source_config);
|
51
|
|
52
|
/**
|
53
|
* Validate user entered values submitted by sourceForm().
|
54
|
*/
|
55
|
public function sourceFormValidate(&$source_config);
|
56
|
|
57
|
/**
|
58
|
* A source is being saved.
|
59
|
*/
|
60
|
public function sourceSave(FeedsSource $source);
|
61
|
|
62
|
/**
|
63
|
* A source is being deleted.
|
64
|
*/
|
65
|
public function sourceDelete(FeedsSource $source);
|
66
|
}
|
67
|
|
68
|
/**
|
69
|
* Status of an import or clearing operation on a source.
|
70
|
*/
|
71
|
class FeedsState {
|
72
|
/**
|
73
|
* Floating point number denoting the progress made. 0.0 meaning no progress
|
74
|
* 1.0 = FEEDS_BATCH_COMPLETE meaning finished.
|
75
|
*/
|
76
|
public $progress;
|
77
|
|
78
|
/**
|
79
|
* Used as a pointer to store where left off. Must be serializable.
|
80
|
*/
|
81
|
public $pointer;
|
82
|
|
83
|
/**
|
84
|
* Natural numbers denoting more details about the progress being made.
|
85
|
*/
|
86
|
public $total;
|
87
|
public $created;
|
88
|
public $updated;
|
89
|
public $deleted;
|
90
|
public $unpublished;
|
91
|
public $blocked;
|
92
|
public $skipped;
|
93
|
public $failed;
|
94
|
|
95
|
/**
|
96
|
* IDs of entities to be removed.
|
97
|
*/
|
98
|
public $removeList;
|
99
|
|
100
|
/**
|
101
|
* Constructor, initialize variables.
|
102
|
*/
|
103
|
public function __construct() {
|
104
|
$this->progress = FEEDS_BATCH_COMPLETE;
|
105
|
$this->total =
|
106
|
$this->created =
|
107
|
$this->updated =
|
108
|
$this->deleted =
|
109
|
$this->unpublished =
|
110
|
$this->blocked =
|
111
|
$this->skipped =
|
112
|
$this->failed = 0;
|
113
|
}
|
114
|
|
115
|
/**
|
116
|
* Safely report progress.
|
117
|
*
|
118
|
* When $total == $progress, the state of the task tracked by this state is
|
119
|
* regarded to be complete.
|
120
|
*
|
121
|
* Handles the following cases gracefully:
|
122
|
*
|
123
|
* - $total is 0
|
124
|
* - $progress is larger than $total
|
125
|
* - $progress approximates $total so that $finished rounds to 1.0
|
126
|
*
|
127
|
* @param $total
|
128
|
* A natural number that is the total to be worked off.
|
129
|
* @param $progress
|
130
|
* A natural number that is the progress made on $total.
|
131
|
*/
|
132
|
public function progress($total, $progress) {
|
133
|
if ($progress > $total) {
|
134
|
$this->progress = FEEDS_BATCH_COMPLETE;
|
135
|
}
|
136
|
elseif ($total) {
|
137
|
$this->progress = $progress / $total;
|
138
|
if ($this->progress == FEEDS_BATCH_COMPLETE && $total != $progress) {
|
139
|
$this->progress = 0.99;
|
140
|
}
|
141
|
}
|
142
|
else {
|
143
|
$this->progress = FEEDS_BATCH_COMPLETE;
|
144
|
}
|
145
|
}
|
146
|
}
|
147
|
|
148
|
/**
|
149
|
* This class encapsulates a source of a feed. It stores where the feed can be
|
150
|
* found and how to import it.
|
151
|
*
|
152
|
* Information on how to import a feed is encapsulated in a FeedsImporter object
|
153
|
* which is identified by the common id of the FeedsSource and the
|
154
|
* FeedsImporter. More than one FeedsSource can use the same FeedsImporter
|
155
|
* therefore a FeedsImporter never holds a pointer to a FeedsSource object, nor
|
156
|
* does it hold any other information for a particular FeedsSource object.
|
157
|
*
|
158
|
* Classes extending FeedsPlugin can implement a sourceForm to expose
|
159
|
* configuration for a FeedsSource object. This is for instance how FeedsFetcher
|
160
|
* exposes a text field for a feed URL or how FeedsCSVParser exposes a select
|
161
|
* field for choosing between colon or semicolon delimiters.
|
162
|
*
|
163
|
* It is important that a FeedsPlugin does not directly hold information about
|
164
|
* a source but leave all storage up to FeedsSource. An instance of a
|
165
|
* FeedsPlugin class only exists once per FeedsImporter configuration, while an
|
166
|
* instance of a FeedsSource class exists once per feed_nid to be imported.
|
167
|
*
|
168
|
* As with FeedsImporter, the idea with FeedsSource is that it can be used
|
169
|
* without actually saving the object to the database.
|
170
|
*/
|
171
|
class FeedsSource extends FeedsConfigurable {
|
172
|
|
173
|
// Contains the node id of the feed this source info object is attached to.
|
174
|
// Equals 0 if not attached to any node - i. e. if used on a
|
175
|
// standalone import form within Feeds or by other API users.
|
176
|
protected $feed_nid;
|
177
|
|
178
|
// The FeedsImporter object that this source is expected to be used with.
|
179
|
protected $importer;
|
180
|
|
181
|
// A FeedsSourceState object holding the current import/clearing state of this
|
182
|
// source.
|
183
|
protected $state;
|
184
|
|
185
|
// Fetcher result, used to cache fetcher result when batching.
|
186
|
protected $fetcher_result;
|
187
|
|
188
|
// Timestamp when this source was imported the last time.
|
189
|
protected $imported;
|
190
|
|
191
|
// Holds an exception object in case an exception occurs during importing.
|
192
|
protected $exception;
|
193
|
|
194
|
/**
|
195
|
* Instantiate a unique object per class/id/feed_nid. Don't use
|
196
|
* directly, use feeds_source() instead.
|
197
|
*/
|
198
|
public static function instance($importer_id, $feed_nid) {
|
199
|
$class = variable_get('feeds_source_class', 'FeedsSource');
|
200
|
static $instances = array();
|
201
|
if (!isset($instances[$class][$importer_id][$feed_nid])) {
|
202
|
$instances[$class][$importer_id][$feed_nid] = new $class($importer_id, $feed_nid);
|
203
|
}
|
204
|
return $instances[$class][$importer_id][$feed_nid];
|
205
|
}
|
206
|
|
207
|
/**
|
208
|
* Constructor.
|
209
|
*/
|
210
|
protected function __construct($importer_id, $feed_nid) {
|
211
|
$this->feed_nid = $feed_nid;
|
212
|
$this->importer = feeds_importer($importer_id);
|
213
|
parent::__construct($importer_id);
|
214
|
$this->load();
|
215
|
}
|
216
|
|
217
|
/**
|
218
|
* Returns the FeedsImporter object that this source is expected to be used with.
|
219
|
*/
|
220
|
public function importer() {
|
221
|
return $this->importer;
|
222
|
}
|
223
|
|
224
|
/**
|
225
|
* Preview = fetch and parse a feed.
|
226
|
*
|
227
|
* @return
|
228
|
* FeedsParserResult object.
|
229
|
*
|
230
|
* @throws
|
231
|
* Throws Exception if an error occurs when fetching or parsing.
|
232
|
*/
|
233
|
public function preview() {
|
234
|
$result = $this->importer->fetcher->fetch($this);
|
235
|
$result = $this->importer->parser->parse($this, $result);
|
236
|
module_invoke_all('feeds_after_parse', $this, $result);
|
237
|
return $result;
|
238
|
}
|
239
|
|
240
|
/**
|
241
|
* Start importing a source.
|
242
|
*
|
243
|
* This method starts an import job. Depending on the configuration of the
|
244
|
* importer of this source, a Batch API job or a background job with Job
|
245
|
* Scheduler will be created.
|
246
|
*
|
247
|
* @throws Exception
|
248
|
* If processing in background is enabled, the first batch chunk of the
|
249
|
* import will be executed on the current page request. This means that this
|
250
|
* method may throw the same exceptions as FeedsSource::import().
|
251
|
*/
|
252
|
public function startImport() {
|
253
|
$config = $this->importer->getConfig();
|
254
|
if ($config['process_in_background']) {
|
255
|
$this->startBackgroundJob('import');
|
256
|
}
|
257
|
else {
|
258
|
$this->startBatchAPIJob(t('Importing'), 'import');
|
259
|
}
|
260
|
}
|
261
|
|
262
|
/**
|
263
|
* Start deleting all imported items of a source.
|
264
|
*
|
265
|
* This method starts a clear job. Depending on the configuration of the
|
266
|
* importer of this source, a Batch API job or a background job with Job
|
267
|
* Scheduler will be created.
|
268
|
*
|
269
|
* @throws Exception
|
270
|
* If processing in background is enabled, the first batch chunk of the
|
271
|
* clear task will be executed on the current page request. This means that
|
272
|
* this method may throw the same exceptions as FeedsSource::clear().
|
273
|
*/
|
274
|
public function startClear() {
|
275
|
$config = $this->importer->getConfig();
|
276
|
if ($config['process_in_background']) {
|
277
|
$this->startBackgroundJob('clear');
|
278
|
}
|
279
|
else {
|
280
|
$this->startBatchAPIJob(t('Deleting'), 'clear');
|
281
|
}
|
282
|
}
|
283
|
|
284
|
/**
|
285
|
* Schedule all periodic tasks for this source.
|
286
|
*/
|
287
|
public function schedule() {
|
288
|
$this->scheduleImport();
|
289
|
$this->scheduleExpire();
|
290
|
}
|
291
|
|
292
|
/**
|
293
|
* Schedule periodic or background import tasks.
|
294
|
*/
|
295
|
public function scheduleImport() {
|
296
|
// Check whether any fetcher is overriding the import period.
|
297
|
$period = $this->importer->config['import_period'];
|
298
|
$fetcher_period = $this->importer->fetcher->importPeriod($this);
|
299
|
if (is_numeric($fetcher_period)) {
|
300
|
$period = $fetcher_period;
|
301
|
}
|
302
|
$job = array(
|
303
|
'type' => $this->id,
|
304
|
'id' => $this->feed_nid,
|
305
|
'period' => $period,
|
306
|
'periodic' => TRUE,
|
307
|
);
|
308
|
if ($period == FEEDS_SCHEDULE_NEVER && $this->progressImporting() === FEEDS_BATCH_COMPLETE) {
|
309
|
JobScheduler::get('feeds_source_import')->remove($job);
|
310
|
}
|
311
|
elseif ($this->progressImporting() === FEEDS_BATCH_COMPLETE) {
|
312
|
JobScheduler::get('feeds_source_import')->set($job);
|
313
|
}
|
314
|
else {
|
315
|
// Feed is not fully imported yet, so we put this job back in the queue
|
316
|
// immediately for further processing.
|
317
|
$queue = DrupalQueue::get('feeds_source_import');
|
318
|
$queue->createItem($job);
|
319
|
}
|
320
|
}
|
321
|
|
322
|
/**
|
323
|
* Schedule background expire tasks.
|
324
|
*/
|
325
|
public function scheduleExpire() {
|
326
|
// Schedule as soon as possible if a batch is active.
|
327
|
$period = $this->progressExpiring() === FEEDS_BATCH_COMPLETE ? 3600 : 0;
|
328
|
|
329
|
$job = array(
|
330
|
'type' => $this->id,
|
331
|
'id' => $this->feed_nid,
|
332
|
'period' => $period,
|
333
|
'periodic' => TRUE,
|
334
|
);
|
335
|
if ($this->importer->processor->expiryTime() == FEEDS_EXPIRE_NEVER) {
|
336
|
JobScheduler::get('feeds_source_expire')->remove($job);
|
337
|
}
|
338
|
else {
|
339
|
JobScheduler::get('feeds_source_expire')->set($job);
|
340
|
}
|
341
|
}
|
342
|
|
343
|
/**
|
344
|
* Schedule background clearing tasks.
|
345
|
*/
|
346
|
public function scheduleClear() {
|
347
|
$job = array(
|
348
|
'type' => $this->id,
|
349
|
'id' => $this->feed_nid,
|
350
|
'period' => 0,
|
351
|
'periodic' => TRUE,
|
352
|
);
|
353
|
// Remove job if batch is complete.
|
354
|
if ($this->progressClearing() === FEEDS_BATCH_COMPLETE) {
|
355
|
JobScheduler::get('feeds_source_clear')->remove($job);
|
356
|
}
|
357
|
// Schedule as soon as possible if batch is not complete.
|
358
|
else {
|
359
|
JobScheduler::get('feeds_source_clear')->set($job);
|
360
|
}
|
361
|
}
|
362
|
|
363
|
/**
|
364
|
* Import a source: execute fetching, parsing and processing stage.
|
365
|
*
|
366
|
* This method only executes the current batch chunk, then returns. If you are
|
367
|
* looking to import an entire source, use FeedsSource::startImport() instead.
|
368
|
*
|
369
|
* @return
|
370
|
* FEEDS_BATCH_COMPLETE if the import process finished. A decimal between
|
371
|
* 0.0 and 0.9 periodic if import is still in progress.
|
372
|
*
|
373
|
* @throws
|
374
|
* Throws Exception if an error occurs when importing.
|
375
|
*/
|
376
|
public function import() {
|
377
|
$this->acquireLock();
|
378
|
try {
|
379
|
// If fetcher result is empty, we are starting a new import, log.
|
380
|
if (empty($this->fetcher_result)) {
|
381
|
module_invoke_all('feeds_before_import', $this);
|
382
|
$this->state[FEEDS_START] = time();
|
383
|
}
|
384
|
|
385
|
// Fetch.
|
386
|
if (empty($this->fetcher_result) || FEEDS_BATCH_COMPLETE == $this->progressParsing()) {
|
387
|
$this->fetcher_result = $this->importer->fetcher->fetch($this);
|
388
|
// Clean the parser's state, we are parsing an entirely new file.
|
389
|
unset($this->state[FEEDS_PARSE]);
|
390
|
}
|
391
|
|
392
|
// Parse.
|
393
|
$parser_result = $this->importer->parser->parse($this, $this->fetcher_result);
|
394
|
module_invoke_all('feeds_after_parse', $this, $parser_result);
|
395
|
|
396
|
// Process.
|
397
|
$this->importer->processor->process($this, $parser_result);
|
398
|
|
399
|
// Import finished without exceptions, so unset any potentially previously
|
400
|
// recorded exceptions.
|
401
|
unset($this->exception);
|
402
|
}
|
403
|
catch (Exception $e) {
|
404
|
// $e is stored and re-thrown once we've had a chance to log our progress.
|
405
|
// Set the exception so that other modules can check if an exception
|
406
|
// occurred in hook_feeds_after_import().
|
407
|
$this->exception = $e;
|
408
|
}
|
409
|
$this->releaseLock();
|
410
|
|
411
|
// Clean up.
|
412
|
$result = $this->progressImporting();
|
413
|
if ($result == FEEDS_BATCH_COMPLETE || isset($e)) {
|
414
|
$this->imported = time();
|
415
|
$this->log('import', 'Imported in !s s', array('!s' => $this->imported - $this->state[FEEDS_START]), WATCHDOG_INFO);
|
416
|
module_invoke_all('feeds_after_import', $this);
|
417
|
unset($this->fetcher_result, $this->state);
|
418
|
}
|
419
|
$this->save();
|
420
|
if (isset($e)) {
|
421
|
throw $e;
|
422
|
}
|
423
|
return $result;
|
424
|
}
|
425
|
|
426
|
/**
|
427
|
* Remove all items from a feed.
|
428
|
*
|
429
|
* This method only executes the current batch chunk, then returns. If you are
|
430
|
* looking to delete all items of a source, use FeedsSource::startClear()
|
431
|
* instead.
|
432
|
*
|
433
|
* @return
|
434
|
* FEEDS_BATCH_COMPLETE if the clearing process finished. A decimal between
|
435
|
* 0.0 and 0.9 periodic if clearing is still in progress.
|
436
|
*
|
437
|
* @throws
|
438
|
* Throws Exception if an error occurs when clearing.
|
439
|
*/
|
440
|
public function clear() {
|
441
|
$this->acquireLock();
|
442
|
try {
|
443
|
$this->importer->fetcher->clear($this);
|
444
|
$this->importer->parser->clear($this);
|
445
|
$this->importer->processor->clear($this);
|
446
|
}
|
447
|
catch (Exception $e) {
|
448
|
// $e is stored and re-thrown once we've had a chance to log our progress.
|
449
|
}
|
450
|
$this->releaseLock();
|
451
|
|
452
|
// Clean up.
|
453
|
$result = $this->progressClearing();
|
454
|
if ($result == FEEDS_BATCH_COMPLETE || isset($e)) {
|
455
|
module_invoke_all('feeds_after_clear', $this);
|
456
|
unset($this->state);
|
457
|
}
|
458
|
$this->save();
|
459
|
if (isset($e)) {
|
460
|
throw $e;
|
461
|
}
|
462
|
return $result;
|
463
|
}
|
464
|
|
465
|
/**
|
466
|
* Removes all expired items from a feed.
|
467
|
*/
|
468
|
public function expire() {
|
469
|
$this->acquireLock();
|
470
|
try {
|
471
|
$result = $this->importer->processor->expire($this);
|
472
|
}
|
473
|
catch (Exception $e) {
|
474
|
// Will throw after the lock is released.
|
475
|
}
|
476
|
$this->releaseLock();
|
477
|
|
478
|
if (isset($e)) {
|
479
|
throw $e;
|
480
|
}
|
481
|
|
482
|
return $result;
|
483
|
}
|
484
|
|
485
|
/**
|
486
|
* Report progress as float between 0 and 1. 1 = FEEDS_BATCH_COMPLETE.
|
487
|
*/
|
488
|
public function progressParsing() {
|
489
|
return $this->state(FEEDS_PARSE)->progress;
|
490
|
}
|
491
|
|
492
|
/**
|
493
|
* Report progress as float between 0 and 1. 1 = FEEDS_BATCH_COMPLETE.
|
494
|
*/
|
495
|
public function progressImporting() {
|
496
|
$fetcher = $this->state(FEEDS_FETCH);
|
497
|
$parser = $this->state(FEEDS_PARSE);
|
498
|
if ($fetcher->progress == FEEDS_BATCH_COMPLETE && $parser->progress == FEEDS_BATCH_COMPLETE) {
|
499
|
return FEEDS_BATCH_COMPLETE;
|
500
|
}
|
501
|
// Fetching envelops parsing.
|
502
|
// @todo: this assumes all fetchers neatly use total. May not be the case.
|
503
|
$fetcher_fraction = $fetcher->total ? 1.0 / $fetcher->total : 1.0;
|
504
|
$parser_progress = $parser->progress * $fetcher_fraction;
|
505
|
$result = $fetcher->progress - $fetcher_fraction + $parser_progress;
|
506
|
if ($result == FEEDS_BATCH_COMPLETE) {
|
507
|
return 0.99;
|
508
|
}
|
509
|
return $result;
|
510
|
}
|
511
|
|
512
|
/**
|
513
|
* Report progress on clearing.
|
514
|
*/
|
515
|
public function progressClearing() {
|
516
|
return $this->state(FEEDS_PROCESS_CLEAR)->progress;
|
517
|
}
|
518
|
|
519
|
/**
|
520
|
* Report progress on expiry.
|
521
|
*/
|
522
|
public function progressExpiring() {
|
523
|
return $this->state(FEEDS_PROCESS_EXPIRE)->progress;
|
524
|
}
|
525
|
|
526
|
/**
|
527
|
* Return a state object for a given stage. Lazy instantiates new states.
|
528
|
*
|
529
|
* @todo Rename getConfigFor() accordingly to config().
|
530
|
*
|
531
|
* @param $stage
|
532
|
* One of FEEDS_FETCH, FEEDS_PARSE, FEEDS_PROCESS or FEEDS_PROCESS_CLEAR.
|
533
|
*
|
534
|
* @return
|
535
|
* The FeedsState object for the given stage.
|
536
|
*/
|
537
|
public function state($stage) {
|
538
|
if (!is_array($this->state)) {
|
539
|
$this->state = array();
|
540
|
}
|
541
|
if (!isset($this->state[$stage])) {
|
542
|
$this->state[$stage] = new FeedsState();
|
543
|
}
|
544
|
return $this->state[$stage];
|
545
|
}
|
546
|
|
547
|
/**
|
548
|
* Count items imported by this source.
|
549
|
*/
|
550
|
public function itemCount() {
|
551
|
return $this->importer->processor->itemCount($this);
|
552
|
}
|
553
|
|
554
|
/**
|
555
|
* Save configuration.
|
556
|
*/
|
557
|
public function save() {
|
558
|
// Alert implementers of FeedsSourceInterface to the fact that we're saving.
|
559
|
foreach ($this->importer->plugin_types as $type) {
|
560
|
$this->importer->$type->sourceSave($this);
|
561
|
}
|
562
|
$config = $this->getConfig();
|
563
|
|
564
|
// Store the source property of the fetcher in a separate column so that we
|
565
|
// can do fast lookups on it.
|
566
|
$source = '';
|
567
|
if (isset($config[get_class($this->importer->fetcher)]['source'])) {
|
568
|
$source = $config[get_class($this->importer->fetcher)]['source'];
|
569
|
}
|
570
|
$object = array(
|
571
|
'id' => $this->id,
|
572
|
'feed_nid' => $this->feed_nid,
|
573
|
'imported' => $this->imported,
|
574
|
'config' => $config,
|
575
|
'source' => $source,
|
576
|
'state' => isset($this->state) ? $this->state : FALSE,
|
577
|
'fetcher_result' => isset($this->fetcher_result) ? $this->fetcher_result : FALSE,
|
578
|
);
|
579
|
if (db_query_range("SELECT 1 FROM {feeds_source} WHERE id = :id AND feed_nid = :nid", 0, 1, array(':id' => $this->id, ':nid' => $this->feed_nid))->fetchField()) {
|
580
|
drupal_write_record('feeds_source', $object, array('id', 'feed_nid'));
|
581
|
}
|
582
|
else {
|
583
|
drupal_write_record('feeds_source', $object);
|
584
|
}
|
585
|
}
|
586
|
|
587
|
/**
|
588
|
* Load configuration and unpack.
|
589
|
*
|
590
|
* @todo Patch CTools to move constants from export.inc to ctools.module.
|
591
|
*/
|
592
|
public function load() {
|
593
|
if ($record = db_query("SELECT imported, config, state, fetcher_result FROM {feeds_source} WHERE id = :id AND feed_nid = :nid", array(':id' => $this->id, ':nid' => $this->feed_nid))->fetchObject()) {
|
594
|
// While FeedsSource cannot be exported, we still use CTool's export.inc
|
595
|
// export definitions.
|
596
|
ctools_include('export');
|
597
|
$this->export_type = EXPORT_IN_DATABASE;
|
598
|
$this->imported = $record->imported;
|
599
|
$this->config = unserialize($record->config);
|
600
|
if (!empty($record->state)) {
|
601
|
$this->state = unserialize($record->state);
|
602
|
}
|
603
|
if (!is_array($this->state)) {
|
604
|
$this->state = array();
|
605
|
}
|
606
|
if (!empty($record->fetcher_result)) {
|
607
|
$this->fetcher_result = unserialize($record->fetcher_result);
|
608
|
}
|
609
|
}
|
610
|
}
|
611
|
|
612
|
/**
|
613
|
* Delete configuration. Removes configuration information
|
614
|
* from database, does not delete configuration itself.
|
615
|
*/
|
616
|
public function delete() {
|
617
|
// Alert implementers of FeedsSourceInterface to the fact that we're
|
618
|
// deleting.
|
619
|
foreach ($this->importer->plugin_types as $type) {
|
620
|
$this->importer->$type->sourceDelete($this);
|
621
|
}
|
622
|
db_delete('feeds_source')
|
623
|
->condition('id', $this->id)
|
624
|
->condition('feed_nid', $this->feed_nid)
|
625
|
->execute();
|
626
|
// Remove from schedule.
|
627
|
$job = array(
|
628
|
'type' => $this->id,
|
629
|
'id' => $this->feed_nid,
|
630
|
);
|
631
|
JobScheduler::get('feeds_source_import')->remove($job);
|
632
|
JobScheduler::get('feeds_source_expire')->remove($job);
|
633
|
}
|
634
|
|
635
|
/**
|
636
|
* Only return source if configuration is persistent and valid.
|
637
|
*
|
638
|
* @see FeedsConfigurable::existing().
|
639
|
*/
|
640
|
public function existing() {
|
641
|
// If there is no feed nid given, there must be no content type specified.
|
642
|
// If there is a feed nid given, there must be a content type specified.
|
643
|
// Ensure that importer is persistent (= defined in code or DB).
|
644
|
// Ensure that source is persistent (= defined in DB).
|
645
|
if ((empty($this->feed_nid) && empty($this->importer->config['content_type'])) ||
|
646
|
(!empty($this->feed_nid) && !empty($this->importer->config['content_type']))) {
|
647
|
$this->importer->existing();
|
648
|
return parent::existing();
|
649
|
}
|
650
|
throw new FeedsNotExistingException(t('Source configuration not valid.'));
|
651
|
}
|
652
|
|
653
|
/**
|
654
|
* Returns the configuration for a specific client class.
|
655
|
*
|
656
|
* @param FeedsSourceInterface $client
|
657
|
* An object that is an implementer of FeedsSourceInterface.
|
658
|
*
|
659
|
* @return
|
660
|
* An array stored for $client.
|
661
|
*/
|
662
|
public function getConfigFor(FeedsSourceInterface $client) {
|
663
|
$class = get_class($client);
|
664
|
return isset($this->config[$class]) ? $this->config[$class] : $client->sourceDefaults();
|
665
|
}
|
666
|
|
667
|
/**
|
668
|
* Sets the configuration for a specific client class.
|
669
|
*
|
670
|
* @param FeedsSourceInterface $client
|
671
|
* An object that is an implementer of FeedsSourceInterface.
|
672
|
* @param $config
|
673
|
* The configuration for $client.
|
674
|
*
|
675
|
* @return
|
676
|
* An array stored for $client.
|
677
|
*/
|
678
|
public function setConfigFor(FeedsSourceInterface $client, $config) {
|
679
|
$this->config[get_class($client)] = $config;
|
680
|
}
|
681
|
|
682
|
/**
|
683
|
* Return defaults for feed configuration.
|
684
|
*/
|
685
|
public function configDefaults() {
|
686
|
// Collect information from plugins.
|
687
|
$defaults = array();
|
688
|
foreach ($this->importer->plugin_types as $type) {
|
689
|
if ($this->importer->$type->hasSourceConfig()) {
|
690
|
$defaults[get_class($this->importer->$type)] = $this->importer->$type->sourceDefaults();
|
691
|
}
|
692
|
}
|
693
|
return $defaults;
|
694
|
}
|
695
|
|
696
|
/**
|
697
|
* Override parent::configForm().
|
698
|
*/
|
699
|
public function configForm(&$form_state) {
|
700
|
// Collect information from plugins.
|
701
|
$form = array();
|
702
|
foreach ($this->importer->plugin_types as $type) {
|
703
|
if ($this->importer->$type->hasSourceConfig()) {
|
704
|
$class = get_class($this->importer->$type);
|
705
|
$config = isset($this->config[$class]) ? $this->config[$class] : array();
|
706
|
$form[$class] = $this->importer->$type->sourceForm($config);
|
707
|
$form[$class]['#tree'] = TRUE;
|
708
|
}
|
709
|
}
|
710
|
return $form;
|
711
|
}
|
712
|
|
713
|
/**
|
714
|
* Override parent::configFormValidate().
|
715
|
*/
|
716
|
public function configFormValidate(&$values) {
|
717
|
foreach ($this->importer->plugin_types as $type) {
|
718
|
$class = get_class($this->importer->$type);
|
719
|
if (isset($values[$class]) && $this->importer->$type->hasSourceConfig()) {
|
720
|
$this->importer->$type->sourceFormValidate($values[$class]);
|
721
|
}
|
722
|
}
|
723
|
}
|
724
|
|
725
|
/**
|
726
|
* Writes to feeds log.
|
727
|
*/
|
728
|
public function log($type, $message, $variables = array(), $severity = WATCHDOG_NOTICE) {
|
729
|
feeds_log($this->id, $this->feed_nid, $type, $message, $variables, $severity);
|
730
|
}
|
731
|
|
732
|
/**
|
733
|
* Background job helper. Starts a background job using Job Scheduler.
|
734
|
*
|
735
|
* Execute the first batch chunk of a background job on the current page load,
|
736
|
* moves the rest of the job processing to a cron powered background job.
|
737
|
*
|
738
|
* Executing the first batch chunk is important, otherwise, when a user
|
739
|
* submits a source for import or clearing, we will leave her without any
|
740
|
* visual indicators of an ongoing job.
|
741
|
*
|
742
|
* @see FeedsSource::startImport().
|
743
|
* @see FeedsSource::startClear().
|
744
|
*
|
745
|
* @param $method
|
746
|
* Method to execute on importer; one of 'import' or 'clear'.
|
747
|
*
|
748
|
* @throws Exception $e
|
749
|
*/
|
750
|
protected function startBackgroundJob($method) {
|
751
|
if (FEEDS_BATCH_COMPLETE != $this->$method()) {
|
752
|
$job = array(
|
753
|
'type' => $this->id,
|
754
|
'id' => $this->feed_nid,
|
755
|
'period' => 0,
|
756
|
'periodic' => FALSE,
|
757
|
);
|
758
|
JobScheduler::get("feeds_source_{$method}")->set($job);
|
759
|
}
|
760
|
}
|
761
|
|
762
|
/**
|
763
|
* Batch API helper. Starts a Batch API job.
|
764
|
*
|
765
|
* @see FeedsSource::startImport().
|
766
|
* @see FeedsSource::startClear().
|
767
|
* @see feeds_batch()
|
768
|
*
|
769
|
* @param $title
|
770
|
* Title to show to user when executing batch.
|
771
|
* @param $method
|
772
|
* Method to execute on importer; one of 'import' or 'clear'.
|
773
|
*/
|
774
|
protected function startBatchAPIJob($title, $method) {
|
775
|
$batch = array(
|
776
|
'title' => $title,
|
777
|
'operations' => array(
|
778
|
array('feeds_batch', array($method, $this->id, $this->feed_nid)),
|
779
|
),
|
780
|
'progress_message' => '',
|
781
|
);
|
782
|
batch_set($batch);
|
783
|
}
|
784
|
|
785
|
/**
|
786
|
* Acquires a lock for this source.
|
787
|
*
|
788
|
* @throws FeedsLockException
|
789
|
* If a lock for the requested job could not be acquired.
|
790
|
*/
|
791
|
protected function acquireLock() {
|
792
|
if (!lock_acquire("feeds_source_{$this->id}_{$this->feed_nid}", 60.0)) {
|
793
|
throw new FeedsLockException(t('Cannot acquire lock for source @id / @feed_nid.', array('@id' => $this->id, '@feed_nid' => $this->feed_nid)));
|
794
|
}
|
795
|
}
|
796
|
|
797
|
/**
|
798
|
* Releases a lock for this source.
|
799
|
*/
|
800
|
protected function releaseLock() {
|
801
|
lock_release("feeds_source_{$this->id}_{$this->feed_nid}");
|
802
|
}
|
803
|
|
804
|
/**
|
805
|
* Implements FeedsConfigurable::dependencies().
|
806
|
*/
|
807
|
public function dependencies() {
|
808
|
$dependencies = parent::dependencies();
|
809
|
return array_merge($dependencies, $this->importer()->dependencies());
|
810
|
}
|
811
|
|
812
|
}
|