diff --git a/framework/core/migrations/2015_02_24_000000_create_posts_table.php b/framework/core/migrations/2015_02_24_000000_create_posts_table.php index d5ab988e2..b172f342e 100644 --- a/framework/core/migrations/2015_02_24_000000_create_posts_table.php +++ b/framework/core/migrations/2015_02_24_000000_create_posts_table.php @@ -2,6 +2,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; +use DB; class CreatePostsTable extends Migration { @@ -14,7 +15,6 @@ class CreatePostsTable extends Migration public function up() { Schema::create('posts', function (Blueprint $table) { - $table->increments('id'); $table->integer('discussion_id')->unsigned(); $table->integer('number')->unsigned()->nullable(); @@ -29,10 +29,11 @@ class CreatePostsTable extends Migration $table->integer('edit_user_id')->unsigned()->nullable(); $table->dateTime('hide_time')->nullable(); $table->integer('hide_user_id')->unsigned()->nullable(); + + $table->unique(['discussion_id', 'number']); }); - // add fulltext index to content (and title?) - // add unique index on [discussion_id, number] !!! + DB::statement('ALTER TABLE posts ADD FULLTEXT content (content)'); } /** diff --git a/framework/core/src/Core/CoreServiceProvider.php b/framework/core/src/Core/CoreServiceProvider.php index 55833f4f1..5556a18f7 100644 --- a/framework/core/src/Core/CoreServiceProvider.php +++ b/framework/core/src/Core/CoreServiceProvider.php @@ -89,6 +89,11 @@ class CoreServiceProvider extends ServiceProvider 'Flarum\Core\Repositories\EloquentActivityRepository' ); + $this->app->bind( + 'Flarum\Core\Search\Discussions\Fulltext\DriverInterface', + 'Flarum\Core\Search\Discussions\Fulltext\MySqlFulltextDriver' + ); + $avatarFilesystem = function (Container $app) { return $app->make('Illuminate\Contracts\Filesystem\Factory')->disk('flarum-avatars')->getDriver(); }; diff --git a/framework/core/src/Core/Repositories/EloquentPostRepository.php b/framework/core/src/Core/Repositories/EloquentPostRepository.php index 886e0a415..b08d12ffb 100644 --- a/framework/core/src/Core/Repositories/EloquentPostRepository.php +++ b/framework/core/src/Core/Repositories/EloquentPostRepository.php @@ -3,9 +3,17 @@ use Illuminate\Database\Eloquent\Builder; use Flarum\Core\Models\Post; use Flarum\Core\Models\User; +use Flarum\Core\Search\Discussions\Fulltext\DriverInterface; class EloquentPostRepository implements PostRepositoryInterface { + protected $fulltext; + + public function __construct(DriverInterface $fulltext) + { + $this->fulltext = $fulltext; + } + /** * Find a post by ID, optionally making sure it is visible to a certain * user, or throw an exception. @@ -72,10 +80,13 @@ class EloquentPostRepository implements PostRepositoryInterface */ public function findByContent($string, User $user = null) { - $query = Post::select('id', 'discussion_id') - ->where('content', 'like', '%'.$string.'%'); - // ->whereRaw('MATCH (`content`) AGAINST (? IN BOOLEAN MODE)', [$string]) - // ->orderByRaw('MATCH (`content`) AGAINST (?) DESC', [$string]) + $ids = $this->fulltext->match($string); + + $query = Post::select('id', 'discussion_id')->whereIn('id', $ids); + + foreach ($ids as $id) { + $query->orderByRaw('id != ?', [$id]); + } return $this->scopeVisibleForUser($query, $user)->get(); } diff --git a/framework/core/src/Core/Search/Discussions/Fulltext/DriverInterface.php b/framework/core/src/Core/Search/Discussions/Fulltext/DriverInterface.php new file mode 100644 index 000000000..a80c6f07f --- /dev/null +++ b/framework/core/src/Core/Search/Discussions/Fulltext/DriverInterface.php @@ -0,0 +1,6 @@ +orderByRaw('MATCH (`content`) AGAINST (?) DESC', [$string]) + ->lists('id'); + } +}