PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/magmi/plugins/utilities/customsql/customsql.php

https://bitbucket.org/jit_bec/shopifine
PHP | 146 lines | 17 code | 5 blank | 124 comment | 0 complexity | 443f3360b96c952fa9556c3bcb50eb99 MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. class CustomSQLUtility extends Magmi_UtilityPlugin
  3. {
  4. protected $_magdbh;
  5. public function getPluginInfo()
  6. {
  7. return array("name"=>"Custom Magento SQL Utility",
  8. "author"=>"Dweeves",
  9. "version"=>"1.0.3");
  10. }
  11. public function getShortDescription()
  12. {
  13. return "This Utility enables to perform custom parameterized sql scripts on magento DB";
  14. }
  15. public function getRequestFileList()
  16. {
  17. $files=glob(dirname(__file__)."/prequests/*.sql");
  18. return $files;
  19. }
  20. public function getRequestParameters($file,$noprefix=false)
  21. {
  22. $params=array();
  23. $content=file_get_contents($file);
  24. $hasp=preg_match_all('|\[\[(.*?)\]\]|msi',$content,$matches);
  25. if($hasp)
  26. {
  27. $params=$matches[1];
  28. }
  29. $outparams=array();
  30. foreach($params as $param)
  31. {
  32. $pinfo=explode("/",$param);
  33. $plabel=(count($pinfo)==1)?$pinfo[0]:$pinfo[1];
  34. $pdefault=(count($pinfo)>2?$pinfo[2]:"");
  35. $pname=$pinfo[0];
  36. $epar=explode(":",$pname);
  37. $addit=$noprefix && count($epar)==1 || !$noprefix;
  38. if($addit)
  39. {
  40. $outparams[$plabel]=array("name"=>$pname,"default"=>$pdefault);
  41. }
  42. }
  43. return $outparams;
  44. }
  45. public function fillPrefixedParameters($stmt,&$params)
  46. {
  47. $pparams=array();
  48. $hasnps=preg_match_all('|\[\[(\w+?)/.*?\]\]|msi',$stmt,$matches);
  49. if($hasnps)
  50. {
  51. $namedparams=$matches[1];
  52. foreach($params as $k=>$v)
  53. {
  54. if(!in_array($k,$namedparams))
  55. {
  56. unset($params[$k]);
  57. }
  58. }
  59. }
  60. $hasp=preg_match_all('|\[\[(tn:.*?)\]\]|msi',$stmt,$matches);
  61. if($hasp)
  62. {
  63. $pparams=$matches[1];
  64. }
  65. foreach($pparams as $pparam)
  66. {
  67. $info=explode(":",$pparam);
  68. switch($info[0]){
  69. case "tn":
  70. $params[$pparam]=$this->tablename($info[1]);
  71. }
  72. }
  73. }
  74. public function getRequestInfo($file)
  75. {
  76. if(file_exists("$file.desc"))
  77. {
  78. return file_get_contents("$file.desc");
  79. }
  80. else
  81. {
  82. return basename($file);
  83. }
  84. }
  85. public function getPluginParams($params)
  86. {
  87. $pp=array();
  88. foreach($params as $k=>$v)
  89. {
  90. if(preg_match("/^UTCSQL:.*$/",$k))
  91. {
  92. $pp[$k]=$v;
  93. }
  94. }
  95. return $pp;
  96. }
  97. public function runUtility()
  98. {
  99. $this->connectToMagento();
  100. $params=$this->getPluginParams($this->_params);
  101. $this->persistParams($params);
  102. $rqfile=$params["UTCSQL:queryfile"];
  103. unset($params["UTCSQL:queryfile"]);
  104. $sql=file_get_contents($rqfile);
  105. $rparams=array();
  106. foreach($params as $pname=>$pval)
  107. {
  108. $rpname=substr($pname,strlen("UTCSQL:"));
  109. $rparams[$rpname]=$pval;
  110. }
  111. $this->fillPrefixedParameters($sql,$rparams);
  112. $results=$this->multipleParamRequests($sql,$rparams,true);
  113. foreach($results as $rq=>$res)
  114. {
  115. if(count($res)==0)
  116. {
  117. $this->log("No records found","info");
  118. }
  119. else
  120. {
  121. for($i=0;$i<count($res);$i++)
  122. {
  123. $str="";
  124. foreach($res[$i] as $k=>$v)
  125. {
  126. $str.="$k=$v;";
  127. }
  128. $this->log($str,"info");
  129. }
  130. }
  131. }
  132. $this->disconnectFromMagento();
  133. }
  134. }