/kernel/classes/datatypes/ezisbn/ezisbn10to13converter.php

https://bitbucket.org/ericsagnes/ezpublish-multisite · PHP · 355 lines · 261 code · 30 blank · 64 comment · 37 complexity · 5ed8617328936ff72ba01bb97d900154 MD5 · raw file

  1. <?php
  2. /**
  3. * File containing the eZISBN10To13Converter class.
  4. *
  5. * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
  6. * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
  7. * @version 2012.8
  8. * @package kernel
  9. */
  10. class eZISBN10To13Converter
  11. {
  12. /*!
  13. Constructor
  14. \param $script The variable is set earlier in the script and transfered to the class.
  15. \param $cli Is set earlier in the script, and used to send output / feedback to the user.
  16. \param $params custom parameters to the class. The Force parameter is now set as a
  17. class variable for the other functions.
  18. */
  19. function eZISBN10To13Converter( $script, $cli, $params )
  20. {
  21. $this->Script = $script;
  22. $this->Cli = $cli;
  23. $this->AttributeArray = array();
  24. if ( isset( $params['force'] ) )
  25. {
  26. $this->Force = $params['force'];
  27. }
  28. else
  29. {
  30. $this->Force = false;
  31. }
  32. }
  33. /*!
  34. Add all classes. Will fetch all class attributes from the database that has the ISBN
  35. datatype and register it in a class variable AttributeArray for later processing.
  36. \return true if successfull and false if not.
  37. */
  38. function addAllClasses()
  39. {
  40. $db = eZDB::instance();
  41. $this->Cli->output( $this->Cli->stylize( 'strong', 'Fetch All' ) . ' classes:' );
  42. $sql = "SELECT id, data_int1 FROM ezcontentclass_attribute WHERE " .
  43. "data_type_string='ezisbn' and version='0'";
  44. $classAttributeList = $db->arrayQuery( $sql );
  45. $status = false;
  46. if ( count( $classAttributeList ) > 0 )
  47. {
  48. foreach ( $classAttributeList as $classAttributeItem )
  49. {
  50. $classAttributeID = $classAttributeItem['id'];
  51. $isIsbn13 = $classAttributeItem['data_int1'];
  52. $classAttribute = eZContentClassAttribute::fetch( $classAttributeID );
  53. if ( $this->Force === true or $isIsbn13 == 1 )
  54. {
  55. $this->AttributeArray[$classAttributeID] = $classAttribute;
  56. $status = true;
  57. }
  58. else
  59. {
  60. $this->Cli->output( $this->Cli->stylize( 'warning', 'Warning:' ) . ' The Class id ' .
  61. $this->Cli->stylize( 'strong', $classAttribute->attribute( 'contentclass_id' ) ) . ' attribute id ' .
  62. $this->Cli->stylize( 'strong', $classAttributeID ) . ' is not set to ISBN-13. Use --force to set the ISBN-13 flag' );
  63. }
  64. }
  65. }
  66. return $status;
  67. }
  68. /*!
  69. Add all ezisbn class attributes from a class with a specific id and register them
  70. in a class variable AttributeArray for later processing.
  71. \return true if successfull and false if not.
  72. */
  73. function addClass( $classID )
  74. {
  75. $status = false;
  76. if ( is_numeric( $classID ) )
  77. {
  78. $class = eZContentClass::fetch( $classID );
  79. if ( $class instanceof eZContentClass )
  80. {
  81. $classFilter = array( 'data_type_string' => 'ezisbn' );
  82. $classAttributes = $class->fetchAttributes();
  83. $attributeFound = false;
  84. if ( count( $classAttributes ) > 0 )
  85. {
  86. foreach ( $classAttributes as $attribute )
  87. {
  88. if ( $attribute->attribute( 'data_type_string' ) == 'ezisbn' )
  89. {
  90. if ( $attribute->attribute( 'data_int1' ) == 1 or $this->Force === true )
  91. {
  92. $attributeFound = true;
  93. $this->AttributeArray[$attribute->attribute( 'id' )] = $attribute;
  94. }
  95. else
  96. {
  97. $this->Cli->output( $this->Cli->stylize( 'warning', 'Warning:' ) . ' The attribute id ' .
  98. $this->Cli->stylize( 'strong', $attribute->attribute( 'id' ) ) . ' is not set to ISBN-13. Use --force to set the ISBN-13 flag' );
  99. }
  100. }
  101. }
  102. }
  103. if ( $attributeFound === false )
  104. {
  105. $this->Cli->output( $this->Cli->stylize( 'warning', 'Warning:' ) . ' Did not find any ISBN attributes in contentclass: ' .
  106. $this->Cli->stylize( 'strong', $classID ) . '.' );
  107. }
  108. }
  109. else
  110. {
  111. $this->Cli->output( $this->Cli->stylize( 'warning', 'Warning:' ) . ' the class id ' .
  112. $this->Cli->stylize( 'strong', $classID ) . ' does not exist.' );
  113. }
  114. }
  115. else if ( $classID !== null )
  116. {
  117. $status = true;
  118. $this->Cli->output( $this->Cli->stylize( 'error', 'Error:' ) . ' the class id need to be numeric.' );
  119. }
  120. return $status;
  121. }
  122. /*!
  123. Add one ezisbn class attribute with a specific class attribute id and register it
  124. in a class variable AttributeArray for later processing.
  125. \return true if successfull and false if not.
  126. */
  127. function addAttribute( $attributeID )
  128. {
  129. $status = false;
  130. if ( is_numeric( $attributeID ) )
  131. {
  132. $classAttribute = eZContentClassAttribute::fetch( $attributeID );
  133. if ( $classAttribute instanceof eZContentClassAttribute )
  134. {
  135. if ( $classAttribute->attribute( 'data_type_string' ) == 'ezisbn' )
  136. {
  137. if ( $classAttribute->attribute( 'data_int1' ) == 1 or $this->Force === true )
  138. {
  139. $this->AttributeArray[$classAttribute->attribute( 'id' )] = $classAttribute;
  140. }
  141. else
  142. {
  143. $this->Cli->output( $this->Cli->stylize( 'warning', 'Warning:' ) . ' The attribute id ' .
  144. $this->Cli->stylize( 'strong', $attributeID ) . ' is not set to ISBN-13. Use --force to set the ISBN-13 flag' );
  145. }
  146. }
  147. else
  148. {
  149. $this->Cli->output( $this->Cli->stylize( 'warning', 'Warning:' ) . ' The attribute id ' .
  150. $this->Cli->stylize( 'strong', $attributeID ) . ' is not an ISBN datatype but of type ' .
  151. $this->Cli->stylize( 'strong', $classAttribute->attribute( 'data_type_string' ) ) . '.' );
  152. }
  153. }
  154. else
  155. {
  156. $this->Cli->output( $this->Cli->stylize( 'warning', 'Warning:' ) . ' The attribute id ' .
  157. $this->Cli->stylize( 'strong', $attributeID ) . ' does not exist.' );
  158. }
  159. }
  160. else if ( $attributeID !== null )
  161. {
  162. $this->Cli->output( $this->Cli->stylize( 'error', 'Error:' ) . ' the attribute id need to be numeric.' );
  163. $status = true;
  164. }
  165. return $status;
  166. }
  167. /*!
  168. \return count of the current amount of class attributes registered in the attribute array.
  169. */
  170. function attributeCount()
  171. {
  172. return count( $this->AttributeArray );
  173. }
  174. /*!
  175. Start processing the content object attributes.
  176. */
  177. function execute()
  178. {
  179. foreach ( $this->AttributeArray as $classAttribute )
  180. {
  181. $contentClass = eZContentClass::fetch( $classAttribute->attribute( 'contentclass_id' ) );
  182. $this->Cli->output( "Process class: " . $this->Cli->stylize( 'strong', $classAttribute->attribute( 'contentclass_id' ) ) .
  183. " (" . $contentClass->attribute( 'name' ) . "), attribute id: " .
  184. $this->Cli->stylize( 'strong', $classAttribute->attribute( 'id' ) ) .
  185. " (" . $classAttribute->attribute( 'name' ) . "):" );
  186. $this->updateContentFromClassAttribute( $classAttribute->attribute( 'id' ) );
  187. $this->updateClassAttributeToISBN13( $classAttribute->attribute( 'id' ) );
  188. $this->Cli->output( " Finished." );
  189. }
  190. }
  191. /*!
  192. Update content in a ezisbn datatype for one specific class attribute id.
  193. \param $classAttributeID is the class attribute id for for the ISBN datatype.
  194. */
  195. function updateContentFromClassAttribute( $classAttributeID )
  196. {
  197. $asObject = true;
  198. $i = 0;
  199. $offset = 0;
  200. $countList = 0;
  201. $limit = 100;
  202. $conditions = array( "contentclassattribute_id" => $classAttributeID );
  203. $limitArray = array( 'offset' => $offset,
  204. 'limit' => $limit );
  205. $sortArray = array( 'id' => 'asc' );
  206. // Only fetch some objects each time to avoid memory problems.
  207. while ( true )
  208. {
  209. $contentObjectAttributeList = eZPersistentObject::fetchObjectList( eZContentObjectAttribute::definition(),
  210. null,
  211. $conditions,
  212. $sortArray,
  213. $limitArray,
  214. $asObject );
  215. if ( count( $contentObjectAttributeList ) == 0 )
  216. {
  217. break;
  218. }
  219. foreach ( $contentObjectAttributeList as $contentObjectAttribute )
  220. {
  221. $this->updateContentObjectAttribute( $contentObjectAttribute );
  222. }
  223. $this->Cli->output( ".", false );
  224. $i++;
  225. if ( ( $i % 70 ) == 0 )
  226. {
  227. $this->Cli->output( ' ' . $this->Cli->stylize( 'strong', $i * $limit ) );
  228. }
  229. $countList = count( $contentObjectAttributeList );
  230. unset( $contentObjectList );
  231. $offset += $limit;
  232. $limitArray = array( 'offset' => $offset,
  233. 'limit' => $limit );
  234. }
  235. $repeatLength = 70 - ( $i % 70 );
  236. $count = ( ( $i - 1 ) * $limit ) + $countList;
  237. $this->Cli->output( str_repeat( ' ', $repeatLength ) . ' ' . $this->Cli->stylize( 'strong', $count ), false );
  238. }
  239. /*!
  240. Convert the ISBN number for a content object attribute with the specific
  241. content attribute id.
  242. \param $contentObjectAttribute Should be a object of eZContentObjectAttribute.
  243. */
  244. function updateContentObjectAttribute( $contentObjectAttribute )
  245. {
  246. $isbnNumber = $contentObjectAttribute->attribute( 'data_text' );
  247. $isbnValue = trim( $isbnNumber );
  248. $error = false;
  249. // If the number only consists of hyphen, it should be emty.
  250. if ( preg_match( "/^\-+$/", $isbnValue ) )
  251. {
  252. $emtyValue = '';
  253. $this->updateContentISBNNumber( $contentObjectAttribute, $emtyValue );
  254. return true;
  255. }
  256. // Validate the ISBN number.
  257. $digits = preg_replace( "/\-/", "", $isbnValue );
  258. if ( trim( $digits ) != "" )
  259. {
  260. // If the length of the number is 10, it is an ISBN-10 number and need
  261. // to be converted to ISBN-13.
  262. if ( strlen( $digits ) == 10 )
  263. {
  264. $ean = eZISBNType::convertISBN10toISBN13( $digits );
  265. }
  266. else if ( strlen( $digits ) == 13 )
  267. {
  268. $ean = $digits;
  269. }
  270. else
  271. {
  272. $error = true;
  273. }
  274. if ( $error === false )
  275. {
  276. $isbn13 = new eZISBN13();
  277. $formatedISBN13Value = $isbn13->formatedISBNValue( $ean, $error );
  278. }
  279. if ( $error === false )
  280. {
  281. $this->updateContentISBNNumber( $contentObjectAttribute, $formatedISBN13Value );
  282. }
  283. else
  284. {
  285. $this->Cli->output( $this->Cli->stylize( 'warning', 'Warning:' ) . ' ISBN: ' .
  286. $this->Cli->stylize( 'strong', $isbnNumber ) . ' is not valid. You need to update contentobject: ' .
  287. $this->Cli->stylize( 'strong', $contentObjectAttribute->attribute( 'contentobject_id' ) ) . ' version: ' .
  288. $this->Cli->stylize( 'strong', $contentObjectAttribute->attribute( 'version' ) ) . ' manually.' );
  289. }
  290. }
  291. }
  292. /*!
  293. Does the update of the class attribute directly to the database, which will only alter
  294. the attribute for if the ISBN datatype is ISBN-13.
  295. \param $classAttributeID is the Class attribute id for the ISBN datatype.
  296. */
  297. function updateClassAttributeToISBN13( $classAttributeID )
  298. {
  299. $db = eZDB::instance();
  300. $sql = "UPDATE ezcontentclass_attribute SET data_int1='1' WHERE id='" . $classAttributeID . "'";
  301. $db->query( $sql );
  302. }
  303. /*!
  304. Does the update of the content object attribute directly to the database, which will only alter
  305. the attribute for if the ISBN datatype is ISBN-13.
  306. \param $contentObjectAttribute Is an object of eZContentObjectAttribute.
  307. \param $formatedISBN13Value contains the formated version of the ISBN-13 number with hyphen as delimiter.
  308. */
  309. function updateContentISBNNumber( $contentObjectAttribute, $formatedISBN13Value )
  310. {
  311. $contentObjectAttributeID = $contentObjectAttribute->attribute( 'id' );
  312. $version = $contentObjectAttribute->attribute( 'version' );
  313. $db = eZDB::instance();
  314. $sql = "UPDATE ezcontentobject_attribute SET data_text='" . $db->escapeString( $formatedISBN13Value ) .
  315. "' WHERE id='" . $contentObjectAttributeID . "' AND version='" . $version . "'" ;
  316. $db->query( $sql );
  317. }
  318. public $Cli;
  319. public $Script;
  320. public $AttributeArray;
  321. }
  322. ?>