Exemplo n.º 1
0
 insert_label_page(1, $item, $id_artwork);
 // Other properties
 $tab_multi = array(31, 135, 136, 144, 170, 179, 180, 186, 195, 276, 361, 921, 941, 1639);
 for ($i = 0; $i < count($tab_multi); $i++) {
     if ($claims["P" . $tab_multi[$i]]) {
         foreach ($claims["P" . $tab_multi[$i]] as $value) {
             $val = intval($value["mainsnak"]["datavalue"]["value"]["numeric-id"]);
             $sql = "SELECT id FROM p" . $tab_multi[$i] . " WHERE qwd={$val}";
             $rep = mysqli_query($link, $sql);
             $newid = "";
             $found = false;
             if (mysqli_num_rows($rep) == 0) {
                 //Value of property inserted
                 $p18_str = img_qwd($val);
                 if ($p18_str != "") {
                     $p18 = id_commons($p18_str);
                 } else {
                     $p18 = 0;
                 }
                 $sql = "INSERT INTO p" . $tab_multi[$i] . " (qwd,P18) VALUES ({$val}," . $p18 . ")";
                 $rep = mysqli_query($link, $sql);
                 $sql = "SELECT id FROM p" . $tab_multi[$i] . " WHERE qwd={$val}";
                 $rep = mysqli_query($link, $sql);
                 $row = mysqli_fetch_assoc($rep);
                 $id_prop = $row['id'];
                 $newid = $id_prop;
                 //Labels of property inserted
                 insert_label_page($tab_multi[$i], $val, $id_prop);
             } else {
                 $row = mysqli_fetch_assoc($rep);
                 $id_prop = $row['id'];
Exemplo n.º 2
0
function parent_cherche($prop, $val_prop, $id_artw, $new_ids)
{
    global $link;
    //test if ($prop=="276") echo "\n parent_cherche($prop,$val_prop,$id_artw,$new_ids)";
    if ($new_ids == "") {
        // already exists
        $sql = "SELECT id, id_parent FROM p{$prop} WHERE qwd={$val_prop}";
        $rep = mysqli_query($link, $sql);
        $row = mysqli_fetch_assoc($rep);
        $id_parent = $row['id_parent'];
        $sql = "INSERT INTO artw_prop (prop,id_artw,id_prop) VALUES ({$prop},{$id_artw},{$id_parent})";
        //test if ($prop=="276") echo "\nINSERT INTO artw_prop (prop,id_artw,id_prop) VALUES ($prop,$id_artw,$id_parent)";
        $rep = mysqli_query($link, $sql);
        $sql = "SELECT qwd,level FROM p{$prop} WHERE id={$id_parent}";
        $rep = mysqli_query($link, $sql);
        $row = mysqli_fetch_assoc($rep);
        $qwd_parent = $row['qwd'];
        $level = $row['level'];
        //test if ($prop=="276") echo "\nlevel $level id_parent $id_parent($prop,$qwd_parent,$id_artw,)";
        if ($level != 0) {
            parent_cherche($prop, $qwd_parent, $id_artw, "");
        }
    } else {
        // new
        $dfic = get_WDjson($val_prop);
        $data_item = json_decode($dfic, true);
        $claims_qwd = $data_item["entities"]["Q" . $val_prop]["claims"];
        $nouv_qwd = "";
        if ($claims_qwd["P361"] && !$claims_qwd["P856"] || $claims_qwd["P276"] && !$claims_qwd["P856"]) {
            if ($claims_qwd["P361"]) {
                foreach ($claims_qwd["P361"] as $val) {
                    if ($nouv_qwd == "") {
                        $nouv_qwd = $val["mainsnak"]["datavalue"]["value"]["numeric-id"];
                    } else {
                        break;
                    }
                }
            }
            if ($claims_qwd["P276"]) {
                foreach ($claims_qwd["P276"] as $val) {
                    if ($nouv_qwd == "") {
                        $nouv_qwd = $val["mainsnak"]["datavalue"]["value"]["numeric-id"];
                    } else {
                        break;
                    }
                }
            }
            if ($nouv_qwd != $val_prop) {
                // security against infinite loop
                // parent found
                $sql = "SELECT id,level FROM p{$prop} WHERE qwd={$nouv_qwd}";
                $rep = mysqli_query($link, $sql);
                $nids = "";
                $level_found = -1;
                if (mysqli_num_rows($rep) == 0) {
                    //Value of property inserted
                    $p18_str = img_qwd($nouv_qwd);
                    if ($p18_str != "") {
                        $p18 = id_commons($p18_str);
                    } else {
                        $p18 = 0;
                    }
                    $sql = "INSERT INTO p{$prop} (qwd,P18) VALUES ({$nouv_qwd}," . $p18 . ")";
                    $rep = mysqli_query($link, $sql);
                    $sql = "SELECT id FROM p{$prop} WHERE qwd={$nouv_qwd}";
                    $rep = mysqli_query($link, $sql);
                    $row = mysqli_fetch_assoc($rep);
                    $id_prop = $row['id'];
                    $nids = $new_ids . "|" . $id_prop;
                    //Labels of property inserted
                    insert_label_page($prop, $nouv_qwd, $id_prop);
                } else {
                    $row = mysqli_fetch_assoc($rep);
                    $id_prop = $row['id'];
                    $level_found = $row['level'];
                }
                // Update levels of ids already found and update parent of lest id
                $tab_ids = explode("|", $new_ids);
                if ($nids != "") {
                    // new prop value
                    for ($i = 0; $i < count($tab_ids); $i++) {
                        $sql = "SELECT level FROM p{$prop} WHERE id=" . $tab_ids[$i];
                        $rep = mysqli_query($link, $sql);
                        $row = mysqli_fetch_assoc($rep);
                        $new_level = $row['level'] + 1;
                        $sql = "UPDATE p{$prop} SET level={$new_level} WHERE id=" . $tab_ids[$i];
                        $rep = mysqli_query($link, $sql);
                        if ($i == count($tab_ids) - 1) {
                            $sql = "UPDATE p{$prop} SET id_parent={$id_prop} WHERE id=" . $tab_ids[$i];
                            $rep = mysqli_query($link, $sql);
                        }
                    }
                } else {
                    $cpt = 1;
                    for ($i = count($tab_ids) - 1; $i > -1; $i--) {
                        $sql = "UPDATE p{$prop} SET level=" . ($level_found + $cpt) . " WHERE id=" . $tab_ids[$i];
                        $rep = mysqli_query($link, $sql);
                        if ($i == count($tab_ids) - 1) {
                            $sql = "UPDATE p{$prop} SET id_parent={$id_prop} WHERE id=" . $tab_ids[$i];
                            $rep = mysqli_query($link, $sql);
                        }
                        $cpt++;
                    }
                }
                $sql = "INSERT INTO artw_prop (prop,id_artw,id_prop) VALUES ({$prop},{$id_artw},{$id_prop})";
                //test if ($prop=="276") echo "\nINSERT INTO artw_prop (prop,id_artw,id_prop) VALUES ($prop,$id_artw,$id_prop)";
                $rep = mysqli_query($link, $sql);
                if ($level_found != 0) {
                    parent_cherche($prop, $nouv_qwd, $id_artw, $nids);
                }
            }
        }
    }
}