| 1 | <?php |
| 2 | /** |
| 3 | * $Id: rssimport_planete.php 74 2009-11-07 12:32:25Z dapob $ |
| 4 | * $HeadURL: file:///var/svn/pezfr/trunk/www/extension/planete/cronjobs/rssimport_planete.php $ |
| 5 | */ |
| 6 | |
| 7 | $ini = eZINI::instance(); |
| 8 | $planetINI = eZINI::instance( 'planete.ini' ); |
| 9 | $planetRootNodeID = $planetINI->variable( 'TreeSettings', 'PlanetRootNodeID' ); |
| 10 | $planetariumNodeID = $planetINI->variable( 'TreeSettings', 'PlanetariumNodeID' ); |
| 11 | $siteClassID = $planetINI->variable( 'ImportSettings', 'SiteClassID' ); |
| 12 | $postClassID = $planetINI->variable( 'ImportSettings', 'PostClassID' ); |
| 13 | $postOwnerID = $planetINI->variable( 'ImportSettings', 'PostOwnerID' ); |
| 14 | |
| 15 | $titleAttributeID = $planetINI->variable( 'ImportSettings', 'PostTitleAttributeID' ); |
| 16 | $descriptionAttributeID = $planetINI->variable( 'ImportSettings', 'PostDescriptionAttributeID' ); |
| 17 | $urlAttributeID = $planetINI->variable( 'ImportSettings', 'PostURLAttributeID' ); |
| 18 | $twitterMessageAttributeID = $planetINI->variable( 'ImportSettings', 'PostTwitterMessageAttributeID' ); |
| 19 | |
| 20 | $params = array( 'ClassFilterType' => 'include', |
| 21 | 'ClassFilterArray' => array( $siteClassID ) ); |
| 22 | |
| 23 | $blogNodes = eZContentObjectTreeNode::subTreeByNodeID( $params, $planetRootNodeID ); |
| 24 | $blogPostContentClass = eZContentClass::fetch( $postClassID ); |
| 25 | |
| 26 | $db = eZDB::instance(); |
| 27 | |
| 28 | eZLog::setMaxLogSize( $planetINI->variable( 'ImportSettings', 'MaxLogSize' ) ); |
| 29 | $logFile = $planetINI->variable( 'ImportSettings', 'LogFile' ); |
| 30 | |
| 31 | $updated = false; |
| 32 | foreach ( $blogNodes as $blog ) |
| 33 | { |
| 34 | eZLog::write( $blog->attribute( 'name' ), $logFile ); |
| 35 | $dataMap = $blog->attribute( 'data_map' ); |
| 36 | $rssSource = $dataMap['rss']->attribute( 'content' ); |
| 37 | try |
| 38 | { |
| 39 | $feed = ezcFeed::parse( $rssSource ); |
| 40 | } |
| 41 | catch( Exception $e ) |
| 42 | { |
| 43 | eZLog::write( 'Planet RSSImport ' . $rssSource |
| 44 | . ' failed to import document', $logFile ); |
| 45 | continue ; |
| 46 | } |
| 47 | if ( !is_array( $feed->item ) ) |
| 48 | { |
| 49 | continue; |
| 50 | } |
| 51 | |
| 52 | $blogContentObject = $blog->attribute( 'object' ); |
| 53 | foreach( $feed->item as $item ) |
| 54 | { |
| 55 | eZLog::write( $item->title, $logFile ); |
| 56 | if ( isset( $item->id ) ) |
| 57 | { |
| 58 | $md5Sum = md5( $item->id ); |
| 59 | } |
| 60 | else |
| 61 | { |
| 62 | $md5Sum = md5( $item->link[0] ); |
| 63 | } |
| 64 | $remoteID = $md5Sum . '_' . $blog->attribute( 'node_id' ) . '_Planete_RSSImport'; |
| 65 | $contentObject = eZContentObject::fetchByRemoteID( $remoteID ); |
| 66 | $dataMap = null; |
| 67 | $newObject = false; |
| 68 | $db->begin(); |
| 69 | if ( !is_object( $contentObject ) ) |
| 70 | { |
| 71 | eZLog::write( '-> Creating new content object', $logFile ); |
| 72 | $contentObject = $blogPostContentClass->instantiate( $postOwnerID, $blogContentObject->attribute( 'section_id' ) ); |
| 73 | $contentObject->store(); |
| 74 | $contentObjectID = $contentObject->attribute( 'id' ); |
| 75 | |
| 76 | // Create node assignment |
| 77 | $nodeAssignment = eZNodeAssignment::create( array( 'contentobject_id' => $contentObjectID, |
| 78 | 'contentobject_version' => $contentObject->attribute( 'current_version' ), |
| 79 | 'is_main' => 1, |
| 80 | 'parent_node' => $blog->attribute( 'node_id' ) ) ); |
| 81 | $nodeAssignment->store(); |
| 82 | |
| 83 | $version = $contentObject->version( 1 ); |
| 84 | $version->setAttribute( 'status', eZContentObjectVersion::STATUS_DRAFT ); |
| 85 | $version->store(); |
| 86 | $dataMap = $version->attribute( 'data_map' ); |
| 87 | $newObject = true; |
| 88 | } |
| 89 | else |
| 90 | { |
| 91 | // need to check if the object required an update |
| 92 | $dataMapObject = $contentObject->attribute( 'data_map' ); |
| 93 | foreach( $dataMapObject as $k => $contentAttribute ) |
| 94 | { |
| 95 | $data = $dataMapObject[$k]->attribute( 'content' ); |
| 96 | if ( $contentAttribute->attribute( 'contentclassattribute_id' ) == $titleAttributeID ) |
| 97 | { |
| 98 | if ( $data != trim( $item->title ) ) |
| 99 | { |
| 100 | $version = $contentObject->createNewVersion(); |
| 101 | $dataMap = $version->attribute( 'data_map' ); |
| 102 | break; |
| 103 | } |
| 104 | } |
| 105 | elseif ( $contentAttribute->attribute( 'contentclassattribute_id' ) == $descriptionAttributeID ) |
| 106 | { |
| 107 | $content = trim( $item->description ); |
| 108 | if ( isset( $item->Content ) && isset( $item->Content->encoded ) ) |
| 109 | { |
| 110 | $content = trim( $item->Content->encoded ); |
| 111 | } |
| 112 | if ( $data != $content ) |
| 113 | { |
| 114 | $version = $contentObject->createNewVersion(); |
| 115 | $dataMap = $version->attribute( 'data_map' ); |
| 116 | break; |
| 117 | } |
| 118 | } |
| 119 | elseif ( $contentAttribute->attribute( 'contentclassattribute_id' ) == $urlAttributeID ) |
| 120 | { |
| 121 | if ( $data != trim( $item->link[0] ) ) |
| 122 | { |
| 123 | $version = $contentObject->createNewVersion(); |
| 124 | $dataMap = $version->attribute( 'data_map' ); |
| 125 | break; |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | } |
| 131 | if ( $dataMap !== null ) |
| 132 | { |
| 133 | eZLog::write( '-> ' . $contentObject->attribute( 'name' ) |
| 134 | . ' need update', $logFile ); |
| 135 | foreach( $dataMap as $k => $contentAttribute ) |
| 136 | { |
| 137 | if ( $contentAttribute->attribute( 'contentclassattribute_id' ) == $titleAttributeID ) |
| 138 | { |
| 139 | $dataMap[$k]->fromString( trim( $item->title ) ); |
| 140 | $dataMap[$k]->store(); |
| 141 | } |
| 142 | elseif ( $contentAttribute->attribute( 'contentclassattribute_id' ) == $descriptionAttributeID ) |
| 143 | { |
| 144 | $content = trim( $item->description ); |
| 145 | if ( isset( $item->Content ) && isset( $item->Content->encoded ) ) |
| 146 | { |
| 147 | $content = trim( $item->Content->encoded ); |
| 148 | } |
| 149 | $dataMap[$k]->fromString( $content ); |
| 150 | $dataMap[$k]->store(); |
| 151 | } |
| 152 | elseif ( $contentAttribute->attribute( 'contentclassattribute_id' ) == $urlAttributeID ) |
| 153 | { |
| 154 | $dataMap[$k]->fromString( trim( $item->link[0] ) ); |
| 155 | $dataMap[$k]->store(); |
| 156 | } |
| 157 | elseif ( $contentAttribute->attribute( 'contentclassattribute_id' ) == $twitterMessageAttributeID ) |
| 158 | { |
| 159 | if ( !$newObject || $planetariumNodeID == $blog->attribute( 'parent_node_id' ) ) |
| 160 | { |
| 161 | // empty twitter message attribute, if it's an update of an existing object |
| 162 | // or it's a post in the planetarium |
| 163 | $dataMap[$k]->fromString( '' ); |
| 164 | $dataMap[$k]->store(); |
| 165 | } |
| 166 | else |
| 167 | { |
| 168 | $url = trim( $item->link[0] ); |
| 169 | $dataMap[$k]->fromString( '%title ' . $url . ' #ezpublish' ); |
| 170 | $dataMap[$k]->store(); |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | $contentObject->setAttribute( 'remote_id', $remoteID ); |
| 175 | $contentObject->store(); |
| 176 | $updated = true; |
| 177 | $operationResult = eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $contentObject->attribute( 'id' ), |
| 178 | 'version' => $version->attribute( 'version' ) ) ); |
| 179 | if ( isset( $item->published ) ) |
| 180 | { |
| 181 | $ts = $item->published->date->format( 'U' ); |
| 182 | $contentObject->setAttribute( 'published', $ts ); |
| 183 | $contentObject->setAttribute( 'modified', $ts ); |
| 184 | $contentObject->store(); |
| 185 | $version = $contentObject->attribute( 'current' ); |
| 186 | $version->setAttribute( 'created', $ts ); |
| 187 | $version->store(); |
| 188 | } |
| 189 | } |
| 190 | else |
| 191 | { |
| 192 | eZLog::write( ' -> nothing to do', $logFile ); |
| 193 | } |
| 194 | $db->commit(); |
| 195 | } |
| 196 | } |
| 197 | if ( $updated ) |
| 198 | { |
| 199 | // need to clear feed/planet cache |
| 200 | eZLog::write( 'Need to clear feed/planet cache', $logFile ); |
| 201 | $cacheInfo = eZPlaneteUtils::rssCacheInfo(); |
| 202 | eZFSFileHandler::fileDelete( $cacheInfo['cache-dir'] . '/' . $cacheInfo['cache-file'] ); |
| 203 | // make sure we always serve a cached file |
| 204 | file_get_contents( 'http://' . $ini->variable( 'SiteSettings', 'SiteURL' ) . '/feed/planet' ); |
| 205 | } |
| 206 | |
| 207 | eZStaticCache::executeActions(); |
| 208 | |
| 209 | ?> |
| 210 | |