<commit_before><?php

App::uses('AppController', 'Controller');

class DecayingModelController extends AppController
{
    public $components = array('Security' ,'RequestHandler');

    public $paginate = array(
            'limit' => 50,
            'order' => array(
                    'DecayingModel.ID' => 'desc'
            )
    );

    public function update($force=false)
    {
        if (!$this->_isSiteAdmin()) {
            throw new MethodNotAllowedException(__('You are not authorised to update.'));
        }

        if ($this->request->is('post') || $this->request->is('put')) {
            $this->DecayingModel->update($force, $this->Auth->user());
            $message = __('Default decaying models updated');
            if ($this->_isRest()) {
                return $this->RestResponse->saveSuccessResponse('DecayingModel', 'update', false, $this->response->type(), $message);
            } else {
                $this->Flash->success($message);
                $this->redirect(array('controller' => 'decayingModel', 'action' => 'index'));
            }
        } else {
            throw new MethodNotAllowedException(__("This method is not allowed"));
        }
    }

    public function export($model_id)
    {
        $model = $this->DecayingModel->fetchModel($this->Auth->user(), $model_id, true);
        unset($model['DecayingModel']['id'], $model['DecayingModel']['org_id'], $model['DecayingModelMapping']);
        return $this->RestResponse->viewData($model, $this->response->type());
    }

    public function import()
    {
        if ($this->request->is('post') || $this->request->is('put')) {
            $data = $this->request->data['DecayingModel'];
            if ($data['submittedjson']['name'] != '' && $data['json'] != '') {
                throw new MethodNotAllowedException(__('Only one import field can be used'));
            }
            if ($data['submittedjson']['size'] > 0) {
                $filename = basename($data['submittedjson']['name']);
                $file_content = file_get_contents($data['submittedjson']['tmp_name']);
                if ((isset($data['submittedjson']['error']) && $data['submittedjson']['error'] == 0) ||
                    (!empty($data['submittedjson']['tmp_name']) && $data['submittedjson']['tmp_name'] != '')
                ) {
                    if (!$file_content) {
                        throw new InternalErrorException(__('PHP says file was not uploaded. Are you attacking me?'));
                    }
                }
                $text = $file_content;
            } else {
                $text = $data['json'];
            }
            $json = json_decode($text, true);
            if ($json === null) {
                throw new MethodNotAllowedException(__('Error while decoding JSON'));
            }
            
            unset($json['id']);
            unset($json['uuid']);
            $json['default'] = 0;
            $json['org_id'] = $this->Auth->user()['org_id'];

            if ($this->DecayingModel->save($json)) {
                $this->Flash->success(__('The model has been imported.'));
            } else {
                $this->Flash->error(__('Error while importing model.'));
            }
            $this->redirect(array('action' => 'index'));
        }
    }

    public function view($id)
    {
        if (!$this->request->is('get')) {
            throw new MethodNotAllowedException(__('This method is not allowed'));
        }

        $decaying_model = $this->DecayingModel->fetchModel($this->Auth->user(), $id, true);<commit_msg>chg: [decaying:import] When importing a model, mapping is imported as custom mapping<commit_after><?php

App::uses('AppController', 'Controller');

class DecayingModelController extends AppController
{
    public $components = array('Security' ,'RequestHandler');

    public $paginate = array(
            'limit' => 50,
            'order' => array(
                    'DecayingModel.ID' => 'desc'
            )
    );

    public function update($force=false)
    {
        if (!$this->_isSiteAdmin()) {
            throw new MethodNotAllowedException(__('You are not authorised to update.'));
        }

        if ($this->request->is('post') || $this->request->is('put')) {
            $this->DecayingModel->update($force, $this->Auth->user());
            $message = __('Default decaying models updated');
            if ($this->_isRest()) {
                return $this->RestResponse->saveSuccessResponse('DecayingModel', 'update', false, $this->response->type(), $message);
            } else {
                $this->Flash->success($message);
                $this->redirect(array('controller' => 'decayingModel', 'action' => 'index'));
            }
        } else {
            throw new MethodNotAllowedException(__("This method is not allowed"));
        }
    }

    public function export($model_id)
    {
        $model = $this->DecayingModel->fetchModel($this->Auth->user(), $model_id, true);
        unset($model['DecayingModel']['id'], $model['DecayingModel']['org_id'], $model['DecayingModelMapping']);
        return $this->RestResponse->viewData($model, $this->response->type());
    }

    public function import()
    {
        if ($this->request->is('post') || $this->request->is('put')) {
            $data = $this->request->data['DecayingModel'];
            if ($data['submittedjson']['name'] != '' && $data['json'] != '') {
                throw new MethodNotAllowedException(__('Only one import field can be used'));
            }
            if ($data['submittedjson']['size'] > 0) {
                $filename = basename($data['submittedjson']['name']);
                $file_content = file_get_contents($data['submittedjson']['tmp_name']);
                if ((isset($data['submittedjson']['error']) && $data['submittedjson']['error'] == 0) ||
                    (!empty($data['submittedjson']['tmp_name']) && $data['submittedjson']['tmp_name'] != '')
                ) {
                    if (!$file_content) {
                        throw new InternalErrorException(__('PHP says file was not uploaded. Are you attacking me?'));
                    }
                }
                $text = $file_content;
            } else {
                $text = $data['json'];
            }
            $json = json_decode($text, true);
            if ($json === null) {
                throw new MethodNotAllowedException(__('Error while decoding JSON'));
            }
            
            unset($json['id']);
            unset($json['uuid']);
            $json['default'] = 0;
            $json['org_id'] = $this->Auth->user()['org_id'];

            $attribute_types = array();
            if (!empty($json['attribute_types'])) {
                $attribute_types = $json['attribute_types'];
                unset($json['attribute_types']);
            }

            if ($this->DecayingModel->save($json)) {
                $saved_model = array(
                    'model_id' => $this->DecayingModel->id,
                    'attribute_types' => $attribute_types
                );
                if (!empty($saved_model['attribute_types'])) {
                    $result = $this->DecayingModel->DecayingModelMapping->resetMappingForModel($saved_model, $this->Auth->user());
                } else {
                    $result = true;
                }
                if (!empty($result)) {
                    $this->Flash->success(__('The model has been imported.'));
                } else {
                    $this->Flash->error(__('The model has been but mapping. However importing mapping failed.'));
                }
            } else {
                $this->Flash->error(__('Error while importing model.'));
            }
            $this->redirect(array('action' => 'index'));
        }
    }

    public function view($id)
    {
        if (!$this->request->is('get')) {
            throw new MethodNotAllowedException(__('This method is not allowed'));
        }

        $decaying_model = $this->DecayingModel->fetchModel($this->Auth->user(), $id, true);
{'fraction_non_alphanumeric': 0.17156983930778738, 'fraction_numerical': 0.0012360939431396785, 'mean_word_length': 2.023542600896861, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 120, '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': '6876601', 'n_tokens_mistral': 2165, 'n_tokens_neox': 2063, 'n_words': 400}