PageRenderTime 56ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/gamecrawler/wp/youxisoso/search.php

http://collectgame.googlecode.com/
PHP | 506 lines | 353 code | 40 blank | 113 comment | 86 complexity | c42abfbe8c493992ea320a95c4b8f484 MD5 | raw file
  1. <?php
  2. /**
  3. * ????????
  4. * @param $title
  5. * @param $word
  6. * @return unknown_type
  7. */
  8. //function getTitleHtml($title,$word){
  9. // $startSapnChar = "<span style='color: red;'>";
  10. // $endSpanChar = "</span>";
  11. // $html = $title ;
  12. // $keywordArray = explode(" ",$word);
  13. // foreach($keywordArray as $value) {
  14. // if($value !=null && $value != ""){
  15. // $startFindPos = 0 ;
  16. // $html = "" ;
  17. // $indexPos = stripos($title,$value,$startFindPos) ;
  18. // if($indexPos >-1){
  19. // do{
  20. // $html = $html.substr($title,$startFindPos,$indexPos-$startFindPos).$startSapnChar.
  21. // substr($title,$indexPos,strlen($value)).$endSpanChar.substr($title,$indexPos+strlen($value)) ;
  22. // $startFindPos = $indexPos + strlen($value);
  23. // } while(($indexPos = stripos($title,$value,$startFindPos)) >-1) ;
  24. // }else{
  25. // $html = $title ;
  26. // }
  27. // $title = $html ;
  28. // }
  29. // }
  30. // return $html ;
  31. //}
  32. /**
  33. * ?????????
  34. * @param $content
  35. * @param $word
  36. * @return unknown_type
  37. */
  38. //function getContentHtml($content,$word){
  39. // $html = $content ;
  40. // return $html ;
  41. //}
  42. $game_sites = array('4399'=>1, '7k7k'=>2, 'game'=>3, 'kaixin001'=>4, 'android'=>5, '17173'=>6, 'app111'=>7, 'cnzz'=>8,
  43. '4399.com'=>1, '7k7k.com'=>2, 'game.com'=>3, 'kaixin001.com'=>4, 'android.com'=>5, '17173.com'=>6, 'app111.com'=>7, 'cnzz.com'=>8);
  44. /**
  45. * ?????????????????????????????
  46. *
  47. */
  48. function keywordConSite($keyword){
  49. global $game_sites;
  50. $site = false;
  51. $strs = explode(' ',$keyword);
  52. foreach ($strs as $str){
  53. foreach ($game_sites as $key=>$game_site){
  54. if($key == $str){
  55. $site[] = $game_site;
  56. $str = '';
  57. break;
  58. }
  59. }
  60. }
  61. $keyword = implode('',$strs);
  62. return $site;
  63. }
  64. /**
  65. * ?????????1????
  66. * @param $str
  67. * @param $lenth
  68. * @param $start
  69. * @return unknown_type
  70. */
  71. function subString_UTF8($str, $lenth, $start=0){
  72. $len = strlen($str);
  73. $r = array();
  74. $n = 0;
  75. $m = 0;
  76. for($i = 0; $i < $len; $i++) {
  77. $x = substr($str, $i, 1);
  78. $a = base_convert(ord($x), 10, 2);
  79. $a = substr('00000000'.$a, -8);
  80. if ($n < $start){
  81. if (substr($a, 0, 1) == 0) {
  82. }elseif (substr($a, 0, 3) == 110) {
  83. $i += 1;
  84. }elseif (substr($a, 0, 4) == 1110) {
  85. $i += 2;
  86. }
  87. $n++;
  88. }else{
  89. if (substr($a, 0, 1) == 0) {
  90. $r[ ] = substr($str, $i, 1);
  91. }elseif (substr($a, 0, 3) == 110) {
  92. $r[ ] = substr($str, $i, 2);
  93. $i += 1;
  94. }elseif (substr($a, 0, 4) == 1110) {
  95. $r[ ] = substr($str, $i, 3);
  96. $i += 2;
  97. }else{
  98. $r[ ] = '';
  99. }
  100. if (++$m >= $lenth){
  101. break;
  102. }
  103. }
  104. }
  105. return join('',$r);
  106. }
  107. /**
  108. * ??????????
  109. * @param $str
  110. * @param $words
  111. * @return unknown_type
  112. */
  113. function highlight_word($str, $words){
  114. if(is_array($words)) {
  115. foreach($words as $k => $word){
  116. $pattern[$k] = '/(?!<[^>]*)'.
  117. '('.$word.')'.
  118. '(?![^<]*>)/ui';
  119. $replace[$k] = "<span>\\1</span>";
  120. }
  121. } else {
  122. $pattern = '/(?!<[^>]*)'.
  123. '('.$word.')'.
  124. '(?![^<]*>)/ui';
  125. $replace = "<span>\\1</span>";
  126. }
  127. return preg_replace($pattern,$replace,$str);
  128. }
  129. /**
  130. * ????????????
  131. * @param $str
  132. * @param $words
  133. * @return unknown_type
  134. */
  135. function highlight_words($str, $words)
  136. {
  137. $special = ",|.|?|?|;|?|?";
  138. if(is_array($words)) {
  139. foreach($words as $k => $word){
  140. $pattern[$k] = '/(?!<[^>]*)'.
  141. '(['.$special.']?[\s\x{4e00}-\x{9fa5}\w]*'.$word.'[\s\x{4e00}-\x{9fa5}\w]*['.$special.']?)'.
  142. '(?![^<]*>)/ui';
  143. $replace[$k] = "<font style='color:red'>\\1</font>";
  144. }
  145. } else {
  146. $pattern = '/(?!<[^>]*)'.
  147. '(['.$special.']?[\s\x{4e00}-\x{9fa5}\w]*'.$word.'[\s\x{4e00}-\x{9fa5}\w]*['.$special.']?)'.
  148. '(?![^<]*>)/ui';
  149. }
  150. return preg_replace($pattern,$replace,$str);
  151. // $replace = "<font style='color:red'>\\1</font>";
  152. // $pattern='/(?!<[^>]*)'.
  153. // '(['.$special.']?[\s\x{4e00}-\x{9fa5}\w]*'.$words.'[\s\x{4e00}-\x{9fa5}\w]*['.$special.']?)'.
  154. // '(?![^<]*>)/u';
  155. // return preg_replace($pattern,$replace,$str);
  156. }
  157. //????()?
  158. $pageSize = 10 ;
  159. $pageModel= 10 ; //???????
  160. $selfPageLink = "/" ; //?????
  161. $autoLink = "" ;
  162. $siteurl = get_bloginfo('template_url');
  163. //??????????????
  164. $cat = $_POST['cat'] ;
  165. if($cat == null || "" == $cat){
  166. $cat = $_GET['cat'] ;
  167. }
  168. //$categories = "" ;
  169. //$catStr = "" ;
  170. if($cat != null && "" != $cat){
  171. $category_id = get_cat_ID($cat);
  172. //$categories = get_categories ('child_of='.$category_id) ;
  173. //$categories = $wpdb->get_results("select p.name,p.term_id as pterm_id ,t.name,t.term_id as term_id ".
  174. // " from wp_terms t ,wp_term_taxonomy m , wp_terms p ".
  175. // " where t.term_id = m.term_id and p.term_id = m.parent and p.term_id ='".$category_id."'") ;
  176. //foreach ($categories as $category) {
  177. // $catStr =$catStr."'".$category->term_id."'," ;
  178. //}
  179. }
  180. //?????
  181. $keyword = $_POST['s'] ;
  182. if($keyword == null || "" == $keyword ){
  183. $keyword = $_GET["s"];
  184. }
  185. $site = false;
  186. $strs = explode(' ',$keyword);
  187. $ks = array();
  188. foreach ($strs as $k=>$str){
  189. foreach ($game_sites as $key=>$game_site){
  190. if($key == $str){
  191. $site[] = $game_site;
  192. $ks[] = $k;
  193. break;
  194. }
  195. }
  196. }
  197. foreach ($ks as $k){
  198. $strs[$k] = '';
  199. }
  200. $keyword1 = implode('',$strs);
  201. //????????????
  202. if($keyword==null){
  203. $keyword = "" ;
  204. }
  205. $keyword = trim($keyword) ;
  206. keywordStat($keyword) ;
  207. $keyword = preg_replace("/\s+/" ," " ,$keyword) ;
  208. //????
  209. $paged = $_POST["paged"];
  210. if($paged == null || "" == $paged ){
  211. $paged = $_GET["paged"];
  212. if($paged == null || "" == $paged ){
  213. $paged = 0 ;
  214. }
  215. }
  216. $statNum = $pageSize * $paged ;
  217. //??sphinx
  218. require 'sphinxapi.php';
  219. //////////??sphinx?????///////////////
  220. $cl = new SphinxClient ();
  221. $cl->SetServer ( 'localhost', 9312);
  222. $cl->SetConnectTimeout ( 3 );
  223. //$catArray = array() ;
  224. //$catArray[0] = $cat ;
  225. if($category_id != null && "" != $category_id){
  226. $catArray = array() ;
  227. $catArray[0] = $category_id ;
  228. $cl->setFilter("post_category_id",$catArray);
  229. }
  230. if($site){
  231. $cl->setFilter('game_site', $site);
  232. }
  233. $cl->SetArrayResult ( true );
  234. $cl->SetMatchMode ( SPH_MATCH_ANY);
  235. $cl->SetLimits ( $statNum, $pageSize ,5000);
  236. $cl->SetFieldWeights(array("post_title"=>1000, "post_content"=>1));
  237. //$cl->SetSelect("*") ;
  238. $cl->SetSortMode ( SPH_SORT_EXTENDED, "@relevance DESC, post_pi DESC" );
  239. $res = $cl->Query ( $keyword1, "*" );
  240. $opts = array(
  241. 'before_match' => '<span style="color:red">',
  242. 'after_match' => '</span>',
  243. 'chunk_separator' => '...'//,
  244. //'limit'=>300
  245. );
  246. ////////??sphinx?????///////////////
  247. //????post?id?
  248. $unionSql = "" ;
  249. //$postids = '';
  250. if($res['matches'] !=null && count($res['matches'])>0){
  251. foreach($res['matches'] as $postidsArray){
  252. //$postids .= $postidsArray['id'].",";
  253. $unionSql = $unionSql." select ".$postidsArray['id']." as postid ,".$postidsArray['weight']." as weight from dual union all ";
  254. }
  255. }
  256. //$postids = substr($postids, 0, strlen($postids)-1);
  257. //????
  258. //$postQue = "select ID,post_title, post_content,post_game_url,post_game_image,post_category_name from wp_posts where ID in (".$postids.")";
  259. $postQue = "" ;
  260. if(""==$unionSql){
  261. $postQue = "select ID,post_title, post_content,post_game_url,post_game_image,post_category_name from wp_posts where 1!=1 " ;
  262. }else {
  263. $unionSql = substr($unionSql,0,strlen($unionSql)-11) ;
  264. $postQue = "select post.ID,post.post_title, post.post_content,post.post_game_url,post.post_game_image,post.post_category_name from wp_posts post, ".
  265. "(".$unionSql.") temp where temp.postid = post.ID " ;
  266. }
  267. $myposts = $wpdb->get_results($postQue);
  268. //????
  269. get_header();
  270. ?>
  271. <!-- ????? -->
  272. <div id="list_left">
  273. <ul>
  274. <li id="xuanzhong">>????</li>
  275. <?php if($category_id != null && "" != $category_id){ ?>
  276. <li class="fenleileftfloat NOxuanzhong" ><a href="<?php echo $selfPageLink."?s=".$keyword ;?>">????</a></li>
  277. <?php }else{ ?>
  278. <li id="xuanzhong" class="fenleileftfloat"><a href="<?php echo $selfPageLink."?s=".$keyword ;?>">????</a></li>
  279. <?php } if($category_id == "6"){ ?>
  280. <li id="xuanzhong" class="fenleileftfloat"><a href="<?php echo $selfPageLink."?s=".$keyword."&cat=Flash???" ;?>" >Flash???</a></li>
  281. <?php }else{?>
  282. <li class="fenleileftfloat NOxuanzhong"><a href="<?php echo $selfPageLink."?s=".$keyword."&cat=Flash???" ;?>" >Flash???</a></li>
  283. <?php } if($category_id == "7"){ ?>
  284. <li id="xuanzhong" class="fenleileftfloat"><a href="<?php echo $selfPageLink."?s=".$keyword."&cat=????" ;?>" >????</a></li>
  285. <?php }else{?>
  286. <li class="fenleileftfloat NOxuanzhong"><a href="<?php echo $selfPageLink."?s=".$keyword."&cat=????" ;?>" >????</a></li>
  287. <?php } if($category_id == "8"){ ?>
  288. <li id="xuanzhong" class="fenleileftfloat"><a href="<?php echo $selfPageLink."?s=".$keyword."&cat=????" ;?>" >????</a></li>
  289. <?php }else{?>
  290. <li class="fenleileftfloat NOxuanzhong"><a href="<?php echo $selfPageLink."?s=".$keyword."&cat=????" ;?>" >????</a></li>
  291. <?php }?>
  292. </ul>
  293. </div>
  294. <!-- ????? -->
  295. <!-- ??????????????? -->
  296. <div id="list_center">
  297. <!-- ???????? -->
  298. <!-- <span id="mianbaoxie" class="list_centtopspan">?????<a href="?">??</a>=><a href="<?php //echo $selfPageLink ;?>">????</a></span> -->
  299. <span id="soso_result" class="list_centtopspan">&nbsp;&nbsp;&nbsp;&nbsp;??<?php echo $res['total_found']; ?></>????? ????:"<?php echo $res['time']?>" </span>
  300. <!-- ???????? -->
  301. <!-- ????? -->
  302. <?php
  303. $keyArray = explode(" ",$keyword) ;
  304. //$contentArray = array() ;
  305. if($myposts != null && count($myposts)>0){
  306. foreach($myposts as $mypost) {
  307. $gamePic = $mypost->post_game_image;
  308. $gameLink = $mypost->post_game_url;
  309. $postLink = get_permalink($mypost->ID);
  310. $postTitle = $mypost->post_title ;
  311. $titleHtml = highlight_word($postTitle,$keyArray) ;
  312. $desContent = $mypost->post_content ;
  313. $desHtml = highlight_word(subString_UTF8($desContent,150),$keyArray) ;
  314. //$contentArray[] = $desContent ;
  315. $cat_name = $mypost->post_category_name ;
  316. $loadUrl = $siteurl."/images/load.gif";
  317. $delayImgId= "delayId".$mypost->ID ;
  318. ?>
  319. <!-- add by shiwei 2011-12-07 delay img print -->
  320. <script type="text/javascript">
  321. delayImagPrint.put("<?php echo $delayImgId;?>","<?php echo $gamePic;?>") ;
  322. </script>
  323. <!-- add end by shiwei -->
  324. <div class="list">
  325. <span class="list_img" style="text-align: center;"><a href="<?php echo $postLink; ?>">
  326. <img id="<?php echo $delayImgId;?>" src="<?php echo $loadUrl ?>" width="50" height="50" style="padding-top:12px;" onerror="this.src='<?php echo $siteurl?>/images/onerror.jpg'" alt="<?php echo $titleHtml; ?>" title="<?php echo $titleHtml; ?>" />
  327. </a></span>
  328. <h3 class="list_title"><a href="<?php echo $postLink; ?>"><?php echo $titleHtml;//$mypost->post_title; ?></a></h3>
  329. <span class="list_cat">
  330. <?php
  331. if($cat_name == 'andriod' ||$cat_name == 'iphone' || $cat_name == 'windows phone' ){
  332. echo "????:";
  333. }else {
  334. echo "??:";
  335. }
  336. ?>
  337. <samp><?php echo $cat_name?></samp></span>
  338. <p class="list_neirong"><?php echo $desHtml.'... ...'; ?></p>
  339. <span class="list_add"><samp><?php echo $gameLink; ?></samp></span>
  340. </div>
  341. <?php
  342. }
  343. }
  344. ?>
  345. <!-- ????? -->
  346. <!-- ???? -->
  347. <!-- ???? -->
  348. <!-- ??2011-12-08?? -->
  349. <!-- -->
  350. <div id="scroll">
  351. <?php
  352. $MAX_PAGE = 500 ;
  353. $PAGE_LENGTH = 12 ;
  354. $PAGE_MID_HALF_LENGTH = 2 ;
  355. $SPECIAL = "..." ;
  356. $indexPage = array() ;
  357. $totalPage = ceil($res['total_found']/$pageSize) ;
  358. if($totalPage > $MAX_PAGE){
  359. $totalPage = $MAX_PAGE;
  360. }
  361. if($totalPage >0){
  362. if($paged<0){
  363. $paged = 0 ;
  364. }
  365. if($paged >= $totalPage){
  366. $paged = $totalPage-1 ;
  367. }
  368. $pagedLink = $selfPageLink."?s=".$keyword."&cat=".$cat;
  369. //???
  370. if(intval($paged) > 0 ){
  371. ?>
  372. <a href="<?php echo $pagedLink."&paged=".(intval($paged)-1); ?>" ><img src="<?php echo $siteurl ?>/images/next-top.gif" alt="???" title="???" class="f_l"/></a>
  373. <?php
  374. }
  375. if($totalPage <= $PAGE_LENGTH){
  376. for($index = 0;$index < $totalPage ; $index ++){
  377. $indexPage[$index] = $index +1 ;
  378. }
  379. }else{
  380. /**
  381. * ???????
  382. */
  383. $mid_index = 0 ;
  384. if($paged <$totalPage/2){
  385. //???????
  386. $mid_index= $PAGE_LENGTH/2 ;
  387. $indexPage[$mid_index] = $mid_index +1 ;
  388. }else{
  389. //???????
  390. $mid_index = $PAGE_LENGTH/2 -1;
  391. $indexPage[$mid_index] = $totalPage -$mid_index -1 ;
  392. }
  393. //???????
  394. for($index = 0 ; $index <$mid_index ;$index ++){
  395. $indexPage[$index] = $index +1 ;
  396. }
  397. for($index = 0 ; $index <$PAGE_LENGTH -$mid_index -1 ;$index ++){
  398. $indexPage[$PAGE_LENGTH-1-$index] = $totalPage - $index ;
  399. }
  400. /**
  401. * ????
  402. */
  403. //???
  404. if($paged <$totalPage/2){
  405. if($indexPage[$mid_index] >= $paged +1){
  406. $indexPage[$mid_index + 1] = $mid_index +2 ;
  407. $indexPage[$mid_index + 2] = $SPECIAL ;
  408. }else {
  409. $indexPage[$mid_index - $PAGE_MID_HALF_LENGTH -1 ] = $SPECIAL ;
  410. $indexPage[$mid_index + $PAGE_MID_HALF_LENGTH +1 ] = $SPECIAL ;
  411. for($index = 0 ; $index <= 2*$PAGE_MID_HALF_LENGTH ; $index ++){
  412. $indexPage[$mid_index - $PAGE_MID_HALF_LENGTH +$index] = $paged+1-$PAGE_MID_HALF_LENGTH+$index ;
  413. }
  414. }
  415. }else{
  416. //???
  417. if($indexPage[$mid_index] <= $paged+1){
  418. $indexPage[$mid_index-1] = $indexPage[$mid_index] -1 ;
  419. $indexPage[$mid_index-2] = $SPECIAL;
  420. }else {
  421. $indexPage[$mid_index - $PAGE_MID_HALF_LENGTH -1 ] = $SPECIAL ;
  422. $indexPage[$mid_index + $PAGE_MID_HALF_LENGTH +1 ] = $SPECIAL ;
  423. for($index = 0 ; $index <= 2*$PAGE_MID_HALF_LENGTH ; $index ++){
  424. $indexPage[$mid_index - $PAGE_MID_HALF_LENGTH +$index] = $paged+1-$PAGE_MID_HALF_LENGTH+$index ;
  425. }
  426. }
  427. }
  428. }
  429. //??HMTL
  430. for($index = 0; $index < count($indexPage); $index++ ){
  431. if($indexPage[$index] == $paged+1){
  432. ?>
  433. <font class="linknone"><?php echo $indexPage[$index];?></font>
  434. <?php
  435. }elseif ($indexPage[$index] == $SPECIAL) {
  436. ?>
  437. <a class="linkstyle03"><?php echo $SPECIAL;?></a>
  438. <?php
  439. }else {
  440. ?>
  441. <a href="<?php echo $pagedLink."&paged=".($indexPage[$index]-1); ?>" class="linkstyle03"><?php echo $indexPage[$index];?></a>
  442. <?php
  443. }
  444. }
  445. //???
  446. if(intval($paged)<$totalPage-1){
  447. ?>
  448. <a href="<?php echo $pagedLink."&paged=".(intval($paged)+1); ?>" ><img src="<?php echo $siteurl ?>/images/next-bottom.gif" alt="???" title="???" class="f_l" /></a>
  449. <?php
  450. }
  451. }
  452. ?>
  453. </div>
  454. <!-- -->
  455. </div>
  456. <!-- ??????????????? -->
  457. <!-- ?????? -->
  458. <div id="list_right">
  459. <h2>??????</h2>
  460. </div>
  461. <!-- ?????? -->
  462. </div>
  463. <?php
  464. //????
  465. get_footer();
  466. ?>