<commit_before><?php

    require_once(dirname(__FILE__).'/../class.templateparser.php');

    class Download extends Page {
        
        public static $arg_list = array('id' => '\d+',
                              'aid' => '\d+',
                              'run' => '\d+',
                              'visit' => '\w+\d+-\d+',
                              'u' => '\w+\d+',
                              's' => '\d',
                              'log' => '\d',
                              'archive' => '\d',
                              'LogFiles' => '([\w|\.])+',
                              'ty' => '\w+',
                              'pdb' => '\d',
                              'map' => '\d',
                              'validity' => '.*',

                              'dt' => '\w+',
                              'ppl' => '\w+',

                              'filetype' => '\w+',
                              'blsampleid' => '\d+',
                              'dcg' => '\d+',

                              'download' => '\d',
                              'AUTOPROCPROGRAMID' => '\d+',
                              'AUTOPROCPROGRAMATTACHMENTID' => '\d+',

                              'PHASINGPROGRAMATTACHMENTID' => '\d+',
                              );

    
        public static $dispatch = array(array('/map(/pdb/:pdb)(/ty/:ty)(/dt/:dt)(/ppl/:ppl)(/id/:id)(/map/:map)', 'get', '_map'),
                              array('/id/:id/aid/:aid(/log/:log)(/1)(/LogFiles/:LogFiles)(/archive/:archive)', 'get', '_auto_processing'),
                              array('/plots', 'get', '_auto_processing_plots'),
                              array('/ep/id/:id(/log/:log)', 'get', '_ep_mtz'),
                              array('/dimple/id/:id(/log/:log)', 'get', '_dimple_mtz'),
                              array('/mrbump/id/:id(/log/:log)', 'get', '_mrbump_mtz'),
                              array('/csv/visit/:visit', 'get', '_csv_report'),
                              array('/bl/visit/:visit/run/:run', 'get', '_blend_mtz'),
                              array('/sign', 'post', '_sign_url'),
                              array('/bigep/id/:id/dt/:dt/ppl/:ppl(/log/:log)', 'get', '_bigep_mtz'),
                              array('/data/visit/:visit', 'get', '_download_visit'),
                              array('/attachments', 'get', '_get_attachments'),
                              array('/attachment/id/:id/aid/:aid', 'get', '_get_attachment'),
                              array('/dc/id/:id', 'get', '_download'),

                              array('/ap/attachments(/:AUTOPROCPROGRAMATTACHMENTID)(/dl/:download)', 'get', '_get_autoproc_attachments'),
                              array('/ph/attachments(/:PHASINGPROGRAMATTACHMENTID)(/dl/:download)', 'get', '_get_phasing_attachments'),
            );

        
        # ------------------------------------------------------------------------
        # Generate a one time token for access to downloads
        function _sign_url() {
            if (!$this->has_arg('validity')) $this->_error('No validity specified');
            $token = md5(uniqid());

            $this->db->pq("INSERT INTO SW_onceToken (token, validity, proposalid, personid) VALUES (:1, :2, :3, :4)", array($token, $this->arg('validity'), $this->proposalid, $this->user->personid));
            $this->_output(array('token' => $token));
        }


        # ------------------------------------------------------------------------
        # SAXS Specific Visit Download Link
        function _download_visit() {
            $tp = new TemplateParser($this->db);
            $visit = $tp->visit_dir(array('VISIT' => $this->arg('visit')));
            
            $data = $visit.'/.ispyb/download/download.zip';
            if (file_exists($data)) {
                $this->_header($this->arg('visit').'_download.zip');
                readfile($data);
            } else $this->_error('There doesnt seem to be a data archive available for this visit');
        }

        # ------------------------------------------------------------------------
        # Download mtz/log file for Fast DP / XIA2
        #   TODO: Delete me
        function _auto_processing_plots() {
            global $ap_types;
            if (!$this->has_arg('id')) $this->_error('No data collection', 'No data collection id specified');
            
            $rows = $this->db->pq("SELECT appa.filename, appa.filepath, api.autoprocprogramid, app.processingcommandline as type
                FROM autoprocintegration api 
                INNER JOIN autoprocscaling_has_int aph ON api.autoprocintegrationid = aph.autoprocintegrationid 
                INNER JOIN autoprocscaling aps ON aph.autoprocscalingid = aps.autoprocscalingid 
                INNER JOIN autoproc ap ON aps.autoprocid = ap.autoprocid 
                INNER JOIN autoprocprogram app ON api.autoprocprogramid = app.autoprocprogramid 
                INNER JOIN autoprocprogramattachment appa ON appa.autoprocprogramid = app.autoprocprogramid 
                WHERE appa.filetype='Graph' AND api.datacollectionid = :1", array($this->arg('id')));

            foreach ($rows as &$r) {
                foreach ($ap_types as $id => $name) {
                    if (strpos($r['TYPE'], $id)) {
                        $r['TYPE'] = $name;
                        break;
                    }
                }

                $json = $r['FILEPATH'].'/'.$r['FILENAME'];
                $r['PLOTS'] = array();
                if (file_exists($json)) {
                    $cont = file_get_contents($json);
                    $r['PLOTS'] = json_decode($cont);
                }

                unset($r['FILENAME']);
                unset($r['FILEPATH']);
            }
            
            $this->_output($rows);
        }


        # ------------------------------------------------------------------------
        # Download mtz/log file for Fast DP / XIA2
        #   TODO: Delete me
        function _auto_processing() {
            ini_set('memory_limit', '512M');

            if (!$this->has_arg('id')) $this->_error('No data collection', 'No data collection id specified');
            if (!$this->has_arg('aid')) $this->_error('No auto processing id', 'No auto processing id specified');
            
            $rows = $this->db->pq('SELECT appa.filename,appa.filepath,appa.filetype FROM autoprocintegration api INNER JOIN autoprocscaling_has_int aph ON api.autoprocintegrationid = aph.autoprocintegrationid INNER JOIN autoprocscaling aps ON aph.autoprocscalingid = aps.autoprocscalingid INNER JOIN autoproc ap ON aps.autoprocid = ap.autoprocid INNER JOIN autoprocprogram app ON api.autoprocprogramid = app.autoprocprogramid INNER JOIN autoprocprogramattachment appa ON appa.autoprocprogramid = app.autoprocprogramid WHERE api.datacollectionid = :1 AND api.autoprocprogramid=:2', array($this->arg('id'), $this->arg('aid')));
            
            $this->db->close();

            // print_r($rows);

            if (!sizeof($rows)) $this->_error('No such auto processing');
            // else $r = $rows[0];
        
            foreach ($rows as $r) {
                if ($this->has_arg('log') || $this->has_arg('archive')) {
                    if ($r['FILETYPE'] == 'Log') {
                        if ($this->has_arg('LogFiles')) {
                            $f = $r['FILEPATH'].'/LogFiles/'.$this->arg('LogFiles');
                            if (pathinfo($this->arg('LogFiles'), PATHINFO_EXTENSION) == 'html') $this->app->contentType("text/html");
                            else $this->app->contentType("text/plain");
                            
                        } else {

                            if ($this->has_arg('archive')) {
                                $tar = '/tmp/'.$this->arg('aid').'.tar';
                                $f = $tar.'.gz';

                                if (!file_exists($f)) {
                                    $a = new PharData($tar);
                                    $a->buildFromDirectory($r['FILEPATH'], '/^((?!(HKL|cbf|dimple)).)*$/');
                                    $a->compress(Phar::GZ);

                                    unlink($tar);
                                }

                                $this->_header($this->arg('aid').'.tar.gz');
                                readfile($f);
                                exit;

                            } else {
                                $f = $r['FILEPATH'].'/'.$r['FILENAME'];
                                if ($r['FILENAME'] == 'fast_dp.log') $this->app->contentType("text/plain");
                                if ($r['FILENAME'] == 'autoPROC.log') $this->app->contentType("text/plain");
                            }
                        }
                        
                        if (file_exists($f)) readfile($f);
                    }
                
                } else {
                    // XIA2
                    if ($r['FILETYPE'] == 'Result') {
                        $f = $r['FILEPATH'].'/'.$r['FILENAME'];
                        if (file_exists($f)) {<commit_msg>limit to the first log file found<commit_after><?php

    require_once(dirname(__FILE__).'/../class.templateparser.php');

    class Download extends Page {
        
        public static $arg_list = array('id' => '\d+',
                              'aid' => '\d+',
                              'run' => '\d+',
                              'visit' => '\w+\d+-\d+',
                              'u' => '\w+\d+',
                              's' => '\d',
                              'log' => '\d',
                              'archive' => '\d',
                              'LogFiles' => '([\w|\.])+',
                              'ty' => '\w+',
                              'pdb' => '\d',
                              'map' => '\d',
                              'validity' => '.*',

                              'dt' => '\w+',
                              'ppl' => '\w+',

                              'filetype' => '\w+',
                              'blsampleid' => '\d+',
                              'dcg' => '\d+',

                              'download' => '\d',
                              'AUTOPROCPROGRAMID' => '\d+',
                              'AUTOPROCPROGRAMATTACHMENTID' => '\d+',

                              'PHASINGPROGRAMATTACHMENTID' => '\d+',
                              );

    
        public static $dispatch = array(array('/map(/pdb/:pdb)(/ty/:ty)(/dt/:dt)(/ppl/:ppl)(/id/:id)(/map/:map)', 'get', '_map'),
                              array('/id/:id/aid/:aid(/log/:log)(/1)(/LogFiles/:LogFiles)(/archive/:archive)', 'get', '_auto_processing'),
                              array('/plots', 'get', '_auto_processing_plots'),
                              array('/ep/id/:id(/log/:log)', 'get', '_ep_mtz'),
                              array('/dimple/id/:id(/log/:log)', 'get', '_dimple_mtz'),
                              array('/mrbump/id/:id(/log/:log)', 'get', '_mrbump_mtz'),
                              array('/csv/visit/:visit', 'get', '_csv_report'),
                              array('/bl/visit/:visit/run/:run', 'get', '_blend_mtz'),
                              array('/sign', 'post', '_sign_url'),
                              array('/bigep/id/:id/dt/:dt/ppl/:ppl(/log/:log)', 'get', '_bigep_mtz'),
                              array('/data/visit/:visit', 'get', '_download_visit'),
                              array('/attachments', 'get', '_get_attachments'),
                              array('/attachment/id/:id/aid/:aid', 'get', '_get_attachment'),
                              array('/dc/id/:id', 'get', '_download'),

                              array('/ap/attachments(/:AUTOPROCPROGRAMATTACHMENTID)(/dl/:download)', 'get', '_get_autoproc_attachments'),
                              array('/ph/attachments(/:PHASINGPROGRAMATTACHMENTID)(/dl/:download)', 'get', '_get_phasing_attachments'),
            );

        
        # ------------------------------------------------------------------------
        # Generate a one time token for access to downloads
        function _sign_url() {
            if (!$this->has_arg('validity')) $this->_error('No validity specified');
            $token = md5(uniqid());

            $this->db->pq("INSERT INTO SW_onceToken (token, validity, proposalid, personid) VALUES (:1, :2, :3, :4)", array($token, $this->arg('validity'), $this->proposalid, $this->user->personid));
            $this->_output(array('token' => $token));
        }


        # ------------------------------------------------------------------------
        # SAXS Specific Visit Download Link
        function _download_visit() {
            $tp = new TemplateParser($this->db);
            $visit = $tp->visit_dir(array('VISIT' => $this->arg('visit')));
            
            $data = $visit.'/.ispyb/download/download.zip';
            if (file_exists($data)) {
                $this->_header($this->arg('visit').'_download.zip');
                readfile($data);
            } else $this->_error('There doesnt seem to be a data archive available for this visit');
        }

        # ------------------------------------------------------------------------
        # Download mtz/log file for Fast DP / XIA2
        #   TODO: Delete me
        function _auto_processing_plots() {
            global $ap_types;
            if (!$this->has_arg('id')) $this->_error('No data collection', 'No data collection id specified');
            
            $rows = $this->db->pq("SELECT appa.filename, appa.filepath, api.autoprocprogramid, app.processingcommandline as type
                FROM autoprocintegration api 
                INNER JOIN autoprocscaling_has_int aph ON api.autoprocintegrationid = aph.autoprocintegrationid 
                INNER JOIN autoprocscaling aps ON aph.autoprocscalingid = aps.autoprocscalingid 
                INNER JOIN autoproc ap ON aps.autoprocid = ap.autoprocid 
                INNER JOIN autoprocprogram app ON api.autoprocprogramid = app.autoprocprogramid 
                INNER JOIN autoprocprogramattachment appa ON appa.autoprocprogramid = app.autoprocprogramid 
                WHERE appa.filetype='Graph' AND api.datacollectionid = :1", array($this->arg('id')));

            foreach ($rows as &$r) {
                foreach ($ap_types as $id => $name) {
                    if (strpos($r['TYPE'], $id)) {
                        $r['TYPE'] = $name;
                        break;
                    }
                }

                $json = $r['FILEPATH'].'/'.$r['FILENAME'];
                $r['PLOTS'] = array();
                if (file_exists($json)) {
                    $cont = file_get_contents($json);
                    $r['PLOTS'] = json_decode($cont);
                }

                unset($r['FILENAME']);
                unset($r['FILEPATH']);
            }
            
            $this->_output($rows);
        }


        # ------------------------------------------------------------------------
        # Download mtz/log file for Fast DP / XIA2
        #   TODO: Delete me
        function _auto_processing() {
            ini_set('memory_limit', '512M');

            if (!$this->has_arg('id')) $this->_error('No data collection', 'No data collection id specified');
            if (!$this->has_arg('aid')) $this->_error('No auto processing id', 'No auto processing id specified');
            
            $rows = $this->db->pq('SELECT appa.filename,appa.filepath,appa.filetype FROM autoprocintegration api INNER JOIN autoprocscaling_has_int aph ON api.autoprocintegrationid = aph.autoprocintegrationid INNER JOIN autoprocscaling aps ON aph.autoprocscalingid = aps.autoprocscalingid INNER JOIN autoproc ap ON aps.autoprocid = ap.autoprocid INNER JOIN autoprocprogram app ON api.autoprocprogramid = app.autoprocprogramid INNER JOIN autoprocprogramattachment appa ON appa.autoprocprogramid = app.autoprocprogramid WHERE api.datacollectionid = :1 AND api.autoprocprogramid=:2', array($this->arg('id'), $this->arg('aid')));
            
            $this->db->close();

            // print_r($rows);

            if (!sizeof($rows)) $this->_error('No such auto processing');
            // else $r = $rows[0];
        
            foreach ($rows as $r) {
                if ($this->has_arg('log') || $this->has_arg('archive')) {
                    if ($r['FILETYPE'] == 'Log') {
                        if ($this->has_arg('LogFiles')) {
                            $f = $r['FILEPATH'].'/LogFiles/'.$this->arg('LogFiles');
                            if (pathinfo($this->arg('LogFiles'), PATHINFO_EXTENSION) == 'html') $this->app->contentType("text/html");
                            else $this->app->contentType("text/plain");
                            
                        } else {

                            if ($this->has_arg('archive')) {
                                $tar = '/tmp/'.$this->arg('aid').'.tar';
                                $f = $tar.'.gz';

                                if (!file_exists($f)) {
                                    $a = new PharData($tar);
                                    $a->buildFromDirectory($r['FILEPATH'], '/^((?!(HKL|cbf|dimple)).)*$/');
                                    $a->compress(Phar::GZ);

                                    unlink($tar);
                                }

                                $this->_header($this->arg('aid').'.tar.gz');
                                readfile($f);
                                exit;

                            } else {
                                $f = $r['FILEPATH'].'/'.$r['FILENAME'];
                                if ($r['FILENAME'] == 'fast_dp.log') $this->app->contentType("text/plain");
                                if ($r['FILENAME'] == 'autoPROC.log') $this->app->contentType("text/plain");
                            }
                        }
                        
                        if (file_exists($f)) readfile($f);
                        exit;
                    }
                
                } else {
                    // XIA2
                    if ($r['FILETYPE'] == 'Result') {
                        $f = $r['FILEPATH'].'/'.$r['FILENAME'];
                        if (file_exists($f)) {
{'fraction_non_alphanumeric': 0.185625814863103, 'fraction_numerical': 0.001738374619730552, 'mean_word_length': 1.2099639855942377, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 155, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 52, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3730659', 'n_tokens_mistral': 5020, 'n_tokens_neox': 4826, 'n_words': 871}