Commit 1b7f97a1 authored by Dominik Hebeler's avatar Dominik Hebeler
Browse files

Die Quicktips werden nun auch in der angegebenen Localization abgerufen.

Wikipedia Quciktips werden nun über die Opensearch Api abgerufen (https://www.mediawiki.org/wiki/API:Opensearch)
Dies liefert wesentlich bessere Ergebnisse.
parent 46f465fe
...@@ -2,11 +2,10 @@ ...@@ -2,11 +2,10 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App; use App;
use App\Http\Controllers\Controller;
use App\MetaGer; use App\MetaGer;
use Illuminate\Http\Request;
class MetaGerSearch extends Controller class MetaGerSearch extends Controller
{ {
...@@ -40,12 +39,10 @@ class MetaGerSearch extends Controller ...@@ -40,12 +39,10 @@ class MetaGerSearch extends Controller
# Zunächst den Spruch # Zunächst den Spruch
$spruecheFile = storage_path() . "/app/public/sprueche.txt"; $spruecheFile = storage_path() . "/app/public/sprueche.txt";
if( file_exists($spruecheFile) && $request->has('sprueche') ) if (file_exists($spruecheFile) && $request->has('sprueche')) {
{
$sprueche = file($spruecheFile); $sprueche = file($spruecheFile);
$spruch = $sprueche[array_rand($sprueche)]; $spruch = $sprueche[array_rand($sprueche)];
}else } else {
{
$spruch = ""; $spruch = "";
} }
...@@ -53,20 +50,19 @@ class MetaGerSearch extends Controller ...@@ -53,20 +50,19 @@ class MetaGerSearch extends Controller
$file = storage_path() . "/app/public/qtdata.csv"; $file = storage_path() . "/app/public/qtdata.csv";
$mquicktips = []; $mquicktips = [];
if( file_exists($file) && $q !== '') if (file_exists($file) && $q !== '') {
{
$file = fopen($file, 'r'); $file = fopen($file, 'r');
while (($line = fgetcsv($file)) !== FALSE) { while (($line = fgetcsv($file)) !== false) {
$words = array_slice($line,3); $words = array_slice($line, 3);
$isIn = FALSE; $isIn = false;
foreach($words as $word){ foreach ($words as $word) {
$word = strtolower($word); $word = strtolower($word);
if(strpos($q, $word) !== FALSE){ if (strpos($q, $word) !== false) {
$isIn = TRUE; $isIn = true;
break; break;
} }
} }
if($isIn === TRUE){ if ($isIn === true) {
$quicktip = array('QT_Type' => "MQT"); $quicktip = array('QT_Type' => "MQT");
$quicktip["URL"] = $line[0]; $quicktip["URL"] = $line[0];
$quicktip["title"] = $line[1]; $quicktip["title"] = $line[1];
...@@ -79,30 +75,28 @@ class MetaGerSearch extends Controller ...@@ -79,30 +75,28 @@ class MetaGerSearch extends Controller
# Wikipedia Quicktip # Wikipedia Quicktip
$quicktips = []; $quicktips = [];
$url = "http://de.wikipedia.org/w/api.php?action=query&titles=".urlencode(implode("_",array_diff(explode(" ",$q),array("wikipedia"))))."&prop=info|extracts|categories&inprop=url|displaytitle&exintro&exsentences=3&format=json"; if (App::isLocale('en')) {
$url = "https://en.wikipedia.org/w/api.php?action=opensearch&search=" . urlencode($q) . "&limit=1&namespace=0&format=json";
} else {
$url = "https://de.wikipedia.org/w/api.php?action=opensearch&search=" . urlencode($q) . "&limit=1&namespace=0&format=json";
}
$decodedResponse = json_decode($this->get($url), true); $decodedResponse = json_decode($this->get($url), true);
if( isset($decodedResponse["query"]["pages"]) ) if (isset($decodedResponse[1][0]) && isset($decodedResponse[2][0]) && isset($decodedResponse[3][0])) {
{
foreach($decodedResponse["query"]["pages"] as $result)
{
if( isset($result['displaytitle']) && isset($result['fullurl']) && isset($result['extract']) )
{
$quicktip = []; $quicktip = [];
$quicktip["title"] = $result['displaytitle']; $quicktip["title"] = $decodedResponse[1][0];
$quicktip["URL"] = $result['fullurl']; $quicktip["URL"] = $decodedResponse[3][0];
$quicktip["descr"] = strip_tags($result['extract']); $quicktip["descr"] = $decodedResponse[2][0];
$quicktip['gefVon'] = "aus <a href=\"https://de.wikipedia.org\" target=\"_blank\">Wikipedia, der freien Enzyklopädie</a>"; $quicktip['gefVon'] = "aus <a href=\"https://de.wikipedia.org\" target=\"_blank\">Wikipedia, der freien Enzyklopädie</a>";
$quicktips[] = $quicktip; $quicktips[] = $quicktip;
} }
}
}
$mquicktips = array_merge($mquicktips, $quicktips); $mquicktips = array_merge($mquicktips, $quicktips);
# Uns Natürlich das wussten Sie schon: # Uns Natürlich das wussten Sie schon:
$file = storage_path() . "/app/public/tips.txt"; $file = storage_path() . "/app/public/tips.txt";
if( file_exists($file) ) if (file_exists($file)) {
{
$tips = file($file); $tips = file($file);
$tip = $tips[array_rand($tips)]; $tip = $tips[array_rand($tips)];
...@@ -111,8 +105,7 @@ class MetaGerSearch extends Controller ...@@ -111,8 +105,7 @@ class MetaGerSearch extends Controller
# Uns die Werbelinks: # Uns die Werbelinks:
$file = storage_path() . "/app/public/ads.txt"; $file = storage_path() . "/app/public/ads.txt";
if( file_exists($file) ) if (file_exists($file)) {
{
$ads = json_decode(file_get_contents($file), true); $ads = json_decode(file_get_contents($file), true);
$ad = $ads[array_rand($ads)]; $ad = $ads[array_rand($ads)];
...@@ -122,15 +115,13 @@ class MetaGerSearch extends Controller ...@@ -122,15 +115,13 @@ class MetaGerSearch extends Controller
->with('spruch', $spruch) ->with('spruch', $spruch)
->with('mqs', $mquicktips); ->with('mqs', $mquicktips);
} }
public function tips() public function tips()
{ {
$file = storage_path() . "/app/public/tips.txt"; $file = storage_path() . "/app/public/tips.txt";
$tips = []; $tips = [];
if( file_exists($file) ) if (file_exists($file)) {
{
$tips = file($file); $tips = file($file);
} }
return view('tips') return view('tips')
...@@ -138,7 +129,8 @@ class MetaGerSearch extends Controller ...@@ -138,7 +129,8 @@ class MetaGerSearch extends Controller
->with('tips', $tips); ->with('tips', $tips);
} }
function get($url) { public function get($url)
{
return file_get_contents($url); return file_get_contents($url);
} }
......
...@@ -44,7 +44,7 @@ ...@@ -44,7 +44,7 @@
@if( $metager->showQuicktips() ) @if( $metager->showQuicktips() )
<div class="hidden-xs col-md-4" id="quicktips"> <div class="hidden-xs col-md-4" id="quicktips">
<iframe class="col-mod-4 hidden-xs hidden-sm" src="/qt?q={{ $metager->getQ() }}&sprueche={{ $metager->getSprueche() }}"></iframe> <iframe class="col-mod-4 hidden-xs hidden-sm" src="{{ LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), "/qt") }}?q={{ $metager->getQ() }}&sprueche={{ $metager->getSprueche() }}"></iframe>
</div> </div>
@endif @endif
@endsection @endsection
...@@ -35,5 +35,5 @@ ...@@ -35,5 +35,5 @@
</nav> </nav>
</div> </div>
<div class="hidden-xs col-md-4" id="quicktips"> <div class="hidden-xs col-md-4" id="quicktips">
<iframe class="col-mod-4 hidden-xs hidden-sm" src="/qt?q={{ $metager->getQ() }}&sprueche={{ $metager->getSprueche() }}"></iframe> <iframe class="col-mod-4 hidden-xs hidden-sm" src="{{ LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), "/qt") }}?q={{ $metager->getQ() }}&sprueche={{ $metager->getSprueche() }}"></iframe>
</div> </div>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment