📄 entry.pm
字号:
# Copyright 2001-2005 Six Apart.# SCRiPTMAFiA 2005 - THE DiRTY HANDS ON YOUR SCRiPTS## $Id: Entry.pm 10197 2005-03-09 00:27:57Z ezra $package MT::Entry;use strict;use MT::Author;use MT::Category;use MT::Placement;use MT::Comment;use MT::Util qw( archive_file_for discover_tb start_end_period );use MT::Object;@MT::Entry::ISA = qw( MT::Object );__PACKAGE__->install_properties({ columns => [ 'id', 'blog_id', 'status', 'author_id', 'allow_comments', 'title', 'excerpt', 'text', 'text_more', 'convert_breaks', 'to_ping_urls', 'pinged_urls', 'allow_pings', 'keywords', 'tangent_cache', 'basename',## Have to keep this around for use in mt-upgrade.cgi. 'category_id', ], indexes => { blog_id => 1, status => 1, author_id => 1, created_on => 1, modified_on => 1, basename => 1, }, audit => 1, datasource => 'entry', primary_key => 'id',});use constant HOLD => 1;use constant RELEASE => 2;use constant REVIEW => 3;use constant FUTURE => 4;use Exporter;*import = \&Exporter::import;use vars qw( @EXPORT_OK %EXPORT_TAGS);@EXPORT_OK = qw( HOLD RELEASE FUTURE );%EXPORT_TAGS = (constants => [ qw(HOLD RELEASE FUTURE) ]);sub status_text { my $s = $_[0]; $s == HOLD ? "Draft" : $s == RELEASE ? "Publish" : $s == REVIEW ? "Review" : $s == FUTURE ? "Future" : '';}sub status_int { my $s = lc $_[0]; ## Lower-case it so that it's case-insensitive $s eq 'draft' ? HOLD : $s eq 'publish' ? RELEASE : $s eq 'review' ? REVIEW : $s eq 'future' ? FUTURE : undef;}sub next { my $entry = shift; my($publish_only) = @_; unless ($entry->{__next}) { $entry->{__next} = MT::Entry->load( { blog_id => $entry->blog_id, $publish_only ? (status => RELEASE) : () }, { limit => 1, 'sort' => 'created_on', direction => 'ascend', start_val => $entry->created_on }); } $entry->{__next};}sub previous { my $entry = shift; my($publish_only) = @_; unless ($entry->{__previous}) { $entry->{__previous} = MT::Entry->load( { blog_id => $entry->blog_id, $publish_only ? (status => RELEASE) : () }, { limit => 1, 'sort' => 'created_on', direction => 'descend', start_val => $entry->created_on }); } $entry->{__previous};}sub author { my $entry = shift; my $req = MT::Request->instance(); unless ($entry->{__author}) { my $author_cache = $req->stash('author_cache'); $entry->{__author} = $author_cache->{$entry->author_id}; unless ($entry->{__author}) { $entry->{__author} = MT::Author->load($entry->author_id); $author_cache->{$entry->author_id} = $entry->{__author}; $req->stash('author_cache', $author_cache); } } $entry->{__author};}## To speed up <$MTEntryCategory$> (and category-loading in general),## the first time either ->category or ->categories is used, we load the## list of placements (entry-category mappings) into memory into an## MT::Request object. Lookups to determine if an entry has a category are## thus very fast. We add a new config setting NoPlacementCache in case## this causes problems for anyone. :)sub _placement_cache { my($blog_id) = @_; my $r = MT::Request->instance; my $cache = $r->cache('all_placements'); unless ($cache->{$blog_id}) { $cache->{$blog_id} = {}; $r->cache('all_placements', $cache); require MT::Placement; my @p = MT::Placement->load({ blog_id => $blog_id }); for my $p (@p) { push @{ $cache->{$blog_id}{all}{$p->entry_id} }, $p->category_id; $cache->{$blog_id}{primary}{$p->entry_id} = $p->category_id if $p->is_primary; } } $cache->{$blog_id};}sub category { my $entry = shift; unless ($entry->{__category}) { unless (MT::ConfigMgr->instance->NoPlacementCache) { my $cache = _placement_cache($entry->blog_id); my $p = $cache->{primary}{$entry->id} or return; $entry->{__category} = MT::Category->load($p); } else { $entry->{__category} = MT::Category->load(undef, { 'join' => [ 'MT::Placement', 'category_id', { entry_id => $entry->id, is_primary => 1 } ] }, ); } } $entry->{__category};}sub categories { my $entry = shift; unless ($entry->{__categories}) { unless (MT::ConfigMgr->instance->NoPlacementCache) { my $cache = _placement_cache($entry->blog_id); my $p = $cache->{all}{$entry->id} or return; for my $place (@$p) { push @{ $entry->{__categories} }, MT::Category->load($place); } } else { $entry->{__categories} = [ MT::Category->load(undef, { 'join' => [ 'MT::Placement', 'category_id', { entry_id => $entry->id } ] }, ) ]; } $entry->{__categories} = [ sort { $a->label cmp $b->label } @{ $entry->{__categories} } ]; } $entry->{__categories};}sub is_in_category { my $entry = shift; my($cat) = @_; my $cats = $entry->categories; for my $c (@$cats) { return 1 if $c->id == $cat->id; } 0;}sub comments { my $entry = shift; unless ($entry->{__comments}) { $entry->{__comments} = [ MT::Comment->load({ entry_id => $entry->id }) ]; } $entry->{__comments};}sub comment_count { my $entry = shift; unless ($entry->{__comment_count}) { $entry->{__comment_count} = MT::Comment->count({ entry_id => $entry->id, visible => 1 }); } $entry->{__comment_count};}sub ping_count { my $entry = shift; unless ($entry->{__ping_count}) { require MT::Trackback; require MT::TBPing; my $tb = MT::Trackback->load({ entry_id => $entry->id }); $entry->{__ping_count} = $tb ? MT::TBPing->count({ tb_id => $tb->id }) : 0; } $entry->{__ping_count};}sub archive_file { my $entry = shift; my($at) = @_; my $blog = $entry->blog() || return $entry->error(MT->translate( "Load of blog failed: [_1]", MT::Blog->errstr)); unless ($at) { $at = $blog->archive_type_preferred || $blog->archive_type; return '' if !$at || $at eq 'None'; my %at = map { $_ => 1 } split /,/, $at; for my $tat (qw( Individual Daily Weekly Monthly Category )) { $at = $tat if $at{$tat}; } } archive_file_for($entry, $blog, $at);}sub archive_url { my $entry = shift; my $blog = $entry->blog() || return $entry->error(MT->translate( "Load of blog failed: [_1]", MT::Blog->errstr)); my $url = $blog->archive_url || ""; $url .= '/' unless $url =~ m!/$!; $url . $entry->archive_file(@_);}sub permalink { my $entry = shift; my $blog = $entry->blog() || return $entry->error(MT->translate( "Load of blog failed: [_1]", MT::Blog->errstr)); my $url = $entry->archive_url($_[0]); my $effective_archive_type = ($_[0] || $blog->archive_type_preferred || $blog->archive_type); $url .= '#' . ($_[1]->{valid_html} ? 'a' : '') . sprintf("%06d", $entry->id) unless ($effective_archive_type eq 'Individual' || $_[1]->{no_anchor}); $url;}sub all_permalinks { my $entry = shift; my $blog = $entry->blog || return $entry->error(MT->translate( "Load of blog failed: [_1]", MT::Blog->errstr)); my @at = split /,/, $blog->archive_type; return unless @at; my @urls; for my $at (@at) { push @urls, $entry->permalink($at); } @urls;}sub text_filters { my $entry = shift; my $filters = $entry->convert_breaks; if (!defined $filters) { my $blog = $entry->blog() || return []; $filters = $blog->convert_paras; } return [] unless $filters; if ($filters eq '1') { return [ '__default__' ]; } else { return [ split /\s*,\s*/, $filters ]; }}sub get_excerpt { my $entry = shift; my($words) = @_; return $entry->excerpt if $entry->excerpt; my $excerpt = MT->apply_text_filters($entry->text, $entry->text_filters); my $blog = $entry->blog() || return $entry->error(MT->translate( "Load of blog failed: [_1]", MT::Blog->errstr)); MT::Util::first_n_words($excerpt, $words || $blog->words_in_excerpt || 40) . '...';}*pinged_url_list = _mk_url_list_meth('pinged_urls');*to_ping_url_list = _mk_url_list_meth('to_ping_urls');sub _mk_url_list_meth { my($meth) = @_; sub { my $entry = shift; return [] unless $entry->$meth() && $entry->$meth() =~ /\S/; [ split /\r?\n/, $entry->$meth() ]; }}sub save { my $entry = shift; ## If there's no basename specified, create a unique basename. unless ($entry->basename) { my $name = MT::Util::make_unique_basename($entry); $entry->basename($name); } my $dt = $entry->created_on_obj; ## If we need to auto-discover TrackBack ping URLs, do that here. my $blog = $entry->blog(); if ($blog && $blog->autodiscover_links) { my $archive_url = $blog->archive_url; my %to_ping = map { $_ => 1 } @{ $entry->to_ping_url_list }; my %pinged = map { $_ => 1 } @{ $entry->pinged_url_list }; my $body = $entry->text . ($entry->text_more || ""); $body = MT->apply_text_filters($body, $entry->text_filters); while ($body =~ m!<a.*?href=(["']?)([^'">]+)\1!gsi) { my $url = $2;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -