#!/usr/bin/perl # AUTHORS # Sean Forman # Hans Van Slooten # $Date: 2017-03-07 00:33:47 -0500 (Tue, 07 Mar 2017) $ # $Author: sean $ # $Rev: 49697 $ # $HeadURL: http://svn.sports-reference.com/svn/br_repos/br/trunk/register/league.cgi $ # Copyright 2000-2016, SPORTS REFERENCE, LLC All rights reserved. ################################################################### # Usage : league.cgi # Purpose : returns a league summary # Returns : a-z full webpage # Parameters : id (the sabr key_league which is an integer) # Throws : none # See Also : # Comments : ################################################################### use Carp; use CGI; use FindBin qw($Bin); use Modern::Perl '2010'; use lib "$Bin/../lib"; use SR::Cache; use SR::Defaults; use SR::Statline; use SRlocal::Constants; use SRlocal::DB; use SRlocal::Defaults; use SRlocal::Formatting; use SRlocal::Players; use SRlocal::Register; use SRlocal::Register::Batting; use SRlocal::Register::Fetch; use SRlocal::Register::Fielding; use SRlocal::Register::Grids; use SRlocal::Register::Managers; use SRlocal::Register::Model; use SRlocal::Register::Pitching; use SRlocal::Stats; use SRlocal::Templates; chomp($0); #### [] Starting: "$Bin/$0 " . join(' ',@ARGV) MAIN: { # Get the db handle and a reference to a hash of site params our $dbh = get_site_db_connection($Bin); our $site_params_ref = get_site_params($Bin); our $q = new CGI; our $srtemplates = new SRlocal::Templates; my $id = url_chadwick_id( $q, 'id' ); my $year_id = url_integer( $q, 'year' ); my $code = url_alpha( $q, 'code' ); my $class = url_classification( $q, 'class' ); my $group = url_alpha( $q, 'group' ); my $class_name = $SRlocal::Constants::MILB_CLASSIFICATIONS{$class}{name} if ( is_nonempty($class) ); # redirect to new id with 301 if an old-style id. Necessary from # when sabr switched id's on us in early 2011. The table # sabr_redirect is static and should never need to change. if ( $id =~ /^([0-9]{1,7})$/ ) { my $old_id = $1; my $new_id = gtrc( $dbh, 'sabr_redirect', { id => $old_id, type => 'leagues' }, 'uuid' ); my $url = "http://$ENV{'HTTP_HOST'}$ENV{'REQUEST_URI'}"; $url =~ s/$old_id/$new_id/; print "Status: 301 Moved Permanantly\n"; print "Location: $url\n\n"; $dbh->disconnect; exit; } # build a unique id for this page's data, and retrieve from # mysql or memcached if available. Print if it is returned, # build if not and then add to cache. my $memd_key = $ENV{'REQUEST_URI'} // ''; my $page_data = cache_get( 'register:league', $memd_key ); # check to see if we got a result. if ( is_nonempty($page_data) ) { print "CONTENT-TYPE: text/html\n\n"; $page_data =~ s|CONTENT-TYPE: *text/html||i; print $page_data; $dbh->disconnect; exit; } # Define the site header values within a hash and send the hash to # the header script. # Set the page title. my $general_name = $group ? $group : $SRlocal::Register::DIR_DESC; my $template = 'Pages/General.tt2'; my $page_title = ''; my $page_url_suffix = ''; my @yah; # we have two alternate outputs, none, which gives a league index # or code which gives a league's output. if ($code) { my $league_name_code = gtrc( $dbh, 'register_leagues', { name_code => $code }, 'name_full' ); $page_title = qq{$league_name_code ($class_name) Encyclopedia and History}; $page_url_suffix = qq{?code=$code&class=$class}; # Set up the "You Are Here" line. @yah = ( qq{$SRlocal::Register::DIR_DESCS}, qq{Leagues}, qq{$league_name_code} ); } elsif ($year_id) { $page_title = qq{$year_id $general_name League Encyclopedia}; $page_url_suffix = qq{?year=$year_id&group=$group}; # Set up the "You Are Here" line. @yah = ( qq{$SRlocal::Register::DIR_DESCS}, qq{Leagues}, qq{$year_id Season Summary} ); } elsif ( !$id ) { $page_title = qq{$general_name League Encyclopedia}; $page_url_suffix = qq{}; # Set up the "You Are Here" line. @yah = ( qq{$SRlocal::Register::DIR_DESCS}, qq{Leagues}, qq{$page_title} ); } elsif ( !$group ) { $page_title = qq{$general_name League Encyclopedia}; $page_url_suffix = qq{?id=$id}; # Set up the "You Are Here" line. @yah = ( qq{$SRlocal::Register::DIR_DESCS}, qq{Leagues}, qq{$page_title} ); } my %model = ( header => { page_description => 'All Major, Minor, and Independent Baseball Leagues', }, ); ## display the league details if we have an id. if ($id) { my $data = register_fetch_league( $dbh, $id ); my $year = $data->{year}; my $league_name = $data->{name_full}; $page_title = qq{$year $league_name}; $template = 'Pages/League.tt2'; # Set up the "You Are Here" line. @yah = ( qq{$SRlocal::Register::DIR_DESCS}, qq{Leagues}, qq{$year $league_name} ); $model{key_league} = $id; $model{year_id} = $year; $model{data} = $data; $model{summary} = register_build_league_header_model( $dbh, $srtemplates, \%model ); register_get_standings_from_id( $dbh, $srtemplates, \%model ); register_get_league_batting_from_id( $dbh, $srtemplates, \%model ); register_get_league_pitching_from_id( $dbh, $srtemplates, \%model ); register_get_league_fielding_from_id( $dbh, $srtemplates, \%model ); } elsif ($year_id) { $model{year_id} = $year_id; ## Print summary of all of the stats. register_get_league_grid( $dbh, \%model, 'batting', $year_id, $group ); register_get_league_grid( $dbh, \%model, 'pitching', $year_id, $group ); register_get_league_grid( $dbh, \%model, 'fielding', $year_id, $group ); _register_get_individual_year_index( $dbh, \%model, $year_id, $group ); } elsif ( $code && $class ) { ## Display the list of years for this league _register_get_individual_league_index( $dbh, $srtemplates, \%model, $code, $class ); ## Add table of other classifications this league was in. _register_get_individual_league_index_other( $dbh, $srtemplates, \%model, $code, $class ); } else { # No id _register_get_league_index( $dbh, $srtemplates, \%model, $group ); } $model{header}{page_title} = $page_title; $model{header}{you_are_here} = generate_you_are_here( \@yah ); $model{header}{page_url} = $SITE_URL . '/register/league.cgi' . $page_url_suffix; $model{inner_nav} = build_register_inner_nav( $dbh, 'current', \%model ); push @{ $model{pagecontent} }, register_data_note( srtemplates => $srtemplates ); ## Generate the page from a template $page_data = $srtemplates->process( $template, \%model ); ## Show the page to the user. print $q->header( -type => 'text/html', -charset => 'utf-8' ); print $page_data; ## stuff the page into our cache. cache_set( 'register:league', $memd_key, $page_data ); $dbh->disconnect; } #### [] Finished: "$Bin/$0 " . join(' ',@ARGV) ################################################################### # Usage : # Purpose : # Returns : # Parameters : # Throws : ################################################################### sub _register_get_season_team_map { my ( $dbh, $year, $fh, $group ) = @_; my $return = $EMPTY_STR; $return .= qq{
\n

Pro Baseball Map for $year

\n}; my @params = ($year); my $group_clause = ''; if ($group) { $group_clause = " AND register_league_groups.group_name = ? \n"; push @params, $group; } # Get the teams for a state. my $query = <<"END_SQL"; SELECT a.city, a.state, a.country, CONCAT(register_teams.year,' ',register_teams.name_full) AS team_name, register_leagues.name_full AS lg_name, lat,lng, a.key_team, register_leagues.key_league FROM register_team_locations AS a JOIN register_teams USING (key_team) JOIN register_leagues USING (key_league, year) JOIN register_league_groups USING (key_league, year) JOIN city_locations USING (city, state, country) WHERE IFNULL(a.city,'') != '' AND register_leagues.year = ? $group_clause AND ABS(lat)+ABS(lng) > 2 END_SQL $return .= <<"END_HTML";

ALL TEAMS ARE LOCATED IN THE CENTER OF THE TOWN THEY ARE LISTED IN. We have not located the actual location of the ballpark.

We list over 30,000 of teams from minor league history. This list is not exhaustive and may be missing teams from certain leagues and years or with a small number of games played. We apologize for any omissions. Report any errors or omissions

END_HTML my $markers = $EMPTY_STR; my $sth = $dbh->prepare($query) || croak qq{Error Running query\n$query\n}; $sth->execute(@params); #############################################################3 #############################################################3 #############################################################3 # U.S. States while ( my $tmp = $sth->fetchrow_hashref ) { my $lat = $tmp->{lat}; my $lng = $tmp->{lng}; my $desc = '' . $tmp->{team_name} . '' . qq{
} . $tmp->{lg_name} . qq{
} . $tmp->{city} . ', ' . ( is_empty( $tmp->{state} ) ? $tmp->{country} : $tmp->{state} ); $lat += rand(.02) - .01; $lng += rand(.02) - .01; $desc =~ s/'/\\'/g; $markers .= <<"END_JS"; \$('#map_canvas').gmap('addMarker', {'position': '$lat,$lng', 'bounds': true}).click(function() { \$('#map_canvas').gmap('openInfoWindow', {'content': '$desc'}, this); }); END_JS } $sth->finish; $return .= <<"END_HTML";
END_HTML print $fh $return; return $TRUE; } ################################################################### # Usage : zzzz # Purpose : zzzz # Returns : zzzz # Parameters : zzzz # Throws : zzzz # See Also : zzzz # Comments : zzzz ################################################################### sub _register_get_individual_year_index { my ( $dbh, $model, $year, $group ) = @_; my @params = ($year); my $group_clause = ''; if ($group) { $group_clause = " and register_league_groups.group_name=?\n"; push @params, $group; } ############################################################## # Run through all of the teams that played in the league that # season. my $query = <<"END_SQL"; select register_leagues.name_full as lg_ID, register_leagues.classification, concat('/register/league.cgi?id=',register_leagues.key_league) as lg_ID_link, group_concat(key_team order by register_teams.name_full) as league_teams, $defn_of_stat{register_classification_csk}{sum} as classification_csk from register_leagues join register_league_groups using (key_league, year) left join register_teams using (key_league) where register_leagues.year=? $group_clause and register_leagues.classification!='MLB' group by register_leagues.key_league order by classification_csk desc, register_leagues.name_full END_SQL # Standard; my @stats = qw(lg_ID level team_ID); my $table_id = "lg_history"; my $table_title = "League Teams"; ## The table settings my %table_defn = ( table_id => $table_id, title => $table_title, caption => $table_title, table_cols_to_freeze => 1, comment_by_default => $TRUE, hide_long => $FALSE, ); # Use Statline to start the table and get the table header. my $table = generate_statline_header_wrap( \%table_defn ); $table .= generate_statline_header( \@stats ); my $sth = db_prepare_and_execute( $dbh, $query, @params ); my $prev_class_name = ''; while ( my $tmp = $sth->fetchrow_hashref ) { my $new_class_name = $SRlocal::Constants::MILB_CLASSIFICATIONS{ $tmp->{classification} }{name}; $tmp->{level} = $new_class_name; ## Spacer between classifications if ( $prev_class_name ne '' && $new_class_name ne $prev_class_name ) { my $blank; $blank->{LINE_class} = 'spacer'; $table .= generate_statline( \@stats, $blank ); } ## Display all the teams in this league if ( is_nonempty( $tmp->{league_teams} ) ) { my @tms = (); foreach my $tm ( split( /,/, $tmp->{league_teams} ) ) { push( @tms, sprintf( qq{%s}, $tm, gtrc( $dbh, 'register_teams', { key_team => $tm }, 'name_full' ) ) ); } $tmp->{team_ID} = join( $COMMA . $SPACE, @tms ); } $table .= generate_statline( \@stats, $tmp ); $prev_class_name = $new_class_name; } $table .= generate_statline_footer_wrap( \%table_defn ); push @{ $model->{pagecontent} }, $table if $table =~ /register/; } ################################################################### # Usage : _get_individual_league_index # Purpose : returns a summary of all leagues in the db # Returns : the interior of a page. # Parameters : dbh # Throws : nothing # See Also : # Comments : ################################################################### sub _register_get_individual_league_index { my ( $dbh, $srtemplates, $model, $code, $class ) = @_; # Run through all of the teams that are listed as members of the league. my $query = <<"END_SQL"; select register_leagues.year as year_ID, concat('/register/league.cgi?id=',register_leagues.key_league) as year_ID_link, count(distinct key_team) as teams, group_concat(key_team order by register_teams.name_full) as league_teams from register_leagues left join register_teams using (key_league) where register_leagues.name_code=? and classification=? group by register_leagues.year order by register_leagues.year desc END_SQL # Standard; my @stats = qw(year_ID team_ID); my $table_id = "lg_history"; my $table_title = "League History"; my $toggle_text = $EMPTY_STR; ## The table settings my %table_defn = ( table_id => $table_id, table_class => 'suppress_all', title => $table_title, caption => $table_title, table_cols_to_freeze => 1, comment_by_default => $FALSE, hide_long => $TRUE, ); my $sth = db_prepare_and_execute( $dbh, $query, $code, $class ); if ( !$sth->rows ) { $sth->finish; print <<"END_HTML"; LOCATION: http://www.baseball-reference.com/register/league.cgi END_HTML $dbh->disconnect; exit; } # Use Statline to start the table and get the table header. my $table = generate_statline_header_wrap( \%table_defn ); $table .= generate_statline_header( \@stats ); while ( my $tmp = $sth->fetchrow_hashref ) { $tmp->{year_ID_class} = 'small_text' if ( !$tmp->{teams} ); my @tms = (); foreach my $tm ( split( /,/, $tmp->{league_teams} ) ) { push( @tms, sprintf( qq{%s}, $tm, gtrc( $dbh, 'register_teams', { key_team => $tm }, 'name_full' ) ) ); } $tmp->{team_ID} = join( ', ', @tms ); ## Output the stats. $table .= generate_statline( \@stats, $tmp ); } $table .= generate_statline_footer_wrap( \%table_defn ); ## Add the table to the page. push @{ $model->{pagecontent} }, $table; } ################################################################### # Usage : _register_get_individual_league_index_other # Purpose : # Returns : # Parameters : # Throws : # See Also : # Comments : ################################################################### sub _register_get_individual_league_index_other { my ( $dbh, $srtemplates, $model, $code, $class ) = @_; ## Run through all of the teams that are listed as affiliates. my $query = <<"..."; select register_leagues.year as year_ID, classification, concat('/register/league.cgi?id=',register_leagues.key_league) as year_ID_link, group_concat(key_team order by register_teams.name_full) as league_teams from register_leagues left join register_teams using (key_league) where register_leagues.name_code=? and classification!=? group by register_leagues.year order by register_leagues.year desc ... my @stats = qw(year_ID classification team_ID); my $table_id = "lg_history_other"; my $table_title = "League History in Other Classifications"; ## The table settings my %table_defn = ( table_id => $table_id, table_class => 'suppress_all', title => $table_title, caption => $table_title, table_cols_to_freeze => 1, comment_by_default => $FALSE, hide_long => $TRUE, ); # Use Statline to start the table and get the table header. my $table = generate_statline_header_wrap( \%table_defn ); $table .= generate_statline_header( \@stats, ); my $sth = db_prepare_and_execute( $dbh, $query, $code, $class ); while ( my $tmp = $sth->fetchrow_hashref ) { my @tms = (); foreach my $tm ( split( /,/, _nempp( $tmp->{league_teams} ) ) ) { push( @tms, sprintf( qq{%s}, $tm, gtrc( $dbh, 'register_teams', { key_team => $tm }, 'name_full' ) ) ); } $tmp->{team_ID} = join( $COMMA . $SPACE, @tms ); ## Output the line. $table .= generate_statline( \@stats, $tmp ); } ## Close out the page $table .= generate_statline_footer_wrap( \%table_defn ); ## Add the table to the page. push @{ $model->{pagecontent} }, $table if $table =~ /register/; } ################################################################### # Usage : _get_league_index # Purpose : returns a summary of all leagues in the db # Returns : the interior of a page. # Parameters : dbh # Throws : nothing # See Also : # Comments : ################################################################### sub _register_get_league_index { my ( $dbh, $srtemplates, $model, $group ) = @_; my @params = (); my $group_clause = ''; if ($group) { $group_clause = " and register_league_groups.group_name = ? \n"; push @params, $group; } my $query = <<"END_SQL"; select name_code, classification, name_full as league, CONCAT('/register/league.cgi?code=', name_code, '&class=', replace(classification, '+', '%2B')) AS league_link, min(register_leagues.year) as year_min, max(register_leagues.year) as year_max, if(max(register_leagues.year) = $CURRENT_YEAR, 'bold', '') as LINE_class, count(distinct register_leagues.year) as seasons, $defn_of_stat{register_classification_csk}{sum} as classification_csk from register_leagues where classification != 'MLB' $group_clause group by name_code, classification order by classification_csk desc, year_max desc END_SQL my $sth = db_prepare_and_execute( $dbh, $query, @params ); my $table_id = 'leagues'; my $table_title = 'Leagues'; ## The table settings my %table_defn = ( table_id => $table_id, table_class => 'suppress_all', title => $table_title, caption => $table_title, table_cols_to_freeze => 1, comment_by_default => $FALSE, hide_long => $TRUE, ); $STATLINE_DEFAULTS{league}{justify} = 'left'; $STATLINE_DEFAULTS{league}{header} = 'League'; ## Start the table my $table = generate_statline_header_wrap( \%table_defn ); my @stats = qw(league year_min year_max seasons); my @in_table_headers = qw(league year_min year_max seasons); my $prev_class_name = ''; while ( my $tmp = $sth->fetchrow_hashref ) { my $new_class_name = $SRlocal::Constants::MILB_CLASSIFICATIONS{ $tmp->{classification} }{name}; if ( $new_class_name ne $prev_class_name ) { $in_table_headers[0] = $new_class_name; $STATLINE_DEFAULTS{$new_class_name}{justify} = 'left'; ## In-table Headers $table .= generate_statline_header( \@in_table_headers, { suppress_thead_output => $prev_class_name ne '' } ); } ## Statline $table .= generate_statline( \@stats, $tmp ); $prev_class_name = $new_class_name; } ## End Table $table .= generate_statline_footer_wrap( \%table_defn ); ## Add the table to the output push @{ $model->{pagecontent} }, $table if $table =~ /register/; }