|
eZ Publish
[4.0]
|
00001 #!/usr/bin/env php 00002 <?php 00003 // 00004 // Created on: <17-Apr-2007 15:47:58 bjorn> 00005 // 00006 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ## 00007 // SOFTWARE NAME: eZ Publish 00008 // SOFTWARE RELEASE: 4.0.x 00009 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS 00010 // SOFTWARE LICENSE: GNU General Public License v2.0 00011 // NOTICE: > 00012 // This program is free software; you can redistribute it and/or 00013 // modify it under the terms of version 2.0 of the GNU General 00014 // Public License as published by the Free Software Foundation. 00015 // 00016 // This program is distributed in the hope that it will be useful, 00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 // GNU General Public License for more details. 00020 // 00021 // You should have received a copy of version 2.0 of the GNU General 00022 // Public License along with this program; if not, write to the Free 00023 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 00024 // MA 02110-1301, USA. 00025 // 00026 // 00027 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ## 00028 // 00029 00030 00031 /*! \file updateisbn13.php 00032 \brief Updates the different ranges used by the ISBN standard to 00033 calculate the length of Registration group, Registrant and Publication element. 00034 */ 00035 00036 //include_once( 'lib/ezutils/classes/ezcli.php' ); 00037 //include_once( 'kernel/classes/ezscript.php' ); 00038 00039 //include_once( 'kernel/classes/datatypes/ezisbn/ezisbngroup.php' ); 00040 //include_once( 'kernel/classes/datatypes/ezisbn/ezisbngrouprange.php' ); 00041 //include_once( 'kernel/classes/datatypes/ezisbn/ezisbnregistrantrange.php' ); 00042 require 'autoload.php'; 00043 00044 00045 $fileAdded = false; 00046 $file = ''; // url to get file "http://www.isbn-international.org/converter/ranges.js"; 00047 00048 $cli = eZCLI::instance(); 00049 $script = eZScript::instance( array( 'description' => "eZ Publish ISBN-13 update\n\n" . 00050 "Update the database with new updated ISBN data to the database.", 00051 'use-session' => false, 00052 'use-modules' => true, 00053 'use-extensions' => true ) ); 00054 00055 $script->startup(); 00056 00057 00058 00059 $options = $script->getOptions( "[file:][db-host:][db-user:][db-password:][db-database:][db-driver:]", 00060 "", 00061 array( 'file' => "Path to the file which contains a JavaScript file for the different ranges", 00062 'db-host' => "Database host.", 00063 'db-user' => "Database user.", 00064 'db-password' => "Database password.", 00065 'db-database' => "Database name.", 00066 'db-driver' => "Database driver." ) ); 00067 00068 if ( isset( $options['file'] ) ) 00069 { 00070 $file = $options['file']; 00071 } 00072 else 00073 { 00074 $cli->error( 'Error: you need to specify a Javascript file for the script with --file=ranges.js' ); 00075 $script->shutdown( 1 ); 00076 } 00077 00078 $script->initialize(); 00079 00080 $db = eZDB::instance(); 00081 if( !$db->IsConnected ) 00082 { 00083 // default settings are not valid 00084 // try user-defined settings 00085 00086 $dbUser = $options['db-user'] ? $options['db-user'] : false; 00087 $dbPassword = $options['db-password'] ? $options['db-password'] : false; 00088 $dbHost = $options['db-host'] ? $options['db-host'] : false; 00089 $dbName = $options['db-database'] ? $options['db-database'] : false; 00090 $dbImpl = $options['db-driver'] ? $options['db-driver'] : false; 00091 00092 if ( $dbHost or $dbName or $dbUser or $dbImpl ) 00093 { 00094 $params = array(); 00095 if ( $dbHost !== false ) 00096 $params['server'] = $dbHost; 00097 if ( $dbUser !== false ) 00098 { 00099 $params['user'] = $dbUser; 00100 $params['password'] = ''; 00101 } 00102 if ( $dbPassword !== false ) 00103 $params['password'] = $dbPassword; 00104 if ( $dbName !== false ) 00105 $params['database'] = $dbName; 00106 $db = eZDB::instance( $dbImpl, $params, true ); 00107 eZDB::setInstance( $db ); 00108 } 00109 00110 // still no success? 00111 if( !$db->IsConnected ) 00112 { 00113 $cli->error( "Error: couldn't connect to database '" . $db->DB . "'" ); 00114 $cli->error( ' for mysql try: ' ); 00115 $cli->error( ' mysql -e "create database ' . $db->DB . ';"' ); 00116 $cli->error( ' mysql tmp < kernel/sql/mysql/kernel_schema.sql' ); 00117 $cli->error( ' or use --help for more info' ); 00118 00119 $script->shutdown( 1 ); 00120 } 00121 } 00122 00123 $cli->output( "Using database '" . $cli->terminalStyle( 'red' ) . $db->DB . $cli->terminalStyle( 'normal' ) . "'" ); 00124 00125 $content = ''; 00126 $dataPage = ''; 00127 $isbnArray = array(); 00128 00129 $fp = fopen( $file, "r" ); 00130 if ( !$fp ) 00131 { 00132 $cli->error( "Error: file '$file' with update data not found" ); 00133 $cli->error( ' use --help for more info' ); 00134 $script->shutdown( 1 ); 00135 } 00136 00137 while ( !feof( $fp ) ) 00138 { 00139 $content .= fread( $fp, 4096 ); 00140 } 00141 fclose( $fp ); 00142 00143 // Parse the JavaScript file and add the different ranges into an array. 00144 $contentArray = preg_split( "/\n+|\r\n+/", $content ); 00145 foreach ( $contentArray as $contentItem ) 00146 { 00147 if ( preg_match( "/^\s*gi\.area(\d+)\.text\s*=\s*\"?([^\"]*)\"?\;?\s*$/i", $contentItem, $matchArray ) ) 00148 { 00149 $regGroupElement = (string)$matchArray[1]; 00150 $regGroupElementText = (string)$matchArray[2]; 00151 $isbnArray[$regGroupElement]['text'] = trim( $regGroupElementText ); 00152 } 00153 else if ( preg_match( "/^\s*gi\.area(\d+)\.pubrange\s*=\s*\"?([^\"]*?)\"?\;*\s*$/i", $contentItem, $matchArray ) ) 00154 { 00155 $regGroupElement = (string)$matchArray[1]; 00156 $regGroupElementRange = (string)$matchArray[2]; 00157 if ( trim( $regGroupElementRange ) != "" ) 00158 { 00159 $pubRangeArray = preg_split( "/;/", $regGroupElementRange ); 00160 foreach ( $pubRangeArray as $pubRangeItem ) 00161 { 00162 $pubRangeItemArray = preg_split( "/\-+/", $pubRangeItem ); 00163 00164 // The ranges need to be stored as a string, since the can start 00165 // with 0. The value from the javaScript may also only contain the 00166 // start value. 00167 if ( count( $pubRangeItemArray ) == 2 ) 00168 { 00169 $pubRangeFrom = (string)$pubRangeItemArray[0]; 00170 $pubRangeTo = (string)$pubRangeItemArray[1]; 00171 } 00172 else if ( count( $pubRangeItemArray ) == 1 ) 00173 { 00174 $pubRangeFrom = (string)$pubRangeItemArray[0]; 00175 $pubRangeTo = (string)$pubRangeItemArray[0]; 00176 } 00177 else 00178 { 00179 $cli->output( "Should not happend (Data: $pubRangeItem)" ); 00180 continue; 00181 } 00182 00183 $length = strlen( $pubRangeFrom ); 00184 $pubLength = 10 - strlen( trim( $regGroupElement ) ) - $length; 00185 $isbnArray[$regGroupElement]['pubrange'][] = array( 'from' => $pubRangeFrom, 00186 'group_from' => $pubRangeFrom, 00187 'to' => $pubRangeTo, 00188 'group_to' => $pubRangeTo, 00189 'length' => $length, 00190 'pub_length' => $pubLength ); 00191 } 00192 } 00193 else 00194 { 00195 $isbnArray[$regGroupElement]['pubrange'] = array(); 00196 } 00197 } 00198 } 00199 00200 // Clean up all tables to add everything from the start. 00201 eZISBNGroup::cleanAll(); 00202 eZISBNGroupRange::cleanAll(); 00203 eZISBNRegistrantRange::cleanAll(); 00204 00205 $registrationGroupArray = array(); 00206 foreach ( $isbnArray as $isbnRegGroupElement => $isbnItem ) 00207 { 00208 // Calculate the range for the registration group by adding it to an array and 00209 // extend the range by checking the existing array. 00210 $length = strlen( $isbnRegGroupElement ); 00211 $isbnRegGroupElementFrom = (int)str_pad( $isbnRegGroupElement, 5, 0, STR_PAD_RIGHT ); 00212 $isbnRegGroupElementTo = (int)str_pad( $isbnRegGroupElement, 5, 9, STR_PAD_RIGHT ); 00213 if ( isset( $registrationGroupArray[$length] ) ) 00214 { 00215 $registrationGroupArrayCount = count( $registrationGroupArray[$length] ); 00216 $added = false; 00217 00218 // Extend the length of the range, if it exists allready from one number in front. 00219 for ( $i = 0; $i < $registrationGroupArrayCount; $i++ ) 00220 { 00221 $fromTestValue = (int)$registrationGroupArray[$length][$i]['from']; 00222 $toTestValue = (int)$registrationGroupArray[$length][$i]['to']; 00223 if ( ( $isbnRegGroupElementFrom - 1 ) == $toTestValue ) 00224 { 00225 $registrationGroupArray[$length][$i]['to'] = $isbnRegGroupElementTo; 00226 $added = true; 00227 } 00228 00229 if ( ( $isbnRegGroupElementTo + 1 ) == $fromTestValue ) 00230 { 00231 $registrationGroupArray[$length][$i]['from'] = $isbnRegGroupElementFrom; 00232 $added = true; 00233 } 00234 } 00235 00236 // Since the range is not found as a continued range from the other ranges, so 00237 // create a new one. 00238 if ( $added == false ) 00239 { 00240 $registrationGroupArray[$length][] = array( 'from' => $isbnRegGroupElementFrom, 00241 'to' => $isbnRegGroupElementTo ); 00242 } 00243 } 00244 else // Range for this length does not exist, create a new one. 00245 { 00246 $registrationGroupArray[$length][] = array( 'from' => $isbnRegGroupElementFrom, 00247 'to' => $isbnRegGroupElementTo ); 00248 } 00249 00250 $isbnGroup = eZISBNGroup::create( $isbnRegGroupElement, $isbnItem['text'] ); 00251 $isbnGroup->store(); 00252 $isbnGroupID = $isbnGroup->attribute( 'id' ); 00253 $pubRangeArray = $isbnItem['pubrange']; 00254 if ( is_array( $pubRangeArray ) ) 00255 { 00256 foreach ( $pubRangeArray as $isbnRegistrantRange ) 00257 { 00258 // The test number should have a base with 5 digits where from should be padded with 0 and 00259 // to should be padded with 9. 00260 $fromValue = (int)substr( str_pad( $isbnRegistrantRange['from'], 5, 0, STR_PAD_RIGHT ), 0, 5 ); 00261 $toValue = (int)substr( str_pad( $isbnRegistrantRange['to'], 5, 9, STR_PAD_RIGHT ), 0, 5 ); 00262 $length = $isbnRegistrantRange['length']; 00263 00264 $registrationGroup = eZISBNRegistrantRange::create( $isbnGroupID, 00265 $fromValue, 00266 $toValue, 00267 $isbnRegistrantRange['from'], 00268 $isbnRegistrantRange['to'], 00269 $length ); 00270 $registrationGroup->store(); 00271 } 00272 } 00273 } 00274 00275 00276 // Add the registration group ranges to the database. 00277 if ( count( $registrationGroupArray ) > 0 ) 00278 { 00279 foreach ( $registrationGroupArray as $registrationGroupItemLength => $registrationGroupItemArray ) 00280 { 00281 foreach ( $registrationGroupItemArray as $registrationGroupItemRange ) 00282 { 00283 // Will cut the last part of the numbers, since it's up to each registrant to use the other 00284 // numbers. 00285 $fromValue = $registrationGroupItemRange['from']; 00286 $toValue = $registrationGroupItemRange['to']; 00287 00288 // Create the group: from and to string with the correct length. 00289 $groupFrom = str_pad( substr( $fromValue, 0, $registrationGroupItemLength ), 0, $registrationGroupItemLength ); 00290 $groupTo = str_pad( substr( $toValue, 0, $registrationGroupItemLength ), 0, $registrationGroupItemLength ); 00291 00292 $registrationGroupRange = eZISBNGroupRange::create( $fromValue, 00293 $toValue, 00294 $groupFrom, 00295 $groupTo, 00296 $registrationGroupItemLength ); 00297 $registrationGroupRange->store(); 00298 } 00299 } 00300 } 00301 00302 $cli->output( 'Complete' ); 00303 00304 $script->shutdown(); 00305 00306 ?>