00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102 include_once( "kernel/classes/ezdatatype.php" );
00103 include_once( "lib/ezxml/classes/ezxml.php" );
00104 include_once( "kernel/common/template.php" );
00105 include_once( 'lib/eztemplate/classes/eztemplateincludefunction.php' );
00106 include_once( 'kernel/classes/datatypes/ezurl/ezurl.php' );
00107 include_once( 'kernel/classes/datatypes/ezurl/ezurlobjectlink.php' );
00108 include_once( "lib/ezutils/classes/ezini.php" );
00109
00110
00111 define( "EZ_DATATYPESTRING_XML_TEXT", "ezxmltext" );
00112 define( 'EZ_DATATYPESTRING_XML_TEXT_COLS_FIELD', 'data_int1' );
00113 define( 'EZ_DATATYPESTRING_XML_TEXT_COLS_VARIABLE', '_ezxmltext_cols_' );
00114
00115
00116 define( 'EZ_XMLTEXT_VERSION_30_TIMESTAMP', 1045487555 );
00117
00118
00119 define( 'EZ_XMLTEXT_VERSION_TIMESTAMP', EZ_XMLTEXT_VERSION_30_TIMESTAMP );
00120
00121 class eZXMLTextType extends eZDataType
00122 {
00123 function eZXMLTextType()
00124 {
00125 $this->eZDataType( EZ_DATATYPESTRING_XML_TEXT, ezi18n( 'kernel/classes/datatypes', "XML block", 'Datatype name' ),
00126 array( 'serialize_supported' => true ) );
00127 }
00128
00129
00130
00131
00132 function initializeClassAttribute( &$classAttribute )
00133 {
00134 if ( $classAttribute->attribute( EZ_DATATYPESTRING_XML_TEXT_COLS_FIELD ) == null )
00135 $classAttribute->setAttribute( EZ_DATATYPESTRING_XML_TEXT_COLS_FIELD, 10 );
00136 $classAttribute->store();
00137 }
00138
00139
00140
00141
00142 function initializeObjectAttribute( &$contentObjectAttribute, $currentVersion, &$originalContentObjectAttribute )
00143 {
00144 if ( $currentVersion != false )
00145 {
00146 $xmlText = eZXMLTextType::rawXMLText( $originalContentObjectAttribute );
00147 $contentObjectAttribute->setAttribute( "data_text", $xmlText );
00148 }
00149 else
00150 {
00151 include_once( 'kernel/classes/datatypes/ezxmltext/ezxmlinputparser.php' );
00152 $parser = new eZXMLInputParser;
00153 $doc = $parser->createRootNode();
00154 $xmlText = eZXMLTextType::domString( $doc );
00155 $contentObjectAttribute->setAttribute( "data_text", $xmlText );
00156 }
00157 }
00158
00159
00160
00161
00162
00163 function validateObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
00164 {
00165
00166
00167 $xmlText =& $this->objectAttributeContent( $contentObjectAttribute );
00168 $input =& $xmlText->attribute( 'input' );
00169 $isValid = $input->validateInput( $http, $base, $contentObjectAttribute );
00170
00171 return $isValid;
00172 }
00173
00174 function fetchClassAttributeHTTPInput( &$http, $base, &$classAttribute )
00175 {
00176 $column = $base . EZ_DATATYPESTRING_XML_TEXT_COLS_VARIABLE . $classAttribute->attribute( 'id' );
00177 if ( $http->hasPostVariable( $column ) )
00178 {
00179 $columnValue = $http->postVariable( $column );
00180 $classAttribute->setAttribute( EZ_DATATYPESTRING_XML_TEXT_COLS_FIELD, $columnValue );
00181 return true;
00182 }
00183 return false;
00184 }
00185
00186
00187
00188
00189 function fetchObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
00190 {
00191
00192
00193
00194
00195 return true;
00196 }
00197
00198
00199
00200
00201
00202
00203 function postInitializeObjectAttribute( &$objectAttribute, $currentVersion, &$originalContentObjectAttribute )
00204 {
00205 }
00206
00207
00208
00209
00210 function storeObjectAttribute( &$attribute )
00211 {
00212 $attribute->setAttribute( 'data_int', EZ_XMLTEXT_VERSION_TIMESTAMP );
00213 }
00214
00215
00216
00217
00218 function &viewTemplate( &$contentobjectAttribute )
00219 {
00220 $template = $this->DataTypeString;
00221 $suffix = $this->viewTemplateSuffix( $contentobjectAttribute );
00222 if ( $suffix )
00223 $template .= '_' . $suffix;
00224 return $template;
00225 }
00226
00227
00228
00229
00230 function &editTemplate( &$contentobjectAttribute )
00231 {
00232 $template = $this->DataTypeString;
00233 $suffix = $this->editTemplateSuffix( $contentobjectAttribute );
00234 if ( $suffix )
00235 $template .= '_' . $suffix;
00236 return $template;
00237 }
00238
00239
00240
00241
00242 function &informationTemplate( &$contentobjectAttribute )
00243 {
00244 $template = $this->DataTypeString;
00245 $suffix = $this->informationTemplateSuffix( $contentobjectAttribute );
00246 if ( $suffix )
00247 $template .= '_' . $suffix;
00248 return $template;
00249 }
00250
00251
00252
00253
00254 function &viewTemplateSuffix( &$contentobjectAttribute )
00255 {
00256 $content =& $this->objectAttributeContent( $contentobjectAttribute );
00257 $outputHandler =& $content->attribute( 'output' );
00258 $suffix = $outputHandler->viewTemplateSuffix( $contentobjectAttribute );
00259 return $suffix;
00260 }
00261
00262
00263
00264
00265 function &editTemplateSuffix( &$contentobjectAttribute )
00266 {
00267 $content =& $this->objectAttributeContent( $contentobjectAttribute );
00268 $inputHandler =& $content->attribute( 'input' );
00269 $suffix =& $inputHandler->editTemplateSuffix( $contentobjectAttribute );
00270 return $suffix;
00271 }
00272
00273
00274
00275
00276 function &informationTemplateSuffix( &$contentobjectAttribute )
00277 {
00278 $content =& $this->objectAttributeContent( $contentobjectAttribute );
00279 $inputHandler =& $content->attribute( 'input' );
00280 $suffix =& $inputHandler->informationTemplateSuffix( $contentobjectAttribute );
00281 return $suffix;
00282 }
00283
00284
00285
00286
00287
00288
00289 function rawXMLText( &$contentObjectAttribute )
00290 {
00291 $text = $contentObjectAttribute->attribute( 'data_text' );
00292 $timestamp = $contentObjectAttribute->attribute( 'data_int' );
00293 if ( $timestamp < EZ_XMLTEXT_VERSION_30_TIMESTAMP )
00294 {
00295 include_once( 'lib/ezi18n/classes/eztextcodec.php' );
00296 $charset = 'UTF-8';
00297 $codec =& eZTextCodec::instance( false, $charset );
00298 $text = $codec->convertString( $text );
00299 $timestamp = EZ_XMLTEXT_VERSION_30_TIMESTAMP;
00300 }
00301 return $text;
00302 }
00303
00304
00305
00306
00307
00308
00309
00310 function domString( &$domDocument )
00311 {
00312 $ini =& eZINI::instance();
00313 $xmlCharset = $ini->variable( 'RegionalSettings', 'ContentXMLCharset' );
00314 if ( $xmlCharset == 'enabled' )
00315 {
00316 include_once( 'lib/ezi18n/classes/eztextcodec.php' );
00317 $charset = eZTextCodec::internalCharset();
00318 }
00319 else if ( $xmlCharset == 'disabled' )
00320 $charset = true;
00321 else
00322 $charset = $xmlCharset;
00323 if ( $charset !== true )
00324 {
00325 include_once( 'lib/ezi18n/classes/ezcharsetinfo.php' );
00326 $charset = eZCharsetInfo::realCharsetCode( $charset );
00327 }
00328 $domString = $domDocument->toString( $charset );
00329
00330 $eZXMLini =& eZINI::instance( 'ezxml.ini' );
00331 if ( $eZXMLini->hasVariable( 'InputSettings', 'AllowNumericEntities' ) )
00332 {
00333 if ( $eZXMLini->variable( 'InputSettings', 'AllowNumericEntities' ) == 'true' )
00334 {
00335 $domString = preg_replace( '/&#([0-9]+);/', '&#\1;', $domString );
00336 }
00337 }
00338
00339 return $domString;
00340 }
00341
00342
00343
00344
00345 function &objectAttributeContent( &$contentObjectAttribute )
00346 {
00347 include_once( 'kernel/classes/datatypes/ezxmltext/ezxmltext.php' );
00348 $xmlText = new eZXMLText( eZXMLTextType::rawXMLText( $contentObjectAttribute ), $contentObjectAttribute );
00349 return $xmlText;
00350 }
00351
00352
00353
00354
00355 function metaData( $contentObjectAttribute )
00356 {
00357 $metaData = "";
00358
00359 $xml = new eZXML();
00360 $dom =& $xml->domTree( eZXMLTextType::rawXMLText( $contentObjectAttribute ) );
00361
00362 if ( $dom )
00363 {
00364 $textNodes = $dom->elementsByName( "#text" );
00365 if ( is_array( $textNodes ) )
00366 {
00367 foreach ( $textNodes as $node )
00368 {
00369 $metaData .= " " . $node->content();
00370 }
00371 }
00372 }
00373 return $metaData;
00374 }
00375
00376
00377
00378
00379 function toString( $contentObjectAttribute )
00380 {
00381 return $contentObjectAttribute->attribute( 'data_text' );
00382 }
00383
00384 function fromString( &$contentObjectAttribute, $string )
00385 {
00386 return $contentObjectAttribute->setAttribute( 'data_text', $string );
00387 }
00388
00389
00390
00391
00392
00393 function title( &$contentObjectAttribute )
00394 {
00395 $text = eZXMLTextType::rawXMLText( $contentObjectAttribute );
00396
00397 include_once( 'lib/ezxml/classes/ezxml.php' );
00398 $xml = new eZXML();
00399 $document =& $xml->domTree( $text );
00400
00401
00402 if ( !$document )
00403 return $text;
00404
00405 $root =& $document->root();
00406 $section =& $root->firstChild();
00407 $textDom = false;
00408 if ( $section )
00409 $textDom =& $section->firstChild();
00410
00411 if ( $textDom and $textDom->hasChildren() )
00412 {
00413 $textDomContent =& $textDom->firstChild();
00414 $text = $textDomContent->content();
00415 }
00416 elseif ( $textDom )
00417 {
00418 $text = $textDom->content();
00419 }
00420
00421 return $text;
00422 }
00423
00424 function hasObjectAttributeContent( &$contentObjectAttribute )
00425 {
00426 $content = $this->objectAttributeContent( $contentObjectAttribute );
00427 if ( is_object( $content ) and
00428 !$content->attribute( 'is_empty' ) )
00429 return true;
00430 return false;
00431 }
00432
00433
00434
00435
00436 function isIndexable()
00437 {
00438 return true;
00439 }
00440
00441
00442
00443
00444 function isInformationCollector()
00445 {
00446 return false;
00447 }
00448
00449
00450
00451
00452
00453 function templateList()
00454 {
00455 return array( array( 'regexp',
00456 '#^content/datatype/[a-zA-Z]+/ezxmltags/#' ) );
00457 }
00458
00459
00460
00461
00462 function serializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode )
00463 {
00464 $textColumns = $classAttribute->attribute( EZ_DATATYPESTRING_XML_TEXT_COLS_FIELD );
00465 $attributeParametersNode->appendChild( eZDOMDocument::createElementTextNode( 'text-column-count', $textColumns ) );
00466 }
00467
00468
00469
00470
00471 function unserializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode )
00472 {
00473 $textColumns = $attributeParametersNode->elementTextContentByName( 'text-column-count' );
00474 $classAttribute->setAttribute( EZ_DATATYPESTRING_XML_TEXT_COLS_FIELD, $textColumns );
00475 }
00476
00477
00478
00479 function customObjectAttributeHTTPAction( $http, $action, &$contentObjectAttribute )
00480 {
00481 $content =& $this->objectAttributeContent( $contentObjectAttribute );
00482 $inputHandler =& $content->attribute( 'input' );
00483 $inputHandler->customObjectAttributeHTTPAction( $http, $action, $contentObjectAttribute );
00484 }
00485
00486
00487
00488
00489
00490 function serializeContentObjectAttribute( &$package, &$objectAttribute )
00491 {
00492 include_once( 'lib/ezxml/classes/ezxml.php' );
00493
00494 $DOMNode = $this->createContentObjectAttributeDOMNode( $objectAttribute );
00495
00496 $xml = new eZXML();
00497 $doc =& $xml->domTree( $objectAttribute->attribute( 'data_text' ) );
00498
00499 if ( $doc )
00500 {
00501
00502
00503
00504
00505 {
00506 include_once( 'kernel/classes/datatypes/ezurl/ezurlobjectlink.php' );
00507 include_once( 'kernel/classes/datatypes/ezurl/ezurl.php' );
00508
00509 $links =& $doc->elementsByName( 'link' );
00510 $embeds =& $doc->elementsByName( 'embed' );
00511 $objects =& $doc->elementsByName( 'object' );
00512 $embedsInline =& $doc->elementsByName( 'embed-inline' );
00513
00514 $allTags = array_merge( $links, $embeds, $embedsInline, $objects );
00515
00516 if ( is_array( $allTags ) )
00517 {
00518 foreach ( array_keys( $allTags ) as $index )
00519 {
00520 $tag =& $allTags[$index];
00521 $linkID = $tag->getAttribute( 'url_id' );
00522 if ( $tag->nodeName == 'object' )
00523 $objectID = $tag->getAttribute( 'id' );
00524 else
00525 $objectID = $tag->getAttribute( 'object_id' );
00526 $nodeID = $tag->getAttribute( 'node_id' );
00527 if ( $linkID )
00528 {
00529 $urlObj = eZURL::fetch( $linkID );
00530 if ( !$urlObj )
00531 continue;
00532 $url =& $urlObj->attribute( 'url' );
00533 $tag->setAttribute( 'href', $url );
00534 $tag->removeAttribute( 'url_id' );
00535 unset( $urlObj );
00536 }
00537 elseif ( $objectID )
00538 {
00539 $object = eZContentObject::fetch( $objectID, false );
00540 if ( is_array( $object ) )
00541 $tag->setAttribute( 'object_remote_id', $object['remote_id'] );
00542
00543 if ( $tag->nodeName == 'object' )
00544 $tag->removeAttribute( 'id' );
00545 else
00546 $tag->removeAttribute( 'object_id' );
00547 }
00548 elseif ( $nodeID )
00549 {
00550 $node = eZContentObjectTreeNode::fetch( $nodeID, false, false );
00551 if ( is_array( $node ) )
00552 $tag->setAttribute( 'node_remote_id', $node['remote_id'] );
00553 $tag->removeAttribute( 'node_id' );
00554 }
00555 }
00556 }
00557 }
00558
00559 $DOMNode->appendChild( $doc->root() );
00560 }
00561
00562 return $DOMNode;
00563 }
00564
00565
00566
00567
00568
00569
00570 function unserializeContentObjectAttribute( &$package, &$objectAttribute, $attributeNode )
00571 {
00572 $rootNode = $attributeNode->firstChild();
00573 if ( $rootNode )
00574 {
00575
00576
00577
00578
00579
00580
00581
00582
00583 include_once( 'kernel/classes/datatypes/ezurl/ezurlobjectlink.php' );
00584 include_once( 'kernel/classes/datatypes/ezurl/ezurl.php' );
00585
00586 $xml = new eZXML();
00587 $domDocument =& $xml->domTree( $rootNode->toString( 0 ), array ( 'CharsetConversion' => false ) );
00588
00589 if ( $domDocument )
00590 {
00591 $links =& $domDocument->elementsByName( 'link' );
00592 if ( !is_array( $links ) )
00593 $links = array();
00594
00595 foreach ( array_keys( $links ) as $index )
00596 {
00597 $linkRef =& $links[$index];
00598 $href = $linkRef->attributeValue( 'href' );
00599 if ( !$href )
00600 continue;
00601 $urlObj = eZURL::urlByURL( $href );
00602
00603 if ( !$urlObj )
00604 {
00605 $urlObj = eZURL::create( $href );
00606 $urlObj->store();
00607 }
00608
00609 $linkRef->remove_attribute( 'href' );
00610 $linkRef->set_attribute( 'url_id', $urlObj->attribute( 'id' ) );
00611 $urlObjectLink = eZURLObjectLink::create( $urlObj->attribute( 'id' ),
00612 $objectAttribute->attribute( 'id' ),
00613 $objectAttribute->attribute( 'version' ) );
00614 $urlObjectLink->store();
00615
00616 }
00617 $objectAttribute->setAttribute( 'data_text', eZXMLTextType::domString( $domDocument ) );
00618 $domDocument->cleanup();
00619 }
00620 }
00621 }
00622
00623 function postUnserializeContentObjectAttribute( &$package, &$objectAttribute )
00624 {
00625 $xmlString = $objectAttribute->attribute( 'data_text' );
00626 $xml = new eZXML();
00627 $doc =& $xml->domTree( $xmlString );
00628
00629 if ( !is_object( $doc ) )
00630 return false;
00631
00632 $links =& $doc->elementsByName( 'link' );
00633 $objects =& $doc->elementsByName( 'object' );
00634 $embeds =& $doc->elementsByName( 'embed' );
00635 $embedsInline =& $doc->elementsByName( 'embed-inline' );
00636
00637 $allTags = array_merge( $links, $embeds, $embedsInline, $objects );
00638 $modified = false;
00639
00640 $contentObject =& $objectAttribute->attribute( 'object' );
00641 foreach( array_keys( $allTags ) as $key )
00642 {
00643 $tag =& $allTags[$key];
00644
00645 $objectRemoteID = $tag->getAttribute( 'object_remote_id' );
00646 $nodeRemoteID = $tag->getAttribute( 'node_remote_id' );
00647 if ( $objectRemoteID )
00648 {
00649 $objectArray = eZContentObject::fetchByRemoteID( $objectRemoteID, false );
00650 if ( !is_array( $objectArray ) )
00651 {
00652 eZDebug::writeWarning( "Can't fetch object with remoteID = $objectRemoteID", 'eZXMLTextType::unserialize' );
00653 continue;
00654 }
00655
00656 $objectID = $objectArray['id'];
00657 if ( $tag->nodeName == 'object' )
00658 $tag->setAttribute( 'id', $objectID );
00659 else
00660 $tag->setAttribute( 'object_id', $objectID );
00661 $tag->removeAttribute( 'object_remote_id' );
00662 $modified = true;
00663
00664
00665 if ( $contentObject )
00666 {
00667 $relationType = $tag->nodeName == 'link' ? EZ_CONTENT_OBJECT_RELATION_LINK : EZ_CONTENT_OBJECT_RELATION_EMBED;
00668 $contentObject->addContentObjectRelation( $objectID, $objectAttribute->attribute( 'version' ), false, 0, $relationType );
00669 }
00670 }
00671 elseif ( $nodeRemoteID )
00672 {
00673 $nodeArray = eZContentObjectTreeNode::fetchByRemoteID( $nodeRemoteID, false );
00674 if ( !is_array( $nodeArray ) )
00675 {
00676 eZDebug::writeWarning( "Can't fetch node with remoteID = $nodeRemoteID", 'eZXMLTextType::unserialize' );
00677 continue;
00678 }
00679
00680 $nodeID = $nodeArray['node_id'];
00681 $tag->setAttribute( 'node_id', $nodeID );
00682 $tag->removeAttribute( 'node_remote_id' );
00683 $modified = true;
00684
00685
00686 if ( $contentObject )
00687 {
00688 $node = eZContentObjectTreeNode::fetch( $nodeID, false, false );
00689 if ( $node )
00690 {
00691 $relationType = $tag->nodeName == 'link' ? EZ_CONTENT_OBJECT_RELATION_LINK : EZ_CONTENT_OBJECT_RELATION_EMBED;
00692 $contentObject->addContentObjectRelation( $node['contentobject_id'], $objectAttribute->attribute( 'version' ), false, 0, $relationType );
00693 }
00694 }
00695 }
00696 }
00697
00698 if ( $modified )
00699 {
00700 $objectAttribute->setAttribute( 'data_text', eZXMLTextType::domString( $doc ) );
00701 return true;
00702 }
00703 else
00704 {
00705 return false;
00706 }
00707 }
00708
00709
00710
00711 function deleteStoredObjectAttribute( &$contentObjectAttribute, $version = null )
00712 {
00713 $contentObjectAttributeID = $contentObjectAttribute->attribute( "id" );
00714
00715 $db =& eZDB::instance();
00716
00717
00718
00719 include_once( 'kernel/classes/datatypes/ezurl/ezurlobjectlink.php' );
00720 if ( $version == null )
00721 {
00722 eZPersistentObject::removeObject( eZURLObjectLink::definition(),
00723 array( 'contentobject_attribute_id' => $contentObjectAttributeID ) );
00724
00725 }
00726 else
00727 {
00728 eZPersistentObject::removeObject( eZURLObjectLink::definition(),
00729 array( 'contentobject_attribute_id' => $contentObjectAttributeID,
00730 'contentobject_attribute_version' => $version ) );
00731 }
00732
00733
00734 if ( $db->databaseName() == 'oracle' )
00735 {
00736 $res = $db->arrayQuery( "SELECT DISTINCT id
00737 FROM ezurl, ezurl_object_link
00738 WHERE ezurl.id = ezurl_object_link.url_id(+)
00739 AND url_id IS NULL" );
00740 }
00741 else
00742 {
00743 $res = $db->arrayQuery(" SELECT DISTINCT id
00744 FROM ezurl LEFT JOIN ezurl_object_link ON (ezurl.id = ezurl_object_link.url_id)
00745 WHERE url_id IS NULL" );
00746 }
00747
00748
00749 if ( count( $res ) )
00750 {
00751 $unusedUrlIDs = array();
00752 foreach ( $res as $record )
00753 $unusedUrlIDs[] = $record['id'];
00754 $unusedUrlIDString = implode( ', ', $unusedUrlIDs );
00755
00756 $db->query( "DELETE FROM ezurl WHERE id IN ($unusedUrlIDString)" );
00757 }
00758 }
00759
00760
00761
00762
00763 function diff( $old, $new, $options = false )
00764 {
00765 include_once( 'lib/ezdiff/classes/ezdiff.php' );
00766 $diff = new eZDiff();
00767 $diff->setDiffEngineType( $diff->engineType( 'xml' ) );
00768 $diff->initDiffEngine();
00769 $diffObject = $diff->diff( $old, $new );
00770 return $diffObject;
00771 }
00772
00773 }
00774
00775 eZDataType::register( EZ_DATATYPESTRING_XML_TEXT, "ezXMLTextType" );
00776
00777 ?>