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

   1  #!/usr/bin/env php
   2  <?php
   3  //
   4  // Created on: <01-Feb-2005 12:05:27 ks>
   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 convertxmllinks.php
  29    Database converter for eZ publish 3.6.
  30    Updates <link> tags in 'ezxmltext' type attributes: replaces 'id' attribute
  31    with 'url_id'.
  32    You should run this script before using database created with eZ publish
  33    version 3.5.* or lower.
  34  */
  35  
  36  define( "QUERY_LIMIT", 100 );
  37  
  38  if( !file_exists( 'update/common/scripts' ) || !is_dir( 'update/common/scripts' ) )
  39  {
  40      echo "Please run this script from the root document directory!\n";
  41      exit;
  42  }
  43  
  44  include_once ( 'lib/ezutils/classes/ezcli.php' );
  45  include_once ( 'kernel/classes/ezscript.php' );
  46  
  47  $cli =& eZCLI::instance();
  48  
  49  $script =& eZScript::instance( array( 'description' => ( "\nDatabase converter for eZ publish 3.6.\n" .
  50                                                           "Updates <link> tags in 'ezxmltext' type attributes.\n" .
  51                                                           "Run this script before using database created with eZ publish version 3.5.* or lower." ),
  52                                        'use-session' => false,
  53                                        'use-modules' => false,
  54                                        'use-extensions' => false ) );
  55  
  56  $script->startup();
  57  
  58  $options = $script->getOptions( "[db-host:][db-user:][db-password:][db-database:][db-type:]",
  59                                  "",
  60                                  array( 'db-host' => "Database host",
  61                                         'db-user' => "Database user",
  62                                         'db-password' => "Database password",
  63                                         'db-database' => "Database name",
  64                                         'db-type' => "Database type, e.g. mysql or postgresql"
  65                                         ) );
  66  $script->initialize();
  67  
  68  $dbUser = $options['db-user'];
  69  $dbPassword = $options['db-password'];
  70  $dbHost = $options['db-host'];
  71  $dbName = $options['db-database'];
  72  $dbImpl = $options['db-type'];
  73  $isQuiet = $script->isQuiet();
  74  
  75  if ( $dbHost or $dbName or $dbUser or $dbImpl )
  76  {
  77      $params = array( 'use_defaults' => false );
  78      if ( $dbHost !== false )
  79          $params['server'] = $dbHost;
  80      if ( $dbUser !== false )
  81      {
  82          $params['user'] = $dbUser;
  83          $params['password'] = '';
  84      }
  85      if ( $dbPassword !== false )
  86          $params['password'] = $dbPassword;
  87      if ( $dbName !== false )
  88          $params['database'] = $dbName;
  89  
  90      $db =& eZDB::instance( $dbImpl, $params, true );
  91      eZDB::setInstance( $db );
  92  }
  93  else
  94  {
  95      $db =& eZDB::instance();
  96  }
  97  
  98  if ( !$db->isConnected() )
  99  {
 100      $cli->notice( "Can't initialize database connection.\n" );
 101      $script->shutdown( 1 );
 102  }
 103  
 104  $totalCount = 0;
 105  
 106  /*!
 107    Finds all link tags in text \a $text and replaces the attribute \c id with \c url_id.
 108    \param $pos The current position to start looking for tags in \a $text.
 109    \param $isTextModified The global flag which tells if a link was modified or not
 110    \return \c true if it has found a link tag, \c false otherwise.
 111  */
 112  function findLinkTags( &$text, &$pos, &$isTextModified )
 113  {
 114      $linkPos = strpos( $text, "<link", $pos );
 115      if ( $linkPos )
 116      {
 117          $linkTagBegin = $linkPos + 5;
 118          $linkTagEnd = strpos( $text, ">", $linkTagBegin );
 119          if ( !$linkTagEnd )
 120          {
 121              return false;
 122          }
 123  
 124          $linkTag = substr( $text, $linkTagBegin, $linkTagEnd - $linkTagBegin );
 125  
 126          if ( strpos( $linkTag, " id=\"" ) !== false )
 127          {
 128              $linkTag = str_replace( " id=\"", " url_id=\"", $linkTag );
 129              $text = substr_replace( $text, $linkTag, $linkTagBegin, $linkTagEnd - $linkTagBegin );
 130  
 131              $isTextModified = true;
 132          }
 133  
 134          $pos = $linkTagEnd;
 135          return true;
 136      }
 137      return false;
 138  }
 139  
 140  $xmlFieldsQuery = "SELECT id, version, contentobject_id, data_text
 141                     FROM ezcontentobject_attribute
 142                     WHERE data_type_string = 'ezxmltext'";
 143  
 144  $xmlFieldsArray = $db->arrayQuery( $xmlFieldsQuery, array( "limit" => QUERY_LIMIT ) );
 145  
 146  // We process the table by parts of QUERY_LIMIT number of records, $pass is the iteration number.
 147  $pass = 1;
 148  
 149  while( count( $xmlFieldsArray ) )
 150  {
 151      foreach ( $xmlFieldsArray as $xmlField )
 152      {
 153          $text = $xmlField['data_text'];
 154          $textLen = strlen ( $text );
 155      
 156          $isTextModified = false;
 157          $pos = 1;
 158      
 159          if ( $textLen == 0 )
 160          {
 161              continue;
 162          }
 163      
 164          $oldPos = false;
 165          do
 166          {
 167              // Avoid infinite loop
 168              if ( $oldPos === $pos or $oldPos > $pos )
 169              {
 170                  break;
 171              }
 172              $oldPos = $pos;
 173      
 174              $literalTagBegin = strpos( $text, "<literal", $pos );
 175              if ( $literalTagBegin )
 176              {
 177      
 178                  $preLiteralText = substr( $text, $pos, $literalTagBegin - $pos );
 179                  $preLiteralLen = strlen( $preLiteralText );
 180                  $tmpPos = 0;
 181                  // We need to check the text before the literal tag for link tags
 182                  if ( findLinkTags( $preLiteralText, $tmpPos, $isTextModified ) )
 183                  {
 184                      // We found some link tags, now replace the text and adjust position
 185                      $diff = strlen( $preLiteralText ) - $preLiteralLen;
 186                      $text = substr_replace( $text, $preLiteralText, $pos, $literalTagBegin - $pos );
 187      
 188                      // Adjust begin position with the changes in text
 189                      $literalTagBegin += $diff;
 190                  }
 191      
 192                  $literalTagEnd = strpos( $text, "</literal>", $literalTagBegin );
 193                  if ( !$literalTagEnd )
 194                  {
 195                      break;
 196                  }
 197                  $pos = $literalTagEnd + 9;
 198              }
 199              else
 200              {
 201                  if ( !findLinkTags( $text, $pos, $isTextModified ) )
 202                      break;
 203              }
 204      
 205          } while ( $pos < $textLen );
 206      
 207          if ( $isTextModified )
 208          {
 209              $sql = "UPDATE ezcontentobject_attribute SET data_text='" . $text .
 210                 "' WHERE id=" . $xmlField['id'] . " AND version=" . $xmlField['version'];
 211              $db->query( $sql );
 212      
 213              if ( !$isQuiet )
 214                  $cli->notice( "Attribute converted. Object ID: " . $xmlField['contentobject_id'] . ", version: ". $xmlField['version'] .
 215                                ", attribute ID :" . $xmlField['id'] );
 216              $totalCount++;
 217          }
 218      }    
 219      $xmlFieldsArray = $db->arrayQuery( $xmlFieldsQuery, array( "limit" => QUERY_LIMIT, "offset" => $pass * QUERY_LIMIT ) );
 220      $pass++;
 221  }
 222  
 223  if ( !$isQuiet )
 224  {
 225      if ( $totalCount )
 226          $cli->notice( "Total: " . $totalCount . " attribute(s) converted." );
 227      else
 228          $cli->notice( "No old-style <link> tags found." );
 229  }
 230  
 231  $script->shutdown();
 232  
 233  ?>


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