Skip to content

Commit

Permalink
generate search documents via Guzzle
Browse files Browse the repository at this point in the history
  • Loading branch information
fonsekaean committed Nov 18, 2018
1 parent 1c2804a commit 7f543aa
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
3 changes: 2 additions & 1 deletion src/Extensions/SearchDocumentGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace SilverStripers\ElementalSearch\Extensions;

use \Exception;
use SilverStripe\Control\Director;
use SilverStripe\ORM\DataExtension;
use SilverStripe\ORM\DataObject;
use SilverStripe\Versioned\Versioned;
Expand All @@ -23,7 +24,7 @@ public function getGenerateSearchLink()
{
$owner = $this->owner;
if(method_exists($owner, 'Link')) {
$link = $owner->Link();
$link = Director::absoluteURL($owner->Link());
if(strpos($link, '?') !== false) {
return $link . '&SearchGen=1';
}
Expand Down
62 changes: 30 additions & 32 deletions src/Model/SearchDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace SilverStripers\ElementalSearch\Model;

use GuzzleHttp\Client;
use SilverStripe\Control\Director;
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DataObject;
Expand Down Expand Up @@ -48,44 +49,41 @@ public function makeSearchContent()
$origin = $this->Origin();
$searchLink = $origin->getGenerateSearchLink();

$mode = Versioned::get_reading_mode();
$themes = SSViewer::get_themes();
SSViewer::set_themes(SSViewer::config()->uninherited('themes'));
Versioned::set_reading_mode('Stage.Stage');
$client = new Client();
$res = $client->request('GET', $searchLink);
if($res->getStatusCode() == 200) {
$body = $res->getBody();

$response = Director::test($searchLink);
$body = $response->getBody();

$x_path = $origin->config()->get('search_x_path');
if(!$x_path) {
$x_path = self::config()->get('search_x_path');
}

if($x_path) {
$domDoc = new \DOMDocument();
@$domDoc->loadHTML($body);
$x_path = $origin->config()->get('search_x_path');
if(!$x_path) {
$x_path = self::config()->get('search_x_path');
}

$finder = new \DOMXPath($domDoc);
$nodes = $finder->query("//*[contains(@class, '$x_path')]");
$nodeValues = [];
if($nodes->length) {
foreach ($nodes as $node) {
$nodeValues[] = $node->nodeValue;
if($x_path) {
$domDoc = new \DOMDocument();
@$domDoc->loadHTML($body);

$finder = new \DOMXPath($domDoc);
$nodes = $finder->query("//*[contains(@class, '$x_path')]");
$nodeValues = [];
if($nodes->length) {
foreach ($nodes as $node) {
$nodeValues[] = $node->nodeValue;
}
}
$contents = implode("\n\n", $nodeValues);
}
$contents = implode("\n\n", $nodeValues);
}
else {
$contents = strip_tags($body);
else {
$contents = strip_tags($body);
}

$this->Title = $origin->getTitle();
if($contents) {
$this->Content = $contents;
}
$this->write();
}

$this->Title = $origin->getTitle();
if($contents) {
$this->Content = $contents;
}
$this->write();
SSViewer::set_themes($themes);
Versioned::set_reading_mode($mode);

}

Expand Down

0 comments on commit 7f543aa

Please sign in to comment.