~ubuntu-branches/debian/squeeze/movabletype-opensource/squeeze

« back to all changes in this revision

Viewing changes to lib/MT/Object.pm

  • Committer: Bazaar Package Importer
  • Author(s): Dominic Hargreaves
  • Date: 2009-06-19 23:03:15 UTC
  • mfrom: (1.2.1 upstream) (9.1.1 karmic)
  • Revision ID: james.westby@ubuntu.com-20090619230315-rm1vcgg5iymu2zh1
Tags: 4.2.6.1-1
* New upstream release
* Update Standards-Version (no changes)
* Don't specify full path to apache2ctl in postinst (thanks, Lintian)
* Remove unused Lintian overrides
* Don't install empty directory in extlib

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd.
 
1
# Movable Type (r) Open Source (C) 2001-2009 Six Apart, Ltd.
2
2
# This program is distributed under the terms of the
3
3
# GNU General Public License, version 2.
4
4
#
5
 
# $Id: Object.pm 3092 2008-10-08 19:29:24Z bchoate $
 
5
# $Id: Object.pm 3774 2009-06-01 21:52:50Z jmarcotte $
6
6
 
7
7
package MT::Object;
8
8
 
136
136
        $type_id = $props->{datasource};
137
137
    }
138
138
 
 
139
    $props->{get_driver} ||= sub {
 
140
        require MT::ObjectDriverFactory;
 
141
        my $coderef = MT::ObjectDriverFactory->driver_for_class($class);
 
142
        $class->get_driver($coderef);
 
143
        return $coderef->(@_);
 
144
    };
 
145
 
139
146
    $class->SUPER::install_properties($props);
140
147
 
141
148
    # check for any supplemental columns from other components
225
232
        $class->add_trigger( post_load => _get_date_translator(\&_db2ts, 0) );
226
233
    }
227
234
 
228
 
    if ( exists($props->{cacheable}) && !$props->{cacheable} ) {
229
 
        no warnings 'redefine';
230
 
        no strict 'refs'; ## no critic
231
 
        *{$class . '::driver'} = sub { $_[0]->dbi_driver(@_) };
232
 
    }
233
 
 
234
235
    # inherit parent's metadata setup
235
236
    if ($props->{meta}) { # if ($super_props && $super_props->{meta_installed}) {
236
237
        $class->install_meta({ ( %meta ? ( column_defs => \%meta ) : ( columns => [] ) ) });
556
557
 
557
558
sub _post_save_save_metadata {
558
559
    my $obj = shift;
 
560
    my ($orig_obj) = @_;
559
561
    if (defined $obj && exists $obj->{__meta}) {
560
562
        $obj->{__meta}->set_primary_keys($obj);
561
563
        $obj->{__meta}->save;
 
564
        $orig_obj->{__meta} = $obj->{__meta};
562
565
    }
563
566
}
564
567
 
855
858
 
856
859
our $DRIVER;
857
860
sub driver {
858
 
    require MT::ObjectDriverFactory;
859
 
    return $DRIVER ||= MT::ObjectDriverFactory->new;
 
861
    my $class = shift;
 
862
    return $DRIVER ||= MT::ObjectDriverFactory->instance
 
863
        if UNIVERSAL::isa($class, 'MT::Object');
 
864
    my $driver = $class->SUPER::driver(@_);
 
865
    return $driver;
860
866
}
861
867
 
862
 
# ref to the fallback driver for non-cacheable classes
863
 
our $DBI_DRIVER;
864
868
sub dbi_driver {
865
 
    unless ($DBI_DRIVER) {
866
 
        my $driver = driver(@_);
 
869
    my $class = shift;
 
870
    my $props = $class->properties || {};
 
871
    unless ($props->{dbi_driver}) {
 
872
        my $driver = $class->driver(@_);
867
873
        while ( $driver->can('fallback') ) {
868
874
            if ($driver->fallback) {
869
875
                $driver = $driver->fallback;
871
877
                last;
872
878
            }
873
879
        }
874
 
        $DBI_DRIVER = $driver;
 
880
        $props->{dbi_driver} = $driver;
875
881
    }
876
 
    return $DBI_DRIVER;
 
882
    return $props->{dbi_driver};
877
883
}
878
884
 
879
885
sub table_name {
887
893
    if ($clone->properties->{meta_installed}) {
888
894
        $clone->init_meta();
889
895
        $clone->meta( $obj->meta );
 
896
        for my $meta ( keys %{ $clone->{__meta}->{__objects} } ) {
 
897
            $clone->{__meta}->{__objects}{$meta}->{changed_cols}
 
898
                = $obj->{__meta}->{__objects}->{$meta}->{changed_cols} || {};
 
899
        }
890
900
    }
891
901
    return $clone;
892
902
}
1083
1093
    return $obj;
1084
1094
}
1085
1095
 
1086
 
# We override D::OD's set_values method here only allowing the
1087
 
# assignment of a column if the value given is defined. There are
1088
 
# some legacy reasons for doing this, mostly for backward
1089
 
# compatibility.
1090
1096
sub set_values {
1091
1097
    my $obj = shift;
1092
1098
    my ($values) = @_;
1093
1099
    for my $col (keys %$values) {
1094
 
        unless ( $obj->has_column($col) ) {
1095
 
            Carp::croak("You tried to set inexistent column $col to value $values->{$col} on " . ref($obj));
 
1100
        if ( $obj->has_column($col) ) {
 
1101
            # there's no point in croaking here; just set the
 
1102
            # values that are defined; ignore any others
 
1103
            $obj->$col($values->{$col}) if defined $values->{$col};
1096
1104
        }
1097
 
        $obj->$col($values->{$col}) if defined $values->{$col};
1098
1105
    }
1099
1106
}
1100
1107
 
 
1108
sub get_values {
 
1109
    my $obj = shift;
 
1110
    # returns a copy of column_values, as accessing column_values directly
 
1111
    # can be problematic.
 
1112
    return { %{ $obj->column_values } };
 
1113
}
 
1114
 
1101
1115
sub column_def {
1102
1116
    my $obj = shift;
1103
1117
    my ($name) = @_;
1198
1212
 
1199
1213
    if (@_) {
1200
1214
        $oc = $oc->{$key};
1201
 
        delete $oc->{shift} if $oc;
 
1215
        delete $oc->{ $_[0] } if $oc;
1202
1216
    } else {
1203
1217
        delete $oc->{$key};
1204
1218
    }
1210
1224
    my $hash = {};
1211
1225
    my $props = $obj->properties;
1212
1226
    my $pfx = $obj->datasource;
1213
 
    my $values = $obj->column_values;
 
1227
    my $values = $obj->get_values;
1214
1228
    foreach (keys %$values) {
1215
1229
        $hash->{"${pfx}.$_"} = $values->{$_};
1216
1230
    }
1248
1262
    };
1249
1263
    my $meta_class = $class->meta_pkg;
1250
1264
    my $meta_pk = $meta_class->primary_key_tuple;
 
1265
    $meta_pk = [ $meta_pk->[0] ]; # we only need the first column, that's the id
1251
1266
    my @metaobjs = $meta_class->search(
1252
1267
        $meta_terms, { %$args, fetchonly => $meta_pk }
1253
1268
    );
1271
1286
    my ($props) = @_;
1272
1287
    $props->{column_defs}->{$_} ||= 'string'
1273
1288
        for @{ $props->{columns} };
 
1289
 
 
1290
    $props->{get_driver} ||= sub {
 
1291
        require MT::ObjectDriverFactory;
 
1292
        my $coderef = MT::ObjectDriverFactory->driver_for_class($class);
 
1293
        $class->get_driver($coderef);
 
1294
        return $coderef->(@_);
 
1295
    };
 
1296
 
1274
1297
    $class->SUPER::install_properties(@_);
1275
1298
}
1276
1299
 
 
1300
sub __properties { }
1277
1301
sub meta_pkg { undef }
1278
1302
 
1279
1303
*table_name = \&MT::Object::table_name;
1284
1308
*__parse_def = \&MT::Object::__parse_def;
1285
1309
*count = \&MT::Object::count;
1286
1310
*columns_of_type = \&MT::Object::columns_of_type;
1287
 
 
1288
 
*driver = \&MT::Object::dbi_driver;
 
1311
*join_on = \&MT::Object::join_on;
1289
1312
 
1290
1313
# TODO: copy this too
1291
1314
sub blob_requires_zip {}
1362
1385
 
1363
1386
    use base 'MT::Object';
1364
1387
 
 
1388
=over 4
 
1389
 
1365
1390
=item * __PACKAGE__->install_properties($args)
1366
1391
 
1367
1392
Then call the I<install_properties> method on your class name; an easy way
1555
1580
 
1556
1581
=back
1557
1582
 
 
1583
=back
 
1584
 
1558
1585
=head1 USAGE
1559
1586
 
1560
1587
=head2 System Initialization
2083
2110
 
2084
2111
=back
2085
2112
 
 
2113
C<get_values> is similar to C<column_values>, in that it returns a hash
 
2114
reference of column names and values, but it returns a new hash reference,
 
2115
and a copy of the data rather that the hash reference that MT::Object uses
 
2116
internally. This is a safer way to access this data, if you intend to also
 
2117
modify the values.
 
2118
 
 
2119
    my $data = $obj->get_values;
 
2120
    $data->{$_} = lc $data->{$_} for keys %$data;
 
2121
 
 
2122
=over 4
 
2123
 
 
2124
=item * $obj->set_values()
 
2125
 
 
2126
=back
 
2127
 
2086
2128
C<set_values> accepts a similar hash ref, which need not give a value
2087
2129
for every field. For example:
2088
2130
 
2223
2265
This method returns all the C<column_defs> of the property of the
2224
2266
object.
2225
2267
 
2226
 
=item Class->index_defs()
 
2268
=item * Class->index_defs()
2227
2269
 
2228
2270
This method returns all the index definitions assigned to this class.
2229
2271
This is the 'indexes' member of the properties installed for the class.