<commit_before><?php

/*
 * Copyright 2011 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.
 */

class ManiphestTaskEditController extends ManiphestController {

  private $id;

  public function willProcessRequest(array $data) {
    $this->id = idx($data, 'id');
  }

  public function processRequest() {

    $request = $this->getRequest();
    $user = $request->getUser();

    $files = array();

    if ($this->id) {
      $task = id(new ManiphestTask())->load($this->id);
      if (!$task) {
        return new Aphront404Response();
      }
    } else {
      $task = new ManiphestTask();
      $task->setPriority(ManiphestTaskPriority::PRIORITY_TRIAGE);
      $task->setAuthorPHID($user->getPHID());

      // These allow task creation with defaults.
      if (!$request->isFormPost()) {
        $task->setTitle($request->getStr('title'));
      }

      $file_phids = $request->getArr('files', array());
      if (!$file_phids) {
        // Allow a single 'file' key instead, mostly since Mac OS X urlencodes
        // square brackets in URLs when passed to 'open', so you can't 'open'
        // a URL like '?files[]=xyz' and have PHP interpret it correctly.
        $phid = $request->getStr('file');
        if ($phid) {
          $file_phids = array($phid);
        }
      }

      if ($file_phids) {
        $files = id(new PhabricatorFile())->loadAllWhere(
          'phid IN (%Ls)',
          $file_phids);
      }
    }

    $errors = array();
    $e_title = true;

    if ($request->isFormPost()) {

      $changes = array();

      $new_title = $request->getStr('title');
      $new_desc = $request->getStr('description');

      if ($task->getID()) {
        if ($new_title != $task->getTitle()) {
          $changes[ManiphestTransactionType::TYPE_TITLE] = $new_title;
        }
        if ($new_desc != $task->getDescription()) {
          $changes[ManiphestTransactionType::TYPE_DESCRIPTION] = $new_desc;
        }
      } else {
        $task->setTitle($new_title);
        $task->setDescription($new_desc);
        $changes[ManiphestTransactionType::TYPE_STATUS] =
          ManiphestTaskStatus::STATUS_OPEN;
      }

      $owner_tokenizer = $request->getArr('assigned_to');
      $owner_phid = reset($owner_tokenizer);

      if (!strlen($new_title)) {
        $e_title = 'Required';
        $errors[] = 'Title is required.';
      }

      if (!$errors) {


        if ($request->getInt('priority') != $task->getPriority()) {
          $changes[ManiphestTransactionType::TYPE_PRIORITY] =
            $request->getInt('priority');
        }

        if ($owner_phid != $task->getOwnerPHID()) {
          $changes[ManiphestTransactionType::TYPE_OWNER] = $owner_phid;
        }

        if ($request->getArr('cc') != $task->getCCPHIDs()) {
          $changes[ManiphestTransactionType::TYPE_CCS] = $request->getArr('cc');
        }

        if ($request->getArr('projects') != $task->getProjectPHIDs()) {
          $changes[ManiphestTransactionType::TYPE_PROJECTS]
            = $request->getArr('projects');
        }

        if ($files) {
          $file_map = mpull($files, 'getPHID');
          $file_map = array_fill_keys($file_map, true);
          $changes[ManiphestTransactionType::TYPE_ATTACH] = array(
            PhabricatorPHIDConstants::PHID_TYPE_FILE => $file_map,
          );
        }

        $template = new ManiphestTransaction();
        $template->setAuthorPHID($user->getPHID());
        $transactions = array();

        foreach ($changes as $type => $value) {
          $transaction = clone $template;
          $transaction->setTransactionType($type);
          $transaction->setNewValue($value);
          $transactions[] = $transaction;
        }

        $editor = new ManiphestTransactionEditor();
        $editor->applyTransactions($task, $transactions);

        return id(new AphrontRedirectResponse())
          ->setURI('/T'.$task->getID());
      }
    } else {
      if (!$task->getID()) {
        $task->setCCPHIDs(array(
          $user->getPHID(),
        ));
      }
    }

    $phids = array_merge(
      array($task->getOwnerPHID()),
      $task->getCCPHIDs(),
      $task->getProjectPHIDs());
    $phids = array_filter($phids);
    $phids = array_unique($phids);<commit_msg>Fix Maniphest fatal when editing a task without changing anything<commit_after><?php

/*
 * Copyright 2011 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.
 */

class ManiphestTaskEditController extends ManiphestController {

  private $id;

  public function willProcessRequest(array $data) {
    $this->id = idx($data, 'id');
  }

  public function processRequest() {

    $request = $this->getRequest();
    $user = $request->getUser();

    $files = array();

    if ($this->id) {
      $task = id(new ManiphestTask())->load($this->id);
      if (!$task) {
        return new Aphront404Response();
      }
    } else {
      $task = new ManiphestTask();
      $task->setPriority(ManiphestTaskPriority::PRIORITY_TRIAGE);
      $task->setAuthorPHID($user->getPHID());

      // These allow task creation with defaults.
      if (!$request->isFormPost()) {
        $task->setTitle($request->getStr('title'));
      }

      $file_phids = $request->getArr('files', array());
      if (!$file_phids) {
        // Allow a single 'file' key instead, mostly since Mac OS X urlencodes
        // square brackets in URLs when passed to 'open', so you can't 'open'
        // a URL like '?files[]=xyz' and have PHP interpret it correctly.
        $phid = $request->getStr('file');
        if ($phid) {
          $file_phids = array($phid);
        }
      }

      if ($file_phids) {
        $files = id(new PhabricatorFile())->loadAllWhere(
          'phid IN (%Ls)',
          $file_phids);
      }
    }

    $errors = array();
    $e_title = true;

    if ($request->isFormPost()) {

      $changes = array();

      $new_title = $request->getStr('title');
      $new_desc = $request->getStr('description');

      if ($task->getID()) {
        if ($new_title != $task->getTitle()) {
          $changes[ManiphestTransactionType::TYPE_TITLE] = $new_title;
        }
        if ($new_desc != $task->getDescription()) {
          $changes[ManiphestTransactionType::TYPE_DESCRIPTION] = $new_desc;
        }
      } else {
        $task->setTitle($new_title);
        $task->setDescription($new_desc);
        $changes[ManiphestTransactionType::TYPE_STATUS] =
          ManiphestTaskStatus::STATUS_OPEN;
      }

      $owner_tokenizer = $request->getArr('assigned_to');
      $owner_phid = reset($owner_tokenizer);

      if (!strlen($new_title)) {
        $e_title = 'Required';
        $errors[] = 'Title is required.';
      }

      if (!$errors) {


        if ($request->getInt('priority') != $task->getPriority()) {
          $changes[ManiphestTransactionType::TYPE_PRIORITY] =
            $request->getInt('priority');
        }

        if ($owner_phid != $task->getOwnerPHID()) {
          $changes[ManiphestTransactionType::TYPE_OWNER] = $owner_phid;
        }

        if ($request->getArr('cc') != $task->getCCPHIDs()) {
          $changes[ManiphestTransactionType::TYPE_CCS] = $request->getArr('cc');
        }

        if ($request->getArr('projects') != $task->getProjectPHIDs()) {
          $changes[ManiphestTransactionType::TYPE_PROJECTS]
            = $request->getArr('projects');
        }

        if ($files) {
          $file_map = mpull($files, 'getPHID');
          $file_map = array_fill_keys($file_map, true);
          $changes[ManiphestTransactionType::TYPE_ATTACH] = array(
            PhabricatorPHIDConstants::PHID_TYPE_FILE => $file_map,
          );
        }

        $template = new ManiphestTransaction();
        $template->setAuthorPHID($user->getPHID());
        $transactions = array();

        foreach ($changes as $type => $value) {
          $transaction = clone $template;
          $transaction->setTransactionType($type);
          $transaction->setNewValue($value);
          $transactions[] = $transaction;
        }

        if ($transactions) {
          $editor = new ManiphestTransactionEditor();
          $editor->applyTransactions($task, $transactions);
        }

        return id(new AphrontRedirectResponse())
          ->setURI('/T'.$task->getID());
      }
    } else {
      if (!$task->getID()) {
        $task->setCCPHIDs(array(
          $user->getPHID(),
        ));
      }
    }

    $phids = array_merge(
      array($task->getOwnerPHID()),
      $task->getCCPHIDs(),
      $task->getProjectPHIDs());
    $phids = array_filter($phids);
    $phids = array_unique($phids);
{'fraction_non_alphanumeric': 0.14956865190728613, 'fraction_numerical': 0.0022866645878806776, 'mean_word_length': 2.5769516728624535, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 101, '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': '3636517', 'n_tokens_mistral': 2907, 'n_tokens_neox': 2838, 'n_words': 610}