Exemplo n.º 1
0
/**
 * Imports articles and comments from a Movable Type dump file.
 *
 * This function parses a file in the 'MovableType Import Format'.
 * The data isn't interpreted at all, just parsed into a
 * structure.
 *
 * This function supports importing comments and articles
 * from MovableType.
 *
 * Returns results as a HTML formatted list.
 *
 * @param  string $file    Path to the dump file
 * @param  string $section The article section
 * @param  string $status  The article status
 * @param  string $invite  The comments invite
 * @return string HTML
 * @see    http://www.movabletype.org/docs/mtimport.html
 */
function doImportMT($file, $section, $status, $invite)
{
    ini_set('auto_detect_line_endings', 1);
    $fp = fopen($file, 'r');
    if (!$fp) {
        return false;
    }
    // Keep some response on some part.
    $results = array();
    $state = 'metadata';
    $item = array();
    while (!feof($fp)) {
        $line = rtrim(fgets($fp, 8192));
        // The states suggested by the spec are inconsisent, but we'll do our
        // best to fake it.
        if ($line == '--------') {
            # End of an item, so we can process it
            $results[] = import_mt_item($item, $section, $status, $invite);
            $item = array();
            $state = 'metadata';
        } elseif ($line == '-----' and $state == 'metadata') {
            $state = 'multiline';
            $multiline_type = '';
        } elseif ($line == '-----' and $state == 'multiline') {
            if (!empty($multiline_type)) {
                $item[$multiline_type][] = import_mt_utf8($multiline_data);
            }
            $state = 'multiline';
            $multiline_type = '';
        } elseif ($state == 'metadata') {
            if (preg_match('/^([A-Z ]+):\\s*(.*)$/', $line, $match)) {
                $item[$match[1]] = import_mt_utf8($match[2]);
            }
        } elseif ($state == 'multiline' and empty($multiline_type)) {
            if (preg_match('/^([A-Z ]+):\\s*$/', $line, $match)) {
                $multiline_type = $match[1];
                $multiline_data = array();
            }
        } elseif ($state == 'multiline') {
            // Here's where things get hinky. Rather than put the multiline
            // metadata before the field name, it goes after, with no clear
            // separation between metadata and data. And either the metadata
            // or data might be missing.
            if (empty($multiline_data['content']) and preg_match('/^([A-Z ]+):\\s*(.*)$/', $line, $match)) {
                // Metadata within the multiline field.
                $multiline_data[$match[1]] = import_mt_utf8($match[2]);
            } elseif (empty($multiline_data['content'])) {
                $multiline_data['content'] = import_mt_utf8($line . "\n");
            } else {
                $multiline_data['content'] .= import_mt_utf8($line . "\n");
            }
        }
    }
    // Catch the last item in the file, if it doesn't end with a separator.
    if (!empty($item)) {
        $results[] = import_mt_item($item, $section, $status, $invite, $blogid);
    }
    fclose($fp);
    return join('<br />', $results);
}
Exemplo n.º 2
0
function doImportMT($file, $section, $status, $invite)
{
    # Parse a file in the MT Import Format, as described here:
    # http://www.movabletype.org/docs/mtimport.html
    # This doesn't interpret the data at all, just parse it into
    # a structure.
    ini_set('auto_detect_line_endings', 1);
    $fp = fopen($file, 'r');
    if (!$fp) {
        return false;
    }
    //Keep some response on some part
    $results = array();
    $state = 'metadata';
    $item = array();
    while (!feof($fp)) {
        $line = rtrim(fgets($fp, 8192));
        # The states suggested by the spec are inconsisent, but
        # we'll do our best to fake it
        if ($line == '--------') {
            # End of an item, so we can process it
            $results[] = import_mt_item($item, $section, $status, $invite);
            $item = array();
            $state = 'metadata';
        } elseif ($line == '-----' and $state == 'metadata') {
            $state = 'multiline';
            $multiline_type = '';
        } elseif ($line == '-----' and $state == 'multiline') {
            if (!empty($multiline_type)) {
                $item[$multiline_type][] = import_mt_utf8($multiline_data);
            }
            $state = 'multiline';
            $multiline_type = '';
        } elseif ($state == 'metadata') {
            if (preg_match('/^([A-Z ]+):\\s*(.*)$/', $line, $match)) {
                $item[$match[1]] = import_mt_utf8($match[2]);
            }
        } elseif ($state == 'multiline' and empty($multiline_type)) {
            if (preg_match('/^([A-Z ]+):\\s*$/', $line, $match)) {
                $multiline_type = $match[1];
                $multiline_data = array();
            }
        } elseif ($state == 'multiline') {
            # Here's where things get hinky.  Rather than put the
            # multiline metadata before the field name, it goes
            # after, with no clear separation between metadata
            # and data.  And either the metadata or data might be
            # missing.
            if (empty($multiline_data['content']) and preg_match('/^([A-Z ]+):\\s*(.*)$/', $line, $match)) {
                # Metadata within the multiline field
                $multiline_data[$match[1]] = import_mt_utf8($match[2]);
            } elseif (empty($multiline_data['content'])) {
                $multiline_data['content'] = import_mt_utf8($line . "\n");
            } else {
                $multiline_data['content'] .= import_mt_utf8($line . "\n");
            }
        }
    }
    # Catch the last item in the file, if it doesn't end with a separator
    if (!empty($item)) {
        $results[] = import_mt_item($item, $section, $status, $invite, $blogid);
    }
    fclose($fp);
    return join('<br />', $results);
}