root/branches/release-26/lib/MT/FileMgr.pm @ 1174

Revision 1174, 5.3 kB (checked in by bchoate, 23 months ago)

Updated copyright year for source.

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