Skip to content

Commit

Permalink
fixed url manager issue with parameterized routes, crawler uses active
Browse files Browse the repository at this point in the history
data provider #1244, remove click data
  • Loading branch information
nadar committed Mar 21, 2017
1 parent cd1e31a commit 6733df8
Show file tree
Hide file tree
Showing 10 changed files with 233 additions and 126 deletions.
12 changes: 6 additions & 6 deletions core/web/UrlManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function parseRequest($request)
{
// extra data from request to composition, which changes the pathInfo of the Request-Object.
$resolver = $this->getComposition()->getResolvedPathInfo($request);

$request->setPathInfo($resolver['route']);

$parsedRequest = parent::parseRequest($request);
Expand Down Expand Up @@ -320,8 +320,10 @@ public function internalCreateAbsoluteUrl($params, $scheme = null)
*/
private function findModuleInRoute($route)
{
$route = parse_url($route, PHP_URL_PATH);

$parts = array_values(array_filter(explode('/', $route)));

if (isset($parts[0]) && array_key_exists($parts[0], Yii::$app->getApplicationModules())) {
return $parts[0];
}
Expand Down Expand Up @@ -355,10 +357,8 @@ private function urlReplaceModule($url, $navItemId, Composition $composition)

// if the item type is (2) module and the current context module is not equals we don't have to remove to replace the module name
// as this is an url rule not related to the current module.
if ($item->type == 2) {
if ($module !== $item->moduleName) {
return $url;
}
if ($item->type == 2 && $module !== $item->moduleName) {
return $url;
}

return preg_replace("/$module/", rtrim($item->link, '/'), ltrim($route, '/'), 1);
Expand Down
File renamed without changes.
45 changes: 25 additions & 20 deletions modules/crawler/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

An easy to use Full-Website page crawler to make provide search results on your page. The crawlermodule gather all informations about the sides on the configured domain and stores the index in the database. From there you can now create search querys to provide search results, there are also helper methods which provides inteligent search results by spliting the input into multiple search querys (used by default).

### Install
## Install

Add the package to your composer file

Expand Down Expand Up @@ -39,7 +39,7 @@ After setup the module in your config you have to run the migrations and import
./vendor/bin/luya import
```

### Execute
### Running the Crawler

To execute the command (and run the crawler proccess) use the crawler command `crawl`, you should put this command in cronjob to make sure your index is up-to-date:

Expand All @@ -49,35 +49,41 @@ To execute the command (and run the crawler proccess) use the crawler command `c

> In order to provide current crawl results you should create a cronjob which crawls the page each night: `cd httpdocs/current && ./vendor/bin/luya crawler/crawl`
### Statistic Command

You can also get statistic Results enabling a cronjob executing each week:

```
./vendor/bin/luya crawler/statistic
```


Create search form
------------------
## Create search form

Make a post request with `query` to the `crawler/default/index` route and render the view as follows
Make a post request with `query` to the `crawler/default/index` route and render the view as follows:

```php
<h1>Search</h1>
<p>You where looking for <b><?= $query; ?></b>.</p>
<h2><?= count($results); ?> results</h2>
<ul>
<? foreach($results as $item): ?>
<li>
<a href="<?= $item->url; ?>"><?= $item->title; ?></a>
<p style="background-color:red;"><?= $item->preview($query); ?></p>
</li>
<? endforeach; ?>
</ul>
<?php
use luya\helpers\Url;
use yii\widgets\LinkPager;

/* @var $query string The lookup query encoded */
/* @var $this \luya\web\View */
/* @var $provider \yii\data\ActiveDataProvider */
?>

<form class="searchpage__searched-form" action="<?= Url::toRoute(['/crawler/default/index']); ?>" method="get">
<input id="search" name="query" type="search" value="<?= $query ?>">
<input type="submit" value="Search"/>
</form>

<h2><?= $provider->totalCount; ?> Results</h2>
<?php foreach($provider->models as $item): /* @var $item \luya\crawler\models\Index */ ?>
<h3><?= $item->title; ?></h3>
<p><?= $item->preview($query); ?></p>
<a href="<?= $item->url; ?>"><?= $item->url; ?></a>
<?php endforeach; ?>
<?= LinkPager::widget(['pagination' => $provider->pagination]); ?>
```


### ASYNC Request

To make async search queries use the restcontroller route (jquery example):
Expand All @@ -95,7 +101,6 @@ $.ajax({

## Crawler Settings


Set the language in your html markup

```html
Expand Down
4 changes: 2 additions & 2 deletions modules/crawler/src/frontend/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ class Module extends \luya\base\Module
*/
public $statisticRecipients = [];

public $searchResultPageSize = 25;

/**
* @inheritdoc
*/
public $urlRules = [
['pattern' => 'crawler', 'route' => 'crawler/default'],
['pattern' => 'crawler/click/<slug:[a-zA-Z0-9\-]+>/<searchId:\d+>/<indexId:\d+>/<position:\d+>', 'route' => 'crawler/click/index'],

];
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function actionIndex($searchId, $indexId, $position)
'position' => $position,
];
// save whether valid or not, as user must be redirected.
$model->save();
$model->save(false);

$index = Index::findOne($indexId);

Expand Down
54 changes: 52 additions & 2 deletions modules/crawler/src/frontend/controllers/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,64 @@
use Yii;
use luya\crawler\models\Index;
use yii\helpers\Html;
use yii\data\ActiveDataProvider;
use luya\crawler\models\Searchdata;
use yii\data\ArrayDataProvider;

/**
* Crawler Index Controller.
*
* Returns an {{\yii\data\ActiveDataProvider}} within $provider.
*
* @author Basil Suter <[email protected]>
*/
class DefaultController extends \luya\web\Controller
{
public function actionIndex($query = null)
/**
* Get search overview.
*
* The index action will return an active data provider object inside the $provider variable:
*
* ```php
* foreach ($provider->models as $item) {
* var_dump($item);
* }
* ```
*
* @return string
*/
public function actionIndex($query = null, $page = null)
{
$language = Yii::$app->composition->getKey('langShortCode');

if (empty($query)) {
$provider = new ArrayDataProvider();
} else {
$activeQuery = Index::activeQuerySearch($query, $language);

$provider = new ActiveDataProvider([
'query' => $activeQuery,
'pagination' => [
'defaultPageSize' => $this->module->searchResultPageSize,
'route' => '/crawler/default',
'params' => ['query' => $query, 'page' => $page]
],
]);

$searchData = new Searchdata();
$searchData->detachBehavior('LogBehavior');
$searchData->attributes = [
'query' => $query,
'results' => $provider->totalCount,
'timestamp' => time(),
'language' => $language,
];
$searchData->save(false);
}

return $this->render('index', [
'query' => Html::encode($query),
'results' => Index::searchByQuery($query, Yii::$app->composition->getKey('langShortCode')),
'provider' => $provider,
]);
}
}
48 changes: 0 additions & 48 deletions modules/crawler/src/models/Click.php

This file was deleted.

Loading

0 comments on commit 6733df8

Please sign in to comment.