Newer
Older
wmostrey
committed
// 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
* 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 $config
* The configuration for $client.
*
* @return
* An array stored for $client.
*/
public function setConfigFor(FeedsSourceInterface $client, $config) {
$this->config[get_class($client)] = $config;
}
Alex Barth
committed
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
/**
* Return defaults for feed configuration.
*/
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.
* @see FeedsSource::startImport()
* @see FeedsSource::startClear()
* @param string $method
* Method to execute on importer; one of 'import' or 'clear'.
*/
protected function startBackgroundJob($method) {
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
$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.
*
* @see FeedsSource::startImport()
* @see FeedsSource::startClear()
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
* @see feeds_batch()
*
* @param $title
* Title to show to user when executing batch.
* @param $method
* Method to execute on importer; one of 'import' or 'clear'.
*/
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}");
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
// Switch back to original account.
$this->switchBack();
}
/**
* Switches to the owner of the feed or user 1 if the importer is not attached
* to a content type.
*/
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());
}