eZ Publish  [4.0]
ezrssimport.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZRSSImport class
00004 //
00005 // Created on: <24-Sep-2003 12:53:56 kk>
00006 //
00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00008 // SOFTWARE NAME: eZ Publish
00009 // SOFTWARE RELEASE: 4.0.x
00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00011 // SOFTWARE LICENSE: GNU General Public License v2.0
00012 // NOTICE: >
00013 //   This program is free software; you can redistribute it and/or
00014 //   modify it under the terms of version 2.0  of the GNU General
00015 //   Public License as published by the Free Software Foundation.
00016 //
00017 //   This program is distributed in the hope that it will be useful,
00018 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //   GNU General Public License for more details.
00021 //
00022 //   You should have received a copy of version 2.0 of the GNU General
00023 //   Public License along with this program; if not, write to the Free
00024 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 //   MA 02110-1301, USA.
00026 //
00027 //
00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00029 //
00030 
00031 /*! \file ezrssimport.php
00032 */
00033 
00034 /*!
00035   \class eZRSSImport ezrssimport.php
00036   \brief Handles RSS Import in eZ Publish
00037 
00038   RSSImport is used to create RSS feeds from published content. See kernel/rss for more files.
00039 */
00040 
00041 //include_once( 'kernel/classes/ezpersistentobject.php' );
00042 
00043 class eZRSSImport extends eZPersistentObject
00044 {
00045     const STATUS_VALID = 1;
00046     const STATUS_DRAFT = 0;
00047 
00048     /*!
00049      Initializes a new RSSImport.
00050     */
00051     function eZRSSImport( $row )
00052     {
00053         $this->eZPersistentObject( $row );
00054     }
00055 
00056     /*!
00057      \reimp
00058     */
00059     static function definition()
00060     {
00061         return array( "fields" => array( "id" => array( 'name' => 'ID',
00062                                                         'datatype' => 'integer',
00063                                                         'default' => 0,
00064                                                         'required' => true ),
00065                                          'modified' => array( 'name' => 'Modified',
00066                                                               'datatype' => 'integer',
00067                                                               'default' => 0,
00068                                                               'required' => true ),
00069                                          'modifier_id' => array( 'name' => 'ModifierID',
00070                                                                  'datatype' => 'integer',
00071                                                                  'default' => 0,
00072                                                                  'required' => true,
00073                                                                  'foreign_class' => 'eZUser',
00074                                                                  'foreign_attribute' => 'contentobject_id',
00075                                                                  'multiplicity' => '1..*' ),
00076                                          'created' => array( 'name' => 'Created',
00077                                                              'datatype' => 'integer',
00078                                                              'default' => 0,
00079                                                              'required' => true ),
00080                                          'creator_id' => array( 'name' => 'CreatorID',
00081                                                                 'datatype' => 'integer',
00082                                                                 'default' => 0,
00083                                                                 'required' => true,
00084                                                                 'foreign_class' => 'eZUser',
00085                                                                 'foreign_attribute' => 'contentobject_id',
00086                                                                 'multiplicity' => '1..*' ),
00087                                          'object_owner_id' => array( 'name' => 'ObjectOwnerID',
00088                                                                      'datatype' => 'integer',
00089                                                                      'default' => 0,
00090                                                                      'required' => true,
00091                                                                      'foreign_class' => 'eZUser',
00092                                                                      'foreign_attribute' => 'contentobject_id',
00093                                                                      'multiplicity' => '1..*' ),
00094                                          'status' => array( 'name' => 'Status',
00095                                                             'datatype' => 'integer',
00096                                                             'default' => 0,
00097                                                             'required' => true ),
00098                                          'name' => array( 'name' => 'Name',
00099                                                           'datatype' => 'string',
00100                                                           'default' => '',
00101                                                           'required' => true ),
00102                                          'url' => array( 'name' => 'URL',
00103                                                          'datatype' => 'string',
00104                                                          'default' => '',
00105                                                          'required' => true ),
00106                                          'destination_node_id' => array( 'name' => 'DestinationNodeID',
00107                                                                          'datatype' => 'int',
00108                                                                          'default' => '',
00109                                                                          'required' => true,
00110                                                                          'foreign_class' => 'eZContentObjectTreeNode',
00111                                                                          'foreign_attribute' => 'node_id',
00112                                                                          'multiplicity' => '1..*' ),
00113                                          'class_id' => array( 'name' => 'ClassID',
00114                                                               'datatype' => 'integer',
00115                                                               'default' => 0,
00116                                                               'required' => true,
00117                                                               'foreign_class' => 'eZContentClass',
00118                                                               'foreign_attribute' => 'id',
00119                                                               'multiplicity' => '1..*' ),
00120                                          'class_title' => array( 'name' => 'ClassTitle', // deprecated
00121                                                                  'datatype' => 'string',
00122                                                                  'default' => '',
00123                                                                  'required' => false ),
00124                                          'class_url' => array( 'name' => 'ClassURL', // deprecated
00125                                                                'datatype' => 'string',
00126                                                                'default' => '',
00127                                                                'required' => false ),
00128                                          'class_description' => array( 'name' => 'ClassDescription', // deprecated
00129                                                                        'datatype' => 'string',
00130                                                                        'default' => '',
00131                                                                        'required' => false ),
00132                                          'active' => array( 'name' => 'Active',
00133                                                             'datatype' => 'integer',
00134                                                             'default' => 1,
00135                                                             'required' => true ),
00136                                          'import_description' => array( 'name' => 'ImportDescriptionValue',
00137                                                                         'datatype' => 'string',
00138                                                                         'default' => '',
00139                                                                         'required' => true ) ),
00140                       "keys" => array( "id", 'status' ),
00141                       'function_attributes' => array( 'class_attributes' => 'classAttributes',
00142                                                       'destination_path' => 'destinationPath',
00143                                                       'modifier' => 'modifier',
00144                                                       'object_owner' => 'objectOwner',
00145                                                       'import_description_array' => 'importDescription',
00146                                                       'field_map' => 'fieldMap',
00147                                                       'object_attribute_list' => 'objectAttributeList' ),
00148                       "increment_key" => "id",
00149                       "class_name" => "eZRSSImport",
00150                       "name" => "ezrss_import" );
00151     }
00152 
00153     /*!
00154      \static
00155      Creates a new RSS Import
00156      \param User ID
00157 
00158      \return the new RSS Import object
00159     */
00160     static function create( $userID = false )
00161     {
00162         if ( $userID === false )
00163         {
00164             //include_once( "kernel/classes/datatypes/ezuser/ezuser.php" );
00165             $user = eZUser::currentUser();
00166             $userID = $user->attribute( "contentobject_id" );
00167         }
00168 
00169         $dateTime = time();
00170         $row = array( 'id' => null,
00171                       'name' => ezi18n( 'kernel/rss', 'New RSS Import' ),
00172                       'modifier_id' => $userID,
00173                       'modified' => $dateTime,
00174                       'creator_id' => $userID,
00175                       'created' => $dateTime,
00176                       'object_owner_id' => $userID,
00177                       'url' => '',
00178                       'status' => 0,
00179                       'destination_node_id' => 0,
00180                       'class_id' => 0,
00181                       'class_title' => '',
00182                       'class_url' => '',
00183                       'class_description' => '',
00184                       'active' => 1 );
00185 
00186         return new eZRSSImport( $row );
00187     }
00188 
00189     /*!
00190      Store Object to database
00191      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
00192      the calls within a db transaction; thus within db->begin and db->commit.
00193     */
00194     function store( $fieldFilters = null )
00195     {
00196         //include_once( "kernel/classes/datatypes/ezuser/ezuser.php" );
00197         $dateTime = time();
00198         $user = eZUser::currentUser();
00199 
00200         $this->setAttribute( 'modifier_id', $user->attribute( 'contentobject_id' ) );
00201         $this->setAttribute( 'modified', $dateTime );
00202         eZPersistentObject::store( $fieldFilters );
00203     }
00204 
00205     /*!
00206      \static
00207       Fetches the RSS Import by ID.
00208 
00209      \param RSS Import ID
00210     */
00211     static function fetch( $id, $asObject = true, $status = eZRSSImport::STATUS_VALID )
00212     {
00213         return eZPersistentObject::fetchObject( eZRSSImport::definition(),
00214                                                 null,
00215                                                 array( "id" => $id,
00216                                                        'status' => $status ),
00217                                                 $asObject );
00218     }
00219 
00220     /*!
00221      \static
00222       Fetches complete list of RSS Imports.
00223     */
00224     static function fetchList( $asObject = true, $status = eZRSSImport::STATUS_VALID )
00225     {
00226         $cond = null;
00227         if ( $status !== false )
00228         {
00229             $cond = array( 'status' => $status );
00230         }
00231         return eZPersistentObject::fetchObjectList( eZRSSImport::definition(),
00232                                                     null, $cond, null, null,
00233                                                     $asObject );
00234     }
00235 
00236     /*!
00237      \static
00238       Fetches complete list of active RSS Imports.
00239     */
00240     static function fetchActiveList( $asObject = true )
00241     {
00242         return eZPersistentObject::fetchObjectList( eZRSSImport::definition(),
00243                                                     null,
00244                                                     array( 'status' => 1,
00245                                                            'active' => 1 ),
00246                                                     null,
00247                                                     null,
00248                                                     $asObject );
00249     }
00250 
00251 
00252     function objectOwner()
00253     {
00254         if ( isset( $this->ObjectOwnerID ) and $this->ObjectOwnerID )
00255         {
00256             //include_once( "kernel/classes/datatypes/ezuser/ezuser.php" );
00257             return eZUser::fetch( $this->ObjectOwnerID );
00258         }
00259         return null;
00260     }
00261 
00262     function modifier()
00263     {
00264         if ( isset( $this->ModifierID ) and $this->ModifierID )
00265         {
00266             //include_once( "kernel/classes/datatypes/ezuser/ezuser.php" );
00267             return eZUser::fetch( $this->ModifierID );
00268         }
00269         return null;
00270     }
00271 
00272     function classAttributes()
00273     {
00274         if ( isset( $this->ClassID ) and $this->ClassID )
00275         {
00276             //include_once( 'kernel/classes/ezcontentclass.php' );
00277             $contentClass = eZContentClass::fetch( $this->ClassID );
00278             if ( $contentClass )
00279             {
00280                 return $contentClass->fetchAttributes();
00281             }
00282         }
00283         return null;
00284     }
00285 
00286     function destinationPath()
00287     {
00288         $retValue = null;
00289         if ( isset( $this->DestinationNodeID ) and $this->DestinationNodeID )
00290         {
00291             //include_once( "kernel/classes/ezcontentobjecttreenode.php" );
00292             $objectNode = eZContentObjectTreeNode::fetch( $this->DestinationNodeID );
00293             if ( isset( $objectNode ) )
00294             {
00295                 $path_array = $objectNode->attribute( 'path_array' );
00296                 $path_array_count = count( $path_array );
00297                 for ( $i = 0; $i < $path_array_count; ++$i )
00298                 {
00299                     $treenode = eZContentObjectTreeNode::fetch( $path_array[$i], false, false );
00300                     if ( is_array( $treenode ) && array_key_exists( 'name', $treenode ) )
00301                     {
00302                         if ( $i == 0 )
00303                         {
00304                             $retValue = $treenode['name'];
00305                         }
00306                         else
00307                         {
00308                             $retValue .= '/' . $treenode['name'];
00309                         }
00310                     }
00311                 }
00312             }
00313         }
00314         return $retValue;
00315     }
00316 
00317     /*!
00318      \static
00319      Analize RSS import, and get RSS version number
00320 
00321      \param URL
00322 
00323      \return RSS version number, false if invalid URL
00324     */
00325     static function getRSSVersion( $url )
00326     {
00327         //include_once( "lib/ezutils/classes/ezhttptool.php" );
00328         $xmlData = eZHTTPTool::getDataByURL( $url );
00329 
00330         if ( $xmlData === false )
00331             return false;
00332 
00333         // Create DomDocument from http data
00334 
00335         $domDocument = new DOMDocument( '1.0', 'utf-8' );
00336         $success = $domDocument->loadXML( $xmlData );
00337 
00338         if ( !$success )
00339         {
00340             return false;
00341         }
00342 
00343         $root = $domDocument->documentElement;
00344 
00345         switch( $root->getAttribute( 'version' ) )
00346         {
00347             default:
00348             case '1.0':
00349             {
00350                 return '1.0';
00351             } break;
00352 
00353             case '0.91':
00354             case '0.92':
00355             case '2.0':
00356             {
00357                 return $root->getAttribute( 'version' );
00358             } break;
00359         }
00360     }
00361 
00362     /*!
00363      \static
00364      Object attribute list
00365     */
00366     static function objectAttributeList()
00367     {
00368         return array( 'published' => 'Published',
00369                       'modified' => 'Modified' );
00370     }
00371 
00372     /*!
00373      \static
00374 
00375      Return default RSS field definition
00376 
00377      \param RSS version
00378 
00379      \return RSS field definition array.
00380     */
00381     static function rssFieldDefinition( $version = '2.0' )
00382     {
00383         switch ( $version )
00384         {
00385             case '1.0':
00386             {
00387                 return array( 'item' => array( 'attributes' => array( 'about' ),
00388                                                'elements' => array( 'title',
00389                                                                     'link',
00390                                                                     'description' ) ),
00391                               'channel' => array( 'attributes' => array( 'about' ),
00392                                                   'elements' => array( 'title',
00393                                                                        'link',
00394                                                                        'description'.
00395                                                                        'image' => array( 'attributes' => array( 'resource' ) ) ) ) );
00396             } break;
00397 
00398             case '2.0':
00399             case '0.91':
00400             case '0.92':
00401             {
00402                 return array( 'item' => array( 'elements' => array( 'title',
00403                                                                     'link',
00404                                                                     'description',
00405                                                                     'author',
00406                                                                     'category',
00407                                                                     'comments',
00408                                                                     'guid',
00409                                                                     'pubDate' ) ),
00410                               'channel' => array( 'elements' => array( 'title',
00411                                                                        'link',
00412                                                                        'description',
00413                                                                        'copyright',
00414                                                                        'managingEditor',
00415                                                                        'webMaster',
00416                                                                        'pubDate',
00417                                                                        'lastBuildDate',
00418                                                                        'category',
00419                                                                        'generator',
00420                                                                        'docs',
00421                                                                        'cloud',
00422                                                                        'ttl' ) ) );
00423             }
00424         }
00425     }
00426 
00427     /*!
00428      \static
00429 
00430      \param RSS version
00431 
00432      \return Ordered array of field definitions
00433     */
00434     static function fieldMap( $version = '2.0' )
00435     {
00436         $fieldDefinition = eZRSSImport::rssFieldDefinition();
00437 
00438         $ini = eZINI::instance();
00439         foreach( $ini->variable( 'RSSSettings', 'ActiveExtensions' ) as $activeExtension )
00440         {
00441             if ( file_exists( eZExtension::baseDirectory() . '/' . $activeExtension . '/rss/' . $activeExtension . 'rssimport.php' ) )
00442             {
00443                 include_once( eZExtension::baseDirectory() . '/' . $activeExtension . '/rss/' . $activeExtension . 'rssimport.php' );
00444                 $fieldDefinition = eZRSSImport::arrayMergeRecursive( $fieldDefinition, call_user_func( array(  $activeExtension . 'rssimport', 'rssFieldDefinition' ), array() ) );
00445             }
00446         }
00447 
00448         $returnArray = array();
00449         eZRSSImport::recursiveFieldMap( $fieldDefinition, '', '', $returnArray, 0 );
00450 
00451         return $returnArray;
00452     }
00453 
00454     /*!
00455      \static
00456 
00457      Recursivly build field map
00458 
00459      \param array
00460     */
00461     static function recursiveFieldMap( $definitionArray, $globalKey, $value, &$returnArray, $count )
00462     {
00463         foreach( $definitionArray as $key => $definition )
00464         {
00465             if ( is_string( $definition ) )
00466             {
00467                 $returnArray[$globalKey . ' - ' . $definition ] = $value . ' - ' . ucfirst( $definition );
00468             }
00469             else
00470             {
00471                 eZRSSImport::recursiveFieldMap( $definition,
00472                                                 $globalKey . ( strlen( $globalKey ) ? ' - ' : '' ) . $key ,
00473                                                 $value . ( strlen( $value ) && ( $count % 2 == 0 ) ? ' - ' : '' ) . ( $count % 2 == 0 ? ucfirst( $key ) : '' ),
00474                                                 $returnArray, $count + 1 );
00475             }
00476         }
00477     }
00478 
00479     /*!
00480      Set import description
00481 
00482      Import definition must be set as an multidimentional array.
00483 
00484      Example : array( 'rss_version' => <version>,
00485                       'object_attributes' => array( ... ),
00486                       'class_attributes' => array( <content class attribute id> => <RSS import field>,  ... ) )
00487     */
00488     function setImportDescription( $definition = array() )
00489     {
00490         $this->setAttribute( 'import_description', serialize( $definition ) );
00491     }
00492 
00493     /*!
00494      Get import description
00495 
00496      \return import description
00497     */
00498     function importDescription()
00499     {
00500         $description = @unserialize( $this->attribute( 'import_description' ) );
00501         if ( !$description )
00502         {
00503             $description = array();
00504         }
00505         return $description;
00506     }
00507 
00508     static function arrayMergeRecursive( $arr1, $arr2 )
00509     {
00510         if ( !is_array( $arr1 ) ||
00511              !is_array( $arr2 ) )
00512         {
00513             return $arr2;
00514         }
00515         foreach ($arr2 AS $key => $value )
00516         {
00517             $arr1[$key] = eZRSSImport::arrayMergeRecursive( @$arr1[$key], $value);
00518         }
00519 
00520         return $arr1;
00521     }
00522 }
00523 
00524 ?>