<commit_before><?php

/**
 *
 * @package    Gems
 * @subpackage Export
 * @copyright  Copyright (c) 2015 <NAME>
 * @license    New BSD License
 */

namespace Gems\Export;

/**
 *
 * @package    Gems
 * @subpackage Export
 * @copyright  Copyright (c) 2015 <NAME>
 * @license    New BSD License
 * @since      Class available since version 1.7.1
 */
abstract class ExportAbstract extends \MUtil_Translate_TranslateableAbstract
{
    /**
     * @var \Zend_Session_Namespace    Own session used for non-batch exports
     */
    protected $_session;

    /**
     * @var \Gems_Task_TaskRunnerBatch   The batch object if one is set
     */
    protected $batch;

    /**
     * @var array   Data submitted by export form
     */
    protected $data;

    /**
     * @var string  The temporary filename while the file is being written
     */
    protected $tempFilename;

    /**
     * @var string  Current used file extension
     */
    protected $fileExtension;

    /**
     * @var string     The export file name, how it should be downloaded
     */
    protected $filename;

    /**
     * @var array   Array of all the filenames, new_name => temp_name
     */
    protected $files;

    /**
     * @var array   Model filters for export
     */
    protected $filter;

    /**
     * @var array   Array of the loaded first row of the model
     */
    protected $firstRow;

    /**
     * @var array   Array with the filter options that should be used for this exporter
     */
    protected $modelFilterAttributes = array('multiOptions', 'formatFunction', 'dateFormat', 'storageFormat', 'itemDisplay');

    /**
     *
     * @var integer Model Id for when multiple models are passed
     */
    protected $modelId;

    /**
     * @var \Gems_Loader
     */
    public $loader;

    /**
     * @var \MUtil_Model_ModelAbstract  Current model to export
     */
    protected $model;

    /**
     * @var array Filter settings of the current loaded model
     */
    protected $modelFilter;

    /**
     * @var integer     How many rows the batch will do in one go
     */
    protected $rowsPerBatch = 500;

    /**
     * @return string name of the specific export
     */
    abstract public function getName();

    /**
     * form elements for extra options for this particular export option
     * @param  \MUtil_Form $form Current form to add the form elements
     * @param  array $data current options set in the form
     * @return array Form elements
     */
    public function getFormElements(&$form, &$data) {}

    /**
     * Returns an array of ordered columnnames that have a label
     *
     * @return array Array of columnnames
     */
    public function getLabeledColumns()
    {
        if (!$this->model->hasMeta('labeledColumns')) {
            $orderedCols = $this->model->getItemsOrdered();

            $results = array();
            foreach ($orderedCols as $name) {
                if ($this->model->has($name, 'label')) {
                    $results[] = $name;
                }
            }

            $this->model->setMeta('labeledColumns', $results);
        }

        return $this->model->getMeta('labeledColumns');
    }

    /**
     * @return array Default values in form
     */
    public function getDefaultFormValues() {}
        
    /**
     * Add an export command with specific details. Can be batched.
     * @param array $data    Data submitted by export form
     * @param array $modelId Model Id when multiple models are passed
     */
    public function addExport($data, $modelId = false)
    {

        $this->files   = $this->getFiles();
        $this->data    = $data;
        $this->modelId = $modelId;

        if ($model = $this->getModel()) {
            $totalRows  = $this->getModelCount();
            $this->addFile();
            $this->addHeader($this->tempFilename . $this->fileExtension);
            $currentRow = 0;
            do {
                $filter['limit'] = array($this->rowsPerBatch, $currentRow);
                if ($this->batch) {
                    $this->batch->addTask('Export_ExportCommand', $data['type'], 'addRows', $data, $modelId, $this->tempFilename, $filter);
                } else {
                    $this->addRows($data, $modelId, $this->tempFilename, $filter);
                }
                $currentRow = $currentRow + $this->rowsPerBatch;
            } while ($currentRow < $totalRows);

            if ($this->batch) {
                $this->batch->addTask('Export_ExportCommand', $data['type'], 'addFooter', $this->tempFilename . $this->fileExtension);
                $this->batch->setSessionVariable('files', $this->files);
            } else {
                $this->addFooter($this->tempFilename . $this->fileExtension);
                $this->_session        = new \Zend_Session_Namespace(__CLASS__);
                $this->_session->files = $this->files;
            }
        }
    }

    /**
     * Creates a new file and adds it to the files array
     */
    protected function addFile()
    {
        $tempFilename       = GEMS_ROOT_DIR . '/var/tmp/export-' . md5(time() . rand());
        $this->tempFilename = $tempFilename;
        $basename           = $this->cleanupName($this->model->getName());
        $filename           = $basename;
        $i                  = 1;
        while (isset($this->files[$filename . $this->fileExtension])) {
            $filename = $basename . '_' . $i;
            $i++;
        }
        $this->filename = $filename;
<commit_msg>Export now creates tmp directory if it doesn't exist<commit_after><?php

/**
 *
 * @package    Gems
 * @subpackage Export
 * @copyright  Copyright (c) 2015 <NAME>
 * @license    New BSD License
 */

namespace Gems\Export;

/**
 *
 * @package    Gems
 * @subpackage Export
 * @copyright  Copyright (c) 2015 <NAME>
 * @license    New BSD License
 * @since      Class available since version 1.7.1
 */
abstract class ExportAbstract extends \MUtil_Translate_TranslateableAbstract
{
    /**
     * @var \Zend_Session_Namespace    Own session used for non-batch exports
     */
    protected $_session;

    /**
     * @var \Gems_Task_TaskRunnerBatch   The batch object if one is set
     */
    protected $batch;

    /**
     * @var array   Data submitted by export form
     */
    protected $data;

    /**
     * @var string  The temporary filename while the file is being written
     */
    protected $tempFilename;

    /**
     * @var string  Current used file extension
     */
    protected $fileExtension;

    /**
     * @var string     The export file name, how it should be downloaded
     */
    protected $filename;

    /**
     * @var array   Array of all the filenames, new_name => temp_name
     */
    protected $files;

    /**
     * @var array   Model filters for export
     */
    protected $filter;

    /**
     * @var array   Array of the loaded first row of the model
     */
    protected $firstRow;

    /**
     * @var array   Array with the filter options that should be used for this exporter
     */
    protected $modelFilterAttributes = array('multiOptions', 'formatFunction', 'dateFormat', 'storageFormat', 'itemDisplay');

    /**
     *
     * @var integer Model Id for when multiple models are passed
     */
    protected $modelId;

    /**
     * @var \Gems_Loader
     */
    public $loader;

    /**
     * @var \MUtil_Model_ModelAbstract  Current model to export
     */
    protected $model;

    /**
     * @var array Filter settings of the current loaded model
     */
    protected $modelFilter;

    /**
     * @var integer     How many rows the batch will do in one go
     */
    protected $rowsPerBatch = 500;

    /**
     * @return string name of the specific export
     */
    abstract public function getName();

    /**
     * form elements for extra options for this particular export option
     * @param  \MUtil_Form $form Current form to add the form elements
     * @param  array $data current options set in the form
     * @return array Form elements
     */
    public function getFormElements(&$form, &$data) {}

    /**
     * Returns an array of ordered columnnames that have a label
     *
     * @return array Array of columnnames
     */
    public function getLabeledColumns()
    {
        if (!$this->model->hasMeta('labeledColumns')) {
            $orderedCols = $this->model->getItemsOrdered();

            $results = array();
            foreach ($orderedCols as $name) {
                if ($this->model->has($name, 'label')) {
                    $results[] = $name;
                }
            }

            $this->model->setMeta('labeledColumns', $results);
        }

        return $this->model->getMeta('labeledColumns');
    }

    /**
     * @return array Default values in form
     */
    public function getDefaultFormValues() {}
        
    /**
     * Add an export command with specific details. Can be batched.
     * @param array $data    Data submitted by export form
     * @param array $modelId Model Id when multiple models are passed
     */
    public function addExport($data, $modelId = false)
    {

        $this->files   = $this->getFiles();
        $this->data    = $data;
        $this->modelId = $modelId;

        if ($model = $this->getModel()) {
            $totalRows  = $this->getModelCount();
            $this->addFile();
            $this->addHeader($this->tempFilename . $this->fileExtension);
            $currentRow = 0;
            do {
                $filter['limit'] = array($this->rowsPerBatch, $currentRow);
                if ($this->batch) {
                    $this->batch->addTask('Export_ExportCommand', $data['type'], 'addRows', $data, $modelId, $this->tempFilename, $filter);
                } else {
                    $this->addRows($data, $modelId, $this->tempFilename, $filter);
                }
                $currentRow = $currentRow + $this->rowsPerBatch;
            } while ($currentRow < $totalRows);

            if ($this->batch) {
                $this->batch->addTask('Export_ExportCommand', $data['type'], 'addFooter', $this->tempFilename . $this->fileExtension);
                $this->batch->setSessionVariable('files', $this->files);
            } else {
                $this->addFooter($this->tempFilename . $this->fileExtension);
                $this->_session        = new \Zend_Session_Namespace(__CLASS__);
                $this->_session->files = $this->files;
            }
        }
    }

    /**
     * Creates a new file and adds it to the files array
     */
    protected function addFile()
    {
        $exportTempDir = GEMS_ROOT_DIR . DIRECTORY_SEPARATOR . 'var' . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR;

        if (! is_dir($exportTempDir)) {
            $oldmask = umask(0777);
            if (! @mkdir($exportTempDir, 0777, true)) {
                $this->throwLastError(sprintf($this->translate->_("Could not create '%s' directory."), $exportTempDir));
            }
            umask($oldmask);
        }

        $tempFilename       = $exportTempDir . 'export-' . md5(time() . rand());
        $this->tempFilename = $tempFilename;
        $basename           = $this->cleanupName($this->model->getName());
        $filename           = $basename;
        $i                  = 1;
        while (isset($this->files[$filename . $this->fileExtension])) {
            $filename = $basename . '_' . $i;
            $i++;
        }
        $this->filename = $filename;

{'fraction_non_alphanumeric': 0.1253601047577477, 'fraction_numerical': 0.0036665211697948495, 'mean_word_length': 1.9602067183462533, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 112, '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': '7233892', 'n_tokens_mistral': 3226, 'n_tokens_neox': 3091, 'n_words': 884}