[ 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/ -> updatemultilingual.php (source)

   1  #!/usr/bin/env php
   2  <?php
   3  //
   4  // Created on: <22-Mar-2006 09:23:12 jk>
   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  /*
  29  
  30  -- The following SQLs must be run before running this script:
  31  
  32  CREATE TABLE ezcontent_language
  33  (
  34      id int(11) NOT NULL default '0',
  35      disabled int(11) NOT NULL default '0',
  36      locale varchar(20) NOT NULL default '',
  37      name varchar(255) NOT NULL default '',
  38      PRIMARY KEY (id)
  39  );
  40  
  41  DROP TABLE ezcontent_translation;
  42  
  43  ALTER TABLE ezcontentobject ADD COLUMN language_mask int NOT NULL DEFAULT 0;
  44  ALTER TABLE ezcontentobject ADD COLUMN initial_language_id int NOT NULL DEFAULT 0;
  45  
  46  ALTER TABLE ezcontentobject_name ADD COLUMN language_id int NOT NULL DEFAULT 0;
  47  
  48  ALTER TABLE ezcontentobject_attribute ADD COLUMN language_id int NOT NULL DEFAULT 0;
  49  
  50  ALTER TABLE ezcontentobject_version ADD COLUMN language_mask int NOT NULL DEFAULT 0;
  51  ALTER TABLE ezcontentobject_version ADD COLUMN initial_language_id int NOT NULL DEFAULT 0;
  52  
  53  ALTER TABLE ezcontentclass ADD COLUMN always_available int NOT NULL DEFAULT 0;
  54  
  55  ALTER TABLE ezcontentobject_link ADD COLUMN op_code int NOT NULL DEFAULT 0;
  56  
  57  ALTER TABLE eznode_assignment ADD COLUMN op_code int NOT NULL DEFAULT 0;
  58  
  59  */
  60  
  61  // TODO: check if the sql update script was already run...
  62  
  63  include_once ( 'lib/ezutils/classes/ezcli.php' );
  64  include_once ( 'kernel/classes/ezscript.php' );
  65  include_once ( 'kernel/classes/ezcontentlanguage.php' );
  66  include_once ( 'kernel/classes/ezcontentobjectversion.php' );
  67  include_once ( 'lib/ezutils/classes/ezextension.php' );
  68  
  69  function minBit( $value )
  70  {
  71      $value = (int) $value;
  72      $minBit = 1;
  73  
  74      while ( $value > 0 )
  75      {
  76          if ( $value & 1 )
  77          {
  78              return $minBit;
  79          }
  80          $minBit *= 2;
  81          $value = (int) ( $value / 2 );
  82      }
  83  
  84      return 0;
  85  }
  86  
  87  set_time_limit( 0 );
  88  
  89  $cli =& eZCLI::instance();
  90  
  91  $script =& eZScript::instance( array( 'description' => "Update database for the multilingual suport.",
  92                                        'use-session' => true,
  93                                        'use-modules' => true,
  94                                        'use-extensions' => true ) );
  95  
  96  $script->startup();
  97  
  98  $options = $script->getOptions( "",
  99                                  "" );
 100  
 101  $extensionBaseDir = eZExtension::baseDirectory();
 102  $extensionNameArray = eZExtension::activeExtensions();
 103  $siteAccessPath = '/settings/siteaccess/';
 104  $siteAccessExists = false;
 105  
 106  if ( !$options['siteaccess'] )
 107  {
 108      $cli->error( "Siteaccess was not given. Exiting..." );
 109      exit( -1 );
 110  }
 111  
 112  $siteAccessExists = file_exists( 'settings/siteaccess/' . $options['siteaccess'] );
 113  if ( !$siteAccessExists )
 114  {
 115      // check extensions.
 116      foreach ( $extensionNameArray as $extensionName )
 117      {
 118          $extensionSiteaccessPath = $extensionBaseDir . '/' . $extensionName . $siteAccessPath;
 119          if ( file_exists( $extensionSiteaccessPath . $options['siteaccess'] ) )
 120          {
 121              $siteAccessExists = true;
 122              break;
 123          }
 124      }
 125  }
 126  
 127  if ( !$siteAccessExists )
 128  {
 129      $cli->error( "Siteaccess '" . $options['siteaccess'] . "' does not exist. Exiting..." );
 130      exit( -1 );
 131  }
 132  
 133  $script->setUseSiteAccess( $options['siteaccess'] );
 134  
 135  $script->initialize();
 136  
 137  $cli->warning( "Have you backed up your database? If not, press Ctrl-C and back up your data!" );
 138  
 139  $db =& eZDB::instance();
 140  $ini =& eZINI::instance();
 141  
 142  $defaultLanguageCode = $ini->variable( 'RegionalSettings', 'ContentObjectLocale' );
 143  
 144  $cli->notice( "The default language for your siteaccess is '$defaultLanguageCode' and will be used as the initial language for all your objects." );
 145  $cli->notice( "The script will use the following database settings:" );
 146  $cli->notice( "  user: <" . $db->User . ">, " . ( strlen( $db->Password ) > 0 ? "password: <***>, " : "password: none, " ) . "server: <" . $db->Server . ">, socket: <" . $db->SocketPath . ">, name: <" . $db->DB . ">, charset: <" . $db->Charset . ">" );
 147  
 148  $draftCount = $db->arrayQuery( "SELECT count(*) AS count
 149                                  FROM ezcontentobject_version
 150                                  WHERE status=0" );
 151  $draftCount = $draftCount[0]['count'];
 152  
 153  if ( $draftCount )
 154  {
 155      $cli->warning( "You have $draftCount draft(s). These drafts will be removed." );
 156  }
 157  
 158  $cli->warning( "You have now 10 seconds to break this script (press Ctrl-C) if the settings are incorrect." );
 159  sleep( 10 );
 160  
 161  // ------------------------------------------
 162  
 163  $cli->notice( 'Step 1/6: Removing the drafts:' );
 164  
 165  $count = 0;
 166  if ( $draftCount )
 167  {
 168      $rows = $db->arrayQuery( "SELECT *
 169                                FROM ezcontentobject_version
 170                                WHERE status=0" );
 171      foreach( $rows as $row )
 172      {
 173          ++$count;
 174          if ( ( $count % 100 ) == 0 )
 175              $cli->warning( "Processed: $count of $draftCount " );
 176  
 177          $draft = new eZContentObjectVersion( $row );
 178          $draft->remove();
 179          eZContentObject::clearCache();
 180          unset( $draft );
 181      }
 182  }
 183  
 184  if ( $count > 0 )
 185  {
 186      // last message
 187      $cli->warning( "Processed: $count of $draftCount " );
 188  }
 189  // ------------------------------------------
 190  
 191  $cli->notice( 'Step 2/6: Identifying languages used on the site:' );
 192  
 193  $language = eZContentLanguage::addLanguage( $defaultLanguageCode );
 194  $defaultLanguage = $language->attribute( 'id' );
 195  $cli->notice( "  language: $defaultLanguageCode (id $defaultLanguage)" );
 196  $languages = array( $defaultLanguageCode => $defaultLanguage );
 197  
 198  $rows = $db->arrayQuery( "SELECT DISTINCT content_translation FROM ezcontentobject_name" );
 199  foreach ( $rows as $row )
 200  {
 201      $languageCode = $row['content_translation'];
 202      if ( $languageCode != $defaultLanguageCode )
 203      {
 204          $language = eZContentLanguage::addLanguage( $languageCode );
 205          if ( !$language )
 206          {
 207              $cli->error( "Cannot add language $languageCode! Too many languages used on the site?" );
 208              exit( -1 );
 209          }
 210          $languageID = $language->attribute( 'id' );
 211          $cli->notice( "  language: $languageCode (id $languageID)" );
 212          $languages[$languageCode] = $languageID;
 213      }
 214  }
 215  unset( $rows );
 216  
 217  // ------------------------------------------
 218  
 219  $cli->notice( 'Step 3/6: Fixing the ezcontentclass table.' );
 220  
 221  $db->query( "UPDATE ezcontentclass
 222               SET always_available='1'
 223               WHERE remote_id IN ( 'a3d405b81be900468eb153d774f4f0d2',
 224                                    '25b4268cdcd01921b808a0d854b877ef',
 225                                    '40faa822edc579b02c25f6bb7beec3ad',
 226                                    'f6df12aa74e36230eb675f364fccd25a',
 227                                    '637d58bfddf164627bdfd265733280a0',
 228                                    'ffedf2e73b1ea0c3e630e42e2db9c900',
 229                                    '59b43cd9feaaf0e45ac974fb4bbd3f92' ) " );
 230  
 231  $alwaysAvailableClasses = array();
 232  $rows = $db->arrayQuery( "SELECT id FROM ezcontentclass WHERE always_available='1'" );
 233  foreach( $rows as $row )
 234  {
 235      $alwaysAvailableClasses[] = $row['id'];
 236  }
 237  unset( $rows );
 238  
 239  // ------------------------------------------
 240  
 241  $cli->notice( 'Step 4/6: Fixing the ezcontentobject_name table.' );
 242  
 243  $db->query( "DELETE FROM ezcontentobject_name WHERE content_translation<>real_translation" );
 244  foreach( $languages as $languageCode => $languageID )
 245  {
 246      $db->query( "UPDATE ezcontentobject_name SET language_id='$languageID' WHERE content_translation='$languageCode'" );
 247  }
 248  // Fixing inconsistencies
 249  $db->query( "UPDATE ezcontentobject_name SET language_id='$defaultLanguage', content_translation='$defaultLanguageCode'" );
 250  
 251  
 252  // ------------------------------------------
 253  
 254  $cli->notice( 'Step 5/6: Fixing content object versions and attributes. Please be patient, this might take a while...' );
 255  
 256  $db->query( "UPDATE ezcontentobject_attribute SET language_id='0'" );
 257  foreach( $languages as $languageCode => $languageID )
 258  {
 259      $db->query( "UPDATE ezcontentobject_attribute SET language_id='$languageID' WHERE language_code='$languageCode'" );
 260  }
 261  // Fixing inconsistencies:
 262  $db->query( "UPDATE ezcontentobject_attribute SET language_id='$defaultLanguage', language_code='$defaultLanguageCode' WHERE language_id='0'" );
 263  
 264  $db->query( "CREATE TEMPORARY TABLE version_languages ( contentobject_id int, version int, language_id int )" );
 265  $db->query( "CREATE TEMPORARY TABLE version_language_masks ( contentobject_id int, version int, language_mask int )" );
 266  $db->query( "CREATE TEMPORARY TABLE object_language_masks ( contentobject_id int, language_mask int, PRIMARY KEY( contentobject_id ) )" );
 267  
 268  $db->query( "INSERT INTO version_languages( contentobject_id, version, language_id )
 269               SELECT DISTINCT contentobject_id, version, language_id FROM ezcontentobject_attribute" );
 270  $db->query( "INSERT INTO version_language_masks( contentobject_id, version, language_mask )
 271               SELECT contentobject_id, version, sum( language_id ) as language_mask
 272               FROM version_languages
 273               GROUP BY contentobject_id, version" );
 274  $db->query( "INSERT INTO object_language_masks( contentobject_id, language_mask )
 275               SELECT contentobject_id, version_language_masks.language_mask
 276               FROM version_language_masks, ezcontentobject
 277               WHERE version_language_masks.contentobject_id = ezcontentobject.id
 278                 AND version_language_masks.version = ezcontentobject.current_version" );
 279  
 280  $count = $db->arrayQuery( "SELECT count(*) as count FROM version_language_masks" );
 281  $count = $count[0]['count'];
 282  
 283  $limit = 100;
 284  $offset = 0;
 285  
 286  while ( $rows = $db->arrayQuery( "SELECT a.*, b.language_mask as object_language_mask
 287                                    FROM version_language_masks a, object_language_masks b
 288                                    WHERE a.contentobject_id = b.contentobject_id", array( 'limit' => $limit, 'offset' => $offset ) ) )
 289  {
 290      foreach( $rows as $row )
 291      {
 292          $objectID = $row['contentobject_id'];
 293          $version = $row['version'];
 294          $languageMask = (int) $row['language_mask'] & (int) $row['object_language_mask'];
 295  
 296          // removing attributes which exists in languages which do not exist in published version
 297          $originalLanguageMask = $languageMask;
 298          $maskArray = array();
 299          $candidate = 1;
 300          while ( $originalLanguageMask > 0 )
 301          {
 302              if ( $originalLanguageMask & 1 > 0 )
 303              {
 304                  $maskArray[] = $candidate;
 305                  $maskArray[] = $candidate+1;
 306              }
 307              $originalLanguageMask = (int) ( $originalLanguageMask / 2 );
 308              $candidate *= 2;
 309          }
 310  
 311          $attributes = $db->arrayQuery( "SELECT *
 312                                          FROM ezcontentobject_attribute
 313                                          WHERE contentobject_id = '$objectID'
 314                                            AND version = '$version'".
 315                                         ( ( $maskArray )? " AND language_id NOT IN ( " . implode( ', ', $maskArray ) ." )": '' ) );
 316  
 317  
 318          foreach ( $attributes as $attribute )
 319          {
 320              $attributeObject = new eZContentObjectAttribute( $attribute );
 321              $attributeObject->remove( $attributeObject->attribute( 'id' ), $attributeObject->attribute( 'version' ) );
 322              unset( $attributeObject );
 323          }
 324          if ( $attributes )
 325          {
 326              // removing the rows in the name table which exists in languages which do not exist in published version
 327              $db->query( "DELETE FROM ezcontentobject_name
 328                           WHERE contentobject_id = '$objectID' AND version = '$version'".
 329                           ( ( $maskArray )? " AND language_id NOT IN ( " . implode( ', ', $maskArray ) ." )": '' ) );
 330          }
 331          unset( $attributes );
 332  
 333          if ( $languageMask == 0 )
 334          {
 335              // This version does not contain any language, we will remove it
 336              $db->query( "DELETE FROM ezcontentobject_version
 337                           WHERE contentobject_id='$objectID'
 338                             AND version='$version'" );
 339          }
 340          else
 341          {
 342              if ( $languageMask & $defaultLanguage )
 343              {
 344                  $initialLanguage = $defaultLanguage;
 345              }
 346              else
 347              {
 348                  $initialLanguage = minBit( $languageMask );
 349              }
 350  
 351              $languageMask++;
 352  
 353              $db->query( "UPDATE ezcontentobject_version
 354                           SET initial_language_id='$initialLanguage',
 355                               language_mask='$languageMask'
 356                           WHERE contentobject_id='$objectID'
 357                           AND version='$version'" );
 358          }
 359      }
 360  
 361      unset( $rows );
 362      $offset += $limit;
 363  
 364      $percentage = floor( 100 * $offset / $count );
 365      if ( $percentage > 100 )
 366      {
 367          $percentage = 100;
 368      }
 369  
 370      $cli->notice( "\r  $percentage%", false );
 371  }
 372  
 373  $cli->notice( "\r  done" );
 374  
 375  $db->query( "DROP TEMPORARY TABLE object_language_masks" );
 376  $db->query( "DROP TEMPORARY TABLE version_language_masks" );
 377  $db->query( "DROP TEMPORARY TABLE version_languages" );
 378  
 379  // Fixing inconsistencies
 380  $defaultMask = $defaultLanguage + 1;
 381  $db->query( "UPDATE ezcontentobject_version SET initial_language_id='$defaultLanguage', language_mask='$defaultMask' WHERE initial_language_id='0'" );
 382  
 383  // ------------------------------------------
 384  
 385  $cli->notice( 'Step 6/6: Fixing content objects. Please be patient, this might take a while...' );
 386  
 387  $topLevelObjects = array();
 388  $rows = $db->arrayQuery( "SELECT contentobject_id FROM ezcontentobject_tree WHERE parent_node_id=1 AND node_id<>1" );
 389  foreach( $rows as $row )
 390  {
 391      $topLevelObjects[] = $row['contentobject_id'];
 392  }
 393  unset( $rows );
 394  
 395  $count = $db->arrayQuery( "SELECT count(*) as count FROM ezcontentobject" );
 396  $count = $count[0]['count'];
 397  
 398  $limit = 100;
 399  $offset = 0;
 400  $lastID = 0;
 401  
 402  while ( $objects = $db->arrayQuery( "SELECT ezcontentobject.id, ezcontentobject.current_version, ezcontentobject.contentclass_id,
 403                                              ezcontentobject_version.language_mask, ezcontentobject_version.initial_language_id
 404                                       FROM ezcontentobject, ezcontentobject_version
 405                                       WHERE ezcontentobject.id > '$lastID'
 406                                         AND ezcontentobject_version.contentobject_id=ezcontentobject.id
 407                                         AND ezcontentobject_version.version=ezcontentobject.current_version
 408                                       ORDER BY ezcontentobject.id", array( 'limit' => $limit ) ) )
 409  {
 410      foreach ( $objects as $object )
 411      {
 412          $objectID = $object['id'];
 413          $version = $object['current_version'];
 414          $languageMask = $object['language_mask'];
 415          $languageMask--;
 416          $originalLanguageMask = (int) $languageMask;
 417          $initialLanguage = $object['initial_language_id'];
 418  
 419          // if the object is a folder, a user, a user group etc. or if it is a top-leve object, make it always available
 420          if ( in_array( $object['contentclass_id'], $alwaysAvailableClasses ) ||
 421               in_array( $objectID, $topLevelObjects ) )
 422          {
 423              $languageMask++;
 424              $newLanguageID = $initialLanguage + 1;
 425              $db->query( "UPDATE ezcontentobject_attribute
 426                           SET language_id='$newLanguageID'
 427                           WHERE contentobject_id='$objectID' AND version='$version' AND language_id='$initialLanguage'" );
 428              $db->query( "UPDATE ezcontentobject_name
 429                           SET language_id='$newLanguageID'
 430                           WHERE contentobject_id='$objectID' AND content_version='$version' AND language_id='$initialLanguage'" );
 431          }
 432  
 433          $db->query( "UPDATE ezcontentobject
 434                       SET language_mask='$languageMask',
 435                           initial_language_id='$initialLanguage'
 436                       WHERE id='$objectID'" );
 437  
 438          $lastID = $object['id'];
 439      }
 440  
 441      unset( $objects );
 442      $offset += $limit;
 443  
 444      $percentage = floor( 100 * $offset / $count );
 445      if ( $percentage > 100 )
 446      {
 447          $percentage = 100;
 448      }
 449  
 450      $cli->notice( "\r  $percentage%", false );
 451  }
 452  
 453  $cli->notice( "\r  done" );
 454  
 455  // Fixing inconsistencies
 456  $db->query( "UPDATE ezcontentobject SET initial_language_id='$defaultLanguage', language_mask='$defaultMask' WHERE initial_language_id='0'" );
 457  
 458  $cli->notice( 'Done.' );
 459  
 460  $script->shutdown();
 461  
 462  ?>


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