PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/magmi/inc/magmi_engine.php

https://bitbucket.org/jit_bec/shopifine
PHP | 452 lines | 371 code | 46 blank | 35 comment | 29 complexity | 99010a0a9ca17bec745dd1c3cbc36499 MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. require_once("dbhelper.class.php");
  3. require_once("magmi_config.php");
  4. require_once("magmi_version.php");
  5. require_once("magmi_utils.php");
  6. require_once("magmi_statemanager.php");
  7. require_once("magmi_pluginhelper.php");
  8. /**
  9. *
  10. * This class is the mother class for magmi engines
  11. * A magmi engine is a class that performs operations on DB
  12. * @author dweeves
  13. *
  14. */
  15. abstract class Magmi_Engine extends DbHelper
  16. {
  17. protected $_conf;
  18. protected $_initialized=false;
  19. protected $_exceptions=array();
  20. public $tprefix;
  21. protected $_connected;
  22. protected $_activeplugins;
  23. protected $_pluginclasses;
  24. protected $_builtinplugins=array();
  25. protected $_ploop_callbacks=array();
  26. private $_excid=0;
  27. public $logger=null;
  28. public function getEngineInfo()
  29. {
  30. return array("name"=>"Generic Magmi Engine","version"=>"1.1","author"=>"dweeves");
  31. }
  32. public function __construct()
  33. {
  34. //force PHP internal encoding as UTF 8
  35. mb_internal_encoding("UTF-8");
  36. }
  37. public final function initialize($params=array())
  38. {
  39. try
  40. {
  41. $this->_conf=Magmi_Config::getInstance();
  42. $this->_conf->load();
  43. $this->tprefix=$this->_conf->get("DATABASE","table_prefix");
  44. $this->_excid=0;
  45. $this->_initialized=true;
  46. $this->_exceptions=array();
  47. }
  48. catch(Exception $e)
  49. {
  50. die("Error initializing Engine:{$this->_conf->getConfigFilename()} \n".$e->getMessage());
  51. }
  52. }
  53. /**
  54. * Returns magento directory
  55. */
  56. public function getMagentoDir()
  57. {
  58. return $this->_conf->getMagentoDir();
  59. }
  60. public function getMagentoVersion()
  61. {
  62. return $this->_conf->get("MAGENTO","version");
  63. }
  64. protected function _registerPluginLoopCallback($cbtype,$cb)
  65. {
  66. $this->_ploop_callbacks[$cbtype]=$cb;
  67. }
  68. protected function _unregisterPluginLoopCallback($cbtype)
  69. {
  70. unset($this->_ploop_callbacks[$cbtype]);
  71. }
  72. public function getPluginFamilies()
  73. {
  74. return array();
  75. }
  76. public function getEnabledPluginClasses($profile)
  77. {
  78. $enabledplugins=new EnabledPlugins_Config($profile);
  79. $enabledplugins->load();
  80. return $enabledplugins->getEnabledPluginFamilies($this->getPluginFamilies());
  81. }
  82. public function initPlugins($profile=null)
  83. {
  84. $this->_pluginclasses=$this->getEnabledPluginClasses($profile);
  85. }
  86. public function getBuiltinPluginClasses()
  87. {
  88. $bplarr=array();
  89. foreach($this->_builtinplugins as $pfamily=>$pdef)
  90. {
  91. $plinfo=explode("::",$pdef);
  92. $pfile=$plinfo[0];
  93. $pclass=$plinfo[1];
  94. require_once($pfile);
  95. if(!isset($bplarr[$pfamily]))
  96. {
  97. $bplarr[$pfamily]=array();
  98. }
  99. $bplarr[$pfamily][]=$pclass;
  100. }
  101. return $bplarr;
  102. }
  103. public function getPluginClasses()
  104. {
  105. return $this->_pluginclasses;
  106. }
  107. public function getPluginInstances($family=null)
  108. {
  109. $pil=null;
  110. if($family==null)
  111. {
  112. $pil=$this->_activeplugins();
  113. }
  114. else
  115. {
  116. $pil=(isset($this->_activeplugins[$family])?$this->_activeplugins[$family]:array());
  117. }
  118. return $pil;
  119. }
  120. public function setBuiltinPluginClasses($pfamily,$pclasses)
  121. {
  122. $this->_builtinplugins[$pfamily]=$pclasses;
  123. }
  124. public function sortPlugins($p1,$p2)
  125. {
  126. $m1=$p1->getPluginMeta();
  127. if($m1==null)
  128. {
  129. return 1;
  130. }
  131. $m2=$p2->getPluginMeta();
  132. if($m2==null)
  133. {
  134. return -1;
  135. }
  136. return strcmp($m1["file"],$m2["file"]);
  137. }
  138. public function createPlugins($profile,$params)
  139. {
  140. $plhelper=Magmi_PluginHelper::getInstance($profile);
  141. $this->_pluginclasses = array_merge_recursive($this->_pluginclasses,$this->getBuiltinPluginClasses());
  142. foreach($this->_pluginclasses as $pfamily=>$pclasses)
  143. {
  144. if($pfamily[0]=="*")
  145. {
  146. $this->_pluginclasses[substr($pfamily,1)]=$pclasses;
  147. unset($this->_pluginclasses[$pfamily]);
  148. }
  149. }
  150. foreach($this->_pluginclasses as $pfamily=>$pclasses)
  151. {
  152. if(!isset($this->_activeplugins[$pfamily]))
  153. {
  154. $this->_activeplugins[$pfamily]=array();
  155. }
  156. foreach($pclasses as $pclass)
  157. {
  158. $this->_activeplugins[$pfamily][]=$plhelper->createInstance($pfamily,$pclass,$params,$this);
  159. }
  160. usort($this->_activeplugins[$pfamily],array(&$this,"sortPlugins"));
  161. }
  162. }
  163. public function getPluginInstanceByClassName($pfamily,$pclassname)
  164. {
  165. $inst=null;
  166. if(isset($this->_activeplugins[$pfamily]))
  167. {
  168. foreach($this->_activeplugins[$pfamily] as $pinstance)
  169. {
  170. if(get_class($pinstance)==$pclassname)
  171. {
  172. $inst=$pinstance;
  173. break;
  174. }
  175. }
  176. }
  177. return $inst;
  178. }
  179. public function getPluginInstance($family,$order=-1)
  180. {
  181. if($order<0)
  182. {
  183. $order+=count($this->_activeplugins[$family]);
  184. }
  185. return $this->_activeplugins[$family][$order];
  186. }
  187. public function callPlugins($types,$callback,&$data=null,$params=null,$break=true)
  188. {
  189. $result=true;
  190. if(!is_array($types))
  191. {
  192. if($types!="*")
  193. {
  194. $types=explode(",",$types);
  195. }
  196. else
  197. {
  198. $types=array_keys($this->_activeplugins);
  199. }
  200. }
  201. foreach($types as $ptype)
  202. {
  203. if(isset($this->_activeplugins[$ptype]))
  204. {
  205. foreach($this->_activeplugins[$ptype] as $pinst)
  206. {
  207. if(method_exists($pinst,$callback))
  208. {
  209. $callres=($data==null?($params==null?$pinst->$callback():$pinst->$callback($params)):$pinst->$callback($data,$params));
  210. if($callres===false && $data!=null)
  211. {
  212. $result=false;
  213. }
  214. if(isset($this->_ploop_callbacks[$callback]))
  215. {
  216. $cb=$this->_ploop_callbacks[$callback];
  217. $this->$cb($pinst,$data,$result);
  218. }
  219. if($result===false && $break)
  220. {
  221. return $result;
  222. }
  223. }
  224. }
  225. }
  226. }
  227. return $result;
  228. }
  229. public function getParam($params,$pname,$default=null)
  230. {
  231. return isset($params[$pname])?$params[$pname]:$default;
  232. }
  233. public function setLogger($logger)
  234. {
  235. $this->logger=$logger;
  236. }
  237. /**
  238. * logging function
  239. * @param string $data : string to log
  240. * @param string $type : log type
  241. */
  242. public function microDateTime()
  243. {
  244. list($microSec, $timeStamp) = explode(" ", microtime());
  245. return date('Y-m-d h:i:', $timeStamp) . (date('s', $timeStamp) + $microSec);
  246. }
  247. public function log($data,$type="default",$logger=null)
  248. {
  249. $usedlogger=($logger==null?$this->logger:$logger);
  250. if(isset($usedlogger))
  251. {
  252. $usedlogger->log($data,$type);
  253. }
  254. }
  255. public function logException($e,$data="",$logger=null)
  256. {
  257. $this->trace($e,$data);
  258. $this->log($this->_excid.":".$e->getMessage()." - ".$data,"error",$logger);
  259. }
  260. public function getExceptionTrace($tk,&$traces)
  261. {
  262. $this->_excid++;
  263. $trstr="";
  264. foreach($traces as $trace)
  265. {
  266. if(isset($trace["file"]))
  267. {
  268. $fname=str_replace(dirname(dirname(__FILE__)),"",$trace["file"]);
  269. $trstr.= $fname.":".(isset($trace["line"])?$trace["line"]:"?")." - ";
  270. if(isset($trace["class"]))
  271. {
  272. $trstr.=$trace["class"]."->";
  273. if(isset($trace["function"]))
  274. {
  275. $trstr.=$trace["function"];
  276. }
  277. $trstr.="\n----------------------------------------\n";
  278. if(isset($trace["args"]))
  279. {
  280. $trstr.=print_r($trace["args"],true);
  281. }
  282. $trstr.="\n";
  283. }
  284. }
  285. }
  286. if(!isset($this->_exceptions[$tk]))
  287. {
  288. $this->_exceptions[$tk]=array(0,$this->_excid);
  289. }
  290. $this->_exceptions[$tk][0]++;
  291. $trstr="************************************\n$trstr";
  292. return array($trstr,$this->_exceptions[$tk][0]==1,$this->_exceptions[$tk][1]);
  293. }
  294. public function trace($e,$data="")
  295. {
  296. $traces=$e->getTrace();
  297. $tk=$e->getMessage();
  298. $traceinfo=$this->getExceptionTrace($tk,$traces);
  299. $f=fopen(Magmi_StateManager::getTraceFile(),"a");
  300. fwrite($f,"---- TRACE : $this->_excid -----\n");
  301. try
  302. {
  303. if($traceinfo[1]==true)
  304. {
  305. fwrite($f,$traceinfo[0]);
  306. fwrite($f,"+++++++++++++++++++++++++++++\nCONTEXT DUMP\n+++++++++++++++++++++++++++++\n");
  307. fwrite($f,print_r($this,true));
  308. fwrite($f,"\n+++++++++++++++++++++++++++++\nEND CONTEXT DUMP\n+++++++++++++++++++++++++++++\n");
  309. }
  310. else
  311. {
  312. $tnum=$traceinfo[2];
  313. fwrite($f,"Duplicated exception - same trace as TRACE : $tnum\n");
  314. }
  315. }
  316. catch(Exception $te)
  317. {
  318. fwrite($f,"Exception occured during trace:".$te->getMessage());
  319. }
  320. fwrite($f,"---- ENDTRACE : $this->_excid -----\n");
  321. fclose($f);
  322. }
  323. /**
  324. * Engine run method
  325. * @param array $params - run parameters
  326. */
  327. public final function run($params=array())
  328. {
  329. try
  330. {
  331. $f=fopen(Magmi_StateManager::getTraceFile(),"w");
  332. fclose($f);
  333. $enginf=$this->getEngineInfo();
  334. $this->log("Running {$enginf["name"]} v${enginf["version"]} by ${enginf["author"]}","startup");
  335. if(!$this->_initialized)
  336. {
  337. $this->initialize($params);
  338. }
  339. $this->connectToMagento();
  340. $this->engineInit($params);
  341. $this->engineRun($params);
  342. $this->disconnectFromMagento();
  343. }
  344. catch(Exception $e)
  345. {
  346. $this->disconnectFromMagento();
  347. $this->handleException($e);
  348. }
  349. }
  350. public function handleException($e)
  351. {
  352. $this->logException($e);
  353. if(method_exists($this, "onEngineException"))
  354. {
  355. $this->onEngineException($e);
  356. }
  357. }
  358. /**
  359. shortcut method for configuration properties get
  360. */
  361. public function getProp($sec,$val,$default=null)
  362. {
  363. return $this->_conf->get($sec,$val,$default);
  364. }
  365. /**
  366. * Initialize Connection with Magento Database
  367. */
  368. public function connectToMagento()
  369. {
  370. #get database infos from properties
  371. if(!$this->_connected)
  372. {
  373. $host=$this->getProp("DATABASE","host","localhost");
  374. $dbname=$this->getProp("DATABASE","dbname","magento");
  375. $user=$this->getProp("DATABASE","user");
  376. $pass=$this->getProp("DATABASE","password");
  377. $debug=$this->getProp("DATABASE","debug");
  378. $conn=$this->getProp("DATABASE","connectivity","net");
  379. $port=$this->getProp("DATABASE","port","3306");
  380. $socket=$this->getProp("DATABASE","unix_socket");
  381. $this->initDb($host,$dbname,$user,$pass,$port,$socket,$conn,$debug);
  382. //suggested by pastanislas
  383. $this->_db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY,true);
  384. }
  385. }
  386. /*
  387. * Disconnect Magento db
  388. */
  389. public function disconnectFromMagento()
  390. {
  391. if($this->_connected)
  392. {
  393. $this->exitDb();
  394. }
  395. }
  396. /**
  397. * returns prefixed table name
  398. * @param string $magname : magento base table name
  399. */
  400. public function tablename($magname)
  401. {
  402. return $this->tprefix!=""?$this->tprefix."$magname":$magname;
  403. }
  404. public abstract function engineInit($params);
  405. public abstract function engineRun($params);
  406. }