PMA\libraries\Table::duplicateInfo PHP Method

duplicateInfo() public static method

Inserts existing entries in a PMA_* table by reading a value from an old entry
public static duplicateInfo ( string $work, string $pma_table, array $get_fields, array $where_fields, array $new_fields ) : integer | boolean
$work string The array index, which Relation feature to check ('relwork', 'commwork', ...)
$pma_table string The array index, which PMA-table to update ('bookmark', 'relation', ...)
$get_fields array Which fields will be SELECT'ed from the old entry
$where_fields array Which fields will be used for the WHERE query (array('FIELDNAME' => 'FIELDVALUE'))
$new_fields array Which fields will be used as new VALUES. These are the important keys which differ from the old entry (array('FIELDNAME' => 'NEW FIELDVALUE'))
return integer | boolean
    public static function duplicateInfo($work, $pma_table, $get_fields, $where_fields, $new_fields)
    {
        $last_id = -1;
        if (!isset($GLOBALS['cfgRelation']) || !$GLOBALS['cfgRelation'][$work]) {
            return true;
        }
        $select_parts = array();
        $row_fields = array();
        foreach ($get_fields as $get_field) {
            $select_parts[] = Util::backquote($get_field);
            $row_fields[$get_field] = 'cc';
        }
        $where_parts = array();
        foreach ($where_fields as $_where => $_value) {
            $where_parts[] = Util::backquote($_where) . ' = \'' . $GLOBALS['dbi']->escapeString($_value) . '\'';
        }
        $new_parts = array();
        $new_value_parts = array();
        foreach ($new_fields as $_where => $_value) {
            $new_parts[] = Util::backquote($_where);
            $new_value_parts[] = $GLOBALS['dbi']->escapeString($_value);
        }
        $table_copy_query = '
            SELECT ' . implode(', ', $select_parts) . '
              FROM ' . Util::backquote($GLOBALS['cfgRelation']['db']) . '.' . Util::backquote($GLOBALS['cfgRelation'][$pma_table]) . '
             WHERE ' . implode(' AND ', $where_parts);
        // must use DatabaseInterface::QUERY_STORE here, since we execute
        // another query inside the loop
        $table_copy_rs = PMA_queryAsControlUser($table_copy_query, true, DatabaseInterface::QUERY_STORE);
        while ($table_copy_row = @$GLOBALS['dbi']->fetchAssoc($table_copy_rs)) {
            $value_parts = array();
            foreach ($table_copy_row as $_key => $_val) {
                if (isset($row_fields[$_key]) && $row_fields[$_key] == 'cc') {
                    $value_parts[] = $GLOBALS['dbi']->escapeString($_val);
                }
            }
            $new_table_query = 'INSERT IGNORE INTO ' . Util::backquote($GLOBALS['cfgRelation']['db']) . '.' . Util::backquote($GLOBALS['cfgRelation'][$pma_table]) . ' (' . implode(', ', $select_parts) . ', ' . implode(', ', $new_parts) . ') VALUES (\'' . implode('\', \'', $value_parts) . '\', \'' . implode('\', \'', $new_value_parts) . '\')';
            PMA_queryAsControlUser($new_table_query);
            $last_id = $GLOBALS['dbi']->insertId();
        }
        // end while
        $GLOBALS['dbi']->freeResult($table_copy_rs);
        return $last_id;
    }

Usage Example

Example #1
0
 /**
  * Test for duplicateInfo
  *
  * @return void
  */
 public function testDuplicateInfo()
 {
     $work = "PMA_work";
     $pma_table = "pma_table";
     $get_fields = array("filed0", "field6");
     $where_fields = array("field2", "filed5");
     $new_fields = array("field3", "filed4");
     $GLOBALS['cfgRelation'][$work] = true;
     $GLOBALS['cfgRelation']['db'] = "PMA_db";
     $GLOBALS['cfgRelation'][$pma_table] = "pma_table";
     $ret = Table::duplicateInfo($work, $pma_table, $get_fields, $where_fields, $new_fields);
     $this->assertEquals(true, $ret);
 }
All Usage Examples Of PMA\libraries\Table::duplicateInfo