Committer: gariev
LJINT-445: Comments for side projects crashes partners page when integrated on invalid host1. Removed redirect to 'about:blank'
2. Creating a partner object that has no journal on this (dev) server results in 'die' now
U trunk/cgi-bin/LJ/PartnerSite.pm
Modified: trunk/cgi-bin/LJ/PartnerSite.pm =================================================================== --- trunk/cgi-bin/LJ/PartnerSite.pm 2011-07-26 09:13:21 UTC (rev 10777) +++ trunk/cgi-bin/LJ/PartnerSite.pm 2011-07-26 09:39:08 UTC (rev 10778) @@ -204,14 +204,12 @@ =item * -LJ::PartnerSite->new(%params): bless %params into an object; this function -actually computes journal and journalid based on journal_username, so -you don't need to pass these two. +Private, don't use LJ::PartnerSite->_new_from_config_row(%params) =item * LJ::PartnerSite->find_by_id(1): return an instance with the specified -identifier, or undef. +identifier, or dies. =item * @@ -228,64 +226,58 @@ =cut -my %by_id_map; - -sub new { - my ( $class, %params ) = @_; - - if ( my $self = $by_id_map{ $params{'id'} } ) { - return $self; - } - - $params{'journal'} = LJ::load_user( $params{'journal_username'} ); - return unless $params{'journal'}; - - $params{'journalid'} = $params{'journal'}->userid; - - $params{'widget_version'} ||= $LJ::PARTNER_SITES_DEFAULT_WIDGET_VERSION; - - my $self = bless \%params, $class; - $by_id_map{ $params{'id'} } = $self; - return $self; -} - -my ( %find_cache, $find_cache_initialized ); - my @SEARCHABLE_FIELDS = qw( id api_key journal_username ); -sub _init_find_cache { - my ($class) = @_; +## search cache: search field --> field value --> {config row} +## i.e. $search_cache->{'api_key'}->{'sI9zAC167tWHOlg...'} = { 'id' => 4, 'name' => 'Gazeta', ... } +my (%search_cache, $is_cache_initialized); - my @all_partners; +sub _init_cache { foreach my $row ( @LJ::PARTNER_SITES ) { - push @all_partners, $class->new(%$row); - } - - foreach my $field ( @SEARCHABLE_FIELDS ) { - $find_cache{$field} = {}; - foreach my $partner ( @all_partners ) { - $find_cache{$field}->{ $partner->$field } = $partner; + foreach my $field (@SEARCHABLE_FIELDS) { + my $field_value = $row->{$field}; + $search_cache{$field}->{$field_value} = $row; } } - - $find_cache_initialized = 1; + $is_cache_initialized = 1; } -foreach my $field ( @SEARCHABLE_FIELDS ) { - $find_cache{$field} = {}; - +## create search accessors +foreach my $field (@SEARCHABLE_FIELDS) { no strict 'refs'; *{"find_by_$field"} = sub { - my ( $class, $v ) = @_; + my ($class, $v) = @_; + + _init_cache() unless $is_cache_initialized; - $class->_init_find_cache - unless $find_cache_initialized; - - return $find_cache{$field}->{$v}; + my $row = $search_cache{$field}->{$v}; + die "No such partner ($field=$v)" unless $row; + return $class->_new_from_config_row(%$row); }; } +## singletons: partner id --> partner object +my %by_id_map; +sub _new_from_config_row { + my ( $class, %params ) = @_; + + if (my $self = $by_id_map{ $params{'id'} }) { + return $self; + } + + my $journal = LJ::load_user( $params{'journal_username'} ) + or die "No journal '$params{'journal_username'}' found!"; + + $params{'journal'} = $journal; + $params{'journalid'} = $journal->userid; + $params{'widget_version'} ||= $LJ::PARTNER_SITES_DEFAULT_WIDGET_VERSION; + + my $self = bless \%params, $class; + $by_id_map{ $params{'id'} } = $self; + return $self; +} + ### INSTANCE METHODS ### sub _hashfunc { @@ -562,7 +554,7 @@ if (domainMatch) { $code } else { - window.location.href = 'about:blank'; + document.write("LJ Comments API error: invalid domain " + window.location.href); } function checkDomain(href, trustedDomains) {