You are here:  » RegEx Rule Duplication


RegEx Rule Duplication

Submitted by ChrisNBC on Mon, 2016-06-27 21:44 in

Hi David,

I’m really having fun with the RegEx matching now!..., I have created over 4,000 rules (and the number will grow). A great number of the rules are quite similar with slight difference in the RegEx. I wondered if you could suggest a way I might add a ‘duplicate’ button alongside each RegEx rule which would duplicate the entry and then allow it to be edited and saved as a new rule? This would really help me speed up the RegEx rule creation going forward.

Additionally, please could you let me know if there is a way to identify which products in the product table have been matched to a RegEx rule.

Thanks in advance.

Best regards
Chris

Submitted by support on Tue, 2016-06-28 10:01

Hi Chris,

Here's a series of changes to cover both points - a new field would be required on the products table in order to indicate the Product Mapping by RegExp entry that derived the name which is no problem, and an existing entry can be copied with an intermediate script. First of all, create and run the following dbmod.php script from the top level to create the new `regexp` field on the products table:

<?php
  
require("includes/common.php");
  
$sql "ALTER TABLE `".$config_databaseTablePrefix."products`
            ADD `regexp` INT(11) NOT NULL"
;
  
database_queryModify($sql,$result);
  print 
"Done.";
?>

And also create the following new script admin/productsmap_regexp_copy.php

<?php
  
require("../includes/common.php");
  
$admin_checkPassword TRUE;
  require(
"../includes/admin.php");
  require(
"../includes/widget.php");
  
$submit = (isset($_POST["submit"])?$_POST["submit"]:"");
  
$id = (isset($_GET["id"])?$_GET["id"]:"");
  
$sql "SELECT * FROM `".$config_databaseTablePrefix."productsmap_regexp` WHERE id='".database_safe($id)."'";
  
database_querySelect($sql,$rows);
  
$copy $rows[0];
  if (
$submit == "Add")
  {
    
widget_required("name");
    if (!
widget_errorCount())
    {
      
$_POST["name"] = trim($_POST["name"]);
    }
    if (!
widget_errorCount())
    {
      
$sql "SELECT name FROM `".$config_databaseTablePrefix."productsmap_regexp` WHERE name='".database_safe($_POST["name"])."'";
      if (
database_querySelect($sql,$rows))
      {
        
widget_errorSet("name","mapping name already exists");
      }
    }
    if (!
widget_errorCount())
    {
      
$sql sprintf("INSERT INTO `".$config_databaseTablePrefix."productsmap_regexp` SET
                      `name` = '%s',
                      `trigger_merchant` = '%s',
                      `trigger_category` = '%s',
                      `trigger_brand` = '%s',
                      `regexp` = '%s',
                      `product_name` = '%s',
                      `category` = '%s',
                      `brand` = '%s'
                      "
,
                      
database_safe($_POST["name"]),
                      
database_safe($copy["trigger_merchant"]),
                      
database_safe($copy["trigger_category"]),
                      
database_safe($copy["trigger_brand"]),
                      
database_safe($copy["regexp"]),
                      
database_safe($copy["product_name"]),
                      
database_safe($copy["category"]),
                      
database_safe($copy["brand"])
                      );
      
database_queryModify($sql,$insertId);
      
header("Location: productsmap_regexp_configure.php?id=".$insertId);
      exit();
    }
  }
  else
  {
    
$_POST["name"] = $copy["name"];
  }
  require(
"admin_header.php");
  print 
"<h2>".translate("Product Mapping RegExp")."</h2>";
  print 
"<h3>".translate("Copy Mapping")."</h3>";
  
widget_formBegin();
  
widget_textBox("Name","name",TRUE,(isset($_POST["name"])?$_POST["name"]:""),"",3);
  
widget_formButtons(array("Add"=>TRUE));
  
widget_formEnd();
  require(
"admin_footer.php");
?>

- I recall that you asked in a previous thread about adding additional trigger conditions - if you have implemented this, modify the above as required by duplicating the code for copying trigger_brand in the $sql construction in the above.

Edit admin/productsmap_regexp.php and look for the following code at line 78:

      admin_tool("Configure","productsmap_regexp_configure.php?id=".$productmap["id"],TRUE,FALSE);

...and REPLACE with:

      admin_tool("Configure","productsmap_regexp_configure.php?id=".$productmap["id"],TRUE,FALSE);
      admin_tool("Copy","productsmap_regexp_copy.php?id=".$productmap["id"],TRUE,FALSE);
      admin_tool("Search",$config_baseHREF."search.php?q=regexp:".$productmap["id"],TRUE,FALSE);

Edit search.php and look for the following code at line 230:

      case "bw":

...and REPLACE with:

      case "regexp":
        $where = "`regexp` = '".database_safe($parts[1])."'";
        $sql = "SELECT SQL_CALC_FOUND_ROWS id,COUNT(id) AS numMerchants,MIN(price) as minPrice FROM `".$config_databaseTablePrefix."products` WHERE ".$where.$priceWhere." GROUP BY search_name";
        $orderBySelection = $orderByDefault;
        break;
      case "bw":

And finally edit includes/admin.php and look for the following code at line 333:

          $importRecord["name"] = $v["product_name"];

...and REPLACE with:

          $importRecord["name"] = $v["product_name"];
          $importRecord["regexp"] = $v["id"];

With the above applied you'll now have a "Copy" link next to each Product Mapping by RegExp entry - change the name as required on the next page and then click Add to create the new mapping. The Search link along side each entry will then (after the next import) show all products derived from that entry.

Cheers,
David.
--
PriceTapestry.com

Submitted by ChrisNBC on Tue, 2016-06-28 15:18

Hi David,

Thanks for the mod which is installed and working perfectly. It's going to be a real time saver.

Best regards
Chris