| [ Index ] |
|
Code source de eZ Publish 3.9.0 |
1 #!/usr/bin/env php 2 <?php 3 // 4 // Created on: <04-Feb-2003 11:02:34 amos> 5 // 6 // SOFTWARE NAME: eZ publish 7 // SOFTWARE RELEASE: 3.9.0 8 // BUILD VERSION: 17785 9 // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS 10 // SOFTWARE LICENSE: GNU General Public License v2.0 11 // NOTICE: > 12 // This program is free software; you can redistribute it and/or 13 // modify it under the terms of version 2.0 of the GNU General 14 // Public License as published by the Free Software Foundation. 15 // 16 // This program is distributed in the hope that it will be useful, 17 // but WITHOUT ANY WARRANTY; without even the implied warranty of 18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 // GNU General Public License for more details. 20 // 21 // You should have received a copy of version 2.0 of the GNU General 22 // Public License along with this program; if not, write to the Free 23 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 24 // MA 02110-1301, USA. 25 // 26 // 27 28 /*! \file updatexmltext.php 29 Takes care of fixing the links in the XML format to use the url ID 30 instead of a HREF. 31 Will also fix all /content/download links to use the new format if any 32 is found. 33 Also if $fixAllAttributes is set to true it will make sure that all XML data 34 is stored in correct charset according to site settings. 35 */ 36 37 set_time_limit( 0 ); 38 39 include_once ( 'lib/ezutils/classes/ezcli.php' ); 40 include_once ( 'kernel/classes/ezscript.php' ); 41 42 $fixErrors = true; 43 $fixAllAttributes = true; 44 $fixAttribute = true; 45 $fixURL = true; 46 47 $cli =& eZCLI::instance(); 48 $endl = $cli->endlineString(); 49 50 $script =& eZScript::instance( array( 'description' => ( "eZ publish xml text field updater.\n\n". 51 "Goes trough all objects with XML fields and corrects any broken XML structures and content." . 52 "\n" . 53 "updatexmltext.php" ), 54 'use-session' => true, 55 'use-modules' => true, 56 'use-extensions' => true ) ); 57 58 $script->startup(); 59 60 $options = $script->getOptions( "[sql]", 61 "", 62 array( 'sql' => "Display sql queries" 63 ) ); 64 $script->initialize(); 65 66 $showSQL = $options['sql'] ? true : false; 67 $siteAccess = $options['siteaccess'] ? $options['siteaccess'] : false; 68 69 if ( $siteAccess ) 70 { 71 changeSiteAccessSetting( $siteaccess, $siteAccess ); 72 } 73 74 function changeSiteAccessSetting( &$siteaccess, $optionData ) 75 { 76 global $isQuiet; 77 $cli =& eZCLI::instance(); 78 if ( file_exists( 'settings/siteaccess/' . $optionData ) ) 79 { 80 $siteaccess = $optionData; 81 if ( !$isQuiet ) 82 $cli->notice( "Using siteaccess $siteaccess for xml text field update" ); 83 } 84 else 85 { 86 if ( !$isQuiet ) 87 $cli->notice( "Siteaccess $optionData does not exist, using default siteaccess" ); 88 } 89 } 90 91 include_once ( 'kernel/classes/ezcontentclassattribute.php' ); 92 include_once ( 'kernel/classes/ezcontentobjectattribute.php' ); 93 include_once ( 'kernel/classes/ezcontentobject.php' ); 94 include_once ( 'kernel/classes/ezbinaryfilehandler.php' ); 95 include_once ( 'kernel/classes/datatypes/ezbinaryfile/ezbinaryfile.php' ); 96 97 include_once ( 'lib/ezdb/classes/ezdb.php' ); 98 include_once ( 'kernel/classes/datatypes/ezurl/ezurl.php' ); 99 100 $db =& eZDB::instance(); 101 $db->setIsSQLOutputEnabled( $showSQL ); 102 103 $xmlTypeAttributeList =& eZContentClassAttribute::fetchList( true, array( 'data_type' => 'ezxmltext', 104 'version' => 0 ) ); 105 $classAttributeIDList = array(); 106 for ( $i = 0; $i < count( $xmlTypeAttributeList ); ++$i ) 107 { 108 $xmlTypeAttribute =& $xmlTypeAttributeList[$i]; 109 $classAttributeIDList[] = $xmlTypeAttribute->attribute( 'id' ); 110 } 111 unset( $xmlTypeAttributeList ); 112 113 $urlCount = eZURL::fetchListCount(); 114 115 $attributeCount = eZContentObjectAttribute::fetchListByClassID( $classAttributeIDList, false, array( 'offset' => 0, 116 'length' => 3 ), 117 false, true ); 118 $urlList = eZURL::fetchList(); 119 120 $urlRefMap = array(); 121 $urlIDMap = array(); 122 for ( $i = 0; $i < count( $urlList ); ++$i ) 123 { 124 $url =& $urlList[$i]; 125 $urlRefMap[$url->attribute( 'url' )] =& $url; 126 $urlIDMap[$url->attribute( 'id' )] =& $url; 127 } 128 129 function findAndReplaceLinks( &$doc, &$node ) 130 { 131 global $urlRefMap; 132 global $urlIDMap; 133 global $showDebug; 134 global $fixErrors; 135 $foundLinks = false; 136 unset( $children ); 137 if ( !$node ) 138 return $foundLinks; 139 $children =& $node->Children; 140 for ( $i = 0; $i < count( $children ); ++$i ) 141 { 142 unset( $child ); 143 $child =& $children[$i]; 144 if ( $child->name() == 'link' ) 145 { 146 unset( $linkAttributes ); 147 $linkAttributes =& $child->attributes(); 148 unset( $hrefAttribute ); 149 unset( $idAttribute ); 150 unset( $targetAttribute ); 151 $hrefAttribute = null; 152 $idAttribute = null; 153 $targetAttribute = null; 154 for ( $j = 0; $j < count( $linkAttributes ); ++$j ) 155 { 156 $linkAttribute =& $linkAttributes[$j]; 157 if ( $linkAttribute->name() == 'href' ) 158 $hrefAttribute =& $linkAttributes[$j]; 159 else if ( $linkAttribute->name() == 'id' ) 160 $idAttribute =& $linkAttributes[$j]; 161 else if ( $linkAttribute->name() == 'target' ) 162 $targetAttribute =& $linkAttributes[$j]; 163 } 164 if ( $idAttribute === null and 165 $hrefAttribute !== null ) 166 { 167 $href = $hrefAttribute->content(); 168 if ( array_key_exists( $href, $urlRefMap ) ) 169 { 170 if ( $showDebug ) 171 print( "Found '$href'\n" ); 172 $url =& $urlRefMap[$href]; 173 } 174 else 175 { 176 if ( $showDebug ) 177 print( "Found new '$href'\n" ); 178 $urlID = eZURL::registerURL( $href ); 179 $url = eZURL::fetch( $urlID ); 180 $urlRefMap[$href] =& $url; 181 $urlIDMap[$urlID] =& $url; 182 } 183 $idAttribute = $doc->createAttributeNode( 'id', $url->attribute( 'id' ) ); 184 $child->appendAttribute( $idAttribute ); 185 $child->removeNamedAttribute( 'href' ); 186 $foundLinks = true; 187 } 188 if ( $targetAttribute !== null ) 189 { 190 $target = $targetAttribute->content(); 191 if ( $target == '_self' ) 192 { 193 if ( $showDebug ) 194 print( "Found '$target'\n" ); 195 $child->removeNamedAttribute( 'target' ); 196 $foundLinks = true; 197 } 198 } 199 } 200 if ( findAndReplaceLinks( $doc, $child ) ) 201 $foundLinks = true; 202 unset( $child ); 203 } 204 unset( $children ); 205 return $foundLinks; 206 } 207 208 $wrongURLCount = 0; 209 $fixedURLCount = 0; 210 $wrongLinkCount = 0; 211 212 $attributeOffset = 0; 213 $attributeLimit = 140; 214 215 $dotCount = 0; 216 $dotTotalCount = 0; 217 $dotMax = 70; 218 219 include_once ( 'lib/ezxml/classes/ezxml.php' ); 220 $xml = new eZXML(); 221 222 if ( $fixAttribute ) 223 { 224 print( "Fixing bad xml text links\n" ); 225 $badXMLArray = array(); 226 while ( $attributeOffset < $attributeCount ) 227 { 228 $percent = ( $dotTotalCount * 100.0 ) / ( $attributeCount - 1 ); 229 // if ( $percent > 27.76 ) 230 // print( "cd " . $attributeOffset . ", " . $attributeLimit ); 231 unset( $objectAttributeList ); 232 $objectAttributeList =& eZContentObjectAttribute::fetchListByClassID( $classAttributeIDList, false, array( 'offset' => $attributeOffset, 233 'length' => $attributeLimit ), 234 true, false ); 235 // if ( $percent > 27.76 ) 236 // print( "ef" ); 237 $lastID = false; 238 for ( $i = 0; $i < count( $objectAttributeList ); ++$i ) 239 { 240 $percent = ( $dotTotalCount * 100.0 ) / ( $attributeCount - 1 ); 241 $objectAttribute =& $objectAttributeList[$i]; 242 // if ( $percent > 27.76 ) 243 // { 244 // print( $objectAttribute->attribute( 'id' ) . " (" . $objectAttribute->attribute( 'version' ) . ")\n" ); 245 // } 246 // if ( $lastID !== false and 247 // $objectAttribute->attribute( 'id' ) == $lastID) 248 // print( "Found duplicate " . $objectAttribute->attribute( 'id' ) . "\n" ); 249 $lastID = $objectAttribute->attribute( 'id' ); 250 $dataType = $objectAttribute->dataType(); 251 $handleAttribute = true; 252 $badDataType = false; 253 if ( !$dataType or get_class( $dataType ) != 'ezxmltexttype' ) 254 { 255 $handleAttribute = false; 256 $badDataType = true; 257 } 258 unset( $content ); 259 $content = null; 260 if ( $handleAttribute ) 261 { 262 $content =& $objectAttribute->content(); 263 if ( !$content or !is_object( $content ) ) 264 $handleAttribute = false; 265 } 266 unset( $xmlData ); 267 $xmlData = null; 268 if ( $handleAttribute ) 269 { 270 // if ( !is_object( $content ) ) 271 // print( get_class( $dataType ) . ", " . gettype( $content ) . " [$content]" . "\n" ); 272 // if ( is_object( $content ) ) 273 // { 274 $xmlData = $content->attribute( 'xml_data' ); 275 if ( !$xmlData ) 276 $handleAttribute = false; 277 // } 278 // else 279 // $handleAttribute = false; 280 } 281 unset( $doc ); 282 $doc = null; 283 if ( $handleAttribute ) 284 { 285 $doc =& $xml->domTree( $xmlData ); 286 if ( $doc ) 287 { 288 if ( findAndReplaceLinks( $doc, $doc->root() ) or 289 $objectAttribute->attribute( 'data_int' ) < EZ_XMLTEXT_VERSION_TIMESTAMP or 290 $fixAllAttributes ) 291 { 292 if ( $showDebug ) 293 print( "Links found and replaced\n" ); 294 // print( $doc->toString() . "\n" ); 295 $docString = eZXMLTextType::domString( $doc ); 296 $objectAttribute->setAttribute( 'data_text', $docString ); 297 $objectAttribute->setAttribute( 'data_int', EZ_XMLTEXT_VERSION_TIMESTAMP ); 298 if ( findAndReplaceLinks( $doc, $doc->root() ) or 299 $objectAttribute->attribute( 'data_int' ) < EZ_XMLTEXT_VERSION_TIMESTAMP ) 300 { 301 ++$wrongLinkCount; 302 print( '*' ); 303 } 304 else 305 print( '@' ); 306 } 307 else 308 print( '.' ); 309 } 310 else 311 { 312 if ( $showDebug ) 313 print( "Invalid XML data: $xmlData\n" ); 314 if ( trim( $xmlData ) == '' ) 315 { 316 unset( $doc ); 317 $doc = new eZDOMDocument(); 318 $doc->setRoot( $doc->createElementNode( 'section' ) ); 319 $docString = eZXMLTextType::domString( $doc ); 320 $objectAttribute->setAttribute( 'data_text', $docString ); 321 $objectAttribute->setAttribute( 'data_int', EZ_XMLTEXT_VERSION_TIMESTAMP ); 322 ++$wrongLinkCount; 323 print( '0' ); 324 } 325 else 326 { 327 unset( $doc ); 328 $doc = new eZDOMDocument(); 329 $doc->setRoot( $doc->createElementNode( 'section' ) ); 330 $docString = eZXMLTextType::domString( $doc ); 331 $objectAttribute->setAttribute( 'data_text', $docString ); 332 $objectAttribute->setAttribute( 'data_int', EZ_XMLTEXT_VERSION_TIMESTAMP ); 333 ++$wrongLinkCount; 334 $badXMLArray[] = array( 'id' => $objectAttribute->attribute( 'id' ), 335 'version' => $objectAttribute->attribute( 'version' ) ); 336 print( '-' ); 337 } 338 } 339 if ( $fixErrors ) 340 $objectAttribute->sync(); 341 } 342 else 343 { 344 if ( $badDataType ) 345 print( 'x' ); 346 else 347 print( '.' ); 348 } 349 ++$dotCount; 350 ++$dotTotalCount; 351 if ( $dotCount >= $dotMax or $dotTotalCount >= $attributeCount ) 352 { 353 $percent = number_format( ( $dotTotalCount * 100.0 ) / ( $attributeCount ), 2 ); 354 $dotSpace = ''; 355 if ( $dotTotalCount > $dotMax ) 356 $dotSpace = str_repeat( ' ', $dotMax - $dotCount ); 357 print( $dotSpace . " " . $percent . "% ( $dotTotalCount )\n" ); 358 $dotCount = 0; 359 } 360 // if ( $percent > 27.76 ) 361 // print( "ab" ); 362 } 363 364 $attributeOffset += $attributeLimit; 365 } 366 print( "\n" ); 367 print( ". Ignored\n" ); 368 print( "@ XML data upgrade\n" ); 369 print( "* Fixed url usage\n" ); 370 print( "- Invalid XML data\n" ); 371 print( "0 Empty XML data\n" ); 372 print( "x Wrong datatype, should be ezxmltext\n" ); 373 if ( count( $badXMLArray ) > 0 ) 374 { 375 print( "The following attributes had bad XML\n" ); 376 $len = 0; 377 $i = 0; 378 foreach ( $badXMLArray as $badXML ) 379 { 380 $text = ''; 381 if ( $len > 70 ) 382 { 383 $len = 0; 384 if ( $i > 0 ) 385 print( ',' ); 386 print( "\n" ); 387 } 388 else if ( $i > 0 ) 389 $text = ', '; 390 $text .= sprintf( "%5.d(%d)", 391 $badXML['id'], 392 $badXML['version'] ); 393 print( $text ); 394 $len += strlen( $text ); 395 ++$i; 396 } 397 print( "\n" ); 398 } 399 print( "\n" ); 400 } 401 402 $dotCount = 0; 403 $dotTotalCount = 0; 404 405 if ( $fixURL ) 406 { 407 $failedURLArray = array(); 408 print( "Fixing bad download urls\n" ); 409 for ( $i = 0; $i < count( $urlList ); ++$i ) 410 { 411 $url =& $urlList[$i]; 412 $urlText = $url->attribute( 'url' ); 413 if ( preg_match( "#/?content/download/[0-9]+/[0-9]+/[a-z_-]+/.+$#", $urlText, $matches ) ) 414 { 415 if ( $showDebug ) 416 print( "matched correct download url: '$urlText'\n" ); 417 print( '+' ); 418 } 419 else if ( preg_match( "#/?content/download/([0-9]+)(/([0-9]*))?#", $urlText, $matches ) ) 420 { 421 ++$wrongURLCount; 422 $attributeID = $matches[1]; 423 $version = $matches[3]; 424 if ( $version == false ) 425 $version = 1; 426 if ( $showDebug ) 427 print( "matched faulty download url: '$urlText' $attributeID @ $version\n" ); 428 $contentObjectAttribute = eZContentObjectAttribute::fetch( $attributeID, $version ); 429 if ( $contentObjectAttribute ) 430 { 431 $contentObjectID = $contentObjectAttribute->attribute( 'contentobject_id' ); 432 $contentObject =& eZContentObject::fetch( $contentObjectID ); 433 $downloadURL = eZBinaryFileHandler::downloadURL( $contentObject, $contentObjectAttribute ); 434 $url->setAttribute( 'url', $downloadURL ); 435 $url->setModified(); 436 if ( $showDebug ) 437 print( "correct download url: '$downloadURL/'\n" ); 438 print( '*' ); 439 } 440 else 441 { 442 $url->setAttribute( 'is_valid', false ); 443 $url->setModified(); 444 print( '-' ); 445 $failedURLArray[] = $url; 446 } 447 if ( $fixErrors ) 448 { 449 $url->sync(); 450 ++$fixedURLCount; 451 } 452 } 453 else 454 print( '.' ); 455 ++$dotCount; 456 ++$dotTotalCount; 457 if ( $dotCount >= $dotMax or $dotTotalCount >= $urlCount ) 458 { 459 $percent = number_format( ( $dotTotalCount * 100.0 ) / ( $urlCount ), 2 ); 460 $dotSpace = ''; 461 if ( $dotTotalCount > $dotMax ) 462 $dotSpace = str_repeat( ' ', $dotMax - $dotCount ); 463 print( $dotSpace . " " . $percent . "%\n" ); 464 $dotCount = 0; 465 } 466 } 467 print( "\n" ); 468 print( "+ Correct download url\n" ); 469 print( "* Fixed download url\n" ); 470 print( "- Failed to fix\n" ); 471 print( ". Ignored\n" ); 472 if ( count( $failedURLArray ) > 0 ) 473 { 474 print( "The following urls could not be fixed\n" ); 475 foreach ( $failedURLArray as $failedURL ) 476 { 477 print( sprintf( "%5.d: %s\n", 478 $failedURL->attribute( 'id' ), 479 $failedURL->attribute( 'url' ) ) ); 480 } 481 } 482 print( "\n" ); 483 } 484 485 print( "Number of attributes : $attributeCount\n" ); 486 print( "Number of fixed attributes: $wrongLinkCount\n\n" ); 487 488 print( "Number of urls : $urlCount\n" ); 489 print( "Number of bad urls : $wrongURLCount\n" ); 490 print( "Number of fixed urls: $fixedURLCount\n" ); 491 492 $script->shutdown(); 493 494 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Sat Feb 24 10:30:04 2007 | par Balluche grâce à PHPXref 0.7 |