/importmag/plugins/utilities/customsql/customsql.php

https://gitlab.com/inglobe/mgt-clemente-css · PHP · 126 lines · 15 code · 4 blank · 107 comment · 0 complexity · 70e69a345d84f3fc23905a9549081230 MD5 · raw file

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