-
Notifications
You must be signed in to change notification settings - Fork 242
/
rssimport.php
364 lines (308 loc) · 11.4 KB
/
rssimport.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<?php
//
// Created on: <24-Sep-2003 16:09:21 sp>
//
// Copyright (C) 1999-2005 eZ systems as. All rights reserved.
//
// This source file is part of the eZ publish (tm) Open Source Content
// Management System.
//
// This file may be distributed and/or modified under the terms of the
// "GNU General Public License" version 2 as published by the Free
// Software Foundation and appearing in the file LICENSE.GPL included in
// the packaging of this file.
//
// Licencees holding valid "eZ publish professional licences" may use this
// file in accordance with the "eZ publish professional licence" Agreement
// provided with the Software.
//
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
// THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE.
//
// The "eZ publish professional licence" is available at
// http://ez.no/products/licences/professional/. For pricing of this licence
// please contact us via e-mail to licence@ez.no. Further contact
// information is available at http://ez.no/home/contact/.
//
// The "GNU General Public License" (GPL) is available at
// http://www.gnu.org/copyleft/gpl.html.
//
// Contact licence@ez.no if any conditions of this licencing isn't clear to
// you.
//
/*! \file rssimport.php
*/
include_once( 'kernel/classes/ezrssimport.php' );
include_once( 'kernel/classes/ezcontentclass.php' );
include_once( 'kernel/classes/ezcontentobject.php' );
include_once( 'kernel/classes/ezpersistentobject.php' );
include_once( 'lib/ezxml/classes/ezxml.php' );
include_once( 'kernel/classes/ezcontentobjecttreenode.php' );
include_once( 'kernel/classes/ezcontentobjectversion.php' );
include_once( 'lib/ezutils/classes/ezoperationhandler.php' );
//For ezUser, we would make this the ezUser class id but otherwise just pick and choose.
//fetch this class
$rssImportArray = eZRSSImport::fetchActiveList();
// Loop through all configured and active rss imports. If something goes wrong while processing them, continue to next import
foreach ( array_keys( $rssImportArray ) as $rssImportKey )
{
// Get RSSImport object
$rssImport =& $rssImportArray[$rssImportKey];
$rssSource =& $rssImport->attribute( 'url' );
$addCount = 0;
if ( !$isQuiet )
{
$cli->output( 'RSSImport '.$rssImport->attribute( 'name' ).': Starting.' );
}
// Open and read RSSImport url
$fid = fopen( $rssSource, 'r' );
if ( $fid === false )
{
if ( !$isQuiet )
{
$cli->output( 'RSSImport '.$rssImport->attribute( 'name' ).': Failed to open RSS feed file: '.$rssSource );
}
continue;
}
$xmlData = "";
do {
$data = fread($fid, 8192);
if (strlen($data) == 0) {
break;
}
$xmlData .= $data;
} while(true);
fclose( $fid );
// Create DomDocumnt from http data
$xmlObject = new eZXML();
$domDocument =& $xmlObject->domTree( $xmlData );
if ( $domDocument == null or $domDocument === false )
{
if ( !$isQuiet )
{
$cli->output( 'RSSImport '.$rssImport->attribute( 'name' ).': Invalid RSS document.' );
}
continue;
}
$root =& $domDocument->root();
if ( $root == null )
{
if ( !$isQuiet )
{
$cli->output( 'RSSImport '.$rssImport->attribute( 'name' ).': Invalid RSS document.' );
}
continue;
}
switch( $root->attributeValue( 'version' ) )
{
default:
case '1.0':
{
rssImport1( $root, $rssImport, $cli );
} break;
case '0.91':
case '0.92':
case '2.0':
{
rssImport2( $root, $rssImport, $cli );
} break;
}
}
/*!
Parse RSS 1.0 feed
\param DOM root node
\param RSS Import item
\param cli
*/
function rssImport1( &$root, &$rssImport, &$cli )
{
global $isQuiet;
$addCount = 0;
// Get all items in rss feed
$itemArray =& $root->elementsByName( 'item' );
// Loop through all items in RSS feed
foreach ( array_keys ( $itemArray ) as $itemKey )
{
$item =& $itemArray[$itemKey];
$addCount += importRSSItem( $item, $rssImport, $cli );
}
if ( !$isQuiet )
{
$cli->output( 'RSSImport '.$rssImport->attribute( 'name' ).': End. '.$addCount.' objects added' );
}
}
/*!
Parse RSS 2.0 feed
\param DOM root node
\param RSS Import item
\param cli
*/
function rssImport2( &$root, &$rssImport, &$cli )
{
global $isQuiet;
$addCount = 0;
// Get all items in rss feed
$channel =& $root->elementByName( 'channel' );
// Loop through all items in RSS feed
foreach ( $channel->elementsByName( 'item' ) as $item )
{
$addCount += importRSSItem( $item, $rssImport, $cli );
}
if ( !$isQuiet )
{
$cli->output( 'RSSImport '.$rssImport->attribute( 'name' ).': End. '.$addCount.' objects added' );
}
}
/*!
Import specifiec rss item into content tree
\param RSS item xml element
\param $rssImport Object
\param cli
\return 1 if object added, 0 if not
*/
function importRSSItem( &$item, &$rssImport, &$cli )
{
global $isQuiet;
$rssImportID =& $rssImport->attribute( 'id' );
$rssOwnerID =& $rssImport->attribute( 'object_owner_id' ); // Get owner user id
$parentContentObjectTreeNode =& eZContentObjectTreeNode::fetch( $rssImport->attribute( 'destination_node_id' ) ); // Get parent treenode object
if ( $parentContentObjectTreeNode == null )
{
if ( !$isQuiet )
{
$cli->output( 'RSSImport '.$rssImport->attribute( 'name' ).': Destination tree node seems to be unavailable' );
}
return 0;
}
$parentContentObject =& $parentContentObjectTreeNode->attribute( 'object' ); // Get parent content object
$title =& $item->elementByName( 'title' );
$link =& $item->elementByName( 'link' );
$description =& $item->elementByName( 'description' );
$md5Sum = md5( $link->textContent() );
// Try to fetch RSSImport object with md5 sum matching link.
$existingObject =& eZPersistentObject::fetchObject( eZContentObject::definition(), null,
array( 'remote_id' => 'RSSImport_'.$rssImportID.'_'.$md5Sum ) );
// if object exists, continue to next import item
if ( $existingObject != null )
{
unset( $existingObject ); // delete object to preserve memory
if ( !$isQuiet )
{
$cli->output( 'RSSImport '.$rssImport->attribute( 'name' ).': Object with URL: '.$link->textContent().' already exists' );
}
return 0;
}
// Fetch class, and create ezcontentobject from it.
$contentClass =& eZContentClass::fetch( $rssImport->attribute( 'class_id' ) );
// Instantiate the object with user $rssOwnerID and use section id from parent. And store it.
$contentObject =& $contentClass->instantiate( $rssOwnerID, $parentContentObject->attribute( 'section_id' ) );
$contentObject->store();
// Create node assignment
$nodeAssignment =& eZNodeAssignment::create( array( 'contentobject_id' => $contentObject->attribute( 'id' ),
'contentobject_version' => $contentObject->attribute( 'current_version' ),
'is_main' => 1,
'parent_node' => $parentContentObjectTreeNode->attribute( 'node_id' ) ) );
$nodeAssignment->store();
$version =& $contentObject->version( 1 );
$version->setAttribute( 'status', EZ_VERSION_STATUS_DRAFT );
$version->store();
// Get object attributes, and set their values and store them.
$dataMap =& $contentObject->dataMap();
// set title
$attributeTitle =& $dataMap[$rssImport->attribute( 'class_title' )];
if ( $attributeTitle != null && $title != null )
{
if ( $attributeTitle->attribute( 'data_type_string' ) == 'ezxmltext' )
{
setEZXMLAttribute( $attributeTitle, $title->textContent() );
}
else
{
$attributeTitle->setAttribute( 'data_text', $title->textContent() );
}
$attributeTitle->store();
}
else if ( !$isQuiet )
{
$cli->output( 'RSSImport '.$rssImport->attribute( 'name' ).': Could not find title map for : '.$rssImport->attribute( 'class_title' ) );
}
// set url
$attributeLink =& $dataMap[$rssImport->attribute( 'class_url' )];
if ( $attributeLink != null && $link != null )
{
$dataType = $attributeLink->attribute( 'data_type_string' );
if ( $dataType == 'ezxmltext' )
{
setEZXMLAttribute( $attributeLink, $link->textContent() );
}
elseif ( $dataType == 'ezurl' )
{
$attributeLink->setContent( $link->textContent() );
}
else
{
$attributeLink->setAttribute( 'data_text', $link->textContent() );
}
$attributeLink->store();
unset( $dataType );
}
else if ( !$isQuiet )
{
$cli->output( 'RSSImport '.$rssImport->attribute( 'name' ).': Could not find link map for : '.$rssImport->attribute( 'class_link' ) );
}
// set description
$attributeDescription =& $dataMap[$rssImport->attribute( 'class_description' )];
if ( $attributeDescription != null && $description != null )
{
if ( $attributeDescription->attribute( 'data_type_string' ) == 'ezxmltext' )
{
setEZXMLAttribute( $attributeDescription, $description->textContent() );
}
else
{
$attributeDescription->setAttribute( 'data_text', $description->textContent() );
}
$attributeDescription->store();
}
else if ( !$isQuiet )
{
$cli->output( 'RSSImport '.$rssImport->attribute( 'name' ).': Could not find description map for : '.$rssImport->attribute( 'class_descriptione' ) );
}
$contentObject->setAttribute( 'remote_id', 'RSSImport_'.$rssImportID.'_'.md5( $link->textContent() ) );
$contentObject->store();
//publish new object
$operationResult = eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $contentObject->attribute( 'id' ),
'version' => 1 ) );
if ( !isset( $operationResult['status'] ) || $operationResult['status'] != EZ_MODULE_OPERATION_CONTINUE )
{
if ( isset( $operationResult['result'] ) && isset( $operationResult['result']['content'] ) )
$failReason = $operationResult['result']['content'];
else
$failReason = "unknown error";
$cli->error( "Publishing failed: $failReason" );
unset( $failReason );
}
if ( !$isQuiet )
{
$cli->output( 'RSSImport '.$rssImport->attribute( 'name' ).': Object created; '.$title->textContent() );
}
return 1;
}
function setEZXMLAttribute( &$attribute, &$attributeValue, $link = false )
{
include_once( "kernel/classes/datatypes/ezxmltext/handlers/input/ezsimplifiedxmlinput.php" );
$inputData = "<?xml version=\"1.0\"?>";
$inputData .= "<section>";
$inputData .= "<paragraph>";
$inputData .= $attributeValue;
$inputData .= "</paragraph>";
$inputData .= "</section>";
$dumpdata = "";
$simplifiedXMLInput = new eZSimplifiedXMLInput( $dumpdata, null, null );
$inputData = $simplifiedXMLInput->convertInput( $inputData );
$description = $inputData[0]->toString();
$attribute->setAttribute( 'data_text', $description );
$attribute->store();
}
?>