Esempio n. 1
0
function parseBulkFile($text, $filename)
{
    global $Conf;
    $text = cleannl($text);
    if (!is_valid_utf8($text)) {
        $text = windows_1252_to_utf8($text);
    }
    $filename = $filename ? "{$filename}:" : "line ";
    $success = array();
    if (!preg_match('/\\A[^\\r\\n]*(?:,|\\A)(?:user|email)(?:[,\\r\\n]|\\z)/', $text) && !preg_match('/\\A[^\\r\\n]*,[^\\r\\n]*,/', $text)) {
        $tarr = CsvParser::split_lines($text);
        foreach ($tarr as &$t) {
            if (($t = trim($t)) && $t[0] !== "#" && $t[0] !== "%") {
                $t = CsvGenerator::quote($t);
            }
            $t .= "\n";
        }
        unset($t);
        $text = join("", $tarr);
    }
    $csv = new CsvParser($text);
    $csv->set_comment_chars("#%");
    $line = $csv->next();
    if ($line && (array_search("email", $line) !== false || array_search("user", $line) !== false)) {
        $csv->set_header($line);
    } else {
        $csv->set_header(array("user"));
        $csv->unshift($line);
    }
    $cj_template = (object) array();
    $topic_revmap = array();
    foreach ($Conf->topic_map() as $id => $name) {
        $topic_revmap[strtolower($name)] = $id;
    }
    $unknown_topics = array();
    $errors = array();
    while (($line = $csv->next()) !== false) {
        $cj = clone $cj_template;
        foreach ($line as $k => $v) {
            $cj->{$k} = $v;
        }
        foreach (array("firstname" => "firstName", "first" => "firstName", "lastname" => "lastName", "last" => "lastName", "fullname" => "name", "fullName" => "name", "voice" => "voicePhoneNumber", "phone" => "voicePhoneNumber", "address1" => "addressLine1", "province" => "state", "region" => "state", "address2" => "addressLine2", "postalcode" => "zipCode", "zip" => "zipCode", "tags" => "contactTags") as $k => $x) {
            if (isset($cj->{$k}) && !isset($cj->{$x})) {
                $cj->{$x} = $cj->{$k};
            }
        }
        // thou shalt not set passwords by bulk update
        unset($cj->password, $cj->password_plaintext, $cj->new_password);
        if (isset($cj->name) && !isset($cj->firstName) && !isset($cj->lastName)) {
            list($cj->firstName, $cj->lastName) = Text::split_name($cj->name);
        }
        if (count($topic_revmap)) {
            foreach (array_keys($line) as $k) {
                if (preg_match('/^topic:\\s*(.*?)\\s*$/i', $k, $m)) {
                    if (($ti = @$topic_revmap[strtolower($m[1])]) !== null) {
                        $x = $line[$k];
                        if (strtolower($x) === "low") {
                            $x = -2;
                        } else {
                            if (strtolower($x) === "high") {
                                $x = 4;
                            } else {
                                if (!is_numeric($x)) {
                                    $x = 0;
                                }
                            }
                        }
                        if (!@$cj->topics) {
                            $cj->topics = (object) array();
                        }
                        $cj->topics->{$ti} = $x;
                    } else {
                        $unknown_topics[$m[1]] = true;
                    }
                }
            }
        }
        $cj->id = "new";
        $ustatus = new UserStatus(array("send_email" => true, "no_deprivilege_self" => true));
        if ($saved_user = save_user($cj, $ustatus, null, true)) {
            $success[] = "<a href=\"" . hoturl("profile", "u=" . urlencode($saved_user->email)) . "\">" . Text::user_html_nolink($saved_user) . "</a>";
        }
        foreach ($ustatus->error_messages() as $e) {
            $errors[] = "<span class='lineno'>" . $filename . $csv->lineno() . ":</span> " . $e;
        }
    }
    if (count($unknown_topics)) {
        $errors[] = "There were unrecognized topics (" . htmlspecialchars(commajoin($unknown_topics)) . ").";
    }
    if (count($success) == 1) {
        $successMsg = "Saved account " . $success[0] . ".";
    } else {
        if (count($success)) {
            $successMsg = "Saved " . plural($success, "account") . ": " . commajoin($success) . ".";
        }
    }
    if (count($errors)) {
        $errorMsg = "<div class='parseerr'><p>" . join("</p>\n<p>", $errors) . "</p></div>";
    }
    if (count($success) && count($errors)) {
        $Conf->confirmMsg($successMsg . "<br />{$errorMsg}");
    } else {
        if (count($success)) {
            $Conf->confirmMsg($successMsg);
        } else {
            if (count($errors)) {
                Conf::msg_error($errorMsg);
            } else {
                $Conf->warnMsg("Nothing to do.");
            }
        }
    }
    return count($errors) == 0;
}
 public function parse_dimacs($str)
 {
     $this->reset();
     $vnames = array();
     $ismax = null;
     $next_cap = $next_cost = null;
     $has_edges = false;
     foreach (CsvParser::split_lines($str) as $lineno => $line) {
         if ($line[0] !== "f") {
             $next_cap = $next_cost = null;
         }
         if (preg_match('/\\An (\\d+) (-?\\d+|s|t)\\s*\\z/', $line, $m)) {
             $issink = $m[2] === "t" || $m[2] < 0;
             assert(!get($vnames, $m[1]));
             $vnames[$m[1]] = $v = $issink ? $this->sink : $this->source;
             if ($m[2] !== "s" && $m[2] !== "t") {
                 $v->excess = (int) $m[2];
                 $this->maxflow = abs($v->excess);
             }
         } else {
             if (preg_match('/\\Ac ninfo (\\d+) (\\S+)\\s*(\\S*)\\s*\\z/', $line, $m)) {
                 $this->dimacs_node($vnames, $m[1], $m[2], $m[3]);
             } else {
                 if (preg_match('/\\Ac nprice (\\d+) (\\S+)\\s*\\z/', $line, $m) && is_numeric($m[2])) {
                     $v = $this->dimacs_node($vnames, $m[1]);
                     $v->price = (double) $m[2];
                 } else {
                     if (preg_match('/\\Aa (\\d+) (\\d+) (\\d+)\\s*\\z/', $line, $m)) {
                         assert(!$has_edges);
                         $this->add_edge($this->dimacs_node($vnames, $m[1]), $this->dimacs_node($vnames, $m[2]), (int) $m[3], 0);
                     } else {
                         if (preg_match('/\\Aa (\\d+) (\\d+) (\\d+) (\\d+) (-?\\d+)\\s*\\z/', $line, $m)) {
                             assert(!$has_edges);
                             $this->add_edge($this->dimacs_node($vnames, $m[1]), $this->dimacs_node($vnames, $m[2]), (int) $m[4], (int) $m[5], (int) $m[3]);
                         } else {
                             if (preg_match('/\\Ac finfo (\\d+)\\s*(|-?\\d+)\\s*\\z/', $line, $m)) {
                                 $next_cap = (int) $m[1];
                                 $next_cost = (int) $m[2];
                             } else {
                                 if (preg_match('/\\Af (\\d+) (\\d+) (-?\\d+)\\s*\\z/', $line, $m)) {
                                     if (!$has_edges) {
                                         $this->initialize_edges();
                                         $has_edges = true;
                                     }
                                     $src = $this->dimacs_node($vnames, $m[1]);
                                     $dst = $this->dimacs_node($vnames, $m[2]);
                                     $found = false;
                                     foreach ($src->e as $e) {
                                         if ($e->dst === $dst && ($next_cap === null || $e->cap === $next_cap) && ($next_cost === null || $e->cost === $next_cost)) {
                                             $e->flow = (int) $m[3];
                                             $src->excess -= $e->flow;
                                             $dst->excess += $e->flow;
                                             $found = true;
                                             break;
                                         }
                                     }
                                     if (!$found) {
                                         error_log("MinCostMaxFlow::parse_dimacs: line " . ($lineno + 1) . ": no such edge");
                                     }
                                     $next_cap = $next_cost = null;
                                 } else {
                                     if (preg_match('/\\As (\\d+)\\s*\\z/', $line, $m) && $this->source->excess === 0) {
                                         $this->source->excess = -(int) $m[1];
                                         $this->sink->excess = (int) $m[1];
                                         $this->maxflow = (int) $m[1];
                                     } else {
                                         if (preg_match('/\\Ac min_epsilon (\\S+)\\s*\\z/', $line, $m) && is_numeric($m[1])) {
                                             $this->epsilon = (double) $m[1];
                                         } else {
                                             if ($line[0] === "a" || $line[0] === "f") {
                                                 error_log("MinCostMaxFlow::parse_dimacs: line " . ($lineno + 1) . ": parse error");
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     ksort($vnames, SORT_NUMERIC);
     $this->v = array_values($vnames);
 }
Esempio n. 3
0
// Dbl::format_query tests
xassert_eqq(Dbl::format_query("Hello"), "Hello");
xassert_eqq(Dbl::format_query("Hello??"), "Hello?");
xassert_eqq(Dbl::format_query("Hello????"), "Hello??");
xassert_eqq(Dbl::format_query("select ?, ?, ?, ?s, ?s, ?s, ?", 1, "a", null, 2, "b", null, 3), "select 1, 'a', NULL, 2, b, , 3");
xassert_eqq(Dbl::format_query_apply("select ?, ?, ?, ?s, ?s, ?s, ?", array(1, "a", null, 2, "b", null, 3)), "select 1, 'a', NULL, 2, b, , 3");
xassert_eqq(Dbl::format_query_apply("select ?{2}, ?{1}, ?, ?s, ?s, ?s, ?", array(1, "a", null, 2, "b", null, 3)), "select 'a', 1, NULL, 2, b, , 3");
xassert_eqq(Dbl::format_query_apply("select ?{2}, ?{1}, ?{ab}, ?{2}s, ?{1}s, ?{ab}s, ?", array(1, "a", "ab" => "Woah", "Leftover")), "select 'a', 1, 'Woah', a, 1, Woah, 'Leftover'");
// Csv::split_lines tests
xassert_array_eqq(CsvParser::split_lines(""), array());
xassert_array_eqq(CsvParser::split_lines("\r"), array("\r"));
xassert_array_eqq(CsvParser::split_lines("\n"), array("\n"));
xassert_array_eqq(CsvParser::split_lines("\r\n"), array("\r\n"));
xassert_array_eqq(CsvParser::split_lines("\r\r\n"), array("\r", "\r\n"));
xassert_array_eqq(CsvParser::split_lines("\r\naaa"), array("\r\n", "aaa"));
xassert_array_eqq(CsvParser::split_lines("\na\r\nb\rc\n"), array("\n", "a\r\n", "b\r", "c\n"));
// random PHP behavior tests
if (PHP_MAJOR_VERSION >= 7) {
    xassert_eqq(substr("", 0, 1), "");
} else {
    xassert_eqq(substr("", 0, 1), false);
}
$s = "";
xassert_eqq(@$s[0], "");
// Json tests
xassert_eqq(json_encode(Json::decode("{}")), "{}");
xassert_eqq(json_encode(Json::decode('"\\u0030"')), '"0"');
xassert_eqq(Json::encode("\n"), '"\\n"');
xassert_eqq(Json::encode(""), '"\\u0007"');
xassert_eqq(json_encode(Json::decode('{"1":"1"}')), '{"1":"1"}');
$x = Json::decode_landmarks('{