From 9fd1bae95f90d6114e1607528bf03fa95b6d267e Mon Sep 17 00:00:00 2001 From: Rasmus Lerdorf Date: Thu, 14 Jul 2011 05:24:48 +0000 Subject: [PATCH] Switching search backend from Yahoo to Bing. and to head off the inevitable bitching about this, it is because the Yahoo BOSS v1 API we are using is going away on July 20 and Yahoo was not willing to give us free access to the new v2 API. It would end up costing us about $600/year at our current usage pattern. The new Google REST API would cost us about $7500/year. The Bing API is free. --- images/bing.png | Bin 0 -> 2403 bytes results.php | 10 ++++----- ws.php | 58 +++++++++++++++++++++++++++--------------------- 3 files changed, 38 insertions(+), 30 deletions(-) create mode 100644 images/bing.png diff --git a/images/bing.png b/images/bing.png new file mode 100644 index 0000000000000000000000000000000000000000..4c64667575814be5c85976d9f7dca6bc02b05eb1 GIT binary patch literal 2403 zcmV-p37qzcP)Px#32;bRa{vGf6951U69E94oEQKA00(qQO+^RW2Mrf41`ldmZ2$lW^+`lQRA}Da zSqV^Fm z``&l{{@3sSJ&`9AbNHjU(hM_wnT0_^Fp+V_d4cY(rtRmLh=+f?0E+0fW7gYtsK2#S zbLTFNKSEvph_v5Eo!v+_t|pH@LLPmXI`uRY8l7GsMfCi~7EvxCk`RmXmqX zl1-12O>3#+>)=W@uA&E;u2ndgIX81!kO4~h76sbTu!3q_MJfNq(9!gb?J{jO4O6}( zIFk2dQenaNViGL?UE=z&t5=$%$k zwoJ5HCd!r;%N-Pr`o4W`pjhg%hDk z(hx&}F)9v@JYs2q{7)x(MOjk3zQtSQ50)8Ts<)IWou_)d(t*j-)`@bJ^W}OA2=I$- zWXaxW?so-Z@ebpyo&f{I4PrJSU zF}PsBA6B&8T%_dolOw&iSogWj$3wsk1?u^eYLjSFD;{cjk@E2m+Qpv@HkdsZY`!{^ z`<-(GYb%Y{15#oh0ttfV{yjR-cTk@igDaWuV39+($YJ^)lp8MEe91hkKSC;o{p+jR zkpx(QeD^)A#xW%{KyZM3V5rTq13H%=PU+y}H+QI^Jwh{A_C%FQHkf#p7)4v$p*NcQ zkg~AQgLv{T4hi07JQ(}-E)Ba+Ie@$2kQv&~HA#}DO|Ae!EtiAi=0c@;EI1h{j73j+l8JG-^sa4cmU zWVI<26C?*$R+Uo2At9FLD>5YHOq8P6RJ3vaDO*6wdiyR9F5`unjI3v`bJ<`bu_U)EHjC!%o!p9K;uQ{EP``IApi8jk1vd#xbmh?iu z?-H0Ggi`QxSikNo(Itap5$~P=4%}zy#?M8Bfr&7RM0f--@z2w?L}7@91IP@pL(cfKWe}6oL50x7PXkiE-3*RfzJ@--9?~YoZ`>+$z9mi)JO>jaYf)@X3+T=jcLBJ1+np$|7+p0I0>;NUycz;3dby{n_1zndf$N54+uC^ zNOOT-XG!r-z-VQ;{?&$_6BbWA?Gp+_b;ugQ;X~YQFuPgfqG7<5Y<=pW$vzp)2#&ve z+6P%B^u4=SJLHRG(wEy8;uBnsBVS)-0>6_u&?X`Ij6Xs_aMn2dkZ~8b3}>Go+%C1l zK2>)A=u`|cLIJ+HZV3zr8o&r}#YO~;;U}(OfEgXxLtn5^+;@`qPnjPgGMsbCL#`KP z3Dar8k>Showing results $start_result to $end_result of $results_count +

Showing results $disp_start_result to $disp_end_result of $results_count

    EOB; $pos = $res['ResultSet']['firstResultPosition']; @@ -131,9 +132,8 @@ EOB; } echo << - - - +Results by

    Results Page:

      EOB; diff --git a/ws.php b/ws.php index 6479e8f3d..b3038266a 100644 --- a/ws.php +++ b/ws.php @@ -1,17 +1,18 @@ 'php.net', 'local'=>'www.php.net', - 'manual'=>'www.php.net', + 'manual'=>"www.php.net/manual/$l", 'news'=>'news.php.net', 'bugs'=>'bugs.php.net', 'pear'=>'pear.php.net', @@ -19,41 +20,48 @@ $sites = array( 'all'=>'php.net', 'talks'=>'talks.php.net', ); +$market = 'en-us'; +if(!empty($LANGUAGES_MAP[$l])) $market = $LANGUAGES_MAP[$l]; + if(isset($sites[$_REQUEST['profile']])) { - $scope = $_REQUEST['profile']; + $scope = htmlspecialchars($_REQUEST['profile'], ENT_QUOTES); + // If they are doing a manual search in a language we don't have a translation for, default to English + if($scope == 'manual' && empty($ACTIVE_ONLINE_LANGUAGES[$l])) { + $sites['manual'] = "www.php.net/manual/en"; + } } else { - $scope = 'all'; + $scope = 'all'; } -$request = "{$conf['svc']}$q?appid={$conf['appid']}&start=$s&count=$r&sites={$sites[$scope]}&lang=$l&format=json"; +$request = "{$conf['svc']}?appid={$conf['appid']}&query=$q%20site:{$sites[$scope]}&version=2.2&Sources=Web&web.offset=$s&web.count=$r&market=$market"; $data = @file_get_contents($request); list($version,$status_code,$msg) = explode(' ',$http_response_header[0], 3); -if($status_code==200) echo ws_boss_massage($data); +if($status_code==200) echo ws_bing_massage($data); else echo serialize($http_response_header[0]); -function ws_boss_massage($data) { +function ws_bing_massage($data) { $results = json_decode($data, true); - $rsp = $results['ysearchresponse']; - $set = $rsp['resultset_web']; + $rsp = $results['SearchResponse']['Web']; + $set = $rsp['Results']; $massaged = array( 'ResultSet' => array( - 'totalResultsAvailable' => $rsp['totalhits'], - 'totalResultsReturned' => $rsp['count'], - 'firstResultPosition' => $rsp['start'], + 'totalResultsAvailable' => $rsp['Total'], + 'totalResultsReturned' => count($set), + 'firstResultPosition' => $rsp['Offset'], 'Result' => array(), ), ); foreach ($set as $result) { $massaged['ResultSet']['Result'][] = array( - 'Title' => $result['title'], - 'Summary' => $result['abstract'], - 'Url' => $result['url'], - 'ClickUrl' => $result['clickurl'], - 'MimeType' => NULL, // Not returned from BOSS - 'ModificationDate' => strtotime($result['date']), - 'Cache' => NULL, // Not returned from BOSS + 'Title' => $result['Title'], + 'Summary' => $result['Description'], + 'Url' => $result['Url'], + 'ClickUrl' => $result['Url'], + 'MimeType' => NULL, // Not returned by Bing + 'ModificationDate' => strtotime($result['DateTime']), + 'Cache' => $result['CacheUrl'] ); } @@ -64,8 +72,8 @@ $dbh = new PDO('mysql:host=localhost;dbname=ws', $conf['db_user'], $conf['db_pw' PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true)); $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); try { - $stmt = $dbh->prepare("INSERT INTO log (query,profile,mirror) VALUES (:query,:profile,:mirror)"); - $stmt->execute(array(':query'=>$raw,':profile'=>$scope,':mirror'=>$m)); + $stmt = $dbh->prepare("INSERT INTO log (query,profile,mirror,lang) VALUES (:query,:profile,:mirror,:lang)"); + $stmt->execute(array(':query'=>$raw,':profile'=>$scope,':mirror'=>$m,':lang'=>$l)); } catch (PDOException $e) { }