<commit_before><?php

/*
 * Copyright 2012 Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

final class DifferentialRevisionUpdateHistoryView extends AphrontView {

  private $diffs = array();
  private $selectedVersusDiffID;
  private $selectedDiffID;
  private $selectedWhitespace;
  private $user;

  public function setDiffs($diffs) {
    $this->diffs = $diffs;
    return $this;
  }

  public function setSelectedVersusDiffID($id) {
    $this->selectedVersusDiffID = $id;
    return $this;
  }

  public function setSelectedDiffID($id) {
    $this->selectedDiffID = $id;
    return $this;
  }

  public function setSelectedWhitespace($whitespace) {
    $this->selectedWhitespace = $whitespace;
    return $this;
  }

  public function setUser($user) {
    $this->user = $user;
    return $this;
  }

  public function getUser() {
    return $this->user;
  }

  public function render() {

    require_celerity_resource('differential-core-view-css');
    require_celerity_resource('differential-revision-history-css');

    $data = array(
      array(
        'name' => 'Base',
        'id'   => null,
        'desc' => 'Base',
        'age'  => null,
        'obj'  => null,
      ),
    );

    $seq = 0;
    foreach ($this->diffs as $diff) {
      $data[] = array(
        'name' => 'Diff '.(++$seq),
        'id'   => $diff->getID(),
        'desc' => $diff->getDescription(),
        'age'  => $diff->getDateCreated(),
        'obj'  => $diff,
      );
    }

    $max_id = $diff->getID();

    $idx = 0;
    $rows = array();
    $disable = false;
    $radios = array();
    $last_base = null;
    foreach ($data as $row) {

      $name = phutil_escape_html($row['name']);
      $id   = phutil_escape_html($row['id']);

      $old_class = null;
      $new_class = null;

      if ($id) {
        $new_checked = ($this->selectedDiffID == $id);
        $new = javelin_render_tag(
          'input',
          array(
            'type' => 'radio',
            'name' => 'id',
            'value' => $id,
            'checked' => $new_checked ? 'checked' : null,
            'sigil' => 'differential-new-radio',
          ));
        if ($new_checked) {
          $new_class = " revhistory-new-now";
          $disable = true;
        }
      } else {
        $new = null;
      }

      if ($max_id != $id) {
        $uniq = celerity_generate_unique_node_id();
        $old_checked = ($this->selectedVersusDiffID == $id);
        $old = phutil_render_tag(
          'input',
          array(
            'type' => 'radio',
            'name' => 'vs',
            'value' => $id,
            'id' => $uniq,
            'checked' => $old_checked ? 'checked' : null,
            'disabled' => $disable ? 'disabled' : null,
          ));
        $radios[] = $uniq;
        if ($old_checked) {
          $old_class = " revhistory-old-now";
        }
      } else {
        $old = null;
      }

      $desc = $row['desc'];

      if ($row['age']) {
        $age = phabricator_datetime($row['age'], $this->getUser());
      } else {
        $age = null;
      }

      if (++$idx % 2) {
        $class = ' class="alt"';
      } else {
        $class = null;
      }

      if ($row['obj']) {
        $lint = self::renderDiffLintStar($row['obj']);
        $unit = self::renderDiffUnitStar($row['obj']);
        $lint_message = self::getDiffLintMessage($diff);
        $unit_message = self::getDiffUnitMessage($diff);
        $lint_title = ' title="'.phutil_escape_html($lint_message).'"';
        $unit_title = ' title="'.phutil_escape_html($unit_message).'"';
      } else {
        $lint = null;
        $unit = null;
        $lint_title = null;
        $unit_title = null;
      }

      $base = $this->renderBaseRevision($diff);
      if ($last_base !== null && $base !== $last_base) {
        // TODO: Render some kind of notice about rebases.
      }
      $last_base = $base;

      $rows[] =
        '<tr'.$class.'>'.
          '<td class="revhistory-name">'.$name.'</td>'.
          '<td class="revhistory-id">'.$id.'</td>'.
          '<td class="revhistory-base">'.phutil_escape_html($base).'</td>'.
          '<td class="revhistory-desc">'.phutil_escape_html($desc).'</td>'.
          '<td class="revhistory-age">'.$age.'</td>'.
          '<td class="revhistory-star"'.$lint_title.'>'.$lint.'</td>'.
          '<td class="revhistory-star"'.$unit_title.'>'.$unit.'</td>'.
          '<td class="revhistory-old'.$old_class.'">'.$old.'</td>'.
          '<td class="revhistory-new'.$new_class.'">'.$new.'</td>'.
        '</tr>';
    }

    Javelin::initBehavior(
      'differential-diff-radios',
      array(
        'radios' => $radios,
      ));

    $options = array(
      'ignore-all' => 'Ignore Most',
      'ignore-trailing' => 'Ignore Trailing',
      'show-all' => 'Show All',
    );
<commit_msg>Fix issue where all rows in "Revision Update History" render the same base<commit_after><?php

/*
 * Copyright 2012 Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

final class DifferentialRevisionUpdateHistoryView extends AphrontView {

  private $diffs = array();
  private $selectedVersusDiffID;
  private $selectedDiffID;
  private $selectedWhitespace;
  private $user;

  public function setDiffs($diffs) {
    $this->diffs = $diffs;
    return $this;
  }

  public function setSelectedVersusDiffID($id) {
    $this->selectedVersusDiffID = $id;
    return $this;
  }

  public function setSelectedDiffID($id) {
    $this->selectedDiffID = $id;
    return $this;
  }

  public function setSelectedWhitespace($whitespace) {
    $this->selectedWhitespace = $whitespace;
    return $this;
  }

  public function setUser($user) {
    $this->user = $user;
    return $this;
  }

  public function getUser() {
    return $this->user;
  }

  public function render() {

    require_celerity_resource('differential-core-view-css');
    require_celerity_resource('differential-revision-history-css');

    $data = array(
      array(
        'name' => 'Base',
        'id'   => null,
        'desc' => 'Base',
        'age'  => null,
        'obj'  => null,
      ),
    );

    $seq = 0;
    foreach ($this->diffs as $diff) {
      $data[] = array(
        'name' => 'Diff '.(++$seq),
        'id'   => $diff->getID(),
        'desc' => $diff->getDescription(),
        'age'  => $diff->getDateCreated(),
        'obj'  => $diff,
      );
    }

    $max_id = $diff->getID();

    $idx = 0;
    $rows = array();
    $disable = false;
    $radios = array();
    $last_base = null;
    foreach ($data as $row) {

      $diff = $row['obj'];
      $name = phutil_escape_html($row['name']);
      $id   = phutil_escape_html($row['id']);

      $old_class = null;
      $new_class = null;

      if ($id) {
        $new_checked = ($this->selectedDiffID == $id);
        $new = javelin_render_tag(
          'input',
          array(
            'type' => 'radio',
            'name' => 'id',
            'value' => $id,
            'checked' => $new_checked ? 'checked' : null,
            'sigil' => 'differential-new-radio',
          ));
        if ($new_checked) {
          $new_class = " revhistory-new-now";
          $disable = true;
        }
      } else {
        $new = null;
      }

      if ($max_id != $id) {
        $uniq = celerity_generate_unique_node_id();
        $old_checked = ($this->selectedVersusDiffID == $id);
        $old = phutil_render_tag(
          'input',
          array(
            'type' => 'radio',
            'name' => 'vs',
            'value' => $id,
            'id' => $uniq,
            'checked' => $old_checked ? 'checked' : null,
            'disabled' => $disable ? 'disabled' : null,
          ));
        $radios[] = $uniq;
        if ($old_checked) {
          $old_class = " revhistory-old-now";
        }
      } else {
        $old = null;
      }

      $desc = $row['desc'];

      if ($row['age']) {
        $age = phabricator_datetime($row['age'], $this->getUser());
      } else {
        $age = null;
      }

      if (++$idx % 2) {
        $class = ' class="alt"';
      } else {
        $class = null;
      }

      if ($diff) {
        $lint = self::renderDiffLintStar($row['obj']);
        $unit = self::renderDiffUnitStar($row['obj']);
        $lint_message = self::getDiffLintMessage($diff);
        $unit_message = self::getDiffUnitMessage($diff);
        $lint_title = ' title="'.phutil_escape_html($lint_message).'"';
        $unit_title = ' title="'.phutil_escape_html($unit_message).'"';
        $base = $this->renderBaseRevision($diff);
      } else {
        $lint = null;
        $unit = null;
        $lint_title = null;
        $unit_title = null;
        $base = null;
      }

      if ($last_base !== null && $base !== $last_base) {
        // TODO: Render some kind of notice about rebases.
      }
      $last_base = $base;

      $rows[] =
        '<tr'.$class.'>'.
          '<td class="revhistory-name">'.$name.'</td>'.
          '<td class="revhistory-id">'.$id.'</td>'.
          '<td class="revhistory-base">'.phutil_escape_html($base).'</td>'.
          '<td class="revhistory-desc">'.phutil_escape_html($desc).'</td>'.
          '<td class="revhistory-age">'.$age.'</td>'.
          '<td class="revhistory-star"'.$lint_title.'>'.$lint.'</td>'.
          '<td class="revhistory-star"'.$unit_title.'>'.$unit.'</td>'.
          '<td class="revhistory-old'.$old_class.'">'.$old.'</td>'.
          '<td class="revhistory-new'.$new_class.'">'.$new.'</td>'.
        '</tr>';
    }

    Javelin::initBehavior(
      'differential-diff-radios',
      array(
        'radios' => $radios,
      ));

    $options = array(
      'ignore-all' => 'Ignore Most',
      'ignore-trailing' => 'Ignore Trailing',
      'show-all' => 'Show All',
    );

{'fraction_non_alphanumeric': 0.17390094602114636, 'fraction_numerical': 0.0020404377666481174, 'mean_word_length': 2.176141384388807, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 123, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5401304', 'n_tokens_mistral': 3505, 'n_tokens_neox': 3471, 'n_words': 740}