Exemplo n.º 1
0
 private static function load_one($f)
 {
     if (!file_exists($f)) {
         return false;
     }
     $csv = new CsvParser(file_get_contents($f), CsvParser::TYPE_GUESS);
     $csv->set_comment_chars("%#");
     if ($req = $csv->next()) {
         if (array_search("name", $req) !== false) {
             $csv->set_header($req);
         } else {
             $csv->set_header(array("name", "html"));
             $csv->unshift($req);
         }
         while (($req = $csv->next()) !== false) {
             self::$messages[$req["name"]] = (object) $req;
         }
         return true;
     } else {
         return false;
     }
 }
Exemplo n.º 2
0
 function parse($text, $filename = null, $defaults = null, $alertf = null)
 {
     global $Conf;
     $this->filename = $filename;
     $this->astate->defaults = $defaults ?: array();
     $csv = new CsvParser($text, CsvParser::TYPE_GUESS);
     $csv->set_comment_chars("%#");
     $csv->set_comment_function(array($this, "parse_csv_comment"));
     if (!($req = $csv->next())) {
         return $this->error($csv->lineno(), "empty file");
     }
     if (!$this->install_csv_header($csv, $req)) {
         return false;
     }
     // parse file, load papers all at once
     $lines = $pids = [];
     while (($req = $csv->next()) !== false) {
         $lines[] = [$csv->lineno(), $req];
         $this->collect_papers($req, $pids, false);
     }
     if (count($pids)) {
         $this->astate->lineno = $csv->lineno();
         $this->astate->fetch_prows(array_keys($pids));
     }
     // now parse assignment
     foreach ($lines as $i => $linereq) {
         $this->astate->lineno = $linereq[0];
         if ($i % 100 == 0) {
             if ($alertf) {
                 call_user_func($alertf, $this, $linereq[0], $linereq[1]);
             }
             set_time_limit(30);
         }
         $this->apply($linereq[1]);
     }
     if ($alertf) {
         call_user_func($alertf, $this, $csv->lineno(), false);
     }
     $this->finish();
 }
Exemplo n.º 3
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;
}