Newer
Older
wmostrey
committed
* - 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.
*
* @return array
Alex Barth
committed
* An array stored for $client.
*/
public function setConfigFor(FeedsSourceInterface $client, $config) {
$this->config[get_class($client)] = $config;
}
Alex Barth
committed
/**
* Return defaults for feed configuration.
*
* @return array
Alex Barth
committed
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
*/
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) {
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
$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()
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
* @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}");
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
// 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());
}