ImportModel::processImportFile PHP Method

processImportFile() public method

public processImportFile ( ) : boolean
return boolean
    public function processImportFile()
    {
        // This one step can take a while so give it more time.
        increaseMaxExecutionTime(60 * 10);
        $Path = $this->ImportPath;
        $BasePath = dirname($Path) . DS . 'import';
        $Tables = array();
        if (!is_readable($Path)) {
            throw new Gdn_UserException(t('The input file is not readable.', 'The input file is not readable.  Please check permissions and try again.'));
        }
        if (!is_writeable($BasePath)) {
            throw new Gdn_UserException(sprintf(t('Data file directory (%s) is not writable.'), $BasePath));
        }
        // Open the import file.
        ini_set('auto_detect_line_endings', true);
        $fpin = gzopen($Path, 'rb');
        $fpout = null;
        // Make sure it has the proper header.
        try {
            $Header = $this->GetImportHeader($fpin);
        } catch (Exception $Ex) {
            fclose($fpin);
            throw $Ex;
        }
        $RowCount = 0;
        $LineNumber = 0;
        while (($Line = fgets($fpin)) !== false) {
            $LineNumber++;
            if ($Line == "\n") {
                if ($fpout) {
                    // We are in a table so close it off.
                    fclose($fpout);
                    $fpout = 0;
                }
            } elseif ($fpout) {
                // We are in a table so dump the line.
                fputs($fpout, $Line);
            } elseif (substr_compare(self::COMMENT, $Line, 0, strlen(self::COMMENT)) == 0) {
                // This is a comment line so do nothing.
            } else {
                // This is the start of a table.
                $TableInfo = $this->ParseInfoLine($Line);
                if (!array_key_exists('Table', $TableInfo)) {
                    throw new Gdn_UserException(sprintf(t('Could not parse import file. The problem is near line %s.'), $LineNumber));
                }
                $Table = $TableInfo['Table'];
                $tableSanitized = preg_replace('#[^A-Z0-9\\-_]#i', '_', $Table);
                $Path = $BasePath . DS . $tableSanitized . '.txt';
                $fpout = fopen($Path, 'wb');
                $TableInfo['Path'] = $Path;
                unset($TableInfo['Table']);
                // Get the column headers from the next line.
                if (($Line = fgets($fpin)) !== false) {
                    $LineNumber++;
                    // Strip \r out of line.
                    $Line = str_replace(array("\r\n", "\r"), array("\n", "\n"), $Line);
                    fwrite($fpout, $Line);
                    $Columns = $this->ParseInfoLine($Line);
                    $TableInfo['Columns'] = $Columns;
                    $Tables[$Table] = $TableInfo;
                }
            }
        }
        gzclose($fpin);
        if ($fpout) {
            fclose($fpout);
        }
        if (count($Tables) == 0) {
            throw new Gdn_UserException(t('The import file does not contain any data.'));
        }
        $this->Data['Tables'] = $Tables;
        return true;
    }