Пример #1
0
<pre><?php 
include_once 'lib.php';
// Apply ?
$apply = param($_GET, 'apply', false);
if ($apply !== false) {
    $apply = true;
}
// Loop
$query = query('SELECT * FROM card ORDER BY `id` DESC');
// Last added cards are managed first, in order not to wait before a die
$nb = 0;
while ($arr = mysql_fetch_array($query)) {
    $arr['text'] = card_text_sanitize($arr['text']);
    $attrs_obj = new attrs($arr);
    $attrs = json_encode($attrs_obj);
    if ($arr['attrs'] != $attrs) {
        $nb++;
        echo '<hr>' . $arr['name'];
        echo '<pre>-' . print_r(obj_diff(json_decode($arr['attrs']), $attrs_obj), true) . '</pre>';
        echo '<pre>+' . print_r(obj_diff($attrs_obj, json_decode($arr['attrs'])), true) . '</pre>';
        if ($apply) {
            query("UPDATE\n\t\t\t\tcard\n\t\t\tSET\n\t\t\t\t`attrs` = '" . mysql_escape_string($attrs) . "'\n\t\t\t\t, `text` = '" . mysql_escape_string($arr['text']) . "'\n\t\t\tWHERE\n\t\t\t\t`id` = '" . $arr['id'] . "'\n\t\t\t; ");
        }
    }
}
die($nb . ' updates <a href="?apply=1">apply</a>');
Пример #2
0
function obj_diff($new, $old)
{
    // Returns only properties that changed between new and old value
    $result = new stdClass();
    foreach ($new as $key => $value) {
        // Each value in new
        if (isset($old->{$key})) {
            // Key is present in old
            $oldvalue = $old->{$key};
            switch (gettype($value)) {
                case 'object':
                    $result->{$key} = obj_diff($value, $oldvalue);
                    break;
                case 'array':
                    $a = arr_diff($value, $oldvalue);
                    if (count($a) > 0) {
                        $result->{$key} = $a;
                    }
                    break;
                default:
                    if ($value != $old->{$key}) {
                        $result->{$key} = $value;
                    }
            }
        } else {
            // Key not present in old
            $result->{$key} = $new->{$key};
        }
        // Set as new value
    }
    if (count(get_object_vars($result)) == 0) {
        $result = null;
    }
    return $result;
}