-
Notifications
You must be signed in to change notification settings - Fork 9
/
mroonga.php
184 lines (153 loc) · 5.51 KB
/
mroonga.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
/*
Plugin Name: Mroonga
Plugin URI: https://github.com/mroonga/wordpress-mroonga
Description: This plugin provides fast and rich full text search features based on Mroonga. Mroonga is a MySQL/MariaDB plugin. You don't need to add a new server only for full text search. You can use existing MySQL/MariaDB server. It reduces maintainance cost.
Version: 0.1.2
Author: Yasuhiro Horimoto
Author URI: https://www.clear-code.com/
License: GPL2 or later
*/
class MroongaSearch
{
private $textdomain = "mroonga";
private $mroonga_install_doc = "http://mroonga.org/docs/install.html";
public function table_name()
{
global $wpdb;
return $wpdb->prefix . "mrn_posts";
}
public function __construct()
{
load_plugin_textdomain( $this->textdomain, false, dirname( plugin_basename( __FILE__ ) ) . "/languages" );
}
public function activate()
{
$this->ensure_mroonga();
$this->create_table();
$this->copy_data();
$this->create_index();
}
public function deactivate()
{
$this->drop_table();
}
private function ensure_mroonga()
{
global $wpdb;
$is_mroonga_installed =
$wpdb->get_var( "SELECT COUNT(*) FROM INFORMATION_SCHEMA.PLUGINS "
. " WHERE PLUGIN_NAME = 'Mroonga'" );
if ( ! $is_mroonga_installed ) {
$message = __( "Mroonga is not installed on your DB Server.",
$this->textdomain );
$message .= " ";
$message .= sprintf( __( "Please install Mroonga refer to %s.",
$this->textdomain ),
$this->mroonga_install_doc );
exit( $messagee );
}
}
private function create_table()
{
global $wpdb;
$wpdb->query("CREATE TABLE {$this->table_name()} ( "
. "`post_id` bigint(20) unsigned PRIMARY KEY, "
. "`post_title` text COLLATE utf8mb4_unicode_ci, "
. "`post_content` longtext COLLATE utf8mb4_unicode_ci"
. ") ENGINE=Mroonga "
. "DEFAULT CHARSET=utf8mb4 "
. "COLLATE=utf8mb4_unicode_ci;");
}
private function copy_data()
{
global $wpdb;
if ( $post_types = $this->get_search_post_types() ) {
$post_types = esc_sql( $post_types );
$post_type_in_string = "'" . implode( "','", $post_types ) . "'";
$wpdb->query("INSERT INTO {$this->table_name()} "
. "(post_id, post_title, post_content) "
. "SELECT ID, post_title, post_content "
. "FROM {$wpdb->posts} "
. "WHERE post_status IN ('publish', 'private') "
. "AND post_type IN ($post_type_in_string)");
}
}
private function create_index()
{
global $wpdb;
$wpdb->query("ALTER TABLE {$this->table_name()} "
. "ADD FULLTEXT INDEX "
. "(post_title, post_content) "
. "COMMENT 'normalizer \"NormalizerAuto\"'");
}
private function drop_table()
{
global $wpdb;
$wpdb->query("DROP TABLE IF EXISTS {$this->table_name()}");
}
private function get_search_post_types ()
{
$post_types = get_post_types( array( "exclude_from_search" => false ) );
return apply_filters( "mroonga_search_post_types", $post_types );
}
public function update_post($post_id, $post)
{
global $wpdb;
if ( in_array( $post->post_status, array( "publish", "private" ) ) && in_array( $post->post_type, $this->get_search_post_types() ) ) {
$wpdb->replace($this->table_name(),
array(
"post_id" => $post_id,
"post_title" => $post->post_title,
"post_content" => $post->post_content
)
);
}
}
public function fulltext_search($search, $wp_query)
{
return "";
}
public function fulltext_search_join($join, $wp_query)
{
global $wpdb;
$search_query = $wp_query->get("s");
if (strlen($search_query) > 0)
{
$boolean_mode_query = "*D+W1:10,2:1 $search_query";
$join .= $wpdb->prepare(" INNER JOIN (SELECT post_id, "
. "MATCH (post_title, post_content) "
. "AGAINST (%s IN BOOLEAN MODE) AS score "
. "FROM {$this->table_name()} WHERE "
. "MATCH (post_title, post_content) "
. "AGAINST (%s IN BOOLEAN MODE)) AS matched_posts "
. "ON {$wpdb->posts}.ID = matched_posts.post_id",
$boolean_mode_query,
$boolean_mode_query);
}
return $join;
}
public function fulltext_search_orderby($orderby, $wp_query)
{
global $wpdb;
$search_query = $wp_query->get("s");
if (strlen($search_query) > 0)
{
$orderby = "score DESC";
}
return $orderby;
}
public function after_delete_post ( $post_id )
{
global $wpdb;
$wpdb->delete( $this->table_name(), array( "post_id" => $post_id ) );
}
}
$MroongaSearch = new MroongaSearch();
register_activation_hook(__FILE__, array($MroongaSearch, "activate"));
register_deactivation_hook(__FILE__, array($MroongaSearch, "deactivate"));
add_action("wp_insert_post", array($MroongaSearch, "update_post"), 10, 2);
add_filter("posts_search", array($MroongaSearch, "fulltext_search"), 10, 2);
add_filter("posts_join", array($MroongaSearch, "fulltext_search_join"), 10, 2);
add_filter("posts_search_orderby", array($MroongaSearch, "fulltext_search_orderby"), 10, 2);
add_action("after_delete_post", array($MroongaSearch, "after_delete_post"), 100, 1 );