Merge branch 'master' into 1236-database-changes

This commit is contained in:
Daniel Klabbers 2018-06-19 09:57:47 +02:00
commit 2cd77e231f
21 changed files with 84 additions and 124 deletions

View File

@ -172,8 +172,13 @@ export default class DiscussionPage extends Page {
// the 'discussion' relationship linked, then sorting and splicing.
let includedPosts = [];
if (discussion.payload && discussion.payload.included) {
const discussionId = discussion.id();
includedPosts = discussion.payload.included
.filter(record => record.type === 'posts' && record.relationships && record.relationships.discussion)
.filter(record => record.type === 'posts'
&& record.relationships
&& record.relationships.discussion
&& record.relationships.discussion.data.id === discussionId)
.map(record => app.store.getById('posts', record.id))
.sort((a, b) => a.id() - b.id())
.slice(0, 20);

View File

@ -21,7 +21,7 @@ use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\Response\JsonResponse;
class TokenController implements RequestHandlerInterface
class CreateTokenController implements RequestHandlerInterface
{
/**
* @var \Flarum\User\UserRepository

View File

@ -11,6 +11,7 @@
namespace Flarum\Api\Controller;
use Flarum\Api\Serializer\CurrentUserSerializer;
use Flarum\Api\Serializer\UserSerializer;
use Flarum\User\UserRepository;
use Psr\Http\Message\ServerRequestInterface;
@ -55,7 +56,7 @@ class ShowUserController extends AbstractShowController
$actor = $request->getAttribute('actor');
if ($actor->id == $id) {
$this->serializer = 'Flarum\Api\Serializer\CurrentUserSerializer';
$this->serializer = CurrentUserSerializer::class;
}
return $this->users->findOrFail($id, $actor);

View File

@ -25,7 +25,7 @@ return function (RouteCollection $map, RouteHandlerFactory $route) {
$map->post(
'/token',
'token',
$route->toController(Controller\TokenController::class)
$route->toController(Controller\CreateTokenController::class)
);
// Send forgot password email

View File

@ -11,16 +11,16 @@
namespace Flarum\Database;
use Illuminate\Database\ConnectionResolverInterface as Resolver;
use Illuminate\Database\ConnectionInterface;
class DatabaseMigrationRepository implements MigrationRepositoryInterface
{
/**
* The database connection resolver instance.
* The name of the database connection to use.
*
* @var \Illuminate\Database\ConnectionResolverInterface
* @var ConnectionInterface
*/
protected $resolver;
protected $connection;
/**
* The name of the migration table.
@ -29,23 +29,16 @@ class DatabaseMigrationRepository implements MigrationRepositoryInterface
*/
protected $table;
/**
* The name of the database connection to use.
*
* @var string
*/
protected $connection;
/**
* Create a new database migration repository instance.
*
* @param \Illuminate\Database\ConnectionResolverInterface $resolver
* @param string $table
* @param \Illuminate\Database\ConnectionInterface $connection
* @param string $table
*/
public function __construct(Resolver $resolver, $table)
public function __construct(ConnectionInterface $connection, $table)
{
$this->connection = $connection;
$this->table = $table;
$this->resolver = $resolver;
}
/**
@ -104,7 +97,7 @@ class DatabaseMigrationRepository implements MigrationRepositoryInterface
*/
public function createRepository()
{
$schema = $this->getConnection()->getSchemaBuilder();
$schema = $this->connection->getSchemaBuilder();
$schema->create($this->table, function ($table) {
$table->string('migration');
@ -119,7 +112,7 @@ class DatabaseMigrationRepository implements MigrationRepositoryInterface
*/
public function repositoryExists()
{
$schema = $this->getConnection()->getSchemaBuilder();
$schema = $this->connection->getSchemaBuilder();
return $schema->hasTable($this->table);
}
@ -131,37 +124,6 @@ class DatabaseMigrationRepository implements MigrationRepositoryInterface
*/
protected function table()
{
return $this->getConnection()->table($this->table);
}
/**
* Get the connection resolver instance.
*
* @return \Illuminate\Database\ConnectionResolverInterface
*/
public function getConnectionResolver()
{
return $this->resolver;
}
/**
* Resolve the database connection instance.
*
* @return \Illuminate\Database\Connection
*/
public function getConnection()
{
return $this->resolver->connection($this->connection);
}
/**
* Set the information source to gather data.
*
* @param string $name
* @return void
*/
public function setSource($name)
{
$this->connection = $name;
return $this->connection->table($this->table);
}
}

View File

@ -11,8 +11,7 @@
namespace Flarum\Database;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Database\ConnectionInterface;
use Flarum\Settings\DatabaseSettingsRepository;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
@ -100,12 +99,20 @@ abstract class Migration
public static function addSettings(array $defaults)
{
return [
'up' => function (SettingsRepositoryInterface $settings) use ($defaults) {
'up' => function (Builder $schema) use ($defaults) {
$settings = new DatabaseSettingsRepository(
$schema->getConnection()
);
foreach ($defaults as $key => $value) {
$settings->set($key, $value);
}
},
'down' => function (SettingsRepositoryInterface $settings) use ($defaults) {
'down' => function (Builder $schema) use ($defaults) {
$settings = new DatabaseSettingsRepository(
$schema->getConnection()
);
foreach (array_keys($defaults) as $key) {
$settings->delete($key);
}
@ -130,7 +137,9 @@ abstract class Migration
}
return [
'up' => function (ConnectionInterface $db) use ($keys) {
'up' => function (Builder $schema) use ($keys) {
$db = $schema->getConnection();
foreach ($keys as $key) {
$instance = $db->table('permissions')->where($key)->first();
@ -140,7 +149,9 @@ abstract class Migration
}
},
'down' => function (ConnectionInterface $db) use ($keys) {
'down' => function (Builder $schema) use ($keys) {
$db = $schema->getConnection();
foreach ($keys as $key) {
$db->table('permissions')->where($key)->delete();
}

View File

@ -52,12 +52,4 @@ interface MigrationRepositoryInterface
* @return bool
*/
public function repositoryExists();
/**
* Set the information source to gather data.
*
* @param string $name
* @return void
*/
public function setSource($name);
}

View File

@ -22,7 +22,7 @@ class MigrationServiceProvider extends AbstractServiceProvider
public function register()
{
$this->app->singleton('Flarum\Database\MigrationRepositoryInterface', function ($app) {
return new DatabaseMigrationRepository($app['db'], 'migrations');
return new DatabaseMigrationRepository($app['flarum.db'], 'migrations');
});
$this->app->bind(MigrationCreator::class, function (Application $app) {

View File

@ -13,7 +13,8 @@ namespace Flarum\Database;
use Exception;
use Flarum\Extension\Extension;
use Illuminate\Database\ConnectionResolverInterface as Resolver;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\Schema\Builder;
use Illuminate\Filesystem\Filesystem;
class Migrator
@ -33,18 +34,11 @@ class Migrator
protected $files;
/**
* The connection resolver instance.
* The database schema builder instance.
*
* @var \Illuminate\Database\ConnectionResolverInterface
* @var Builder
*/
protected $resolver;
/**
* The name of the default connection.
*
* @var string
*/
protected $connection;
protected $schemaBuilder;
/**
* The notes for the current operation.
@ -57,17 +51,18 @@ class Migrator
* Create a new migrator instance.
*
* @param MigrationRepositoryInterface $repository
* @param Resolver $resolver
* @param ConnectionInterface $connection
* @param Filesystem $files
*/
public function __construct(
MigrationRepositoryInterface $repository,
Resolver $resolver,
ConnectionInterface $connection,
Filesystem $files
) {
$this->files = $files;
$this->resolver = $resolver;
$this->repository = $repository;
$this->schemaBuilder = $connection->getSchemaBuilder();
}
/**
@ -199,7 +194,7 @@ class Migrator
protected function runClosureMigration($migration, $direction = 'up')
{
if (is_array($migration) && array_key_exists($direction, $migration)) {
app()->call($migration[$direction]);
call_user_func($migration[$direction], $this->schemaBuilder);
} else {
throw new Exception('Migration file should contain an array with up/down.');
}
@ -268,34 +263,6 @@ class Migrator
return $this->notes;
}
/**
* Resolve the database connection instance.
*
* @param string $connection
* @return \Illuminate\Database\Connection
*/
public function resolveConnection($connection)
{
return $this->resolver->connection($connection);
}
/**
* Set the default connection name.
*
* @param string $name
* @return void
*/
public function setConnection($name)
{
if (! is_null($name)) {
$this->resolver->setDefaultConnection($name);
}
$this->repository->setSource($name);
$this->connection = $name;
}
/**
* Get the migration repository instance.
*

View File

@ -29,6 +29,10 @@ class FulltextGambit implements GambitInterface
throw new LogicException('This gambit can only be applied on a DiscussionSearch');
}
// The @ character crashes fulltext searches on InnoDB tables.
// See https://bugs.mysql.com/bug.php?id=74042
$bit = str_replace('@', '*', $bit);
$search->getQuery()
->selectRaw('SUBSTRING_INDEX(GROUP_CONCAT(posts.id ORDER BY MATCH(posts.content) AGAINST (?) DESC), \',\', 1) as most_relevant_post_id', [$bit])
->leftJoin('posts', 'posts.discussion_id', '=', 'discussions.id')

View File

@ -16,7 +16,7 @@ use Flarum\Frontend\Event\Rendering;
use Illuminate\Contracts\Container\Container;
use Illuminate\Events\Dispatcher;
class Assets implements Extender
class Assets implements ExtenderInterface
{
protected $appName;

View File

@ -23,7 +23,7 @@ use Illuminate\Contracts\Container\Container;
*
* @deprecated
*/
class Compat implements Extender
class Compat implements ExtenderInterface
{
protected $callback;

View File

@ -14,7 +14,7 @@ namespace Flarum\Extend;
use Flarum\Extension\Extension;
use Illuminate\Contracts\Container\Container;
interface Extender
interface ExtenderInterface
{
public function __invoke(Container $container, Extension $extension = null);
}

View File

@ -16,7 +16,7 @@ use Flarum\Formatter\Event\Configuring;
use Illuminate\Contracts\Container\Container;
use Illuminate\Events\Dispatcher;
class FormatterConfiguration implements Extender
class FormatterConfiguration implements ExtenderInterface
{
protected $callback;

View File

@ -18,7 +18,7 @@ use Illuminate\Contracts\Container\Container;
use InvalidArgumentException;
use RuntimeException;
class LanguagePack implements Extender
class LanguagePack implements ExtenderInterface
{
public function __invoke(Container $container, Extension $extension = null)
{

View File

@ -16,7 +16,7 @@ use Flarum\Extension\Extension;
use Flarum\Locale\LocaleManager;
use Illuminate\Contracts\Container\Container;
class Locales implements Extender
class Locales implements ExtenderInterface
{
protected $directory;

View File

@ -15,7 +15,7 @@ use Flarum\Extension\Extension;
use Flarum\Http\RouteHandlerFactory;
use Illuminate\Contracts\Container\Container;
class Routes implements Extender
class Routes implements ExtenderInterface
{
protected $appName;

View File

@ -21,8 +21,13 @@ class ExtensionServiceProvider extends AbstractServiceProvider
*/
public function register()
{
$this->app->bind('flarum.extensions', ExtensionManager::class);
$this->app->singleton(ExtensionManager::class);
$this->app->alias(ExtensionManager::class, 'flarum.extensions');
// Boot extensions when the app is booting. This must be done as a boot
// listener on the app rather than in the service provider's boot method
// below, so that extensions have a chance to register things on the
// container before the core boot code runs.
$this->app->booting(function (Container $app) {
$app->make('flarum.extensions')->extend($app);
});

View File

@ -12,7 +12,7 @@
namespace Flarum\Forum\Controller;
use Flarum\Api\Client;
use Flarum\Api\Controller\TokenController;
use Flarum\Api\Controller\CreateTokenController;
use Flarum\Http\AccessToken;
use Flarum\Http\Rememberer;
use Flarum\Http\SessionAuthenticator;
@ -67,7 +67,7 @@ class LogInController implements RequestHandlerInterface
$body = $request->getParsedBody();
$params = array_only($body, ['identification', 'password']);
$response = $this->apiClient->send(TokenController::class, $actor, [], $params);
$response = $this->apiClient->send(CreateTokenController::class, $actor, [], $params);
if ($response->getStatusCode() === 200) {
$data = json_decode($response->getBody());

View File

@ -90,6 +90,8 @@ class UserRepository
*/
public function getIdsForUsername($string, User $actor = null)
{
$string = $this->escapeLikeString($string);
$query = User::where('username', 'like', '%'.$string.'%')
->orderByRaw('username = ? desc', [$string])
->orderByRaw('username like ? desc', [$string.'%']);
@ -112,4 +114,15 @@ class UserRepository
return $query;
}
/**
* Escape special characters that can be used as wildcards in a LIKE query.
*
* @param string $string
* @return string
*/
private function escapeLikeString($string)
{
return str_replace(['\\', '%', '_'], ['\\\\', '\%', '\_'], $string);
}
}

View File

@ -11,15 +11,15 @@
namespace Flarum\Tests\Api\Controller;
use Flarum\Api\Controller\TokenController;
use Flarum\Api\Controller\CreateTokenController;
use Flarum\Http\AccessToken;
use Flarum\Tests\Test\Concerns\RetrievesAuthorizedUsers;
class TokenControllerTest extends ApiControllerTestCase
class CreateTokenControllerTest extends ApiControllerTestCase
{
use RetrievesAuthorizedUsers;
protected $controller = TokenController::class;
protected $controller = CreateTokenController::class;
/**
* @test