Newer
Older
Alex Barth
committed
/**
megachriz
committed
* Removes the feed source from the database.
Alex Barth
committed
*/
public function delete() {
Alex Barth
committed
// Alert implementers of FeedsSourceInterface to the fact that we're
// deleting.
foreach ($this->importer->plugin_types as $type) {
$this->importer->$type->sourceDelete($this);
}
Alex Barth
committed
db_delete('feeds_source')
->condition('id', $this->id)
->condition('feed_nid', $this->feed_nid)
->execute();
Alex Barth
committed
// Remove from schedule.
$job = array(
'type' => $this->id,
'id' => $this->feed_nid,
);
JobScheduler::get('feeds_source_import')->remove($job);
JobScheduler::get('feeds_source_expire')->remove($job);
Alex Barth
committed
}
wmostrey
committed
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
/**
* Checks whether or not the source configuration is valid.
*
* @return bool
* True if it is valid.
* False otherwise.
*/
public function hasValidConfiguration() {
// If there is no feed nid given, there must be no content type specified.
$standalone = empty($this->feed_nid) && empty($this->importer->config['content_type']);
// If there is a feed nid given, there must be a content type specified.
$attached = !empty($this->feed_nid) && !empty($this->importer->config['content_type']);
if ($standalone || $attached) {
return TRUE;
}
return FALSE;
}
/**
* Overrides FeedsConfigurable::doesExist().
*
* Checks the following:
* - If the importer is persistent (= defined in code or DB).
* - If the source is persistent (= defined in DB).
*/
public function doesExist() {
return $this->importer->doesExist() && parent::doesExist();
}
Alex Barth
committed
/**
* Only return source if configuration is persistent and valid.
Alex Barth
committed
*
* @see FeedsConfigurable::existing()
Alex Barth
committed
*/
public function existing() {
wmostrey
committed
// Ensure that the source configuration is valid.
if (!$this->hasValidConfiguration()) {
throw new FeedsNotExistingException(t('Source configuration not valid.'));
}
// Ensure that the importer is persistent (= defined in code or DB).
$this->importer->existing();
// Ensure that the source is persistent (= defined in DB).
return parent::existing();
Alex Barth
committed
}
/**
Alex Barth
committed
* Returns the configuration for a specific client class.
Alex Barth
committed
*
* @param FeedsSourceInterface $client
* An object that is an implementer of FeedsSourceInterface.
*
* @return array
Alex Barth
committed
* An array stored for $client.
*/
public function getConfigFor(FeedsSourceInterface $client) {
Chris Leppanen
committed
$class = get_class($client);
return isset($this->config[$class]) ? $this->config[$class] : $client->sourceDefaults();
Alex Barth
committed
}
Alex Barth
committed
/**
* Sets the configuration for a specific client class.
*
* @param FeedsSourceInterface $client
* An object that is an implementer of FeedsSourceInterface.
* @param array $config
Alex Barth
committed
* The configuration for $client.
*/
megachriz
committed
public function setConfigFor(FeedsSourceInterface $client, array $config) {
Alex Barth
committed
$this->config[get_class($client)] = $config;
}
Alex Barth
committed
/**
* Return defaults for feed configuration.
*
* @return array
megachriz
committed
* The default feed configuration, keyed per Feeds plugin.
Alex Barth
committed
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
*/
public function configDefaults() {
// Collect information from plugins.
$defaults = array();
foreach ($this->importer->plugin_types as $type) {
if ($this->importer->$type->hasSourceConfig()) {
$defaults[get_class($this->importer->$type)] = $this->importer->$type->sourceDefaults();
}
}
return $defaults;
}
/**
* Override parent::configForm().
*/
public function configForm(&$form_state) {
// Collect information from plugins.
$form = array();
foreach ($this->importer->plugin_types as $type) {
if ($this->importer->$type->hasSourceConfig()) {
$class = get_class($this->importer->$type);
$config = isset($this->config[$class]) ? $this->config[$class] : array();
$form[$class] = $this->importer->$type->sourceForm($config);
Alex Barth
committed
$form[$class]['#tree'] = TRUE;
}
}
return $form;
}
/**
* Override parent::configFormValidate().
*/
public function configFormValidate(&$values) {
foreach ($this->importer->plugin_types as $type) {
$class = get_class($this->importer->$type);
if (isset($values[$class]) && $this->importer->$type->hasSourceConfig()) {
$this->importer->$type->sourceFormValidate($values[$class]);
}
}
}
/**
* Writes to feeds log.
*/
public function log($type, $message, $variables = array(), $severity = WATCHDOG_NOTICE) {
feeds_log($this->id, $this->feed_nid, $type, $message, $variables, $severity);
}
* Background job helper. Starts a background job using the Drupal queue.
* @param string $method
* Method to execute on importer; one of 'import' or 'clear'.
megachriz
committed
*
* @see FeedsSource::startImport()
* @see FeedsSource::startClear()
*/
protected function startBackgroundJob($method) {
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
$job = array(
'type' => $this->id,
'id' => $this->feed_nid,
);
$queue = DrupalQueue::get('feeds_source_' . $method);
$queue->createItem($job);
switch ($method) {
case 'import':
$state = $this->state(FEEDS_PARSE);
break;
case 'clear':
$state = $this->state(FEEDS_PROCESS_CLEAR);
break;
case 'expire':
$state = $this->state(FEEDS_PROCESS_EXPIRE);
break;
}
if (isset($state)) {
$state->progress = 0;
$this->save();
}
}
/**
* Batch API helper. Starts a Batch API job.
*
megachriz
committed
* @param string $title
* Title to show to user when executing batch.
* @param string $method
* Method to execute on importer; one of 'import' or 'clear'.
*
* @see FeedsSource::startImport()
* @see FeedsSource::startClear()
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
* @see feeds_batch()
*/
protected function startBatchAPIJob($title, $method) {
$batch = array(
'title' => $title,
'operations' => array(
array('feeds_batch', array($method, $this->id, $this->feed_nid)),
),
'progress_message' => '',
);
batch_set($batch);
}
/**
* Acquires a lock for this source.
*
* @throws FeedsLockException
* If a lock for the requested job could not be acquired.
*/
protected function acquireLock() {
if (!lock_acquire("feeds_source_{$this->id}_{$this->feed_nid}", 60.0)) {
throw new FeedsLockException(t('Cannot acquire lock for source @id / @feed_nid.', array('@id' => $this->id, '@feed_nid' => $this->feed_nid)));
}
// Switch account.
$this->switchAccount();
}
/**
* Releases a lock for this source.
*/
protected function releaseLock() {
lock_release("feeds_source_{$this->id}_{$this->feed_nid}");
// Switch back to original account.
$this->switchBack();
}
/**
megachriz
committed
* Switches account to the feed owner or user 1.
*
* To the feed owner is switched if the importer is attached to a content
* type. When using the standalone form, there is no feed owner, so then a
* switch to user 1 happens instead.
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
*/
protected function switchAccount() {
// Use author of feed node.
if ($this->feed_nid) {
$node = node_load($this->feed_nid);
if (!empty($node->uid)) {
$account = user_load($node->uid);
}
}
// If the owner of the feed node is anonymous or if the importer is not
// attached to a content type, pick user 1 instead.
if (empty($account)) {
$account = user_load(1);
}
$this->accountSwitcher->switchTo($account);
}
/**
* Switches back to the original user.
*/
protected function switchBack() {
$this->accountSwitcher->switchBack();
megachriz
committed
/**
* Implements FeedsConfigurable::dependencies().
*/
public function dependencies() {
$dependencies = parent::dependencies();
return array_merge($dependencies, $this->importer()->dependencies());
}