source: sipes/modules_contrib/feeds/includes/FeedsBatch.inc @ c43ea01

stableversion-3.0
Last change on this file since c43ea01 was 177a560, checked in by José Gregorio Puentes <jpuentes@…>, 8 años ago

se agrego el directorio de modulos contribuidos de drupal

  • Propiedad mode establecida a 100755
File size: 9.4 KB
Línea 
1<?php
2
3// Batch stages.
4define('FEEDS_FETCHING', 'fetching');
5define('FEEDS_PARSING', 'parsing');
6define('FEEDS_PROCESSING', 'processing');
7define('FEEDS_CLEARING', 'clearing');
8
9/**
10 * A FeedsBatch object holds the state of an import or clear batch.
11 *
12 * Used in FeedsSource class. Counter variables are public for easier access.
13 */
14class FeedsBatch {
15  // Total of each stage of this batch.
16  protected $total;
17  // Progress of each stage of this batch.
18  protected $progress;
19  public function __construct() {
20    $this->total = array();
21    $this->progress = array();
22  }
23
24  /**
25   * Set the total for a stage.
26   */
27  public function setTotal($stage, $total) {
28    $this->total[$stage] = $total;
29  }
30
31  /**
32   * Get the total for a stage.
33   */
34  public function getTotal($stage) {
35    return $this->total[$stage];
36  }
37
38  /**
39   * Set progress for a stage.
40   *
41   * @param $stage
42   *   The stage to set the progress for. One of FEEDS_FETCHING, FEEDS_PARSING,
43   *   FEEDS_PROCESING or FEEDS_CLEARING.
44   * @param $progress
45   *   The number of items worked off for the given stage. This should be the
46   *   number of items worked off across all page loads, not just the present
47   *   page load.
48   */
49  public function setProgress($stage, $progress) {
50    $this->progress[$stage] = $progress;
51  }
52
53  /**
54   * Report progress.
55   *
56   * @param $stage
57   *   The stage to set the progress for. One of FEEDS_FETCHING, FEEDS_PARSING,
58   *   FEEDS_PROCESING or FEEDS_CLEARING.
59   */
60  public function getProgress($stage = NULL) {
61    if ($stage) {
62      $progress = $this->progress[$stage];
63      if ($progress == FEEDS_BATCH_COMPLETE) {
64        return FEEDS_BATCH_COMPLETE;
65      }
66      $total = $this->total[$stage];
67    }
68    else {
69      $complete = TRUE;
70      $progress = 0;
71      foreach ($this->progress as $p) {
72        $progress += $p;
73        $complete &= $p == FEEDS_BATCH_COMPLETE;
74      }
75      if ($complete) {
76        return FEEDS_BATCH_COMPLETE;
77      }
78      $total = array_sum($this->total);
79    }
80    $progress = (1.0 / $total) * $progress;
81    return $progress >= FEEDS_BATCH_COMPLETE ? 0.999 : $progress;
82  }
83}
84
85/**
86 * A FeedsImportBatch wraps the actual content retrieved from a FeedsSource. On
87 * import, it is created on the fetching stage and passed through the parsing
88 * and processing stage where it is normalized and consumed.
89 *
90 * A Fetcher must return a FeedsImportBatch object on fetch(). To that end it
91 * can use one of the existing FeedsImportBatch classes (FeedsImportBatch,
92 * FeedsFileBatch or FeedsHTTPBatch) or provide its own as a direct or indirect
93 * extension of FeedsImportBatch.
94 *
95 * A Parser must populate a FeedsImportBatch object upon parse(). For instance:
96 *
97 * @code
98 * $batch->items = $parsed_rows;
99 * $batch->title = 'My imported document';
100 * @endcode
101 *
102 * Finally, a processor can work off the information produced on the parsing
103 * stage by consuming items with $batch->shiftItem().
104 *
105 * @code
106 * while ($item = $batch->shiftItem()) {
107 *   $object = $this->map($item);
108 *   $object->save();
109 * }
110 * @endcode
111 *
112 * If a processing task is very slow, it can be batched over multiple page
113 * loads. For batching the consumer loop can be left while the current progress
114 * is set on the batch object. If the current progress is not
115 * FEEDS_BATCH_COMPLETE the processor will be called again on a subsequent page
116 * load to continue where it has left off. For an example, see
117 * FeedsNodeProcessor::process().
118 *
119 * @code
120 * $created = 0;
121 * while ($item = $batch->shiftItem()) {
122 *   $object = $this->map($item);
123 *   $object->save();
124 *   $created++; // Created in this page load.
125 *   $batch->created++; // Created total.
126 *   if ($created > MAX_CREATED) {
127 *     $batch->setProgress(FEEDS_PROCESSING, $batch->created);
128 *     return;
129 *   }
130 * }
131 * $batch->setProgress(FEEDS_PROCESSING, FEEDS_BATCH_COMPLETE);
132 * @endcode
133 *
134 * Note: Knowledge of the internal structure of a single item in the $items
135 * array is managed by the mapping API specified in FeedsParser class and
136 * FeedsProcessor class.
137 *
138 * @see FeedsBatch
139 * @see FeedsFileBatch
140 * @see FeedsHTTPBatch
141 */
142class FeedsImportBatch extends FeedsBatch {
143  protected $current_item;
144  protected $raw;
145  public $title;
146  public $description;
147  public $link;
148  public $items;
149  public $feed_nid;
150  public $created;
151  public $updated;
152
153  public function __construct($raw = '', $feed_nid = 0) {
154    parent::__construct();
155    $this->progress = array(
156      FEEDS_FETCHING => FEEDS_BATCH_COMPLETE,
157      FEEDS_PARSING => FEEDS_BATCH_COMPLETE,
158      FEEDS_PROCESSING => FEEDS_BATCH_COMPLETE,
159    );
160    $this->total = array(
161      FEEDS_FETCHING => 0,
162      FEEDS_PARSING => 0,
163      FEEDS_PROCESSING => 0,
164    );
165    $this->title = '';
166    $this->description = '';
167    $this->link = '';
168    $this->items = array();
169    $this->raw = $raw;
170    $this->feed_nid = $feed_nid;
171    $this->created = 0;
172    $this->updated = 0;
173  }
174
175  /**
176   * @return
177   *   The raw content from the source as a string.
178   *
179   * @throws Exception
180   *   Extending classes MAY throw an exception if a problem occurred.
181   */
182  public function getRaw() {
183    return $this->raw;
184  }
185
186  /**
187   * @return
188   *   A path to a file containing the raw content as a source.
189   *
190   * @throws Exception
191   *   If an unexpected problem occurred.
192   */
193  public function getFilePath() {
194    if (!isset($this->file_path)) {
195      $dir = file_directory_path() .'/feeds';
196      if (!file_check_directory($dir, TRUE)) {
197        throw new Exception(t('Feeds directory either cannot be created or is not writable.'));
198      }
199      $dest = file_destination($dir . '/' . get_class($this) .'_'. drupal_get_token($this->url) .'_'. time(), FILE_EXISTS_RENAME);
200      $this->file_path = file_save_data($this->getRaw(), $dest);
201      if ($this->file_path === 0) {
202        throw new Exception(t('Cannot write content to %dest', array('%dest' => $dest)));
203      }
204    }
205    return $this->file_path;
206  }
207
208  /**
209   * @deprecated
210   *   This method is deprecated. Access directly instead. For example:
211   *   $node = node_load($batch->feed_nid);
212   *
213   * Return the feed node related to this batch object.
214   */
215  public function feedNode() {
216    if ($this->feed_nid) {
217      return node_load($this->feed_nid);
218    }
219  }
220
221  /**
222   * @deprecated
223   *   This method is deprecated. Access directly instead. For example:
224   *   $title = $batch->title;
225   *
226   * @return
227   *   A string that is the feed's title.
228   */
229  public function getTitle() {
230    return $this->title;
231  }
232
233  /**
234   * @deprecated
235   *   This method is deprecated. Access directly instead. For example:
236   *   $description = $batch->description;
237   *
238   * @return
239   *   A string that is the feed's description.
240   */
241  public function getDescription() {
242    return $this->description;
243  }
244
245  /**
246   * @deprecated
247   *   This method is deprecated. Access directly instead. For example:
248   *   $link = $batch->link;
249   *
250   * @return
251   *   A string that is the link to the feed's site (not the actual URL of the
252   *   feed). Falls back to URL if not available.
253   */
254  public function getLink() {
255    return $this->link;
256  }
257
258  /**
259   * @todo Move to a nextItem() based approach, not consuming the item array.
260   *   Can only be done once we don't cache the entire batch object between page
261   *   loads for batching anymore.
262   *
263   * @return
264   *   Next available item or NULL if there is none. Every returned item is
265   *   removed from the internal array.
266   */
267  public function shiftItem() {
268    $this->current_item = array_shift($this->items);
269    return $this->current_item;
270  }
271
272  /**
273   * @return
274   *   Current feed item.
275   */
276  public function currentItem() {
277    return empty($this->current_item) ? NULL : $this->current_item;
278  }
279
280  /**
281   * Set title.
282   *
283   * @deprecated
284   *   This method is deprecated. Access directly instead. For example:
285   *   $batch->title = $title;
286   */
287  public function setTitle($title) {
288    $this->title = $title;
289  }
290
291  /**
292   * Set description.
293   *
294   * @deprecated
295   *   This method is deprecated. Access directly instead. For example:
296   *   $batch->description = $description;
297   */
298  public function setDescription($description) {
299    $this->description = $description;
300  }
301
302  /**
303   * Set link.
304   *
305   * @deprecated
306   *   This method is deprecated. Access directly instead. For example:
307   *   $batch->link = $link;
308   */
309  public function setLink($link) {
310    $this->link = $link;
311  }
312
313  /**
314   * Set items.
315   *
316   * @deprecated
317   *   This method is deprecated. Access directly instead. For example:
318   *   $batch->items = $items;
319   *
320   * @param $items
321   *   An array of the items in the feed. Cannot be NULL.
322   */
323  public function setItems($items) {
324    $this->items = $items;
325  }
326
327  /**
328   * Add an item.
329   *
330   * @deprecated
331   *   This method is deprecated. Access directly instead. For example:
332   *   $batch->items[] = $item;
333   */
334  public function addItem($item) {
335    $this->items[] = $item;
336  }
337
338  /**
339   * Get number of items.
340   *
341   * @deprecated
342   *   This method is deprecated. Access directly instead. For example:
343   *   $count = count($batch->items);
344   */
345  public function getItemCount() {
346    return count($this->items);
347  }
348}
349
350/**
351 * Batch class for batched deleting of items.
352 */
353class FeedsClearBatch extends FeedsBatch {
354  // Number of items deleted.
355  public $deleted;
356  public function __construct() {
357    parent::__construct();
358    $this->progress = array(
359      FEEDS_CLEARING => FEEDS_BATCH_COMPLETE,
360    );
361    $this->total = array(
362      FEEDS_CLEARING => 0,
363    );
364    $this->deleted = 0;
365  }
366}
Nota: Vea TracBrowser para ayuda de uso del navegador del repositorio.