コード例 #1
0
 public function search_post_by_text($input_text, $input_options = false)
 {
     $classMethodKey = crc32(__CLASS__ . __METHOD__);
     if (!$input_options) {
         $input_options = array();
     }
     $input_options['limit'] = (int) $input_options['limit'];
     $input_options['exclude_posts_ids'] = (array) $input_options['exclude_posts_ids'];
     if (isset(PepVN_Data::$cacheData[$classMethodKey]['posts_ids_added']) && PepVN_Data::$cacheData[$classMethodKey]['posts_ids_added'] && !PepVN_Data::isEmptyArray(PepVN_Data::$cacheData[$classMethodKey]['posts_ids_added'])) {
         $valueTemp = array_keys(PepVN_Data::$cacheData[$classMethodKey]['posts_ids_added']);
         $input_options['exclude_posts_ids'] = array_merge($input_options['exclude_posts_ids'], $valueTemp);
     }
     $input_options['exclude_posts_ids'] = array_unique($input_options['exclude_posts_ids']);
     arsort($input_options['exclude_posts_ids']);
     $input_options['exclude_posts_ids'] = array_values($input_options['exclude_posts_ids']);
     if (!isset($input_options['key_cache'])) {
         $input_options['key_cache'] = array();
     }
     $input_options['key_cache'] = (array) $input_options['key_cache'];
     $keyCacheProcessText = Utils::hashKey(array($classMethodKey, $input_text, $input_options, 'search_post_by_text'));
     $tmp = TempDataAndCacheFile::get_cache($keyCacheProcessText, true);
     if (null !== $tmp) {
         return $tmp;
     }
     $wpExtend = $this->di->getShared('wpExtend');
     $analyzeText = $this->di->getShared('analyzeText');
     $keyCacheGroupNameOfTagsAndCategories = array($classMethodKey, 'groupNameOfTagsAndCategories');
     $keyCacheGroupNameOfTagsAndCategories = Utils::hashKey($keyCacheGroupNameOfTagsAndCategories);
     $groupNameOfTagsAndCategories = TempDataAndCacheFile::get_cache($keyCacheGroupNameOfTagsAndCategories);
     if (null === $groupNameOfTagsAndCategories) {
         $groupNameOfTagsAndCategories = array();
         $valueTemp = $wpExtend->getAndParseCategories();
         $valueTemp = array_keys($valueTemp);
         $groupNameOfTagsAndCategories = array_merge($groupNameOfTagsAndCategories, $valueTemp);
         unset($valueTemp);
         $valueTemp = $wpExtend->getAndParseTags();
         $valueTemp = array_keys($valueTemp);
         $groupNameOfTagsAndCategories = array_merge($groupNameOfTagsAndCategories, $valueTemp);
         unset($valueTemp);
         $groupNameOfTagsAndCategories = array_values($groupNameOfTagsAndCategories);
         $groupNameOfTagsAndCategories = $this->_clean_terms($groupNameOfTagsAndCategories);
         TempDataAndCacheFile::set_cache($keyCacheGroupNameOfTagsAndCategories, $groupNameOfTagsAndCategories);
     }
     $rsGetKeywordsFromText = AnalyzeText::analysisKeyword_GetKeywordsFromText(array('contents' => $input_text, 'min_word' => 1, 'max_word' => 6, 'min_occur' => 2, 'min_char_each_word' => 4));
     $groupKeywordsFromText = array();
     if (isset($rsGetKeywordsFromText['data']) && is_array($rsGetKeywordsFromText['data'])) {
         foreach ($rsGetKeywordsFromText['data'] as $keyOne => $valueOne) {
             if ($valueOne && is_array($valueOne)) {
                 foreach ($valueOne as $keyTwo => $valueTwo) {
                     if ($keyTwo) {
                         $valueTwo = (int) $valueTwo;
                         $groupKeywordsFromText[$keyTwo] = ceil($valueTwo * (PepVN_Data::countWords($keyTwo) * 1.5));
                     }
                 }
             }
         }
     }
     arsort($groupKeywordsFromText);
     $groupKeywordsFromText2 = $groupKeywordsFromText;
     $groupKeywordsFromText2 = array_slice($groupKeywordsFromText2, 0, 10);
     $groupKeywordsFromText3 = array();
     foreach ($groupKeywordsFromText as $keyOne => $valueOne) {
         unset($groupKeywordsFromText[$keyOne]);
         if (in_array($keyOne, $groupNameOfTagsAndCategories)) {
             $groupKeywordsFromText3[$keyOne] = $valueOne;
         }
     }
     unset($groupNameOfTagsAndCategories);
     arsort($groupKeywordsFromText3);
     $groupKeywordsFromText3 = array_slice($groupKeywordsFromText3, 0, 10);
     $groupKeywordsFromText4 = array();
     foreach ($groupKeywordsFromText2 as $keyOne => $valueOne) {
         unset($groupKeywordsFromText2[$keyOne]);
         if (!isset($groupKeywordsFromText4[$keyOne])) {
             $groupKeywordsFromText4[$keyOne] = 0;
         }
         $groupKeywordsFromText4[$keyOne] += (int) $valueOne;
     }
     foreach ($groupKeywordsFromText3 as $keyOne => $valueOne) {
         unset($groupKeywordsFromText3[$keyOne]);
         if (!isset($groupKeywordsFromText4[$keyOne])) {
             $groupKeywordsFromText4[$keyOne] = 0;
         }
         $groupKeywordsFromText4[$keyOne] += (int) $valueOne * 2;
     }
     if (isset($input_options['group_text_weight']) && !PepVN_Data::isEmptyArray($input_options['group_text_weight'])) {
         foreach ($input_options['group_text_weight'] as $keyOne => $valueOne) {
             if (isset($valueOne['text']) && $valueOne['text']) {
                 $valueOne['text'] = AnalyzeText::cleanRawTextForProcessSearch($valueOne['text']);
                 if ($valueOne['text']) {
                     foreach ($groupKeywordsFromText4 as $keyTwo => $valueTwo) {
                         $valueTemp2 = substr_count($valueOne['text'], PepVN_Data::strtolower($keyTwo));
                         if ($valueTemp2) {
                             $weight1 = $valueOne['weight'];
                             $numberImportantOccurrenceConsecutive = 1;
                             $keyTwoTmp1 = explode(' ', $keyTwo);
                             foreach ($keyTwoTmp1 as $keyThree => $valueThree) {
                                 if (preg_match('#^[^a-z0-9' . preg_quote('`~!@#$%^&*()-_=+{}[]\\\\|;:\'",.<>/?+', '#') . ']+#', $valueThree)) {
                                     $weight1 = $weight1 * 1.6 * ($numberImportantOccurrenceConsecutive * 1.11);
                                     $numberImportantOccurrenceConsecutive++;
                                 } else {
                                     $numberImportantOccurrenceConsecutive = 1;
                                 }
                             }
                             unset($keyTwoTmp1);
                             $groupKeywordsFromText4[$keyTwo] += (int) $valueTemp2 * $weight1;
                         }
                     }
                 }
             }
         }
     }
     $rsSearchPosts = $analyzeText->find_related_posts(array('keywords' => $groupKeywordsFromText4, 'limit' => $input_options['limit'], 'exclude_posts_ids' => $input_options['exclude_posts_ids'], 'post_id_less_than' => $input_options['post_id_less_than']));
     if ($rsSearchPosts && !empty($rsSearchPosts)) {
         foreach ($rsSearchPosts as $key1 => $value1) {
             if (isset($value1['post_id']) && $value1['post_id']) {
                 PepVN_Data::$cacheData[$classMethodKey]['posts_ids_added'][$value1['post_id']] = $value1;
             }
         }
     }
     TempDataAndCacheFile::set_cache($keyCacheProcessText, $rsSearchPosts, true);
     return $rsSearchPosts;
 }