Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
feeds
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
drupal.org
feeds
Commits
967bf290
Commit
967bf290
authored
15 years ago
by
Alex Barth
Browse files
Options
Downloads
Patches
Plain Diff
#708228 Scott Reynolds, alex_b: Break FeedsImportBatch into separate classes.
parent
255a25d6
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
CHANGELOG.txt
+1
-0
1 addition, 0 deletions
CHANGELOG.txt
includes/FeedsBatch.inc
+49
-55
49 additions, 55 deletions
includes/FeedsBatch.inc
plugins/FeedsFileFetcher.inc
+32
-2
32 additions, 2 deletions
plugins/FeedsFileFetcher.inc
plugins/FeedsHTTPFetcher.inc
+45
-2
45 additions, 2 deletions
plugins/FeedsHTTPFetcher.inc
with
127 additions
and
59 deletions
CHANGELOG.txt
+
1
−
0
View file @
967bf290
...
...
@@ -3,6 +3,7 @@
Feeds 6.x 1.0 xxxxx xx, xxxx-xx-xx
----------------------------------
- #708228 Scott Reynolds, alex_b: Break FeedsImportBatch into separate classes.
- alex_b: Support mapping to OpenID, using OpenID as a unique mapping target.
- alex_b: Handle exceptions outside of Importer/Source facade methods.
- #600584 alex_b: Use Batch API. NOTE: third party plugins/extensions
...
...
This diff is collapsed.
Click to expand it.
includes/FeedsBatch.inc
+
49
−
55
View file @
967bf290
...
...
@@ -20,76 +20,70 @@ class FeedsBatch {
}
/**
* A FeedsImportBatch
i
s the actual content retrieved from a FeedsSource. On
* A FeedsImportBatch
wrap
s the actual content retrieved from a FeedsSource. On
* import, it is created on the fetching stage and passed through the parsing
* and processing stage where it is normalized and consumed.
*
* @see FeedsSource class
* @see FeedsFetcher class
* A Fetcher must return a FeedsImportBatch object on fetch(). To that end it
* must use either one of the existing implementations of FeedsImportBatch
* (FeedsFileBatch or FeedsHTTPBatch) or it must extend FeedsImportBatch and
* implement at least
*
* - getRaw() returning the raw content from the source as a string and
* - getFilePath() returning a path to a file containing the raw content from
* the source.
*
* A Parser must populate a FeedsImportBatch object through the set methods upon
* parse(). For instance:
*
* $batch->setTitle('My imported document');
* $batch->setItems($parsed_rows);
*
* Finally, a processor can work off the information produced on the parsing
* stage by consuming items with $batch->shiftItem().
*
* while ($item = $batch->shiftItem()) {
* $object = $this->map($item);
* $object->save();
* }
*
* Note: Knowledge of the internal structure of a single item in the $items
* array is managed by the mapping API specified in FeedsParser class and
* FeedsProcessor class.
*
* @see FeedsFileBatch
* @see FeedsHTTPBatch
*/
class
FeedsImportBatch
extends
FeedsBatch
{
protected
$url
;
protected
$file_path
;
protected
$raw
;
protected
$items
;
abstract
class
FeedsImportBatch
extends
FeedsBatch
{
protected
$title
;
protected
$description
;
protected
$link
;
protected
$items
;
/**
* Constructor.
*
* Either $url or $file_path must be given.
*/
public
function
__construct
(
$url
=
NULL
,
$file_path
=
NULL
)
{
$this
->
url
=
$url
;
$this
->
file_path
=
$file_path
;
public
function
__construct
()
{
$this
->
title
=
''
;
$this
->
description
=
''
;
$this
->
link
=
''
;
$this
->
items
=
array
();
parent
::
__construct
();
}
/**
* @return
* The raw content of the feed.
*/
public
function
getRaw
()
{
if
(
$this
->
file_path
)
{
return
file_get_contents
(
realpath
(
$this
->
file_path
));
}
elseif
(
$this
->
url
)
{
feeds_include_library
(
'http_request.inc'
,
'http_request'
);
$result
=
http_request_get
(
$this
->
url
);
if
(
$result
->
code
!=
200
)
{
throw
new
Exception
(
t
(
'Download of @url failed with code !code.'
,
array
(
'@url'
=>
$this
->
url
,
'!code'
=>
$result
->
code
)));
}
return
$result
->
data
;
}
}
/**
* @return
* Path to the feed. This path is relative to Drupal's root directory.
* If the feed is not local, getFilePath downloads it to file directory.
* The raw content from the source as a string.
*
* @throws Exception
* If an unexpected problem occurred.
*/
public
function
getFilePath
()
{
if
(
!
isset
(
$this
->
file_path
))
{
$dest
=
file_destination
(
file_directory_path
()
.
'/feeds/'
.
get_class
(
$this
)
.
'_'
.
md5
(
$this
->
url
)
.
'_'
.
time
(),
FILE_EXISTS_RENAME
);
$this
->
file_path
=
file_save_data
(
$this
->
getRaw
(),
$dest
);
if
(
$this
->
file_path
===
0
)
{
throw
new
Exception
(
t
(
'Cannot write content to %dest'
,
array
(
'%dest'
=>
$dest
)));
}
}
return
$this
->
file_path
;
}
public
abstract
function
getRaw
();
/**
* @return
* URL to the document.
* A path to a file containing the raw content as a source.
*
* @throws Exception
* If an unexpected problem occurred.
*/
public
function
getURL
()
{
if
(
!
isset
(
$this
->
url
)
&&
isset
(
$this
->
file
))
{
return
$_GLOBALS
[
'base_url'
]
.
'/'
.
$this
->
file
;
}
}
public
abstract
function
getFilePath
();
/**
* @return
...
...
@@ -113,7 +107,7 @@ class FeedsImportBatch extends FeedsBatch {
* feed). Falls back to URL if not available.
*/
public
function
getLink
()
{
return
isset
(
$this
->
link
)
?
$this
->
link
:
$this
->
getURL
()
;
return
$this
->
link
;
}
/**
...
...
This diff is collapsed.
Click to expand it.
plugins/FeedsFileFetcher.inc
+
32
−
2
View file @
967bf290
...
...
@@ -3,9 +3,39 @@
/**
* @file
* Home of the FeedsFileFetcher.
* Home of the FeedsFileFetcher
and related classes
.
*/
/**
* Definition of the import batch object created on the fetching stage by
* FeedsFileFetcher.
*/
class
FeedsFileBatch
extends
FeedsImportBatch
{
protected
$file_path
;
/**
* Constructor.
*/
public
function
__construct
(
$file_path
)
{
$this
->
file_path
=
$file_path
;
parent
::
__construct
();
}
/**
* Implementation of FeedsImportBatch::getRaw();
*/
public
function
getRaw
()
{
return
file_get_contents
(
realpath
(
$this
->
file_path
));
}
/**
* Implementation of FeedsImportBatch::getFilePath().
*/
public
function
getFilePath
()
{
return
$this
->
file_path
;
}
}
/**
* Fetches data via HTTP.
*/
...
...
@@ -16,7 +46,7 @@ class FeedsFileFetcher extends FeedsFetcher {
*/
public
function
fetch
(
FeedsSource
$source
)
{
$source_config
=
$source
->
getConfigFor
(
$this
);
return
new
Feeds
ImportBatch
(
NULL
,
$source_config
[
'source'
]);
return
new
Feeds
FileBatch
(
$source_config
[
'source'
]);
}
/**
...
...
This diff is collapsed.
Click to expand it.
plugins/FeedsHTTPFetcher.inc
+
45
−
2
View file @
967bf290
...
...
@@ -3,9 +3,52 @@
/**
* @file
* Home of the FeedsHTTPFetcher.
* Home of the FeedsHTTPFetcher
and related classes
.
*/
/**
* Definition of the import batch object created on the fetching stage by
* FeedsHTTPFetcher.
*/
class
FeedsHTTPBatch
extends
FeedsImportBatch
{
protected
$url
;
protected
$file_path
;
/**
* Constructor.
*/
public
function
__construct
(
$url
=
NULL
)
{
$this
->
url
=
$url
;
parent
::
__construct
();
}
/**
* Implementation of FeedsImportBatch::getRaw();
*/
public
function
getRaw
()
{
feeds_include_library
(
'http_request.inc'
,
'http_request'
);
$result
=
http_request_get
(
$this
->
url
);
if
(
$result
->
code
!=
200
)
{
throw
new
Exception
(
t
(
'Download of @url failed with code !code.'
,
array
(
'@url'
=>
$this
->
url
,
'!code'
=>
$result
->
code
)));
}
return
$result
->
data
;
}
/**
* Implementation of FeedsImportBatch::getFilePath().
*/
public
function
getFilePath
()
{
if
(
!
isset
(
$this
->
file_path
))
{
$dest
=
file_destination
(
file_directory_path
()
.
'/feeds/'
.
get_class
(
$this
)
.
'_'
.
md5
(
$this
->
url
)
.
'_'
.
time
(),
FILE_EXISTS_RENAME
);
$this
->
file_path
=
file_save_data
(
$this
->
getRaw
(),
$dest
);
if
(
$this
->
file_path
===
0
)
{
throw
new
Exception
(
t
(
'Cannot write content to %dest'
,
array
(
'%dest'
=>
$dest
)));
}
}
return
$this
->
file_path
;
}
}
/**
* Fetches data via HTTP.
*/
...
...
@@ -16,7 +59,7 @@ class FeedsHTTPFetcher extends FeedsFetcher {
*/
public
function
fetch
(
FeedsSource
$source
)
{
$source_config
=
$source
->
getConfigFor
(
$this
);
return
new
Feeds
Import
Batch
(
$source_config
[
'source'
]);
return
new
Feeds
HTTP
Batch
(
$source_config
[
'source'
]);
}
/**
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment