'周考', 2=>'单元考', 3=>'月考', 4=>'联考', 5=>'期中',6=>'期末', 7=>'高考模拟', 8=>'冲刺', 9=>'测试'); public $topicTypeName = array( 3 => array(1=>'选择题',2=>'多选题',5=>'填空题',6=>'填空题',7=>'解答题',11=>'不定项'), 8 => array(1=>'单项填空',2=>'完形填空',3=>'阅读理解',4=>'任务型阅读',5=>'七选五',6=>'语法填空',7=>'短文改错',13=>'书面表达',15=>'听力',16=>'应用文写作',17=>'读后续写'), ); public function init() { parent::init(); @ini_set('memory_limit', '1024M'); set_time_limit(0); } public function actionIndex($school_id = '', $YII_ENV = 'development') { // 连接业务库 // $pdoParam = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES "utf8";'); // $businessServer = Yii::app()->params["businessDb"]; $databaseFields = 'database_host,database_name,database_user,database_password,school_id'; // $busDsn = 'mysql:host=' . $businessServer['addr'] . ';dbname=' . Yii::app()->params["default_db"]['name'] . ';'; // $busDbh = new PDO($busDsn, $businessServer['username'], $businessServer['password'], $pdoParam); $busDbh = Yii::app()->businessDb; // 获取所有可用的学校 if ($school_id) { $dataSql = "SELECT {$databaseFields} FROM `database` WHERE `school_id` IN ({$school_id})"; } else { $dataSql = "SELECT {$databaseFields} FROM `database` WHERE `school_id` IN (SELECT school_id FROM `school` WHERE `status`=0)"; } // 找出学校的链接信息 $dataObj = $busDbh->query($dataSql); $dataInfo = $dataObj->fetchAll(PDO::FETCH_ASSOC); if (empty($dataInfo)) { exit('未找到学校数据库链接信息!'); } // 获取学校所属的省份 $schoolIds = array(); $schoolProvince = array(); foreach($dataInfo as $tempSchool) { $schoolIds[] = $tempSchool['school_id']; } $schoolIdString = implode(',', $schoolIds); $schoolObj = $busDbh->query("SELECT s.school_id,r.region_name FROM `school` s LEFT JOIN `region` r ON r.region_id = s.province_id WHERE s.`school_id` IN ({$schoolIdString})"); $schoolInfo = $schoolObj->fetchAll(PDO::FETCH_ASSOC); foreach ($schoolInfo as $key => $tempProvince) { $schoolProvince[$tempProvince['school_id']] = $tempProvince['region_name']; } // 获取所有学科 $packSubject = array(); $subjectObj = $busDbh->query('SELECT subject_id,subject_name,section FROM `subject`')->fetchAll(PDO::FETCH_ASSOC); foreach($subjectObj as $tempSubject) { $packSubject[$tempSubject['subject_id']]['name'] = $tempSubject['subject_name']; $packSubject[$tempSubject['subject_id']]['section'] = $tempSubject['section']; } $busDbh = null; // 关闭链接 $callbackUrl = ''; // 回调地址 $analysisUrl = ''; // 解析地址 $myPath = dirname(dirname(dirname(__FILE__))); $scienceWordAnalysisUrl = Yii::app()->params['science_word_analysis_url']; // 物理解析地址 $subjectCoachCallbackUrl = Yii::app()->params['subject_coach_callback_url']; // 全学科第三方试卷解析回调 // 新解析工具 $rapidAnalysisUrl = Yii::app()->params['rapid_analysis_url']; // 解析地址 $rapidCallbackUrl = Yii::app()->params['rapid_callback_url']; // 解析回调 // 发送Word处理 foreach ($dataInfo as $data) { echo 'STARTING==>' . $data['school_id'] . "\n"; $temp_dsn = 'mysql:host=' . $data['database_host'] . ';dbname=' . $data['database_name'] . ';'; $temp_dbh = new PDO($temp_dsn, $data['database_user'], $data['database_password'], $pdoParam); $temp_obj = $temp_dbh->query('SELECT word_id,exam_group_id,word_path,uploader_id,type_id,subject_id,allsubject_file_path,source FROM `topic_word` WHERE `status`=0 AND `source` != 4'); $temp_info = $temp_obj->fetchAll(PDO::FETCH_ASSOC); if (empty($temp_info)) { echo 'not found any word in school_' . $data['school_id'] . "\n"; } else { foreach ($temp_info as $info) { // 老版解析工具 if (empty($info['allsubject_file_path'])) { $analysisUrl = $scienceWordAnalysisUrl; // 高中物理解析地址 if ($info['exam_group_id']) { $callbackUrl = $subjectCoachCallbackUrl; } if (empty($callbackUrl) || empty($analysisUrl)) continue; echo $analysisUrl.'----'.$callbackUrl. "\n"; if (false !== strpos($info['word_path'], 'http://')) { $time = time(); $arr = self::getFile($info['word_path'], '/tem', $info['uploader_id'] . $time . '.docx'); if ($arr && file_exists($arr['save_path'])) { $retRes = self::curlWord($analysisUrl, $callbackUrl, $info['word_id'], $arr['save_path'], $data['school_id']); $json_array = json_decode($retRes, true); $temp_dbh = new PDO($temp_dsn, $data['database_user'], $data['database_password'], $pdoParam); if (!$json_array || $json_array['errcode'] > 0) { $temp_dbh->exec("UPDATE `topic_word` SET `status` = '3',`wrong_reason` = '" . $json_array['errmsg'] . "',`content` ='" . $retRes . "' WHERE `word_id` ='" . $info['word_id'] . "'"); } else { $temp_dbh->exec("UPDATE `topic_word` SET `status` = '1',`file_queue`='" . $json_array['docsbefore'] . "' WHERE `word_id` ='" . $info['word_id'] . "'"); } $temp_dbh = null; @unlink($arr['save_path']); echo 'Send the word id : ' . $info['word_id'] . "\n"; } } else { if (file_exists($myPath . '/' . $info['word_path'])) { echo 'Send the word path : ' . $info['word_path'] . "\n"; $retRes = self::curlWord($analysisUrl, $callbackUrl, $info['word_id'], $myPath . '/' . $info['word_path'], $data['school_id']); $json_array = json_decode($retRes, true); $temp_dbh = new PDO($temp_dsn, $data['database_user'], $data['database_password'], $pdoParam); if (!$json_array || $json_array['errcode'] > 0) { $temp_dbh->exec("UPDATE `topic_word` SET `status` = '3',`wrong_reason` = '" . $json_array['errmsg'] . "',`content` ='" . $retRes . "' WHERE `word_id` ='" . $info['word_id'] . "'"); } else { $temp_dbh->exec("UPDATE `topic_word` SET `status` = '1',`file_queue`='" . $json_array['docsbefore'] . "' WHERE `word_id` ='" . $info['word_id'] . "'"); } } else { echo 'lost the word path : ' . $myPath . '/' . $info['word_path'] . "\n"; } } } else { $paperTopicType = array(); $typeNames = $info['subject_id'] == 8 ? $this->topicTypeName[8] : $this->topicTypeName[3]; // 考试信息 $exam = $temp_dbh->query('SELECT `name`,`type`,`exam_id` FROM `exam` WHERE `exam_group_id`="' . $info['exam_group_id'].'"')->fetch(PDO::FETCH_ASSOC); if (empty($exam)) continue; // 在线答题卡PDF $card = $temp_dbh->query('SELECT `online_card_pdf` FROM `third_answer_sheet` WHERE `exam_group_id`="' . $info['exam_group_id'].'"')->fetch(PDO::FETCH_ASSOC); $onlineCardPdf = $card && !empty($card['online_card_pdf']) ? $card['online_card_pdf'] : ''; // 获取考试试题信息 $paperTopics = $temp_dbh->query('select ptr.topic_id,ptr.type from paper p left join paper_topic_relation ptr ON ptr.paper_id = p.paper_id where p.exam_id="'.$exam['exam_id'].'" order by ptr.order asc')->fetchAll(PDO::FETCH_ASSOC); if (empty($paperTopics)) continue; if ($info['subject_id'] == 8) { // 英语试卷获取大题逻辑题型 $largeTopics = $temp_dbh->query('select ptl.logic_type from paper p left join paper_topic_large ptl ON ptl.paper_id = p.paper_id where p.exam_id="'.$exam['exam_id'].'" order by ptl.topic_no asc')->fetchAll(PDO::FETCH_ASSOC); if ($largeTopics) { foreach ($largeTopics as $largeTopic) { $paperTopicType[] = isset($typeNames[$largeTopic['logic_type']]) ? $typeNames[$largeTopic['logic_type']] : ''; } } } else { foreach ($paperTopics as $paperTopic) { $paperTopicType[] = isset($typeNames[$paperTopic['type']]) ? $typeNames[$paperTopic['type']] : '解答题'; } } $wordParams = array( 'source' => 'xue_guan', 'paper_name' => $exam['name'], 'subject' => isset($packSubject[$info['subject_id']]) ? $packSubject[$info['subject_id']]['name'] : '', 'period' => isset($packSubject[$info['subject_id']]) && $packSubject[$info['subject_id']]['section'] == 1 ? '高中' : '初中', 'province' => isset($schoolProvince[$data['school_id']]) ? $schoolProvince[$data['school_id']] : '中国', 'category' => isset($this->examType[$exam['type']]) ? array($this->examType[$exam['type']]) : array(), 'is_cloud' => 1, 'file_list' => json_decode($info['allsubject_file_path'],true), 'item_num_info' => array( 'total_nums' => count($paperTopics), 'paper_topic_type' => $paperTopicType, 'paper_card_pdf' => $onlineCardPdf ), 'callback_url' => $rapidCallbackUrl . '/sid/' . $data['school_id'] . '/wid/' . $info['word_id'] ); $retrunJson = self::curlFile($rapidAnalysisUrl, json_encode($wordParams)); $returnResult = json_decode($retrunJson, true); if (isset($returnResult['id']) && $returnResult['id'] != 0) { $temp_dbh->exec("UPDATE `topic_word` SET `status` = '1',`params_id`='" . $returnResult['id'] . "' WHERE `word_id` ='" . $info['word_id'] . "'"); } else { $temp_dbh->exec("UPDATE `topic_word` SET `status` = '3',`wrong_reason` = '发送解析异常',`content` ='" . $retrunJson . "' WHERE `word_id` ='" . $info['word_id'] . "'"); } echo 'Send the word id : ' . $info['word_id'] . "\n"; } } } $temp_dbh = null; echo 'ENDING==>' . $data['school_id'] . "\n\n"; } } // POST word public function curlWord($analysisUrl, $word_api_url, $word_id, $postFilePath, $school_id) { if (substr(PHP_VERSION, 0, 3) >= '5.5') { $pathName = pathinfo($postFilePath); $cfile = curl_file_create($postFilePath, 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', $pathName['basename']); $imgdata = array( 'callback_url' => $word_api_url . '/sid/' . $school_id . '/wid/' . $word_id . '/is_cloud/1', 'sid' => $school_id, 'is_cloud' => 1, 'mydata' => $cfile ); } else { $imgdata = array( 'callback_url' => $word_api_url . '/sid/' . $school_id . '/wid/' . $word_id . '/is_cloud/1', 'sid' => $school_id, 'is_cloud' => 1, 'mydata' => '@' . $postFilePath ); } $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $analysisUrl); curl_setopt($curl, CURLOPT_USERAGENT, 'Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15'); curl_setopt($curl, CURLOPT_HTTPHEADER, array('User-Agent: Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15', 'Referer: http://someaddress.tld', 'Content-Type: multipart/form-data')); //curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $imgdata); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); $r = curl_exec($curl); curl_close($curl); return $r; } protected function getFile($url, $save_dir = '', $filename = '', $type = 1) { $file_type = strtolower(substr($url, strrpos($url, '.') + 1)); //获取远程文件所采用的方法 if ($type) { $ch = curl_init(); $timeout = 5; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $content = curl_exec($ch); curl_close($ch); } else { ob_start(); readfile($url); $content = ob_get_contents(); ob_end_clean(); } $size = strlen($content); $test_dir = tempnam($save_dir, $filename); //文件大小 $fp2 = @fopen($test_dir, 'w'); fwrite($fp2, $content); fclose($fp2); unset($content, $url); $path = explode('.', $test_dir); @rename($test_dir, $path[0] . '.' . $file_type); return array( 'file_name' => $filename, 'save_path' => $path[0] . '.' . $file_type, 'file_size' => $size ); } // POST WORD public function curlFile($analysisUrl, $wordParams) { $ch = curl_init(); curl_setopt($ch, CURLOPT_HEADER, 0); // 不输出头部 curl_setopt($ch, CURLOPT_TIMEOUT, 10); // 返回数据等待 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3); // 连接等待时间 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 获取到的内容不直接输出, 而是返回 curl_setopt($ch, CURLOPT_URL, $analysisUrl); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)'); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data')); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $wordParams); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($wordParams))); $output = curl_exec($ch); $errnoNumber = curl_errno($ch); curl_close($ch); return $errnoNumber ? 'curl_errno:' . $errnoNumber : $output; } }