function OnLoadPageData()
 {
     # Sanitise the name of the requested statistic
     $this->which_statistic = preg_replace("/[^a-z0-9-]/", "", $_GET["statistic"]);
     # Set up statistics manager
     $statistics_manager = new StatisticsManager($this->GetSettings(), $this->GetDataConnection());
     # Configure the requested statistic
     switch ($this->which_statistic) {
         case "individual-scores":
             require_once "stoolball/statistics/individual-scores.class.php";
             $this->statistic = new IndividualScores($statistics_manager);
             break;
         case "most-runs":
             require_once "stoolball/statistics/most-runs.class.php";
             $this->statistic = new MostRuns($statistics_manager);
             break;
         case "batting-average":
             require_once "stoolball/statistics/batting-average.class.php";
             $this->statistic = new BattingAverage($statistics_manager);
             break;
         case "batting-strike-rate":
             require_once "stoolball/statistics/batting-strike-rate.class.php";
             $this->statistic = new BattingStrikeRate($statistics_manager);
             break;
         case "bowling-performances":
             require_once "stoolball/statistics/bowling-performances.class.php";
             $this->statistic = new BowlingPerformances($statistics_manager);
             break;
         case "most-wickets":
             require_once "stoolball/statistics/most-wickets.class.php";
             $this->statistic = new MostWickets($statistics_manager);
             break;
         case "most-wickets-by-bowler-and-catcher":
             require_once "stoolball/statistics/most-wickets-by-bowler-and-catcher.class.php";
             $this->statistic = new MostWicketsForBowlerAndCatcher($statistics_manager);
             break;
         case "bowling-average":
             require_once "stoolball/statistics/bowling-average.class.php";
             $this->statistic = new BowlingAverage($statistics_manager);
             break;
         case "economy-rate":
             require_once "stoolball/statistics/economy-rate.class.php";
             $this->statistic = new EconomyRate($statistics_manager);
             break;
         case "bowling-strike-rate":
             require_once "stoolball/statistics/bowling-strike-rate.class.php";
             $this->statistic = new BowlingStrikeRate($statistics_manager);
             break;
         case "most-catches":
             require_once "stoolball/statistics/most-catches.class.php";
             $this->statistic = new MostCatches($statistics_manager);
             break;
         case "most-catches-in-innings":
             require_once "stoolball/statistics/most-catches-in-innings.class.php";
             $this->statistic = new MostCatchesInAnInnings($statistics_manager);
             break;
         case "most-run-outs":
             require_once "stoolball/statistics/most-run-outs.class.php";
             $this->statistic = new MostRunOuts($statistics_manager);
             break;
         case "most-run-outs-in-innings":
             require_once "stoolball/statistics/most-run-outs-in-innings.class.php";
             $this->statistic = new MostRunOutsInAnInnings($statistics_manager);
             break;
         case "player-performances":
             require_once "stoolball/statistics/player-performances.class.php";
             $this->statistic = new PlayerPerformances($statistics_manager);
             break;
         case "player-of-match":
             require_once "stoolball/statistics/player-of-match.class.php";
             $this->statistic = new PlayerOfTheMatch($statistics_manager);
             break;
         case "most-player-of-match":
             require_once "stoolball/statistics/most-player-of-match.class.php";
             $this->statistic = new MostPlayerOfTheMatch($statistics_manager);
             break;
         default:
             $matches = array();
             $is_match = preg_match("/^most-scores-of-([0-9]+)\$/", $this->which_statistic, $matches);
             if ($is_match === 1) {
                 $match_scores_of = $matches[1];
                 require_once "stoolball/statistics/most-scores-of.class.php";
                 $this->statistic = new MostScoresOf($match_scores_of, $statistics_manager);
             }
             $is_match = preg_match("/^most-([0-9][0-9]?)-wickets\$/", $this->which_statistic, $matches);
             if ($is_match === 1) {
                 $how_many_wickets = $matches[1];
                 require_once "stoolball/statistics/most-wicket-hauls-of.class.php";
                 $this->statistic = new \Stoolball\Statistics\MostWicketHaulsOf($how_many_wickets, $statistics_manager);
             }
             break;
     }
     # Is this a request for CSV data?
     $csv = (isset($_GET["format"]) and $_GET["format"] == "csv");
     if ($csv) {
         $statistics_manager->OutputAsCsv($this->statistic->ColumnHeaders());
     }
     # Apply player filter first because it can be used to limit the choices for other filters
     if ($this->statistic->SupportsFilterByPlayer()) {
         $this->filter .= StatisticsFilter::ApplyPlayerFilter($this->GetSettings(), $this->GetDataConnection(), $statistics_manager);
     }
     # Apply filters common to all statistics
     $this->filter_control = new StatisticsFilterControl();
     if ($this->statistic->SupportsFilterByBattingPosition()) {
         $filter_batting_position = StatisticsFilter::SupportBattingPositionFilter($statistics_manager);
         $this->filter_control->SupportBattingPositionFilter($filter_batting_position);
         $this->filter .= $filter_batting_position[2];
     }
     $match_type_filter_description = "";
     # If player filter applied, no point filtering by player type because they're associated with a team of one player type.
     # There is the edge case of a ladies team playing in a mixed match, but it would be more confusing than useful to offer it.
     if (!isset($_GET['player'])) {
         $filter_player_type = StatisticsFilter::SupportPlayerTypeFilter($statistics_manager);
         $this->filter_control->SupportPlayerTypeFilter($filter_player_type);
         $match_type_filter_description .= $filter_player_type[2];
     }
     $filter_match_type = StatisticsFilter::SupportMatchTypeFilter($statistics_manager);
     $this->filter_control->SupportMatchTypeFilter($filter_match_type);
     $match_type_filter_description .= $filter_match_type[2];
     $this->filter .= StatisticsFilter::CombineMatchTypeDescriptions($match_type_filter_description);
     # If player filter applied, no point filtering by team. Check for player filter is stricter than this
     # but this is good enough. If this applies, but the player filter isn't applied, it's because someone
     # is playing with the query string, so that's tough.
     if (!isset($_GET['player'])) {
         $filter_team = StatisticsFilter::SupportTeamFilter($statistics_manager);
         $this->filter_control->SupportTeamFilter($filter_team);
         $this->filter .= $filter_team[2];
     }
     $filter_opposition = StatisticsFilter::SupportOppositionFilter($statistics_manager);
     $this->filter_control->SupportOppositionFilter($filter_opposition);
     $this->filter .= $filter_opposition[2];
     $filter_competition = StatisticsFilter::SupportCompetitionFilter($statistics_manager);
     $this->filter_control->SupportCompetitionFilter($filter_competition);
     $this->filter .= $filter_competition[2];
     $this->filter .= StatisticsFilter::ApplySeasonFilter($this->GetSettings(), $this->GetDataConnection(), $statistics_manager);
     $filter_ground = StatisticsFilter::SupportGroundFilter($statistics_manager);
     $this->filter_control->SupportGroundFilter($filter_ground);
     $this->filter .= $filter_ground[2];
     $filter_date = StatisticsFilter::SupportDateFilter($statistics_manager);
     if (!is_null($filter_date[0])) {
         $this->filter_control->SupportAfterDateFilter($filter_date[0]);
     }
     if (!is_null($filter_date[1])) {
         $this->filter_control->SupportBeforeDateFilter($filter_date[1]);
     }
     $this->filter .= $filter_date[2];
     $this->filter .= StatisticsFilter::ApplyTournamentFilter($this->GetSettings(), $this->GetDataConnection(), $statistics_manager);
     $filter_innings = StatisticsFilter::SupportInningsFilter($statistics_manager);
     $this->filter_control->SupportInningsFilter($filter_innings[1]);
     $this->filter .= $filter_innings[2];
     $filter_won_match = StatisticsFilter::SupportMatchResultFilter($statistics_manager);
     $this->filter_control->SupportMatchResultFilter($filter_won_match[1]);
     $this->filter .= $filter_won_match[2];
     # Configure paging of results
     if (!$csv and $this->statistic->SupportsPagedResults()) {
         require_once 'data/paged-results.class.php';
         $this->paging = new PagedResults();
         $this->paging->SetPageName($this->which_statistic);
         $this->paging->SetQueryString(preg_replace("/statistic={$this->which_statistic}&?/", "", $this->paging->GetQueryString()));
         $this->paging->SetPageSize(50);
         $this->paging->SetResultsTextSingular($this->statistic->ItemTypeSingular());
         $this->paging->SetResultsTextPlural($this->statistic->ItemTypePlural());
         $statistics_manager->FilterByPage($this->paging->GetPageSize(), $this->paging->GetCurrentPage());
     }
     $this->data = $this->statistic->ReadStatistic();
     if (is_array($this->data)) {
         if ($csv) {
             require_once "data/csv.class.php";
             CSV::PublishData($this->data);
         } else {
             $this->paging->SetTotalResults(array_shift($this->data));
         }
     }
     unset($statistics_manager);
 }
 public function OnLoadPageData()
 {
     # Always get the player's unfiltered profile, because it's needed for the page description
     require_once "stoolball/player-manager.class.php";
     $player_manager = new PlayerManager($this->GetSettings(), $this->GetDataConnection());
     $player_manager->ReadPlayerById($this->player->GetId());
     $this->player_unfiltered = $player_manager->GetFirst();
     if (!$this->player_unfiltered instanceof Player) {
         http_response_code(404);
         $this->not_found = true;
         return;
     }
     # Update search engine
     if ($this->player_unfiltered->GetSearchUpdateRequired()) {
         require_once "search/player-search-adapter.class.php";
         $this->SearchIndexer()->DeleteFromIndexById("player" . $this->player->GetId());
         $adapter = new PlayerSearchAdapter($this->player_unfiltered);
         $this->SearchIndexer()->Index($adapter->GetSearchableItem());
         $this->SearchIndexer()->CommitChanges();
         $player_manager->SearchUpdated($this->player->GetId());
     }
     unset($player_manager);
     # Check first for a player created using 'add player', who hasn't played yet
     if ($this->player_unfiltered->GetTotalMatches() == 0) {
         $this->player = $this->player_unfiltered;
     } else {
         # Now get statistics for the player
         $statistics_manager = new StatisticsManager($this->GetSettings(), $this->GetDataConnection());
         $statistics_manager->FilterByPlayer(array($this->player->GetId()));
         # Apply filters common to all statistics
         $this->filter_control = new StatisticsFilterControl();
         $filter_batting_position = StatisticsFilter::SupportBattingPositionFilter($statistics_manager);
         $this->filter_control->SupportBattingPositionFilter($filter_batting_position);
         $this->filter .= $filter_batting_position[2];
         $filter_match_type = StatisticsFilter::SupportMatchTypeFilter($statistics_manager);
         $this->filter_control->SupportMatchTypeFilter($filter_match_type);
         $this->filter .= $filter_match_type[2];
         $filter_opposition = StatisticsFilter::SupportOppositionFilter($statistics_manager);
         $this->filter_control->SupportOppositionFilter($filter_opposition);
         $this->filter .= $filter_opposition[2];
         $filter_competition = StatisticsFilter::SupportCompetitionFilter($statistics_manager);
         $this->filter_control->SupportCompetitionFilter($filter_competition);
         $this->filter .= $filter_competition[2];
         $this->filter .= StatisticsFilter::ApplySeasonFilter($this->GetSettings(), $this->GetDataConnection(), $statistics_manager);
         $filter_ground = StatisticsFilter::SupportGroundFilter($statistics_manager);
         $this->filter_control->SupportGroundFilter($filter_ground);
         $this->filter .= $filter_ground[2];
         $filter_date = StatisticsFilter::SupportDateFilter($statistics_manager);
         if (!is_null($filter_date[0])) {
             $this->filter_control->SupportAfterDateFilter($filter_date[0]);
         }
         if (!is_null($filter_date[1])) {
             $this->filter_control->SupportBeforeDateFilter($filter_date[1]);
         }
         $this->filter .= $filter_date[2];
         $filter_innings = StatisticsFilter::SupportInningsFilter($statistics_manager);
         $this->filter_control->SupportInningsFilter($filter_innings[1]);
         $this->filter .= $filter_innings[2];
         $filter_won_match = StatisticsFilter::SupportMatchResultFilter($statistics_manager);
         $this->filter_control->SupportMatchResultFilter($filter_won_match[1]);
         $this->filter .= $filter_won_match[2];
         # Now get the statistics for the player
         $data = $statistics_manager->ReadPlayerSummary();
         if (count($data)) {
             $this->player = $data[0];
         } else {
             if ($this->filter) {
                 # If no matches matched the filter, ensure we have the player's name and team
                 $this->player = $this->player_unfiltered;
                 $this->filter_matched_nothing = true;
             } else {
                 $this->regenerating = true;
             }
         }
         $data = $statistics_manager->ReadBestBattingPerformance(false);
         foreach ($data as $performance) {
             $batting = new Batting($this->player, $performance["how_out"], null, null, $performance["runs_scored"], $performance["balls_faced"]);
             $this->player->Batting()->Add($batting);
         }
         if ($this->player->GetPlayerRole() == Player::PLAYER) {
             $data = $statistics_manager->ReadBestPlayerAggregate("player_of_match");
             $this->player->SetTotalPlayerOfTheMatchNominations(count($data) ? $data[0]["statistic"] : 0);
         }
         unset($statistics_manager);
     }
 }