<commit_before><?php

final class PhabricatorCalendarEventSearchEngine
  extends PhabricatorApplicationSearchEngine {

  private $calendarYear;
  private $calendarMonth;
  private $calendarDay;

  public function getResultTypeDescription() {
    return pht('Calendar Events');
  }

  public function getApplicationClassName() {
    return 'PhabricatorCalendarApplication';
  }

  public function buildSavedQueryFromRequest(AphrontRequest $request) {
    $saved = new PhabricatorSavedQuery();

    $saved->setParameter(
      'rangeStart',
      $this->readDateFromRequest($request, 'rangeStart'));

    $saved->setParameter(
      'rangeEnd',
      $this->readDateFromRequest($request, 'rangeEnd'));

    $saved->setParameter(
      'upcoming',
      $this->readBoolFromRequest($request, 'upcoming'));

    $saved->setParameter(
      'invitedPHIDs',
      $this->readUsersFromRequest($request, 'invited'));

    $saved->setParameter(
      'creatorPHIDs',
      $this->readUsersFromRequest($request, 'creators'));

    $saved->setParameter(
      'isCancelled',
      $request->getStr('isCancelled'));

    $saved->setParameter(
      'display',
      $request->getStr('display'));

    return $saved;
  }

  public function buildQueryFromSavedQuery(PhabricatorSavedQuery $saved) {
    $query = id(new PhabricatorCalendarEventQuery())
      ->setGenerateGhosts(true);
    $viewer = $this->requireViewer();
    $timezone = new DateTimeZone($viewer->getTimezoneIdentifier());

    $min_range = $this->getDateFrom($saved)->getEpoch();
    $max_range = $this->getDateTo($saved)->getEpoch();

    $user_datasource = id(new PhabricatorPeopleUserFunctionDatasource())
      ->setViewer($viewer);

    if ($this->isMonthView($saved) ||
      $this->isDayView($saved)) {
      list($start_year, $start_month, $start_day) =
        $this->getDisplayYearAndMonthAndDay($saved);

      $start_day = new DateTime(
        "{$start_year}-{$start_month}-{$start_day}",
        $timezone);
      $next = clone $start_day;

      if ($this->isMonthView($saved)) {
        $next->modify('+1 month');
      } else if ($this->isDayView($saved)) {
        $next->modify('+6 day');
      }

      $display_start = $start_day->format('U');
      $display_end = $next->format('U');

      $preferences = $viewer->loadPreferences();
      $pref_week_day = PhabricatorUserPreferences::PREFERENCE_WEEK_START_DAY;

      $start_of_week = $preferences->getPreference($pref_week_day, 0);
      $end_of_week = ($start_of_week + 6) % 7;

      $first_of_month = $start_day->format('w');
      $last_of_month = id(clone $next)->modify('-1 day')->format('w');

      if (!$min_range || ($min_range < $display_start)) {
        $min_range = $display_start;

        if ($this->isMonthView($saved) &&
          $first_of_month !== $start_of_week) {
          $interim_day_num = ($first_of_month + 7 - $start_of_week) % 7;
          $min_range = id(clone $start_day)
            ->modify('-'.$interim_day_num.' days')
            ->format('U');
        }
      }
      if (!$max_range || ($max_range > $display_end)) {
        $max_range = $display_end;

        if ($this->isMonthView($saved) &&
          $last_of_month !== $end_of_week) {
          $interim_day_num = ($end_of_week + 7 - $last_of_month) % 7;
          $max_range = id(clone $next)
            ->modify('+'.$interim_day_num.' days')
            ->format('U');
        }

      }
    }

    if ($saved->getParameter('upcoming')) {
      if ($min_range) {
        $min_range = max(time(), $min_range);
      } else {
        $min_range = time();
      }
    }

    if ($min_range || $max_range) {
      $query->withDateRange($min_range, $max_range);
    }

    $invited_phids = $saved->getParameter('invitedPHIDs');
    $invited_phids = $user_datasource->evaluateTokens($invited_phids);
    if ($invited_phids) {
      $query->withInvitedPHIDs($invited_phids);
    }

    $creator_phids = $saved->getParameter('creatorPHIDs');
    $creator_phids = $user_datasource->evaluateTokens($creator_phids);
    if ($creator_phids) {
      $query->withCreatorPHIDs($creator_phids);
    }

    $is_cancelled = $saved->getParameter('isCancelled', 'active');

    switch ($is_cancelled) {
      case 'active':
        $query->withIsCancelled(false);<commit_msg>Only evaluate tokens if phids not null<commit_after><?php

final class PhabricatorCalendarEventSearchEngine
  extends PhabricatorApplicationSearchEngine {

  private $calendarYear;
  private $calendarMonth;
  private $calendarDay;

  public function getResultTypeDescription() {
    return pht('Calendar Events');
  }

  public function getApplicationClassName() {
    return 'PhabricatorCalendarApplication';
  }

  public function buildSavedQueryFromRequest(AphrontRequest $request) {
    $saved = new PhabricatorSavedQuery();

    $saved->setParameter(
      'rangeStart',
      $this->readDateFromRequest($request, 'rangeStart'));

    $saved->setParameter(
      'rangeEnd',
      $this->readDateFromRequest($request, 'rangeEnd'));

    $saved->setParameter(
      'upcoming',
      $this->readBoolFromRequest($request, 'upcoming'));

    $saved->setParameter(
      'invitedPHIDs',
      $this->readUsersFromRequest($request, 'invited'));

    $saved->setParameter(
      'creatorPHIDs',
      $this->readUsersFromRequest($request, 'creators'));

    $saved->setParameter(
      'isCancelled',
      $request->getStr('isCancelled'));

    $saved->setParameter(
      'display',
      $request->getStr('display'));

    return $saved;
  }

  public function buildQueryFromSavedQuery(PhabricatorSavedQuery $saved) {
    $query = id(new PhabricatorCalendarEventQuery())
      ->setGenerateGhosts(true);
    $viewer = $this->requireViewer();
    $timezone = new DateTimeZone($viewer->getTimezoneIdentifier());

    $min_range = $this->getDateFrom($saved)->getEpoch();
    $max_range = $this->getDateTo($saved)->getEpoch();

    $user_datasource = id(new PhabricatorPeopleUserFunctionDatasource())
      ->setViewer($viewer);

    if ($this->isMonthView($saved) ||
      $this->isDayView($saved)) {
      list($start_year, $start_month, $start_day) =
        $this->getDisplayYearAndMonthAndDay($saved);

      $start_day = new DateTime(
        "{$start_year}-{$start_month}-{$start_day}",
        $timezone);
      $next = clone $start_day;

      if ($this->isMonthView($saved)) {
        $next->modify('+1 month');
      } else if ($this->isDayView($saved)) {
        $next->modify('+6 day');
      }

      $display_start = $start_day->format('U');
      $display_end = $next->format('U');

      $preferences = $viewer->loadPreferences();
      $pref_week_day = PhabricatorUserPreferences::PREFERENCE_WEEK_START_DAY;

      $start_of_week = $preferences->getPreference($pref_week_day, 0);
      $end_of_week = ($start_of_week + 6) % 7;

      $first_of_month = $start_day->format('w');
      $last_of_month = id(clone $next)->modify('-1 day')->format('w');

      if (!$min_range || ($min_range < $display_start)) {
        $min_range = $display_start;

        if ($this->isMonthView($saved) &&
          $first_of_month !== $start_of_week) {
          $interim_day_num = ($first_of_month + 7 - $start_of_week) % 7;
          $min_range = id(clone $start_day)
            ->modify('-'.$interim_day_num.' days')
            ->format('U');
        }
      }
      if (!$max_range || ($max_range > $display_end)) {
        $max_range = $display_end;

        if ($this->isMonthView($saved) &&
          $last_of_month !== $end_of_week) {
          $interim_day_num = ($end_of_week + 7 - $last_of_month) % 7;
          $max_range = id(clone $next)
            ->modify('+'.$interim_day_num.' days')
            ->format('U');
        }

      }
    }

    if ($saved->getParameter('upcoming')) {
      if ($min_range) {
        $min_range = max(time(), $min_range);
      } else {
        $min_range = time();
      }
    }

    if ($min_range || $max_range) {
      $query->withDateRange($min_range, $max_range);
    }

    $invited_phids = $saved->getParameter('invitedPHIDs');
    if ($invited_phids) {
      $invited_phids = $user_datasource->evaluateTokens($invited_phids);
      $query->withInvitedPHIDs($invited_phids);
    }

    $creator_phids = $saved->getParameter('creatorPHIDs');
    if ($creator_phids) {
      $creator_phids = $user_datasource->evaluateTokens($creator_phids);
      $query->withCreatorPHIDs($creator_phids);
    }

    $is_cancelled = $saved->getParameter('isCancelled', 'active');

    switch ($is_cancelled) {
      case 'active':
        $query->withIsCancelled(false);
{'fraction_non_alphanumeric': 0.15861185512984743, 'fraction_numerical': 0.00232910213112845, 'mean_word_length': 3.2409876543209877, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 109, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4742224', 'n_tokens_mistral': 2868, 'n_tokens_neox': 2791, 'n_words': 411}