<commit_before><?php
namespace Craft;

class ImportService extends BaseApplicationComponent {

    public $log = array();

    public function columns($file) {
                
        // Open CSV file       
        $data = $this->_open($file);
        
        // Return only column names
        return array_shift($data);
       
    }
    
    public function data($file) {
        
        // Open CSV file
        $data = $this->_open($file);
        
        // Skip first row
        array_shift($data);

        // Return all data
        return $data;
    
    }
    
    public function row($row, $data, $settings) {
    
        // Get max power
        craft()->config->maxPowerCaptain();
        
        // See if map and data match (could not be due to malformed csv)
        if(count($settings['map']) != count($data)) {
        
            // Log errors when unsuccessful
            $this->log[$row] = craft()->import_history->log($settings->history, $row, array(array(Craft::t('Columns and data did not match, could be due to malformed CSV row.'))));            
            return;
        
        }
            
        // Map data to fields
        $fields = array_combine($settings['map'], $data);
        
        // If set, remove fields that will not be imported
        if(isset($fields['dont'])) {
            unset($fields['dont']);
        }
        
        // Set up new entry model
        $entry = new EntryModel();
        $entry->sectionId = $settings['section'];
        $entry->typeId = $settings['entrytype'];
        
        // If unique is non-empty array, we're replacing or deleting
        if(is_array($settings['unique']) && count($settings['unique']) > 1) {
        
            // Match with current data
            $criteria = craft()->elements->getCriteria(ElementType::Entry);
            $criteria->limit = null;
            $criteria->status = isset($settings['map']['status']) ? $settings['map']['status'] : null;
            $criteria->sectionId = $settings['section'];
            
            foreach($settings['map'] as $key => $value) {
                if(isset($criteria->$settings['map'][$key]) && isset($settings['unique'][$key]) && $settings['unique'][$key] == 1) {
                    $criteria->$settings['map'][$key] = $fields[$value];
                }
            } 
            
            // If there's a match...
            if($criteria->total()) {
                
                // If we're deleting
                if($settings['behavior'] == ImportModel::BehaviorDelete) {
                
                    // Get id's of elements to delete<commit_msg>Workaround to match titles as criteria<commit_after><?php
namespace Craft;

class ImportService extends BaseApplicationComponent {

    public $log = array();

    public function columns($file) {
                
        // Open CSV file       
        $data = $this->_open($file);
        
        // Return only column names
        return array_shift($data);
       
    }
    
    public function data($file) {
        
        // Open CSV file
        $data = $this->_open($file);
        
        // Skip first row
        array_shift($data);

        // Return all data
        return $data;
    
    }
    
    public function row($row, $data, $settings) {
    
        // Get max power
        craft()->config->maxPowerCaptain();
        
        // See if map and data match (could not be due to malformed csv)
        if(count($settings['map']) != count($data)) {
        
            // Log errors when unsuccessful
            $this->log[$row] = craft()->import_history->log($settings->history, $row, array(array(Craft::t('Columns and data did not match, could be due to malformed CSV row.'))));            
            return;
        
        }
            
        // Map data to fields
        $fields = array_combine($settings['map'], $data);
        
        // If set, remove fields that will not be imported
        if(isset($fields['dont'])) {
            unset($fields['dont']);
        }
        
        // Set up new entry model
        $entry = new EntryModel();
        $entry->sectionId = $settings['section'];
        $entry->typeId = $settings['entrytype'];
        
        // If unique is non-empty array, we're replacing or deleting
        if(is_array($settings['unique']) && count($settings['unique']) > 1) {
        
            // Match with current data
            $criteria = craft()->elements->getCriteria(ElementType::Entry);
            $criteria->limit = null;
            $criteria->status = isset($settings['map']['status']) ? $settings['map']['status'] : null;
            $criteria->sectionId = $settings['section'];
            
            // WORKAROUND: if we want to match titles
            // https://plus.google.com/105657957336798514916/posts/Si9WvnmoXmt
            if(isset($settings['map']['title'])) {
                $criteria->search("title::'{$title}'");
            }
            
            // Try to match fields on criteria
            foreach($settings['map'] as $key => $value) {
                if(isset($criteria->$settings['map'][$key]) && isset($settings['unique'][$key]) && $settings['unique'][$key] == 1) {
                    $criteria->$settings['map'][$key] = $fields[$value];
                }
            } 
            
            // If there's a match...
            if($criteria->total()) {
                
                // If we're deleting
                if($settings['behavior'] == ImportModel::BehaviorDelete) {
                
                    // Get id's of elements to delete
{'fraction_non_alphanumeric': 0.1447907390917186, 'fraction_numerical': 0.004630454140694568, 'mean_word_length': 1.6316776007497658, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 44, 'https://': 1, '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': '1522792', 'n_tokens_mistral': 1484, 'n_tokens_neox': 1424, 'n_words': 356}