importexport.pm

来自「1. 记录每个帖子的访问人情况」· PM 代码 · 共 508 行 · 第 1/2 页

PM
508
字号
                        my $dt = MT::Date->parse_date($val);                        $comment->created_on($dt->ts_utc);                    } else {                        ## Now we have reached the body of the comment;                        ## everything from here until the end of the                        ## array is body.                        $body_idx = $i;                        last COMMENT;                    }                    $i++;                }                $comment->text( join "\n", @lines[$body_idx..$#lines] );                push @comments, $comment;            }            elsif ($piece =~ s/^PING:\s*\r?\n//) {                ## Pings are: TITLE, URL, IP, DATE, BLOG NAME,                ## then excerpt                require MT::TBPing;                my $ping = MT::TBPing->new;                $ping->blog_id($blog_id);                my @lines = split /\r?\n/, $piece;                my($i, $body_idx) = (0) x 2;                PING:                for my $line (@lines) {                    $line =~ s!^\s*!!;                    my($key, $val) = split /\s*:\s*/, $line, 2;                    if ($key eq 'TITLE') {                        $ping->title($val);                    } elsif ($key eq 'URL') {                        $ping->source_url($val);                    } elsif ($key eq 'IP') {                        $ping->ip($val);                    } elsif ($key eq 'DATE') {                        my $dt = MT::Date->parse_date($val);                        $ping->created_on($dt->ts_utc);                    } elsif ($key eq 'BLOG NAME') {                        $ping->blog_name($val);                    } else {                        ## Now we have reached the ping excerpt;                        ## everything from here until the end of the                        ## array is body.                        $body_idx = $i;                        last PING;                    }                    $i++;                }                $ping->excerpt( join "\n", @lines[$body_idx..$#lines] );                push @pings, $ping;            }        }        ## Assign a title if one is not already assigned.        unless ($entry->title) {            my $body = $entry->text;            if ($t_start && $t_end && $body =~                s!\Q$t_start\E(.*?)\Q$t_end\E\s*!!s) {                (my $title = $1) =~ s/[\r\n]/ /g;                $entry->title($title);                $entry->text($body);            } else {                $entry->title( MT::Util::first_n_words($body, 5) );            }        }        ## If an entry has comments listed along with it, set        ## allow_comments to 1 no matter what the default is.        if (@comments && !$entry->allow_comments) {            $entry->allow_comments(1);        }        ## If an entry has TrackBack pings listed along with it,        ## set allow_pings to 1 no matter what the default is.        if (@pings) {            $entry->allow_pings(1);            ## If the entry has TrackBack pings, we need to make sure            ## that an MT::Trackback object is created. To do that, we            ## need to make sure that $entry->save is called.            $no_save = 0;        }        ## Save entry.        unless ($no_save) {            $cb->("Saving entry ('", $entry->title, "')... ");            if ($entry->save) {                $cb->("ok (ID ", $entry->id, ")\n");            } else {                $cb->("failed\n");                return $class->error(MT->translate(                    "Saving entry failed: [_1]", $entry->errstr));            }        }        ## Save placement.        ## If we have no primary category ID (from a PRIMARY CATEGORY        ## key), we first look to see if we have any placements from        ## CATEGORY tags. If so, we grab the first one and use it as the        ## primary placement. If not, we try to use the default category        ## ID specified.        if (!$primary_cat_id) {            if (@placements) {                $primary_cat_id = shift @placements;            } elsif ($def_cat_id) {                $primary_cat_id = $def_cat_id;            }        } else {            ## If a PRIMARY CATEGORY is also specified as a CATEGORY, we            ## don't want to add it twice; so we filter it out.            @placements = grep { $_ != $primary_cat_id } @placements;        }        ## So if we have a primary placement from any of the means        ## specified above, we add the placement.        if ($primary_cat_id) {            my $place = MT::Placement->new;            $place->is_primary(1);            $place->entry_id($entry->id);            $place->blog_id($blog_id);            $place->category_id($primary_cat_id);            $place->save                or return $class->error(MT->translate(                    "Saving placement failed: [_1]", $place->errstr));        }        ## Save placements.        for my $cat_id (@placements) {            my $place = MT::Placement->new;            $place->is_primary(0);            $place->entry_id($entry->id);            $place->blog_id($blog_id);            $place->category_id($cat_id);            $place->save                or return $class->error(MT->translate(                    "Saving placement failed: [_1]", $place->errstr));        }        ## Save comments.        for my $comment (@comments) {            $comment->entry_id($entry->id);            $cb->("Creating new comment ('", $comment->author,                 "')... ");            if ($comment->save) {                $cb->("ok (ID ", $comment->id, ")\n");            } else {                $cb->("failed\n");                return $class->error(MT->translate(                    "Saving comment failed: [_1]", $comment->errstr));            }        }        ## Save pings.        if (@pings) {            my $tb = MT::Trackback->load({ entry_id => $entry->id })                or return $class->error(MT->translate(                    "Entry has no MT::Trackback object!"));            for my $ping (@pings) {                $ping->tb_id($tb->id);                $cb->("Creating new ping ('", $ping->title,                 "')... ");                if ($ping->save) {                    $cb->("ok (ID ", $ping->id, ")\n");                } else {                    $cb->("failed\n");                    return $class->error(MT->translate(                        "Saving ping failed: [_1]", $ping->errstr));                }            }        }    }    1;}sub export {    my $class = shift;    my($blog, $cb) = @_;    $cb ||= sub { };    ## Make sure dates are in English.    $blog->language('en');    ## Create template for exporting a single entry    require MT::Template;    require MT::Template::Context;    my $tmpl = MT::Template->new;    $tmpl->name('Export Template');    $tmpl->text(<<'TEXT');AUTHOR: <$MTEntryAuthor strip_linefeeds="1"$>TITLE: <$MTEntryTitle strip_linefeeds="1"$>STATUS: <$MTEntryStatus strip_linefeeds="1"$>ALLOW COMMENTS: <$MTEntryFlag flag="allow_comments"$>CONVERT BREAKS: <$MTEntryFlag flag="convert_breaks"$>ALLOW PINGS: <$MTEntryFlag flag="allow_pings"$><MTIfNonEmpty tag="MTEntryCategory">PRIMARY CATEGORY: <$MTEntryCategory$></MTIfNonEmpty><MTEntryCategories>CATEGORY: <$MTCategoryLabel$></MTEntryCategories>DATE: <$MTEntryDate format="%m/%d/%Y %I:%M:%S %p"$>-----BODY:<$MTEntryBody convert_breaks="0"$>-----EXTENDED BODY:<$MTEntryMore convert_breaks="0"$>-----EXCERPT:<$MTEntryExcerpt no_generate="1" convert_breaks="0"$>-----KEYWORDS:<$MTEntryKeywords$>-----<MTComments>COMMENT:AUTHOR: <$MTCommentAuthor strip_linefeeds="1"$>EMAIL: <$MTCommentEmail strip_linefeeds="1"$>IP: <$MTCommentIP strip_linefeeds="1"$>URL: <$MTCommentURL strip_linefeeds="1"$>DATE: <$MTCommentDate format="%m/%d/%Y %I:%M:%S %p"$><$MTCommentBody convert_breaks="0"$>-----</MTComments><MTPings>PING:TITLE: <$MTPingTitle strip_linefeeds="1"$>URL: <$MTPingURL strip_linefeeds="1"$>IP: <$MTPingIP strip_linefeeds="1"$>BLOG NAME: <$MTPingBlogName strip_linefeeds="1"$>DATE: <$MTPingDate format="%m/%d/%Y %I:%M:%S %p"$><$MTPingExcerpt$>-----</MTPings>--------TEXT    my $iter = MT::Entry->load_iter({ blog_id => $blog->id },        { 'sort' => 'created_on', direction => 'ascend' });    while (my $entry = $iter->()) {        my $ctx = MT::Template::Context->new;        $ctx->stash('entry', $entry);        $ctx->stash('blog', $blog);        $ctx->stash('blog_id', $blog->id);	$tmpl->blog_id($blog->id);        $ctx->{current_timestamp} = $entry->created_on;        my $res = $tmpl->build($ctx)            or return $class->error(MT->translate(                "Export failed on entry '[_1]': [_2]", $entry->title,                $tmpl->errstr));        $cb->($res);    }    1;}1;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?