<commit_before><?php

final class DifferentialRevisionViewController extends DifferentialController {

  private $revisionID;
  private $veryLargeDiff;

  public function shouldAllowPublic() {
    return true;
  }

  public function isVeryLargeDiff() {
    return $this->veryLargeDiff;
  }

  public function getVeryLargeDiffLimit() {
    return 1000;
  }

  public function handleRequest(AphrontRequest $request) {
    $viewer = $this->getViewer();
    $this->revisionID = $request->getURIData('id');

    $viewer_is_anonymous = !$viewer->isLoggedIn();

    $revision = id(new DifferentialRevisionQuery())
      ->withIDs(array($this->revisionID))
      ->setViewer($viewer)
      ->needReviewers(true)
      ->needReviewerAuthority(true)
      ->executeOne();
    if (!$revision) {
      return new Aphront404Response();
    }

    $diffs = id(new DifferentialDiffQuery())
      ->setViewer($viewer)
      ->withRevisionIDs(array($this->revisionID))
      ->execute();
    $diffs = array_reverse($diffs, $preserve_keys = true);

    if (!$diffs) {
      throw new Exception(
        pht('This revision has no diffs. Something has gone quite wrong.'));
    }

    $revision->attachActiveDiff(last($diffs));

    $diff_vs = $request->getInt('vs');
    $target_id = $request->getInt('id');
    $target = idx($diffs, $target_id, end($diffs));

    $target_manual = $target;
    if (!$target_id) {
      foreach ($diffs as $diff) {
        if ($diff->getCreationMethod() != 'commit') {
          $target_manual = $diff;
        }
      }
    }

    if (empty($diffs[$diff_vs])) {
      $diff_vs = null;
    }

    $repository = null;
    $repository_phid = $target->getRepositoryPHID();
    if ($repository_phid) {
      if ($repository_phid == $revision->getRepositoryPHID()) {
        $repository = $revision->getRepository();
      } else {
        $repository = id(new PhabricatorRepositoryQuery())
          ->setViewer($viewer)
          ->withPHIDs(array($repository_phid))
          ->executeOne();
      }
    }

    list($changesets, $vs_map, $vs_changesets, $rendering_references) =
      $this->loadChangesetsAndVsMap(
        $target,
        idx($diffs, $diff_vs),
        $repository);

    if (count($rendering_references) > $this->getVeryLargeDiffLimit()) {
      $this->veryLargeDiff = true;
    }

    if ($request->getExists('download')) {
      return $this->buildRawDiffResponse(
        $revision,
        $changesets,
        $vs_changesets,
        $vs_map,
        $repository);
    }

    $map = $vs_map;
    if (!$map) {
      $map = array_fill_keys(array_keys($changesets), 0);
    }

    $old_ids = array();
    $new_ids = array();
    foreach ($map as $id => $vs) {
      if ($vs <= 0) {
        $old_ids[] = $id;
        $new_ids[] = $id;
      } else {
        $new_ids[] = $id;
        $new_ids[] = $vs;
      }
    }

    $this->loadDiffProperties($diffs);
    $props = $target_manual->getDiffProperties();

    $subscriber_phids = PhabricatorSubscribersQuery::loadSubscribersForPHID(
      $revision->getPHID());

    $object_phids = array_merge(
      $revision->getReviewerPHIDs(),
      $subscriber_phids,
      $revision->loadCommitPHIDs(),
      array(
        $revision->getAuthorPHID(),
        $viewer->getPHID(),
      ));

    foreach ($revision->getAttached() as $type => $phids) {
      foreach ($phids as $phid => $info) {
        $object_phids[] = $phid;
      }
    }

    $field_list = PhabricatorCustomField::getObjectFields(
      $revision,
      PhabricatorCustomField::ROLE_VIEW);

    $field_list->setViewer($viewer);
    $field_list->readFieldsFromStorage($revision);

    $warning_handle_map = array();
    foreach ($field_list->getFields() as $key => $field) {
      $req = $field->getRequiredHandlePHIDsForRevisionHeaderWarnings();
      foreach ($req as $phid) {
        $warning_handle_map[$key][] = $phid;
        $object_phids[] = $phid;
      }
    }

    $handles = $this->loadViewerHandles($object_phids);

    $request_uri = $request->getRequestURI();

    $limit = 100;
    $large = $request->getStr('large');
    if (count($changesets) > $limit && !$large) {
      $count = count($changesets);
      $warning = new PHUIInfoView();
      $warning->setTitle(pht('Large Diff'));
      $warning->setSeverity(PHUIInfoView::SEVERITY_WARNING);
      $warning->appendChild(hsprintf(
        '%s <strong>%s</strong>',
        pht(
          'This diff is large and affects %s files. '.
          'You may load each file individually or ',
          new PhutilNumber($count)),
        phutil_tag(
          'a',
          array(<commit_msg>Hide the "large" diff warning on "very large" diffs<commit_after><?php

final class DifferentialRevisionViewController extends DifferentialController {

  private $revisionID;
  private $veryLargeDiff;

  public function shouldAllowPublic() {
    return true;
  }

  public function isVeryLargeDiff() {
    return $this->veryLargeDiff;
  }

  public function getVeryLargeDiffLimit() {
    return 1000;
  }

  public function handleRequest(AphrontRequest $request) {
    $viewer = $this->getViewer();
    $this->revisionID = $request->getURIData('id');

    $viewer_is_anonymous = !$viewer->isLoggedIn();

    $revision = id(new DifferentialRevisionQuery())
      ->withIDs(array($this->revisionID))
      ->setViewer($viewer)
      ->needReviewers(true)
      ->needReviewerAuthority(true)
      ->executeOne();
    if (!$revision) {
      return new Aphront404Response();
    }

    $diffs = id(new DifferentialDiffQuery())
      ->setViewer($viewer)
      ->withRevisionIDs(array($this->revisionID))
      ->execute();
    $diffs = array_reverse($diffs, $preserve_keys = true);

    if (!$diffs) {
      throw new Exception(
        pht('This revision has no diffs. Something has gone quite wrong.'));
    }

    $revision->attachActiveDiff(last($diffs));

    $diff_vs = $request->getInt('vs');
    $target_id = $request->getInt('id');
    $target = idx($diffs, $target_id, end($diffs));

    $target_manual = $target;
    if (!$target_id) {
      foreach ($diffs as $diff) {
        if ($diff->getCreationMethod() != 'commit') {
          $target_manual = $diff;
        }
      }
    }

    if (empty($diffs[$diff_vs])) {
      $diff_vs = null;
    }

    $repository = null;
    $repository_phid = $target->getRepositoryPHID();
    if ($repository_phid) {
      if ($repository_phid == $revision->getRepositoryPHID()) {
        $repository = $revision->getRepository();
      } else {
        $repository = id(new PhabricatorRepositoryQuery())
          ->setViewer($viewer)
          ->withPHIDs(array($repository_phid))
          ->executeOne();
      }
    }

    list($changesets, $vs_map, $vs_changesets, $rendering_references) =
      $this->loadChangesetsAndVsMap(
        $target,
        idx($diffs, $diff_vs),
        $repository);

    if (count($rendering_references) > $this->getVeryLargeDiffLimit()) {
      $this->veryLargeDiff = true;
    }

    if ($request->getExists('download')) {
      return $this->buildRawDiffResponse(
        $revision,
        $changesets,
        $vs_changesets,
        $vs_map,
        $repository);
    }

    $map = $vs_map;
    if (!$map) {
      $map = array_fill_keys(array_keys($changesets), 0);
    }

    $old_ids = array();
    $new_ids = array();
    foreach ($map as $id => $vs) {
      if ($vs <= 0) {
        $old_ids[] = $id;
        $new_ids[] = $id;
      } else {
        $new_ids[] = $id;
        $new_ids[] = $vs;
      }
    }

    $this->loadDiffProperties($diffs);
    $props = $target_manual->getDiffProperties();

    $subscriber_phids = PhabricatorSubscribersQuery::loadSubscribersForPHID(
      $revision->getPHID());

    $object_phids = array_merge(
      $revision->getReviewerPHIDs(),
      $subscriber_phids,
      $revision->loadCommitPHIDs(),
      array(
        $revision->getAuthorPHID(),
        $viewer->getPHID(),
      ));

    foreach ($revision->getAttached() as $type => $phids) {
      foreach ($phids as $phid => $info) {
        $object_phids[] = $phid;
      }
    }

    $field_list = PhabricatorCustomField::getObjectFields(
      $revision,
      PhabricatorCustomField::ROLE_VIEW);

    $field_list->setViewer($viewer);
    $field_list->readFieldsFromStorage($revision);

    $warning_handle_map = array();
    foreach ($field_list->getFields() as $key => $field) {
      $req = $field->getRequiredHandlePHIDsForRevisionHeaderWarnings();
      foreach ($req as $phid) {
        $warning_handle_map[$key][] = $phid;
        $object_phids[] = $phid;
      }
    }

    $handles = $this->loadViewerHandles($object_phids);

    $request_uri = $request->getRequestURI();

    // Revisions with more than 100 files are "large".
    // Revisions with more than 1000 files are "very large".
    $limit = 100;
    $large = $request->getStr('large');

    $large_warning =
      (!$this->isVeryLargeDiff()) &&
      (count($changesets) > $limit) &&
      (!$large);

    if ($large_warning) {
      $count = count($changesets);
      $warning = new PHUIInfoView();
      $warning->setTitle(pht('Large Diff'));
      $warning->setSeverity(PHUIInfoView::SEVERITY_WARNING);
      $warning->appendChild(hsprintf(
        '%s <strong>%s</strong>',
        pht(
          'This diff is large and affects %s files. '.
          'You may load each file individually or ',
          new PhutilNumber($count)),
        phutil_tag(
          'a',
          array(
{'fraction_non_alphanumeric': 0.1526895383312156, 'fraction_numerical': 0.0032825074121135113, 'mean_word_length': 2.762948207171315, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 116, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1893937', 'n_tokens_mistral': 3125, 'n_tokens_neox': 3018, 'n_words': 549}