Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:ilestis/miscellany into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
ilestis committed Feb 1, 2019
2 parents 64b8fee + 2e0c3a8 commit 159161a
Show file tree
Hide file tree
Showing 10 changed files with 195 additions and 155 deletions.
64 changes: 56 additions & 8 deletions app/Console/Commands/GenerateEntityMentionMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class GenerateEntityMentionMap extends Command
*/
protected $entityCount = 0;
protected $mapCount = 0;
protected $mapTotalCount = 0;
protected $redirectFixed = 0;

/**
* @var EntityMappingService
Expand All @@ -55,6 +57,8 @@ public function __construct(EntityMappingService $entityMappingService)
*/
public function handle()
{
$start = date('H:i:s');
$this->info("START " . $start);
$entities = [
//'App\Models\Campaign',
'App\Models\Character',
Expand All @@ -75,38 +79,82 @@ public function handle()
EntityMention::truncate();

foreach ($entities as $entity) {
$this->info("Entity $entity");
// Let's first fix those awful redirects
$this->redirectFixed = 0;
$model = new $entity;
$model->with('entity')->where('entry', 'like', '%redirect?what=%')->chunk(1000, function ($models) use ($entity) {
foreach ($models as $model) {
/** @var MiscModel $model */
$pattern = '<a href="([^"]*)">(.*?)&lt;(.*?)&gt;';
$model->entry = preg_replace("`$pattern`i", '<a href="$1">$2</a>', $model->entry);
if ($model->isDirty('entry')) {
$this->redirectFixed++;
$model->timestamps = false;
$model->save();
}
}
});
$this->info("- Fixed {$this->redirectFixed} redirects.");
$this->mapTotalCount += $this->mapCount;

// Mapping
$this->mapCount = 0;
$model = new $entity;
$model->with('entity')->where('entry', 'like', '%data-toggle="tooltip"%')->chunk(5000, function ($models) use ($entity) {
$bar = $this->output->createProgressBar(count($models));
$bar->start();
foreach ($models as $model) {
$this->entityCount++;
/** @var MiscModel $model */
$this->info("Checking " . $model->getTable() . ":" . $model->id);
//$this->info("Checking " . $model->getTable() . ":" . $model->id);
$this->mapCount += $this->entityMapping->mapModel($model);
$bar->advance();
}
$bar->finish();
});
$this->info("- Created {$this->mapCount} maps.\n");
$this->mapTotalCount += $this->mapCount;
}

// Entity Notes
EntityNote::where('entry', 'like', '%data-toggle="tooltip"%')->chunk(5000, function ($models) use ($entity) {
$this->info("Entity Notes");
$this->mapCount = 0;
EntityNote::where('entry', 'like', '%data-toggle="tooltip"%')->chunk(5000, function ($models) {
$bar = $this->output->createProgressBar(count($models));
$bar->start();
foreach ($models as $model) {
$this->entityCount++;
/** @var EntityNote $model */
$this->info("Checking entity_note:" . $model->id);
//$this->info("Checking entity_note:" . $model->id);
$this->mapCount += $this->entityMapping->mapEntityNote($model);
$bar->advance();
}
$bar->finish();
});
$this->info("- Created {$this->mapCount} maps.\n");
$this->mapTotalCount += $this->mapCount;

// Entity Notes
Campaign::where('entry', 'like', '%data-toggle="tooltip"%')->chunk(5000, function ($models) use ($entity) {
// Campaigns
$this->info("Campaigns");
$this->mapCount = 0;
Campaign::where('entry', 'like', '%data-toggle="tooltip"%')->chunk(5000, function ($models) {
$bar = $this->output->createProgressBar(count($models));
$bar->start();
foreach ($models as $model) {
$this->entityCount++;
/** @var EntityNote $model */
$this->info("Checking campaign:" . $model->id);
/** @var Campaign $model */
//$this->info("Checking campaign:" . $model->id);
$this->mapCount += $this->entityMapping->mapCampaign($model);
$bar->advance();
}
$bar->finish();
});
$this->info("- Created {$this->mapCount} maps.\n");
$this->mapTotalCount += $this->mapCount;

$this->info("Updated {$this->entityCount} entities and created {$this->mapCount} maps.");
$this->info("Updated {$this->entityCount} entities and created {$this->mapTotalCount} maps.");
$this->info("END " . date('H:i:s') . " ($start)");

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public function moons()
}

/**
* Get the moons
* Get the seasons
* @return null
*/
public function seasons()
Expand Down
28 changes: 21 additions & 7 deletions app/Services/EntityMappingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,21 @@ public function mapModel(MiscModel $model)
$type = $data['type'];
$id = $data['id'];

// Old redirects or mapping to something else (like the map of a location) that doesn't have a tooltip
if ($id == 'redirect' || empty($data['name'])) {
continue;
}

$singularType = array_get($this->typeMapping, $type, false);
if ($singularType === false) {
dump($mentions);
throw new Exception("Unknown type $type");
}

/** @var Entity $entity */
$entity = Entity::where(['type' => $singularType, 'entity_id' => $id])->first();
if ($entity) {
$this->log("- Mentions " . $entity->id);
//$this->log("- Mentions " . $entity->id);

$mention = new EntityMention();
$mention->entity_id = $model->entity->id;
Expand All @@ -71,7 +77,7 @@ public function mapModel(MiscModel $model)

$createdMappings++;
} else {
$this->log("- Unknown entity of type $singularType and id $id");
//$this->log("- Unknown entity of type $singularType and id $id");
}
}

Expand Down Expand Up @@ -127,19 +133,25 @@ protected function map($model)
$type = $data['type'];
$id = $data['id'];

// Old redirects or mapping to something else (like the map of a location) that doesn't have a tooltip
if ($id == 'redirect' || empty($data['name'])) {
continue;
}

$singularType = array_get($this->typeMapping, $type, false);
if ($singularType === false) {
dump($data);
throw new Exception("Unknown type $type");
}

/** @var Entity $entity */
$target = Entity::where(['type' => $singularType, 'entity_id' => $id])->first();
if ($target) {
$this->log("- Mentions " . $model->id);
//$this->log("- Mentions " . $model->id);

// Do we already have this mention mapped?
if (!empty($existingTargets[$target->id])) {
$this->log("- already have mapping");
//$this->log("- already have mapping");
unset($existingTargets[$target->id]);
$existingMappings++;
} else {
Expand All @@ -157,7 +169,7 @@ protected function map($model)
$createdMappings++;
}
} else {
$this->log("- Unknown entity of type $singularType and id $id");
//$this->log("- Unknown entity of type $singularType and id $id");
}
}

Expand All @@ -183,6 +195,8 @@ public function updateMentions(Entity $entity, $url = null)
$name = e($entity->name);

$entityLink = !empty($url) ? $url : $entity->url();

//$entityLink = str_replace('http://kanka.loc', 'https://kanka.io', $entityLink);
// Replace the link's locale to avoid issues when people use several languages
$entityLinkSegments = explode('/', $entityLink);
$entityLinkSegments[3] = '(.){2,5}';
Expand All @@ -192,7 +206,7 @@ public function updateMentions(Entity $entity, $url = null)
// Just text, no tooltip
$patternNoTooltip = '<a href=\"' . $entityLinkSearch . '\">(.*?)</a>';
// We need to go 0.300 as the text is encoded, so some html entities will make it longer. It's not great
$patternTooltip = '<a title="([^"]*)" href="' . $entityLinkSearch . '" data-toggle="tooltip" data-html="true">(.*?)</a>';
$patternTooltip = '<a title="([^"]*)" href="' . $entityLinkSearch . '" data-toggle="tooltip"( data-html="true")?>(.*?)</a>';

$replace = '<a href=\"' . $entityLink . '\">' . $name . '</a>';
if (!empty($tooltip)) {
Expand All @@ -202,12 +216,12 @@ public function updateMentions(Entity $entity, $url = null)
// dump($patternNoTooltip);
// dump($patternTooltip);
// dump($replace);

/** @var EntityMention $target */
foreach ($entity->targetMentions()->with(['entity', 'campaign', 'entityNote'])->get() as $target) {
// We've got a target, we need to update its entry field
$realTarget = $target->isEntityNote() ? $target->entityNote : ($target->isCampaign() ? $target->campaign : $target->entity->child);
$text = $realTarget->entry;

// dump($text);
$text = preg_replace("`$patternNoTooltip`i", $replace, $text);
$text = preg_replace("`$patternTooltip`i", $replace, $text);
Expand Down
13 changes: 7 additions & 6 deletions app/Traits/ExportableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ public function export()
}

// Entity values
$foreigns = ['notes', 'attributes', 'relationships'];
foreach ($foreigns as $foreign) {
// Have to do the ()->get because of attributes being otherwise something else
foreach ($this->entity->$foreign()->get() as $model) {
$json[$foreign][] = $model->toArray();
if (!empty($this->entity)) {
$foreigns = ['notes', 'attributes', 'relationships'];
foreach ($foreigns as $foreign) {
// Have to do the ()->get because of attributes being otherwise something else
foreach ($this->entity->$foreign()->get() as $model) {
$json[$foreign][] = $model->toArray();
}
}

}

return json_encode($json);
Expand Down
6 changes: 4 additions & 2 deletions resources/views/cruds/_relations.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@endif
<th class="text-right">
@can('relation', [$model, 'add'])
<a href="{{ route($name . '.relations.create', [$name => $model->id]) }}" class="btn btn-primary btn-sm">
<a href="{{ route($name . '.relations.create', [$name => $model->id]) }}" class="btn btn-primary btn-sm" data-toggle="ajax-modal" data-target="#entity-modal" data-url="{{ route($name . '.relations.create', [$name => $model->id]) }}">
<i class="fa fa-plus"></i> {{ trans('crud.relations.actions.add') }} </a>
@endcan
</th>
Expand Down Expand Up @@ -48,7 +48,9 @@
@endif
<td class="text-right">
@can('relation', [$model, 'edit'])
<a href="{{ route($name . '.relations.edit', [$name => $model, 'relation' => $relation]) }}" class="btn btn-xs btn-primary"><i class="fa fa-edit"></i> {{ trans('crud.edit') }}</a>
<a href="{{ route($name . '.relations.edit', [$name => $model, 'relation' => $relation]) }}" class="btn btn-xs btn-primary"
data-toggle="ajax-modal" data-target="#entity-modal" data-url="{{ route($name . '.relations.edit', [$name => $model, 'relation' => $relation]) }}"
><i class="fa fa-edit"></i> {{ trans('crud.edit') }}</a>
@endcan
@can('relation', [$model, 'delete'])
{!! Form::open(['method' => 'DELETE', 'route' => [$name . '.relations.destroy', $name => $model, 'relation' => $relation], 'style'=>'display:inline']) !!}
Expand Down
28 changes: 12 additions & 16 deletions resources/views/cruds/relations/create.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@extends('layouts.app', [
@extends('layouts.' . (request()->ajax() ? 'ajax' : 'app'), [
'title' => trans('relations.create.title', ['name' => $model->name]),
'description' => trans('relations.create.description'),
'breadcrumbs' => [
Expand All @@ -9,25 +9,21 @@
])

@section('content')
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-body">
@include('partials.errors')
<div class="panel panel-default">
<div class="panel-body">
@include('partials.errors')

{!! Form::open(array('route' => [$route . '.store', $model->id], 'method'=>'POST', 'data-shortcut' => "1")) !!}
@include('cruds.relations._form')
{!! Form::open(array('route' => [$route . '.store', $model->id], 'method'=>'POST', 'data-shortcut' => "1")) !!}
@include('cruds.relations._form')

{!! Form::hidden('owner_id', $model->entity->id) !!}
{!! Form::hidden('owner_id', $model->entity->id) !!}

<div class="form-group">
<button class="btn btn-success">{{ trans('crud.save') }}</button>
{!! trans('crud.or_cancel', ['url' => (!empty($cancel) ? $cancel : url()->previous() . (strpos(url()->previous(), '#relation') === false ? '#relation' : null))]) !!}
</div>

{!! Form::close() !!}
</div>
<div class="form-group">
<button class="btn btn-success">{{ trans('crud.save') }}</button>
{!! trans('crud.or_cancel', ['url' => (!empty($cancel) ? $cancel : url()->previous() . (strpos(url()->previous(), '#relation') === false ? '#relation' : null))]) !!}
</div>

{!! Form::close() !!}
</div>
</div>
@endsection
28 changes: 12 additions & 16 deletions resources/views/cruds/relations/edit.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@extends('layouts.app', [
@extends('layouts.' . (request()->ajax() ? 'ajax' : 'app'), [
'title' => trans('relations.edit.title', ['name' => $model->name]),
'description' => trans('relations.edit.description'),
'breadcrumbs' => [
Expand All @@ -9,25 +9,21 @@
]
])
@section('content')
<div class="row">
<div class="col-md-12 col-md-offset">
<div class="panel panel-default">
<div class="panel-body">
@include('partials.errors')
<div class="panel panel-default">
<div class="panel-body">
@include('partials.errors')

{!! Form::model($relation, ['method' => 'PATCH', 'route' => [$route . '.update', $model->id, $relation->id], 'data-shortcut' => "1"]) !!}
@include('cruds.relations._form')
{!! Form::model($relation, ['method' => 'PATCH', 'route' => [$route . '.update', $model->id, $relation->id], 'data-shortcut' => "1"]) !!}
@include('cruds.relations._form')

{!! Form::hidden('owner_id', $model->entity->id) !!}
{!! Form::hidden('owner_id', $model->entity->id) !!}

<div class="form-group">
<button class="btn btn-success">{{ trans('crud.save') }}</button>
{!! trans('crud.or_cancel', ['url' => (!empty($cancel) ? $cancel : url()->previous() . (strpos(url()->previous(), '#relation') === false ? '#relation' : null))]) !!}
</div>

{!! Form::close() !!}
</div>
<div class="form-group">
<button class="btn btn-success">{{ trans('crud.save') }}</button>
{!! trans('crud.or_cancel', ['url' => (!empty($cancel) ? $cancel : url()->previous() . (strpos(url()->previous(), '#relation') === false ? '#relation' : null))]) !!}
</div>

{!! Form::close() !!}
</div>
</div>
@endsection
Loading

0 comments on commit 159161a

Please sign in to comment.