| 1 | # Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd. |
|---|
| 2 | # This program is distributed under the terms of the |
|---|
| 3 | # GNU General Public License, version 2. |
|---|
| 4 | # |
|---|
| 5 | # $Id$ |
|---|
| 6 | |
|---|
| 7 | package MT::FileInfo; |
|---|
| 8 | use strict; |
|---|
| 9 | |
|---|
| 10 | use base qw(MT::Object); |
|---|
| 11 | |
|---|
| 12 | __PACKAGE__->install_properties({ |
|---|
| 13 | column_defs => { |
|---|
| 14 | 'id' => 'integer not null auto_increment', |
|---|
| 15 | 'blog_id' => 'integer not null', |
|---|
| 16 | 'entry_id' => 'integer', |
|---|
| 17 | 'url' => 'string(255)', |
|---|
| 18 | 'file_path' => 'text', |
|---|
| 19 | 'templatemap_id' => 'integer', |
|---|
| 20 | 'template_id' => 'integer', |
|---|
| 21 | 'archive_type' => 'string(255)', |
|---|
| 22 | 'category_id' => 'integer', |
|---|
| 23 | 'author_id' => 'integer', |
|---|
| 24 | 'startdate' => 'string(80)', |
|---|
| 25 | 'virtual' => 'boolean', |
|---|
| 26 | }, |
|---|
| 27 | indexes => { |
|---|
| 28 | blog_id => 1, |
|---|
| 29 | entry_id => 1, |
|---|
| 30 | template_id => 1, |
|---|
| 31 | templatemap_id => 1, |
|---|
| 32 | archive_type => 1, |
|---|
| 33 | url => 1, |
|---|
| 34 | startdate => 1, |
|---|
| 35 | category_id => 1, |
|---|
| 36 | author_id => 1, |
|---|
| 37 | }, |
|---|
| 38 | datasource => 'fileinfo', |
|---|
| 39 | primary_key => 'id', |
|---|
| 40 | cacheable => 0, |
|---|
| 41 | }); |
|---|
| 42 | |
|---|
| 43 | =pod |
|---|
| 44 | |
|---|
| 45 | =head1 NAME |
|---|
| 46 | |
|---|
| 47 | MT::FileInfo |
|---|
| 48 | |
|---|
| 49 | =head1 SYNOPSIS |
|---|
| 50 | |
|---|
| 51 | A FileInfo gives you information about a certain file or |
|---|
| 52 | server-relative URL (it assumes a one-to-one mapping between files and |
|---|
| 53 | URLs). |
|---|
| 54 | |
|---|
| 55 | More precisely A FileInfo maps a file path to a "Specifier" which |
|---|
| 56 | tells what kind of file it is and how to rebuild it; also included is |
|---|
| 57 | the URL where it is expected to be served. |
|---|
| 58 | |
|---|
| 59 | A Specifier is a variant record with the following types and fields: |
|---|
| 60 | |
|---|
| 61 | Index of template_id |
|---|
| 62 | | Entry of templatemap_id * entry_id |
|---|
| 63 | | DateBased of templatemap_id * date_spec * archive_type |
|---|
| 64 | | Category of templatemap_id * category_id |
|---|
| 65 | |
|---|
| 66 | A FileInfo is tagged with an archive_type field (from the set |
|---|
| 67 | {Individual, Daily, Monthly, Weekly, Category, Index}), which |
|---|
| 68 | determines which of those variants is held in the record. |
|---|
| 69 | |
|---|
| 70 | =head1 METHODS |
|---|
| 71 | |
|---|
| 72 | =over 4 |
|---|
| 73 | |
|---|
| 74 | =item set_info_for_url($url, $file_path, $archive_type, $spec) |
|---|
| 75 | |
|---|
| 76 | Creates a FileInfo record in the database to indicate that the URL |
|---|
| 77 | C<$url> serves the file at C<$file_path> and can be rebuilt from the |
|---|
| 78 | parameters in C<$spec>. |
|---|
| 79 | |
|---|
| 80 | The C<$spec> argument is a hash ref with at least one of {TemplateMap, |
|---|
| 81 | Template} defined, and at least one of the following: {Entry, |
|---|
| 82 | StartDate, Category}. |
|---|
| 83 | |
|---|
| 84 | If $archive_type is 'index' then Template should be given; otherwise |
|---|
| 85 | TemplateMap should be given. |
|---|
| 86 | |
|---|
| 87 | =back |
|---|
| 88 | |
|---|
| 89 | =cut |
|---|
| 90 | |
|---|
| 91 | # think: We need a set({a => v, ...}, {b => u, ...}) routine which |
|---|
| 92 | # guarantees that subsequently load({a => v, ...}) will return a |
|---|
| 93 | # record that matches {b => u} |
|---|
| 94 | |
|---|
| 95 | sub set_info_for_url { |
|---|
| 96 | my $class = shift; |
|---|
| 97 | my ($url, $file_path, $archive_type, $args) = @_; |
|---|
| 98 | my $url_map = MT::FileInfo->new(); |
|---|
| 99 | $url_map->blog_id($args->{Blog}); |
|---|
| 100 | if ($archive_type eq 'index') { |
|---|
| 101 | $url_map->template_id($args->{Template}); |
|---|
| 102 | } else { |
|---|
| 103 | $url_map->templatemap_id($args->{TemplateMap}) if $args->{TemplateMap}; |
|---|
| 104 | $url_map->template_id($args->{Template}) if $args->{Template}; |
|---|
| 105 | $args->{Entry} = $args->{Entry}->id if ref $args->{Entry}; |
|---|
| 106 | $url_map->entry_id($args->{Entry}) if $args->{Entry}; |
|---|
| 107 | $url_map->startdate($args->{StartDate}) if $args->{StartDate}; |
|---|
| 108 | $args->{Category} = $args->{Category}->id if ref $args->{Category}; |
|---|
| 109 | $url_map->category_id($args->{Category}) if $args->{Category}; |
|---|
| 110 | $args->{Author} = $args->{Author}->id if ref $args->{Author}; |
|---|
| 111 | $url_map->author_id($args->{Author}) if $args->{Author}; |
|---|
| 112 | } |
|---|
| 113 | $url_map->archive_type($archive_type); |
|---|
| 114 | $url_map->url($url); |
|---|
| 115 | $url_map->file_path($file_path); |
|---|
| 116 | $url_map->save() || return $class->error($url_map->errstr()); |
|---|
| 117 | return $url_map; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | sub parent_names { |
|---|
| 121 | my $obj = shift; |
|---|
| 122 | my $parents = { |
|---|
| 123 | blog => 'MT::Blog', |
|---|
| 124 | template => 'MT::Template', |
|---|
| 125 | templatemap => 'MT::TemplateMap', |
|---|
| 126 | category => 'MT::Category', |
|---|
| 127 | entry => 'MT::Entry', |
|---|
| 128 | author => 'MT::Author', |
|---|
| 129 | }; |
|---|
| 130 | $parents; |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | 1; |
|---|