[ Index ]
 

Code source de eZ Publish 3.9.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/update/common/scripts/3.9/ -> updatetypedrelation.php (source)

   1  #!/usr/bin/env php
   2  <?php
   3  //
   4  // Created on: <06-Oct-2006 17:06:01 vp>
   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  define( "QUERY_LIMIT", 100 );
  29  
  30  if( !file_exists( 'update/common/scripts/3.9' ) || !is_dir( 'update/common/scripts/3.9' ) )
  31  {
  32      echo "Please run this script from the root document directory!\n";
  33      exit;
  34  }
  35  
  36  include_once ( 'lib/ezutils/classes/ezcli.php' );
  37  include_once ( 'kernel/classes/ezscript.php' );
  38  include_once ( 'kernel/classes/ezcontentobject.php' );
  39  
  40  $cli =& eZCLI::instance();
  41  
  42  $script =& eZScript::instance( array( 'description' => ( "\nThis script performs the task needed to upgrade to 3.9:\n" .
  43                                                           "\nAdds 'embed' & 'link' contentobject relations.\n"  ),
  44                                        'use-session' => false,
  45                                        'use-modules' => false,
  46                                        'use-extensions' => true ) );
  47  
  48  $script->startup();
  49  
  50  $options = $script->getOptions( "[db-host:][db-user:][db-password:][db-database:][db-type:]",
  51                                  "",
  52                                  array( 'db-host' => "Database host",
  53                                         'db-user' => "Database user",
  54                                         'db-password' => "Database password",
  55                                         'db-database' => "Database name",
  56                                         'db-type' => "Database type, e.g. mysql or postgresql"
  57                                         ) );
  58  $script->initialize();
  59  
  60  $dbUser = $options['db-user'];
  61  $dbPassword = $options['db-password'];
  62  $dbHost = $options['db-host'];
  63  $dbName = $options['db-database'];
  64  $dbImpl = $options['db-type'];
  65  $isQuiet = $script->isQuiet();
  66  
  67  if ( $dbHost or $dbName or $dbUser or $dbImpl )
  68  {
  69      $params = array( 'use_defaults' => false );
  70      if ( $dbHost !== false )
  71          $params['server'] = $dbHost;
  72      if ( $dbUser !== false )
  73      {
  74          $params['user'] = $dbUser;
  75          $params['password'] = '';
  76      }
  77      if ( $dbPassword !== false )
  78          $params['password'] = $dbPassword;
  79      if ( $dbName !== false )
  80          $params['database'] = $dbName;
  81  
  82      $db =& eZDB::instance( $dbImpl, $params, true );
  83      eZDB::setInstance( $db );
  84  }
  85  else
  86  {
  87      $db =& eZDB::instance();
  88  }
  89  
  90  if ( !$db->isConnected() )
  91  {
  92      $cli->notice( "Can't initialize database connection.\n" );
  93      $script->shutdown( 1 );
  94  }
  95  
  96  include_once ( 'lib/ezxml/classes/ezxml.php' );
  97  include_once ( 'kernel/classes/datatypes/ezxmltext/ezxmltexttype.php' );
  98  
  99  $xml = new eZXML();
 100  
 101  
 102  function AddObjectRelation( $fromObjectID, $fromObjectVersion, $toObjectID, $relationType )
 103  {
 104      $db =& eZDB::instance();
 105      $relationBaseType = EZ_CONTENT_OBJECT_RELATION_COMMON |
 106                          EZ_CONTENT_OBJECT_RELATION_EMBED |
 107                          EZ_CONTENT_OBJECT_RELATION_LINK;
 108      $query = "SELECT count(*) AS count
 109                FROM   ezcontentobject_link
 110                WHERE  from_contentobject_id=$fromObjectID AND
 111                       from_contentobject_version=$fromObjectVersion AND
 112                       to_contentobject_id=$toObjectID AND
 113                       ( relation_type & $relationBaseType ) != 0  AND
 114                       contentclassattribute_id=0 AND
 115                       op_code='0'";
 116      $count = $db->arrayQuery( $query );
 117      // if current relation does not exists
 118      if ( !isset( $count[0]['count'] ) ||  $count[0]['count'] == '0'  )
 119      {
 120          $db->query( "INSERT INTO ezcontentobject_link ( from_contentobject_id, from_contentobject_version, to_contentobject_id, relation_type )
 121                       VALUES ( $fromObjectID, $fromObjectVersion, $toObjectID, $relationType )" );
 122      }
 123      else
 124      {
 125          $db->query( "UPDATE ezcontentobject_link
 126                       SET    relation_type = ( relation_type | $relationType )
 127                       WHERE  from_contentobject_id=$fromObjectID AND
 128                              from_contentobject_version=$fromObjectVersion AND
 129                              to_contentobject_id=$toObjectID AND
 130                              contentclassattribute_id=0 AND
 131                              op_code='0'" );
 132      }
 133  }
 134  
 135  function AddNewRelations( $objectID, $version, $relatedObjectIDArray, &$cli )
 136  {
 137      $relationCount = 0;
 138      foreach ( $relatedObjectIDArray as $relationType => $relatedObjectIDSubArray )
 139      {
 140          foreach ( $relatedObjectIDSubArray as $relatedObjectID )
 141          {
 142              AddObjectRelation( $objectID, $version, $relatedObjectID, $relationType );
 143              $cli->notice( implode( '', array( 'Added ', ( ( EZ_CONTENT_OBJECT_RELATION_EMBED === $relationType ) ? 'embed' : 'link' ) , ' relation. ',
 144                                     'Object ID ', $objectID,
 145                                     '( ver. ', $version,
 146                                     ' ) => ID ', $relatedObjectID ) ) );
 147              $relationCount++;
 148          }
 149      }
 150      return $relationCount;
 151  }
 152  
 153  
 154  function getRelatedObjectsID( &$domDocument, $tagName, &$objectIDArray )
 155  {
 156      $xmlNodeList = $domDocument->get_elements_by_tagname( $tagName );
 157      if ( !is_array( $xmlNodeList ) )
 158      {
 159          return;
 160      }
 161  
 162      foreach ( $xmlNodeList as $xmlNode )
 163      {
 164          $objectID = ( int ) $xmlNode->getAttribute( 'object_id' );
 165  
 166          if ( $objectID && !in_array( $objectID, $objectIDArray ) )
 167          {
 168             $objectIDArray[] = $objectID;
 169          }
 170      }
 171  }
 172  
 173  
 174  $totalRelationCount = 0;
 175  $totalObjectCount = 0;
 176  
 177  $xmlFieldsQuery = "SELECT id, version, contentobject_id, data_text
 178                     FROM ezcontentobject_attribute
 179                     WHERE data_type_string = 'ezxmltext'
 180                     ORDER BY contentobject_id ASC";
 181  
 182  // We process the table by parts of QUERY_LIMIT number of records, $pass is the iteration number.
 183  $pass = 1;
 184  $relationActionLog = array();
 185  $relatedObjectIDArray = array();
 186  $objectID = null;
 187  $version = null;
 188  $objectWasModified =  false;
 189  
 190  for ( $offset = 0; ; $offset += QUERY_LIMIT )
 191  {
 192      $xmlFieldsArray = $db->arrayQuery( $xmlFieldsQuery, array( "limit" => QUERY_LIMIT, "offset" => $offset ) );
 193  
 194      if ( count( $xmlFieldsArray ) <= 0 )
 195      {
 196          break;
 197      }
 198  
 199      foreach ( $xmlFieldsArray as $xmlField )
 200      {
 201          if ( $objectID == null || $version != $xmlField['version'] || $objectID != $xmlField['contentobject_id'] )
 202          {
 203              $relationAddedCount = AddNewRelations( $objectID, $version, $relatedObjectIDArray, $cli );
 204              if ( $relationAddedCount )
 205              {
 206                  $totalRelationCount += $relationAddedCount;
 207                  $objectWasModified =  true;
 208              }
 209  
 210              $version = ( int ) $xmlField['version'];
 211              $relatedObjectIDArray[EZ_CONTENT_OBJECT_RELATION_EMBED] = array();
 212              $relatedObjectIDArray[EZ_CONTENT_OBJECT_RELATION_LINK] = array();
 213  
 214              if ( $objectID == null || $objectID != $xmlField['contentobject_id'] )
 215              {
 216                  $objectID = ( int ) $xmlField['contentobject_id'];
 217  
 218                  if ( $objectWasModified )
 219                  {
 220                      $objectWasModified =  false;
 221                      $totalObjectCount++;
 222                  }
 223              }
 224          }
 225  
 226          $text = $xmlField['data_text'];
 227          $doc =& $xml->domTree( $text, array( "TrimWhiteSpace" => false ) );
 228  
 229          if ( $doc )
 230          {
 231              getRelatedObjectsID( $doc, 'embed', $relatedObjectIDArray[EZ_CONTENT_OBJECT_RELATION_EMBED] );
 232              getRelatedObjectsID( $doc, 'link', $relatedObjectIDArray[EZ_CONTENT_OBJECT_RELATION_LINK] );
 233          }
 234      }
 235  }
 236  
 237  $relationAddedCount = AddNewRelations( $objectID, $version, $relatedObjectIDArray, $cli );
 238  if ( $relationAddedCount )
 239  {
 240      $totalRelationCount += $relationAddedCount;
 241      $objectWasModified =  true;
 242  }
 243  
 244  if ( !$isQuiet )
 245  {
 246      if ( $totalRelationCount )
 247          $cli->notice( implode( '', array( 'Total: ', $totalRelationCount, ' relation(s) detected at ', $totalObjectCount, ' objects.' ) ) );
 248      else
 249          $cli->notice( 'Nothing to do.' );
 250  }
 251  
 252  $script->shutdown();
 253  
 254  ?>


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7