summaryrefslogtreecommitdiff
path: root/plugins/SphinxSearch/scripts
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/SphinxSearch/scripts')
-rwxr-xr-xplugins/SphinxSearch/scripts/gen_config.php126
-rwxr-xr-xplugins/SphinxSearch/scripts/index_update.php61
-rw-r--r--plugins/SphinxSearch/scripts/sphinx-utils.php63
-rwxr-xr-xplugins/SphinxSearch/scripts/sphinx.sh15
4 files changed, 265 insertions, 0 deletions
diff --git a/plugins/SphinxSearch/scripts/gen_config.php b/plugins/SphinxSearch/scripts/gen_config.php
new file mode 100755
index 000000000..d5a00b6b6
--- /dev/null
+++ b/plugins/SphinxSearch/scripts/gen_config.php
@@ -0,0 +1,126 @@
+#!/usr/bin/env php
+<?php
+/*
+ * StatusNet - the distributed open-source microblogging tool
+ * Copyright (C) 2009, StatusNet, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+define('INSTALLDIR', realpath(dirname(__FILE__) . '/../../..'));
+
+$longoptions = array('base=', 'network');
+
+$helptext = <<<END_OF_TRIM_HELP
+Generates sphinx.conf file based on StatusNet configuration.
+ --base Base dir to Sphinx install
+ (default /usr/local)
+ --network Use status_network global config table
+ (non-functional at present)
+
+
+END_OF_TRIM_HELP;
+
+require_once INSTALLDIR . '/scripts/commandline.inc';
+require dirname(__FILE__) . '/sphinx-utils.php';
+
+
+$timestamp = date('r');
+print <<<END
+#
+# Sphinx configuration for StatusNet
+# Generated {$timestamp}
+#
+
+END;
+
+sphinx_iterate_sites('sphinx_site_template');
+
+print <<<END
+
+indexer
+{
+ mem_limit = 300M
+}
+
+searchd
+{
+ port = 3312
+ log = {$base}/log/searchd.log
+ query_log = {$base}/log/query.log
+ read_timeout = 5
+ max_children = 30
+ pid_file = {$base}/log/searchd.pid
+ max_matches = 1000
+ seamless_rotate = 1
+ preopen_indexes = 0
+ unlink_old = 1
+}
+
+END;
+
+
+
+/**
+ * Build config entries for a single site
+ * @fixme we only seem to have master DB currently available...
+ */
+function sphinx_site_template($sn)
+{
+ return
+ sphinx_template($sn,
+ 'profile',
+ 'SELECT id, UNIX_TIMESTAMP(created) as created_ts, nickname, fullname, location, bio, homepage FROM profile',
+ 'SELECT * FROM profile where id = $id') .
+ sphinx_template($sn,
+ 'notice',
+ 'SELECT id, UNIX_TIMESTAMP(created) as created_ts, content FROM notice',
+ 'SELECT * FROM notice where notice.id = $id AND notice.is_local != -2');
+}
+
+function sphinx_template($sn, $table, $query, $query_info)
+{
+ $base = sphinx_base();
+ $dbtype = common_config('db', 'type');
+
+ print <<<END
+
+#
+# {$sn->sitename}
+#
+source {$sn->dbname}_src_{$table}
+{
+ type = {$dbtype}
+ sql_host = {$sn->dbhost}
+ sql_user = {$sn->dbuser}
+ sql_pass = {$sn->dbpass}
+ sql_db = {$sn->dbname}
+ sql_query_pre = SET NAMES utf8;
+ sql_query = {$query}
+ sql_query_info = {$query_info}
+ sql_attr_timestamp = created_ts
+}
+
+index {$sn->dbname}_{$table}
+{
+ source = {$sn->dbname}_src_{$table}
+ path = {$base}/data/{$sn->dbname}_{$table}
+ docinfo = extern
+ charset_type = utf-8
+ min_word_len = 3
+}
+
+
+END;
+}
diff --git a/plugins/SphinxSearch/scripts/index_update.php b/plugins/SphinxSearch/scripts/index_update.php
new file mode 100755
index 000000000..23c60ced7
--- /dev/null
+++ b/plugins/SphinxSearch/scripts/index_update.php
@@ -0,0 +1,61 @@
+#!/usr/bin/env php
+<?php
+/*
+ * StatusNet - the distributed open-source microblogging tool
+ * Copyright (C) 2009, StatusNet, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+define('INSTALLDIR', realpath(dirname(__FILE__) . '/../../..'));
+
+$longoptions = array('base=', 'network');
+
+$helptext = <<<END_OF_TRIM_HELP
+Runs Sphinx search indexer.
+ --rotate Have Sphinx run index update in background and
+ rotate updated indexes into place as they finish.
+ --base Base dir to Sphinx install
+ (default /usr/local)
+ --network Use status_network global config table for site list
+ (non-functional at present)
+
+
+END_OF_TRIM_HELP;
+
+require_once INSTALLDIR . '/scripts/commandline.inc';
+require dirname(__FILE__) . '/sphinx-utils.php';
+
+sphinx_iterate_sites('sphinx_index_update');
+
+function sphinx_index_update($sn)
+{
+ $base = sphinx_base();
+
+ $baseIndexes = array('notice', 'profile');
+ $params = array();
+
+ if (have_option('rotate')) {
+ $params[] = '--rotate';
+ }
+ foreach ($baseIndexes as $index) {
+ $params[] = "{$sn->dbname}_{$index}";
+ }
+
+ $params = implode(' ', $params);
+ $cmd = "$base/bin/indexer --config $base/etc/sphinx.conf $params";
+
+ print "$cmd\n";
+ system($cmd);
+}
diff --git a/plugins/SphinxSearch/scripts/sphinx-utils.php b/plugins/SphinxSearch/scripts/sphinx-utils.php
new file mode 100644
index 000000000..7bbc25270
--- /dev/null
+++ b/plugins/SphinxSearch/scripts/sphinx-utils.php
@@ -0,0 +1,63 @@
+<?php
+/*
+ * StatusNet - the distributed open-source microblogging tool
+ * Copyright (C) 2009, StatusNet, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+function sphinx_use_network()
+{
+ return have_option('network');
+}
+
+function sphinx_base()
+{
+ if (have_option('base')) {
+ return get_option_value('base');
+ } else {
+ return "/usr/local/sphinx";
+ }
+}
+
+function sphinx_iterate_sites($callback)
+{
+ if (sphinx_use_network()) {
+ // @fixme this should use, like, some kind of config
+ Status_network::setupDB('localhost', 'statusnet', 'statuspass', 'statusnet');
+ $sn = new Status_network();
+ if (!$sn->find()) {
+ die("Confused... no sites in status_network table or lookup failed.\n");
+ }
+ while ($sn->fetch()) {
+ $callback($sn);
+ }
+ } else {
+ if (preg_match('!^(mysqli?|pgsql)://(.*?):(.*?)@(.*?)/(.*?)$!',
+ common_config('db', 'database'), $matches)) {
+ list(/*all*/, $dbtype, $dbuser, $dbpass, $dbhost, $dbname) = $matches;
+ $sn = (object)array(
+ 'sitename' => common_config('site', 'name'),
+ 'dbhost' => $dbhost,
+ 'dbuser' => $dbuser,
+ 'dbpass' => $dbpass,
+ 'dbname' => $dbname);
+ $callback($sn);
+ } else {
+ print "Unrecognized database configuration string in config.php\n";
+ exit(1);
+ }
+ }
+}
+
diff --git a/plugins/SphinxSearch/scripts/sphinx.sh b/plugins/SphinxSearch/scripts/sphinx.sh
new file mode 100755
index 000000000..b8edeb302
--- /dev/null
+++ b/plugins/SphinxSearch/scripts/sphinx.sh
@@ -0,0 +1,15 @@
+#!/bin/bash
+
+if [[ $1 = "start" ]]
+then
+ echo "Stopping any running daemons..."
+ /usr/local/bin/searchd --config /usr/local/etc/sphinx.conf --stop 2> /dev/null
+ echo "Starting sphinx search daemon..."
+ /usr/local/bin/searchd --config /usr/local/etc/sphinx.conf 2> /dev/null
+fi
+
+if [[ $1 = "stop" ]]
+then
+ echo "Stopping sphinx search daemon..."
+ /usr/local/bin/searchd --config /usr/local/etc/sphinx.conf --stop 2> /dev/null
+fi