Projet

Général

Profil

Révision ed9a13f1

Ajouté par Assos Assos il y a plus de 3 ans

Weekly update of contrib modules

Voir les différences:

drupal7/sites/all/modules/feeds/includes/FeedsSource.inc
2 2

  
3 3
/**
4 4
 * @file
5
 * Definition of FeedsSourceInterface and FeedsSource class.
5
 * Definition of FeedsSourceInterface, FeedsState and FeedsSource class.
6 6
 */
7 7

  
8 8
/**
......
21 21
define('FEEDS_PROCESS_EXPIRE', 'process_expire');
22 22

  
23 23
/**
24
 * Declares an interface for a class that defines default values and form
25
 * descriptions for a FeedSource.
24
 * Defines an interface for a feed source.
26 25
 */
27 26
interface FeedsSourceInterface {
28 27

  
29 28
  /**
29
   * Returns if a plugin handles source specific configuration.
30
   *
30 31
   * Crutch: for ease of use, we implement FeedsSourceInterface for every
31 32
   * plugin, but then we need to have a handle which plugin actually implements
32
   * a source.
33
   * source configuration.
33 34
   *
34
   * @see FeedsPlugin class.
35
   * @see FeedsPlugin
35 36
   *
36
   * @return
37
   * @return bool
37 38
   *   TRUE if a plugin handles source specific configuration, FALSE otherwise.
38 39
   */
39 40
  public function hasSourceConfig();
......
44 45
  public function sourceDefaults();
45 46

  
46 47
  /**
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().
48
   * Returns a Form API form array that defines a form configuring values.
49
   *
50
   * Keys correspond to the keys of the return value of sourceDefaults().
49 51
   */
50 52
  public function sourceForm($source_config);
51 53

  
......
63 65
   * A source is being deleted.
64 66
   */
65 67
  public function sourceDelete(FeedsSource $source);
68

  
66 69
}
67 70

  
68 71
/**
69 72
 * Status of an import or clearing operation on a source.
70 73
 */
71 74
class FeedsState {
75

  
72 76
  /**
73
   * Floating point number denoting the progress made. 0.0 meaning no progress
74
   * 1.0 = FEEDS_BATCH_COMPLETE meaning finished.
77
   * Floating point number denoting the progress made.
78
   *
79
   * 0.0 meaning no progress.
80
   * 1.0 = FEEDS_BATCH_COMPLETE, meaning finished.
81
   *
82
   * @var float
75 83
   */
76 84
  public $progress;
77 85

  
78 86
  /**
79 87
   * Used as a pointer to store where left off. Must be serializable.
88
   *
89
   * @var mixed
80 90
   */
81 91
  public $pointer;
82 92

  
83 93
  /**
84 94
   * Natural numbers denoting more details about the progress being made.
95
   *
96
   * @var int
85 97
   */
86 98
  public $total;
87 99
  public $created;
......
94 106

  
95 107
  /**
96 108
   * IDs of entities to be removed.
109
   *
110
   * @var array
97 111
   */
98 112
  public $removeList;
99 113

  
......
102 116
   */
103 117
  public function __construct() {
104 118
    $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;
119
    $this->total
120
      = $this->created
121
      = $this->updated
122
      = $this->deleted
123
      = $this->unpublished
124
      = $this->blocked
125
      = $this->skipped
126
      = $this->failed
127
      = 0;
113 128
  }
114 129

  
115 130
  /**
......
124 139
   * - $progress is larger than $total
125 140
   * - $progress approximates $total so that $finished rounds to 1.0
126 141
   *
127
   * @param $total
142
   * @param int $total
128 143
   *   A natural number that is the total to be worked off.
129
   * @param $progress
144
   * @param int $progress
130 145
   *   A natural number that is the progress made on $total.
131 146
   */
132 147
  public function progress($total, $progress) {
......
143 158
      $this->progress = FEEDS_BATCH_COMPLETE;
144 159
    }
145 160
  }
161

  
146 162
}
147 163

  
148 164
/**
165
 * Holds the source of a feed to import.
166
 *
149 167
 * This class encapsulates a source of a feed. It stores where the feed can be
150 168
 * found and how to import it.
151 169
 *
......
170 188
 */
171 189
class FeedsSource extends FeedsConfigurable {
172 190

  
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.
191
  /**
192
   * Contains the node id of the feed this source info object is attached to.
193
   *
194
   * Equals 0 if not attached to any node - for example when used on a
195
   * standalone import form within Feeds or by other API users.
196
   *
197
   * @var int
198
   */
176 199
  protected $feed_nid;
177 200

  
178
  // The FeedsImporter object that this source is expected to be used with.
201
  /**
202
   * The FeedsImporter object that this source is expected to be used with.
203
   *
204
   * @var FeedsImporter
205
   */
179 206
  protected $importer;
180 207

  
181
  // A FeedsSourceState object holding the current import/clearing state of this
182
  // source.
208
  /**
209
   * Holds the current state of an import, clear or expire task.
210
   *
211
   * Array keys can be:
212
   * - FEEDS_START
213
   *   Timestamp of when a task has started.
214
   * - FEEDS_FETCH
215
   *   A FeedsState object holding the state of the fetch stage, used during
216
   *   imports.
217
   * - FEEDS_PARSE
218
   *   A FeedsState object holding the state of the parse stage, used during
219
   *   imports.
220
   * - FEEDS_PROCESS
221
   *   A FeedsState object holding the state of the process stage, used during
222
   *   imports.
223
   * - FEEDS_PROCESS_CLEAR
224
   *   A FeedsState object holding the state of the clear task.
225
   * - FEEDS_PROCESS_EXPIRE
226
   *   A FeedsState object holding the state of the expire task.
227
   *
228
   * @var FeedsState[]|array|null
229
   */
183 230
  protected $state;
184 231

  
185
  // Fetcher result, used to cache fetcher result when batching.
232
  /**
233
   * Fetcher result, used to cache fetcher result when batching.
234
   *
235
   * @var FeedsFetcherResult
236
   */
186 237
  protected $fetcher_result;
187 238

  
188
  // Timestamp when this source was imported the last time.
239
  /**
240
   * Timestamp of when this source was imported the last time.
241
   *
242
   * @var int
243
   */
189 244
  protected $imported;
190 245

  
191
  // Holds an exception object in case an exception occurs during importing.
246
  /**
247
   * Holds an exception object in case an exception occurs during importing.
248
   *
249
   * @var Exception|null
250
   */
192 251
  protected $exception;
193 252

  
194 253
  /**
195
   * Instantiate a unique object per class/id/feed_nid. Don't use
196
   * directly, use feeds_source() instead.
254
   * The account switcher.
255
   *
256
   * @var FeedsAccountSwitcherInterface
197 257
   */
198
  public static function instance($importer_id, $feed_nid) {
258
  protected $accountSwitcher;
259

  
260
  /**
261
   * Instantiates an unique FeedsSource per class, importer ID and Feed node ID.
262
   *
263
   * Don't use this method directly, use feeds_source() instead.
264
   *
265
   * @param string $importer_id
266
   *   The machine name of the importer.
267
   * @param int $feed_nid
268
   *   The node id of a feed node if the source is attached to a feed node.
269
   * @param FeedsAccountSwitcherInterface $account_switcher
270
   *   The account switcher to use to be able to perform actions as a different
271
   *   user.
272
   */
273
  public static function instance($importer_id, $feed_nid, FeedsAccountSwitcherInterface $account_switcher = NULL) {
199 274
    $class = variable_get('feeds_source_class', 'FeedsSource');
200 275

  
201 276
    $instances = &drupal_static(__METHOD__, array());
202 277

  
203 278
    if (!isset($instances[$class][$importer_id][$feed_nid])) {
204
      $instances[$class][$importer_id][$feed_nid] = new $class($importer_id, $feed_nid);
279
      $instances[$class][$importer_id][$feed_nid] = new $class($importer_id, $feed_nid, $account_switcher);
205 280
    }
206 281
    return $instances[$class][$importer_id][$feed_nid];
207 282
  }
208 283

  
209 284
  /**
210 285
   * Constructor.
286
   *
287
   * @param string $importer_id
288
   *   The machine name of the importer.
289
   * @param int $feed_nid
290
   *   The feed node ID for this Feeds source. This should be '0' if the
291
   *   importer is not attached to a content type.
292
   * @param FeedsAccountSwitcherInterface $account_switcher
293
   *   The account switcher to use to be able to perform actions as a different
294
   *   user.
211 295
   */
212
  protected function __construct($importer_id, $feed_nid) {
296
  protected function __construct($importer_id, $feed_nid, FeedsAccountSwitcherInterface $account_switcher = NULL) {
213 297
    $this->feed_nid = $feed_nid;
214 298
    $this->importer = feeds_importer($importer_id);
299
    if (is_null($account_switcher)) {
300
      $this->accountSwitcher = new FeedsAccountSwitcher();
301
    }
302
    else {
303
      $this->accountSwitcher = $account_switcher;
304
    }
215 305
    parent::__construct($importer_id);
216 306
    $this->load();
217 307
  }
218 308

  
219 309
  /**
220
   * Returns the FeedsImporter object that this source is expected to be used with.
310
   * Returns the FeedsImporter object for this source.
311
   *
312
   * @return FeedsImporter
313
   *   The importer associated with this Feeds source.
221 314
   */
222 315
  public function importer() {
223 316
    return $this->importer;
......
226 319
  /**
227 320
   * Preview = fetch and parse a feed.
228 321
   *
229
   * @return
230
   *   FeedsParserResult object.
322
   * @return FeedsParserResult
323
   *   A FeedsParserResult instance.
231 324
   *
232
   * @throws
233
   *   Throws Exception if an error occurs when fetching or parsing.
325
   * @throws Exception
326
   *   If an error occurs when fetching or parsing.
234 327
   */
235 328
  public function preview() {
236 329
    $result = $this->importer->fetcher->fetch($this);
......
303 396
   * Schedule periodic or background import tasks.
304 397
   *
305 398
   * @param bool $force
306
   *   (optional) force scheduling to happen.
399
   *   (optional) If true, forces the scheduling to happen.
307 400
   *   Defaults to true.
308 401
   */
309 402
  public function scheduleImport($force = TRUE) {
......
346 439
   * Schedule background expire tasks.
347 440
   *
348 441
   * @param bool $force
349
   *   (optional) force scheduling to happen.
442
   *   (optional) If true, forces the scheduling to happen.
350 443
   *   Defaults to true.
351 444
   */
352 445
  public function scheduleExpire($force = TRUE) {
......
399 492
   * This method only executes the current batch chunk, then returns. If you are
400 493
   * looking to import an entire source, use FeedsSource::startImport() instead.
401 494
   *
402
   * @return
495
   * @return float
403 496
   *   FEEDS_BATCH_COMPLETE if the import process finished. A decimal between
404 497
   *   0.0 and 0.9 periodic if import is still in progress.
405 498
   *
406
   * @throws
407
   *   Throws Exception if an error occurs when importing.
499
   * @throws Exception
500
   *   In case an error occurs when importing.
408 501
   */
409 502
  public function import() {
410 503
    $this->acquireLock();
......
533 626
   * looking to delete all items of a source, use FeedsSource::startClear()
534 627
   * instead.
535 628
   *
536
   * @return
629
   * @return float
537 630
   *   FEEDS_BATCH_COMPLETE if the clearing process finished. A decimal between
538 631
   *   0.0 and 0.9 periodic if clearing is still in progress.
539 632
   *
540
   * @throws
541
   *   Throws Exception if an error occurs when clearing.
633
   * @throws Exception
634
   *   In case an error occurs when clearing.
542 635
   */
543 636
  public function clear() {
544 637
    $this->acquireLock();
......
629 722
  /**
630 723
   * Return a state object for a given stage. Lazy instantiates new states.
631 724
   *
632
   * @todo Rename getConfigFor() accordingly to config().
633
   *
634
   * @param $stage
725
   * @param string $stage
635 726
   *   One of FEEDS_FETCH, FEEDS_PARSE, FEEDS_PROCESS or FEEDS_PROCESS_CLEAR.
636 727
   *
637
   * @return
728
   * @return FeedsState|mixed
638 729
   *   The FeedsState object for the given stage.
730
   *   In theory, this could return something else, if $this->state has been
731
   *   polluted with e.g. integer timestamps.
732
   *
733
   * @see FeedsSource::$state
639 734
   */
640 735
  public function state($stage) {
641 736
    if (!is_array($this->state)) {
......
732 827
      }
733 828

  
734 829
      // Special case for PostgreSQL: if using that database type, we cannot
735
      // search in the data column of the queue table, because the Drupal database
736
      // layer adds '::text' to bytea columns, which results into the data column
737
      // becoming unreadable in conditions. So instead, we check for the first 10
738
      // records in the queue to see if the given importer ID + feed NID is
739
      // amongst them.
830
      // search in the data column of the queue table, because the Drupal
831
      // database layer adds '::text' to bytea columns, which results into the
832
      // data column becoming unreadable in conditions. So instead, we check for
833
      // the first 10 records in the queue to see if the given importer ID +
834
      // feed NID is amongst them.
740 835
      if (Database::getConnection()->databaseType() == 'pgsql') {
741 836
        $items = db_query("SELECT data, created FROM {queue} WHERE name = :name AND expire = 0 LIMIT 10", array(
742 837
          ':name' => 'feeds_source_import',
......
838 933
  public function unlock() {
839 934
    $this->clearStates();
840 935
    $this->save();
841
    $this->releaseLock();
936
    try {
937
      $this->releaseLock();
938
    }
939
    catch (FeedsAccountSwitcherException $exception) {
940
      // Ignore switch back exceptions.
941
    }
842 942
  }
843 943

  
844 944
  /**
......
880 980
   * @todo Patch CTools to move constants from export.inc to ctools.module.
881 981
   */
882 982
  public function load() {
883
    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()) {
983
    $record = db_query("SELECT imported, config, state, fetcher_result FROM {feeds_source} WHERE id = :id AND feed_nid = :nid", array(
984
      ':id' => $this->id,
985
      ':nid' => $this->feed_nid,
986
    ))->fetchObject();
987
    if ($record) {
884 988
      // While FeedsSource cannot be exported, we still use CTool's export.inc
885 989
      // export definitions.
886 990
      ctools_include('export');
......
900 1004
  }
901 1005

  
902 1006
  /**
903
   * Delete configuration. Removes configuration information
904
   * from database, does not delete configuration itself.
1007
   * Removes the feed source from the database.
905 1008
   */
906 1009
  public function delete() {
907 1010
    // Alert implementers of FeedsSourceInterface to the fact that we're
......
956 1059
  /**
957 1060
   * Only return source if configuration is persistent and valid.
958 1061
   *
959
   * @see FeedsConfigurable::existing().
1062
   * @see FeedsConfigurable::existing()
960 1063
   */
961 1064
  public function existing() {
962 1065
    // Ensure that the source configuration is valid.
......
977 1080
   * @param FeedsSourceInterface $client
978 1081
   *   An object that is an implementer of FeedsSourceInterface.
979 1082
   *
980
   * @return
1083
   * @return array
981 1084
   *   An array stored for $client.
982 1085
   */
983 1086
  public function getConfigFor(FeedsSourceInterface $client) {
......
990 1093
   *
991 1094
   * @param FeedsSourceInterface $client
992 1095
   *   An object that is an implementer of FeedsSourceInterface.
993
   * @param $config
1096
   * @param array $config
994 1097
   *   The configuration for $client.
995
   *
996
   * @return
997
   *   An array stored for $client.
998 1098
   */
999
  public function setConfigFor(FeedsSourceInterface $client, $config) {
1099
  public function setConfigFor(FeedsSourceInterface $client, array $config) {
1000 1100
    $this->config[get_class($client)] = $config;
1001 1101
  }
1002 1102

  
1003 1103
  /**
1004 1104
   * Return defaults for feed configuration.
1105
   *
1106
   * @return array
1107
   *   The default feed configuration, keyed per Feeds plugin.
1005 1108
   */
1006 1109
  public function configDefaults() {
1007 1110
    // Collect information from plugins.
......
1053 1156
  /**
1054 1157
   * Background job helper. Starts a background job using the Drupal queue.
1055 1158
   *
1056
   * @see FeedsSource::startImport().
1057
   * @see FeedsSource::startClear().
1058
   *
1059 1159
   * @param string $method
1060 1160
   *   Method to execute on importer; one of 'import' or 'clear'.
1161
   *
1162
   * @see FeedsSource::startImport()
1163
   * @see FeedsSource::startClear()
1061 1164
   */
1062 1165
  protected function startBackgroundJob($method) {
1063 1166
    $job = array(
......
1090 1193
  /**
1091 1194
   * Batch API helper. Starts a Batch API job.
1092 1195
   *
1093
   * @see FeedsSource::startImport().
1094
   * @see FeedsSource::startClear().
1095
   * @see feeds_batch()
1096
   *
1097
   * @param $title
1196
   * @param string $title
1098 1197
   *   Title to show to user when executing batch.
1099
   * @param $method
1198
   * @param string $method
1100 1199
   *   Method to execute on importer; one of 'import' or 'clear'.
1200
   *
1201
   * @see FeedsSource::startImport()
1202
   * @see FeedsSource::startClear()
1203
   * @see feeds_batch()
1101 1204
   */
1102 1205
  protected function startBatchAPIJob($title, $method) {
1103 1206
    $batch = array(
......
1120 1223
    if (!lock_acquire("feeds_source_{$this->id}_{$this->feed_nid}", 60.0)) {
1121 1224
      throw new FeedsLockException(t('Cannot acquire lock for source @id / @feed_nid.', array('@id' => $this->id, '@feed_nid' => $this->feed_nid)));
1122 1225
    }
1226

  
1227
    // Switch account.
1228
    $this->switchAccount();
1123 1229
  }
1124 1230

  
1125 1231
  /**
......
1127 1233
   */
1128 1234
  protected function releaseLock() {
1129 1235
    lock_release("feeds_source_{$this->id}_{$this->feed_nid}");
1236

  
1237
    // Switch back to original account.
1238
    $this->switchBack();
1239
  }
1240

  
1241
  /**
1242
   * Switches account to the feed owner or user 1.
1243
   *
1244
   * To the feed owner is switched if the importer is attached to a content
1245
   * type. When using the standalone form, there is no feed owner, so then a
1246
   * switch to user 1 happens instead.
1247
   */
1248
  protected function switchAccount() {
1249
    // Use author of feed node.
1250
    if ($this->feed_nid) {
1251
      $node = node_load($this->feed_nid);
1252
      if (!empty($node->uid)) {
1253
        $account = user_load($node->uid);
1254
      }
1255
    }
1256

  
1257
    // If the owner of the feed node is anonymous or if the importer is not
1258
    // attached to a content type, pick user 1 instead.
1259
    if (empty($account)) {
1260
      $account = user_load(1);
1261
    }
1262

  
1263
    $this->accountSwitcher->switchTo($account);
1264
  }
1265

  
1266
  /**
1267
   * Switches back to the original user.
1268
   */
1269
  protected function switchBack() {
1270
    $this->accountSwitcher->switchBack();
1130 1271
  }
1131 1272

  
1132 1273
  /**

Formats disponibles : Unified diff