Forums / Developer / Import an eZXMLText with custom tags

Import an eZXMLText with custom tags

Author Message

David Ogilo

Monday 03 August 2009 3:20:50 am

Hi,

I have been trying to understand how to import a html data in ezpublish. Here is a copy of the code

case 'ezxmltext':
{
     $parser = new eZSimplifiedXMLInputParser( $contentObjectID, false, 0, false );
     $attributeValue = $dataString;
     $attributeValue = str_replace( "\r", '', $attributeValue );
     $attributeValue = str_replace( "\n", '', $attributeValue );
     $attributeValue = str_replace( "\t", ' ', $attributeValue );
     $document = $parser->process( $attributeValue );
     
     if ( !is_object( $document ) )
     {
         $cli->output( 'Error in xml parsing' );
	 return; 
     }

     $dataString = eZXMLTextType::domString( $document );
}

Some how it strips out all image and object tags in the html data and just import links, text, and other html tags.

I have tried changing all image tags in the html data to <custom name="img"> and still doesn't work.

Does anyone know of a way to resolve this issue?

Thanks,

David

Heath

Monday 03 August 2009 3:37:51 am

Hello David,

In BC ImportCSV we did something similar. Perhaps this example will help.
<i>http://svn.projects.ez.no/bcimportcsv/trunk/extension/bcimportcsv/bin/bccsvjoomlacontenttablehtmlimport.php</i>

            case 'ezxmltext':
            {
		if( $attribute->ContentClassAttributeIdentifier == 'caption' ) {
		  $dataString = null;
                  break;
		}
                // Filter for images, process, store and link
                if ( is_numeric( $imageContainerID ) )
                {
                    $matches = array();
		    $pattern = '/<img\b[^>]*\bsrc=(\\\\["\'])?((?(1)(?:(?!\1).)*|[^\s>]*))(?(1)\1)[^>]*>/si';
                    preg_match_all( $pattern, $dataString, $matches );
		    $matches_count = count( $matches[2] );
  		    // $cli->output( print_r( $matches ) );

                    if ( $matches_count > 0 )
                    {
                        $toReplace = array();
                        $replacements = array();
			$objectComplex = true;

			$cli->output( "Matches Count: " . $matches_count );

                        // $imagenr = 0;
                        foreach ( $matches[2] as $key => $match )
                        {
				$toReplace[] = $matches[0][$key];
				$imageURL = trim( str_replace(chr(32), '%20', str_replace(' ', '%20', str_replace('\"', '', $matches[2][$key] ) ) ) );
                                if ( substr($imageURL, 0, 1) == '/') {
					$imageURL = 'http://www.diariodelhuila.com' . $imageURL;
        	                }
                                $cli->output("Image link: " . $imageURL);

                            	$imageTempURL = 'http://optics.kulgun.net/Blue-Sky/red-sunset-casey1.jpg';
				$imageTempFileName = 'def.jpg';

				$imageFileName = basename( $imageURL );
				$cli->output( 'Image File Name: '.$imageFileName );

				$imagePath = "/tmp/imgtmp/" . $imageTempFileName;
                                // $imagePath = "/tmp/imgtmp" . $imagenr++ . ".jpg";

                                if ( !copy($imageTempURL, $imagePath ) )
                                {
                                    $cli->output("Error copying image from remote server");
                                    $replacements[] = '';
                                }else {
                                    $imageClass = eZContentClass::fetchByIdentifier( 'image' );
                                    $imageObject = $imageClass->instantiate( $creator );
                                    $imageObject->store();
                                
                                    $imageObjectID = $imageObject->attribute( 'id' );
                            
                                    $imageNodeAssignment = eZNodeAssignment::create( array(
                                                                                 'contentobject_id' => $imageObject->attribute( 'id' ),
                                                                                 'contentobject_version' => $imageObject->attribute( 'current_version' ),
                                                                                 'parent_node' => $imageContainerID,
                                                                                 'is_main' => 1
                                                                                 )
                                                                             );
                                    $imageNodeAssignment->store();

                                    $imageVersion = $imageObject->version( 1 );
                                    $imageVersion->setAttribute( 'modified', $createDate );
                                    $imageVersion->setAttribute( 'status', eZContentObjectVersion::STATUS_DRAFT );
                                    $imageVersion->store();

                                    $imageAttributes = $imageObject->attribute( 'contentobject_attributes' );
                                    $cli->output("Image attributes:" . $cli->output( $imageAttributes, true ) );
                                
				    $imageAttributes[0]->fromString( $imageTempFileName );
                                    $imageAttributes[0]->store();

                                    $imageAttributes[2]->fromString( $imagePath );
                                    $imageAttributes[2]->store();

                                    $operationResult = eZOperationHandler::execute( 'content', 'publish',
                                                       array( 'object_id' => $imageObjectID, 'version' => 1 ) );

                                    $replacements[] = '<embed href="ezobject://' . $imageObject->attribute( 'id' ) . '" size="original" />';
                                }
                            }
                        
                            $dataString = str_replace( $toReplace, $replacements, $dataString );
	     		    $cli->output( print_r( $toReplace ) );
			    // $cli->output( print_r( $replacements ) );
			    // $cli->output( $dataString ); echo "\n";
			    unset( $toReplace ); unset( $replacements ); unset( $imageAttributes ); unset( $imageNodeAssignment );
                        }
                    }

                    $parser = new eZSimplifiedXMLInputParser( $contentObjectID, false, 0 );
                    $document = $parser->process( $dataString );
                    // $dataString = eZXMLTextType::domString( $document );
		    // $cli->output( print_r( $dataString ) );

		    // get links
		    $links = $document->getElementsByTagName( 'link' );
		    if( is_numeric( $links->length ) && $links->length > 0 && is_object( $links ) ) {
  		        // $cli->output( print_r( $links ) );
			$li = 0;
                        // for each link
	                for( $li = 0; $li < $links->length; $li++ )
                        { 
			   $linkNode = $links->item( $li );
			   $url_id = $linkNode->getAttribute( 'url_id' );

			   $cli->output( 'Link Item Count: '. $li );
                           $cli->output( 'Link Item ID: '. $url_id );

			   if( is_numeric( $url_id ) ) {
                               // create link between url (link) and object
                               $eZURLObjectLink = eZURLObjectLink::create( $url_id,
                                                  $contentObject->attribute('id'),
                                                  $contentObject->attribute('current_version') );
		               $cli->output( print_r( $eZURLObjectLink ) );
		               // $cli->output( print_r( $url_id ) );

        	               $eZURLObjectLink->store();
                           }
	                }
                    }
                }break;
                default:
            }

Cheers,
Heath

Brookins Consulting | http://brookinsconsulting.com/
Certified | http://auth.ez.no/certification/verify/380350
Solutions | http://projects.ez.no/users/community/brookins_consulting
eZpedia community documentation project | http://ezpedia.org

David Ogilo

Monday 03 August 2009 6:12:02 am

Thanks Heath.

Hmm, do I have to save a copy of the image on the ezPublish backend? How about object tags?

Is there a way to include a custom tag which won't be stripped out from the HTML?

Rainer Krauss

Monday 03 August 2009 6:27:06 am

Hi david,

depends on what you actually want to achieve.

Would you like to get something into eZ Publish that contains
- an image or
- a file?

Would you like to have the image / file be shown or should it be available for download?

If you want to include files / images as links, you can add them to the media library and reference them via links to eznode:// or ezobject:// and the node or object id.

The tags eZXMLObject accepts are documented here:
http://ez.no/doc/ez_publish/technical_manual/4_0/reference/xml_tags
..and you'd have to look at replacing your tags with valid ones.

Best wishes,
Rainer

David Ogilo

Friday 07 August 2009 2:34:48 am

Thanks Rainer!

That was exactly what I wanted, worked like a charm!! :)

David

Sébastien Antoniotti

Friday 28 August 2009 9:35:39 am

Hi,

I wake up this topic because I'm making a similar import, but I would like to create objects and nodes like this :

$billet_attributes = array(
			'titre' 								=> "article 1",
			'contenu' 							=> "<p>some xhtml content</p>"
		);
		$params = array();
		$params['parent_node_id'] = '2';
		$params['class_identifier'] = 'wx_billet';
		$params['attributes'] = $billet_attributes;

		$object = eZContentFunctions::createAndPublishObject( $params );     

The problem is that 'contenu' attribute is not correctly set. I think this is because I need to do parse the xhtml content, but I don't know how to because I'm not in the case were I have the $contentObjectID needed in

$parser = new eZSimplifiedXMLInputParser( $contentObjectID, false, 0, false );

Thanks in advance !

eZ Publish Freelance
web : http://www.webaxis.fr

David Ogilo

Thursday 01 October 2009 7:19:37 am

Hi Sébastien,

You could try this:

'contenu'  => htmlentities("<p>some xhtml content</p>")

Sébastien Antoniotti

Thursday 01 October 2009 10:55:05 am

Thanks ! I'll try it !

eZ Publish Freelance
web : http://www.webaxis.fr

eZ debug

Timing: Jan 18 2025 05:18:30
Script start
Timing: Jan 18 2025 05:18:30
Module start 'content'
Timing: Jan 18 2025 05:18:30
Module end 'content'
Timing: Jan 18 2025 05:18:31
Script end

Main resources:

Total runtime0.9406 sec
Peak memory usage4,096.0000 KB
Database Queries213

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0070 588.8672180.8281
Module start 'content' 0.00700.8196 769.6953704.6719
Module end 'content' 0.82660.1139 1,474.3672348.4297
Script end 0.9406  1,822.7969 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00380.4069210.0002
Check MTime0.00140.1513210.0001
Mysql Total
Database connection0.00110.118910.0011
Mysqli_queries0.866792.13972130.0041
Looping result0.00170.18062110.0000
Template Total0.913997.220.4570
Template load0.00200.212520.0010
Template processing0.911996.945820.4559
Template load and register function0.00010.012710.0001
states
state_id_array0.00070.075210.0007
state_identifier_array0.00080.085420.0004
Override
Cache load0.00180.1923550.0000
Sytem overhead
Fetch class attribute can translate value0.00110.114350.0002
Fetch class attribute name0.00090.0951100.0001
XML
Image XML parsing0.00130.141450.0003
class_abstraction
Instantiating content class attribute0.00000.0018110.0000
General
dbfile0.00390.4168340.0001
String conversion0.00000.000530.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
8content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
16content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
5content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
3content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
5content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 39
 Number of unique templates used: 7

Time used to render debug report: 0.0002 secs