root/branches/release-33/lib/MT/FileMgr.pm @ 1779

Revision 1779, 5.4 kB (checked in by bchoate, 20 months ago)

Avoid rebuilding files within the same build operation (particularly helps for date-based archive rebuilds). BugId:69032

  • Property svn:keywords set to Author Date Id Revision
Line 
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
7package MT::FileMgr;
8
9use strict;
10use base qw( MT::ErrorHandler );
11
12sub new {
13    my $class = shift;
14    my $type = shift;
15    $class .= "::" . $type;
16    eval "use $class;";
17    die "Unsupported file manager $class: $@" if $@;
18    my $fmgr = bless {}, $class;
19    $fmgr->init(@_)
20        or return $class->error( $fmgr->errstr );
21    return $fmgr;
22}
23
24sub init {
25    my $fmgr = shift;
26    $fmgr;
27}
28
29sub get_data;
30sub put;
31sub put_data;
32sub exists;
33sub can_write;
34sub mkpath;
35sub rename;
36sub delete;
37sub content_is_updated { 1 }
38sub file_mod_time { undef }
39
40sub is_handle {
41    my($fmgr, $f) = @_;
42    my $fd = ref($f) || ref(\$f) eq 'GLOB' ? fileno($f) : undef;
43    return defined $fd;
44}
45
461;
47__END__
48
49=head1 NAME
50
51MT::FileMgr - Base class for file management drivers
52
53=head1 SYNOPSIS
54
55    use MT::FileMgr;
56    my $fmgr = MT::FileMgr->new('Local')
57        or die MT::FileMgr->errstr;
58    defined(my $bytes = $fmgr->put($src, $dest))
59        or die $fmgr->errstr;
60
61=head1 DESCRIPTION
62
63I<MT::FileMgr> is a base class for file management driver implementations;
64it provides an abstraction of file management such that other Movable Type
65classes do not have to worry if files should be built locally or through
66FTP. For example, the process that rebuilds a user's site only has to build
67the content, then pass it off to the I<MT::FileMgr> driver; it does not have
68to worry whether the user's site is hosted locally or on an FTP server.
69I<MT::FileMgr> dispatches control to the appropriate driver, and the content
70is pushed to wherever it needs to be pushed.
71
72=head1 USAGE
73
74=head2 MT::FileMgr->new($type [, @args ])
75
76Loads the driver class I<$type> (either C<FTP> or C<Local>, currently) and
77returns a new file manager object blessed into the appropriate driver
78subclass.
79
80I<@args> is optional; it will be passed along to the subclass's I<init>
81method, for driver-specific initialization. For example, the C<FTP> driver
82requires the FTP host, username, and password in I<@args>, whereas the C<Local>
83host does not require anything.
84
85If an initialization error occurs, returns C<undef>; see L<ERROR HANDLING>,
86below.
87
88=head2 $fmgr->init()
89
90This is an internal method called by C<MT::FileMgr-E<gt>new> that
91initializes an L<MT::ConfigMgr/instance> as the I<cfg> attribute.
92
93=head2 $fmgr->put($src, $dest [, $type ])
94
95Puts the contents of the file I<$src> in the path I<$dest>. I<$src> can be
96either a filehandle of the path to a local file; I<$dest> must be a path to
97a file, either local or remote (depending on the driver).
98
99I<$type> is optional and defines whether the I<put> is for an uploaded file
100or for an output HTML file; this tells the I<FileMgr> drivers what mode to
101write the files in, what I<umask> settings to use, etc. The two values for
102I<$type> are C<upload> and C<output>; C<output> is the default.
103
104Returns the number of bytes "put" (can be 0).
105
106On error, returns C<undef>; see L<ERROR HANDLING>, below.
107
108=head2 $fmgr->put_data($data, $dest [, $type ])
109
110Puts the block of data I<$data> in the path I<$dest>. I<$src> should be a
111scalar containing the data, and I<$dest> should be a path to a file, either
112local or remote (depending on the driver).
113
114I<$type> is optional and defines whether the I<put_data> is for an uploaded
115file or for an output HTML file; this tells the I<FileMgr> drivers what mode
116to write the files in, what I<umask> settings to use, etc. The two values for
117I<$type> are C<upload> and C<output>; C<output> is the default.
118
119Returns the number of bytes "put" (can be 0).
120
121On error, returns C<undef>; see L<ERROR HANDLING>, below.
122
123=head2 $fmgr->get_data($src [, $type ])
124
125Gets a block of data from the path I<$src>; returns the block of data.
126I<$src> should be a path to a file, either local or remote (depending on the
127driver).
128
129I<$type> is optional and defines whether the I<get_data> is for an uploaded
130file or for an output HTML file; this tells the I<FileMgr> drivers what mode
131to write the files in, what I<umask> settings to use, etc. The two values for
132I<$type> are C<upload> and C<output>; C<output> is the default.
133
134On error, returns C<undef>; see L<ERROR HANDLING>, below.
135
136=head2 $fmgr->exists($path)
137
138Returns true if the file or directory I<$path> exists, false otherwise.
139
140=head2 $fmgr->mkpath($path)
141
142Creates the path I<$path> recursively; in other words, if any of the
143directories in the path do not exist, they are created. Returns true on
144success.
145
146On error, returns C<undef>; see L<ERROR HANDLING>, below.
147
148=head2 $fmgr->rename($src, $dest)
149
150Renames the file or directory I<$src> to I<$dest>. Returns true on success.
151
152On error, returns C<undef>; see L<ERROR HANDLING>, below.
153
154=head2 content_is_updated()
155
156Return one (1).
157
158=head2 $fmgr->is_handle($file)
159
160Return the file descriptor of I<file> or C<undef> if not found.
161
162=head1 ERROR HANDLING
163
164On an error, all of the above methods (except I<exists>) return C<undef>,
165and the error message can be obtained by calling the method I<errstr> on the
166class or the object (depending on whether the method called was a class method
167or an instance method).
168
169For example, called on a class name:
170
171    my $fmgr = MT::FileMgr->new('Local')
172        or die MT::FileMgr->errstr;
173
174Or, called on an object:
175
176    defined(my $bytes = $fmgr->put($src, $dest))
177        or die $fmgr->errstr;
178
179=head1 AUTHOR & COPYRIGHT
180
181Please see L<MT/AUTHOR & COPYRIGHT>.
182
183=cut
Note: See TracBrowser for help on using the browser.