|
eZ Publish
[4.0]
|
00001 <?php 00002 // 00003 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ## 00004 // SOFTWARE NAME: eZ Publish 00005 // SOFTWARE RELEASE: 4.0.x 00006 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS 00007 // SOFTWARE LICENSE: GNU General Public License v2.0 00008 // NOTICE: > 00009 // This program is free software; you can redistribute it and/or 00010 // modify it under the terms of version 2.0 of the GNU General 00011 // Public License as published by the Free Software Foundation. 00012 // 00013 // This program is distributed in the hope that it will be useful, 00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 // GNU General Public License for more details. 00017 // 00018 // You should have received a copy of version 2.0 of the GNU General 00019 // Public License along with this program; if not, write to the Free 00020 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 00021 // MA 02110-1301, USA. 00022 // 00023 // 00024 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ## 00025 // 00026 00027 class eZISBN10To13Converter 00028 { 00029 /*! 00030 Constructor 00031 \param $script The variable is set earlier in the script and transfered to the class. 00032 \param $cli Is set earlier in the script, and used to send output / feedback to the user. 00033 \param $params custom parameters to the class. The Force parameter is now set as a 00034 class variable for the other functions. 00035 */ 00036 function eZISBN10To13Converter( $script, $cli, $params ) 00037 { 00038 $this->Script = $script; 00039 $this->Cli = $cli; 00040 $this->AttributeArray = array(); 00041 if ( isset( $params['force'] ) ) 00042 { 00043 $this->Force = $params['force']; 00044 } 00045 else 00046 { 00047 $this->Force = false; 00048 } 00049 } 00050 00051 00052 /*! 00053 Add all classes. Will fetch all class attributes from the database that has the ISBN 00054 datatype and register it in a class variable AttributeArray for later processing. 00055 00056 \return true if successfull and false if not. 00057 */ 00058 function addAllClasses() 00059 { 00060 $db = eZDB::instance(); 00061 $this->Cli->output( $this->Cli->stylize( 'strong', 'Fetch All' ) . ' classes:' ); 00062 $sql = "SELECT id, data_int1 FROM ezcontentclass_attribute WHERE " . 00063 "data_type_string='ezisbn' and version='0'"; 00064 00065 $classAttributeList = $db->arrayQuery( $sql ); 00066 $status = false; 00067 if ( count( $classAttributeList ) > 0 ) 00068 { 00069 foreach ( $classAttributeList as $classAttributeItem ) 00070 { 00071 $classAttributeID = $classAttributeItem['id']; 00072 $isIsbn13 = $classAttributeItem['data_int1']; 00073 $classAttribute = eZContentClassAttribute::fetch( $classAttributeID ); 00074 00075 if ( $this->Force === true or $isIsbn13 == 1 ) 00076 { 00077 $this->AttributeArray[$classAttributeID] = $classAttribute; 00078 $status = true; 00079 } 00080 else 00081 { 00082 $this->Cli->output( $this->Cli->stylize( 'warning', 'Warning:' ) . ' The Class id ' . 00083 $this->Cli->stylize( 'strong', $classAttribute->attribute( 'contentclass_id' ) ) . ' attribute id ' . 00084 $this->Cli->stylize( 'strong', $classAttributeID ) . ' is not set to ISBN-13. Use --force to set the ISBN-13 flag' ); 00085 } 00086 00087 } 00088 } 00089 00090 return $status; 00091 } 00092 00093 /*! 00094 Add all ezisbn class attributes from a class with a specific id and register them 00095 in a class variable AttributeArray for later processing. 00096 00097 \return true if successfull and false if not. 00098 */ 00099 function addClass( $classID ) 00100 { 00101 $status = false; 00102 if ( is_numeric( $classID ) ) 00103 { 00104 $class = eZContentClass::fetch( $classID ); 00105 if ( $class instanceof eZContentClass ) 00106 { 00107 $classFilter = array( 'data_type_string' => 'ezisbn' ); 00108 $classAttributes = $class->fetchAttributes(); 00109 $attributeFound = false; 00110 if ( count( $classAttributes ) > 0 ) 00111 { 00112 foreach ( $classAttributes as $attribute ) 00113 { 00114 if ( $attribute->attribute( 'data_type_string' ) == 'ezisbn' ) 00115 { 00116 if ( $attribute->attribute( 'data_int1' ) == 1 or $this->Force === true ) 00117 { 00118 $attributeFound = true; 00119 $this->AttributeArray[$attribute->attribute( 'id' )] = $attribute; 00120 } 00121 else 00122 { 00123 $this->Cli->output( $this->Cli->stylize( 'warning', 'Warning:' ) . ' The attribute id ' . 00124 $this->Cli->stylize( 'strong', $attribute->attribute( 'id' ) ) . ' is not set to ISBN-13. Use --force to set the ISBN-13 flag' ); 00125 } 00126 } 00127 } 00128 } 00129 00130 if ( $attributeFound === false ) 00131 { 00132 $this->Cli->output( $this->Cli->stylize( 'warning', 'Warning:' ) . ' Did not find any ISBN attributes in contentclass: ' . 00133 $this->Cli->stylize( 'strong', $classID ) . '.' ); 00134 } 00135 } 00136 else 00137 { 00138 $this->Cli->output( $this->Cli->stylize( 'warning', 'Warning:' ) . ' the class id ' . 00139 $this->Cli->stylize( 'strong', $classID ) . ' does not exist.' ); 00140 } 00141 } 00142 else if ( $classID !== null ) 00143 { 00144 $status = true; 00145 $this->Cli->output( $this->Cli->stylize( 'error', 'Error:' ) . ' the class id need to be numeric.' ); 00146 } 00147 00148 return $status; 00149 } 00150 00151 /*! 00152 Add one ezisbn class attribute with a specific class attribute id and register it 00153 in a class variable AttributeArray for later processing. 00154 00155 \return true if successfull and false if not. 00156 */ 00157 function addAttribute( $attributeID ) 00158 { 00159 $status = false; 00160 if ( is_numeric( $attributeID ) ) 00161 { 00162 $classAttribute = eZContentClassAttribute::fetch( $attributeID ); 00163 if ( $classAttribute instanceof eZContentClassAttribute ) 00164 { 00165 if ( $classAttribute->attribute( 'data_type_string' ) == 'ezisbn' ) 00166 { 00167 if ( $classAttribute->attribute( 'data_int1' ) == 1 or $this->Force === true ) 00168 { 00169 $this->AttributeArray[$classAttribute->attribute( 'id' )] = $classAttribute; 00170 } 00171 else 00172 { 00173 $this->Cli->output( $this->Cli->stylize( 'warning', 'Warning:' ) . ' The attribute id ' . 00174 $this->Cli->stylize( 'strong', $attributeID ) . ' is not set to ISBN-13. Use --force to set the ISBN-13 flag' ); 00175 } 00176 } 00177 else 00178 { 00179 $this->Cli->output( $this->Cli->stylize( 'warning', 'Warning:' ) . ' The attribute id ' . 00180 $this->Cli->stylize( 'strong', $attributeID ) . ' is not an ISBN datatype but of type ' . 00181 $this->Cli->stylize( 'strong', $classAttribute->attribute( 'data_type_string' ) ) . '.' ); 00182 } 00183 } 00184 else 00185 { 00186 $this->Cli->output( $this->Cli->stylize( 'warning', 'Warning:' ) . ' The attribute id ' . 00187 $this->Cli->stylize( 'strong', $attributeID ) . ' does not exist.' ); 00188 } 00189 } 00190 else if ( $attributeID !== null ) 00191 { 00192 $this->Cli->output( $this->Cli->stylize( 'error', 'Error:' ) . ' the attribute id need to be numeric.' ); 00193 $status = true; 00194 } 00195 00196 return $status; 00197 } 00198 00199 00200 /*! 00201 \return count of the current amount of class attributes registered in the attribute array. 00202 */ 00203 function attributeCount() 00204 { 00205 return count( $this->AttributeArray ); 00206 } 00207 00208 /*! 00209 Start processing the content object attributes. 00210 */ 00211 function execute() 00212 { 00213 foreach ( $this->AttributeArray as $classAttribute ) 00214 { 00215 $contentClass = eZContentClass::fetch( $classAttribute->attribute( 'contentclass_id' ) ); 00216 $this->Cli->output( "Process class: " . $this->Cli->stylize( 'strong', $classAttribute->attribute( 'contentclass_id' ) ) . 00217 " (" . $contentClass->attribute( 'name' ) . "), attribute id: " . 00218 $this->Cli->stylize( 'strong', $classAttribute->attribute( 'id' ) ) . 00219 " (" . $classAttribute->attribute( 'name' ) . "):" ); 00220 00221 $this->updateContentFromClassAttribute( $classAttribute->attribute( 'id' ) ); 00222 $this->updateClassAttributeToISBN13( $classAttribute->attribute( 'id' ) ); 00223 00224 $this->Cli->output( " Finished." ); 00225 } 00226 } 00227 00228 /*! 00229 Update content in a ezisbn datatype for one specific class attribute id. 00230 \param $classAttributeID is the class attribute id for for the ISBN datatype. 00231 */ 00232 function updateContentFromClassAttribute( $classAttributeID ) 00233 { 00234 $asObject = true; 00235 00236 $i = 0; 00237 $offset = 0; 00238 $countList = 0; 00239 $limit = 100; 00240 $conditions = array( "contentclassattribute_id" => $classAttributeID ); 00241 $limitArray = array( 'offset' => $offset, 00242 'limit' => $limit ); 00243 00244 $sortArray = array( 'id' => 'asc' ); 00245 00246 // Only fetch some objects each time to avoid memory problems. 00247 while ( true ) 00248 { 00249 $contentObjectAttributeList = eZPersistentObject::fetchObjectList( eZContentObjectAttribute::definition(), 00250 null, 00251 $conditions, 00252 $sortArray, 00253 $limitArray, 00254 $asObject ); 00255 if ( count( $contentObjectAttributeList ) == 0 ) 00256 { 00257 break; 00258 } 00259 foreach ( $contentObjectAttributeList as $contentObjectAttribute ) 00260 { 00261 $this->updateContentObjectAttribute( $contentObjectAttribute ); 00262 } 00263 $this->Cli->output( ".", false ); 00264 $i++; 00265 if ( ( $i % 70 ) == 0 ) 00266 { 00267 $this->Cli->output( ' ' . $this->Cli->stylize( 'strong', $i * $limit ) ); 00268 } 00269 $countList = count( $contentObjectAttributeList ); 00270 unset( $contentObjectList ); 00271 $offset += $limit; 00272 $limitArray = array( 'offset' => $offset, 00273 'limit' => $limit ); 00274 } 00275 $repeatLength = 70 - ( $i % 70 ); 00276 $count = ( ( $i - 1 ) * $limit ) + $countList; 00277 $this->Cli->output( str_repeat( ' ', $repeatLength ) . ' ' . $this->Cli->stylize( 'strong', $count ), false ); 00278 } 00279 00280 /*! 00281 Convert the ISBN number for a content object attribute with the specific 00282 content attribute id. 00283 \param $contentObjectAttribute Should be a object of eZContentObjectAttribute. 00284 */ 00285 function updateContentObjectAttribute( $contentObjectAttribute ) 00286 { 00287 $isbnNumber = $contentObjectAttribute->attribute( 'data_text' ); 00288 $isbnValue = trim( $isbnNumber ); 00289 $error = false; 00290 00291 // If the number only consists of hyphen, it should be emty. 00292 if ( preg_match( "/^\-+$/", $isbnValue ) ) 00293 { 00294 $emtyValue = ''; 00295 $this->updateContentISBNNumber( $contentObjectAttribute, $emtyValue ); 00296 return true; 00297 } 00298 // Validate the ISBN number. 00299 $digits = preg_replace( "/\-/", "", $isbnValue ); 00300 00301 if ( trim( $digits ) != "" ) 00302 { 00303 // If the length of the number is 10, it is an ISBN-10 number and need 00304 // to be converted to ISBN-13. 00305 if ( strlen( $digits ) == 10 ) 00306 { 00307 $ean = eZISBNType::convertISBN10toISBN13( $digits ); 00308 } 00309 else if ( strlen( $digits ) == 13 ) 00310 { 00311 $ean = $digits; 00312 } 00313 else 00314 { 00315 $error = true; 00316 } 00317 00318 if ( $error === false ) 00319 { 00320 $isbn13 = new eZISBN13(); 00321 $formatedISBN13Value = $isbn13->formatedISBNValue( $ean, $error ); 00322 } 00323 00324 if ( $error === false ) 00325 { 00326 $this->updateContentISBNNumber( $contentObjectAttribute, $formatedISBN13Value ); 00327 } 00328 else 00329 { 00330 $this->Cli->output( $this->Cli->stylize( 'warning', 'Warning:' ) . ' ISBN: ' . 00331 $this->Cli->stylize( 'strong', $isbnNumber ) . ' is not valid. You need to update contentobject: ' . 00332 $this->Cli->stylize( 'strong', $contentObjectAttribute->attribute( 'contentobject_id' ) ) . ' version: ' . 00333 $this->Cli->stylize( 'strong', $contentObjectAttribute->attribute( 'version' ) ) . ' manually.' ); 00334 } 00335 } 00336 } 00337 00338 /*! 00339 Does the update of the class attribute directly to the database, which will only alter 00340 the attribute for if the ISBN datatype is ISBN-13. 00341 \param $classAttributeID is the Class attribute id for the ISBN datatype. 00342 */ 00343 function updateClassAttributeToISBN13( $classAttributeID ) 00344 { 00345 $db = eZDB::instance(); 00346 $sql = "UPDATE ezcontentclass_attribute SET data_int1='1' WHERE id='" . $classAttributeID . "'"; 00347 $db->query( $sql ); 00348 } 00349 00350 /*! 00351 Does the update of the content object attribute directly to the database, which will only alter 00352 the attribute for if the ISBN datatype is ISBN-13. 00353 \param $contentObjectAttribute Is an object of eZContentObjectAttribute. 00354 \param $formatedISBN13Value contains the formated version of the ISBN-13 number with hyphen as delimiter. 00355 */ 00356 function updateContentISBNNumber( $contentObjectAttribute, $formatedISBN13Value ) 00357 { 00358 $contentObjectAttributeID = $contentObjectAttribute->attribute( 'id' ); 00359 $version = $contentObjectAttribute->attribute( 'version' ); 00360 $db = eZDB::instance(); 00361 $sql = "UPDATE ezcontentobject_attribute SET data_text='" . $db->escapeString( $formatedISBN13Value ) . 00362 "' WHERE id='" . $contentObjectAttributeID . "' AND version='" . $version . "'" ; 00363 $db->query( $sql ); 00364 } 00365 00366 public $Cli; 00367 public $Script; 00368 public $AttributeArray; 00369 } 00370 00371 ?>