问题
I want to change minimum character length for search from 4 to 1.
I found this documentation https://doc.osclass.org/Fine-Tuning_MySQL_Full-Text_Search_-_Improving_search about osclass.
The thing is that, from the databases I use on my host, only this one has this 4 character limit, the rest of them have 0 or not set.
So I need to modify this ft_min_word_len=4
to `ft_min_word_len=1' ONLY in the osclass database.
Can someone help with a sollution? I have access to cpanel and phpMyAdmin
回答1:
It turns out that, from my scripts, only osclass uses this ft_min_word_len=4 variable from the server.
So I can't change it because I have a shared server host, and the providers won't change it because of that.
回答2:
Turns out that there is another way to avoid changing this variable.
I write this answer here because I wouldn't have been able to figure it out by my own without the help of others that shared information on forums ( https://forums.osclass.org/development/i-am-not-able-to-apply-a-regex-item-title/ or https://forums.osclass.org/general-help/brilliant-3-letter-word-search-is-possible!!!-read-this-tip/).
So here goes 2 days work:
The ideea is to filter the title and description of items before adding it to the database. This filter must add '_' for every letter missing from a less than 4 letter word. For example: e___ ex__ exa_
For this to happen we need 2 functions: addunderline($t) and removeunderline($t)
The ideea is to turn all the words with less than 4 characters from title, description and search pattern, to min 4 character words using underscore characters.
So, in the database there will be words like "e___" etc. Then, when the information is displayed we use removeunderline function.
Please do a back-up for your files before starting!!!
Follow these steps
For Osclass 3.7.1 and Bender theme:
Stop and analize the code before copy-pasting it. I'm a human and error can happen, as I made the below instructions after many modifications....
1. /oc-content/themes/bender/item-post.php
Replace
<?php ItemForm::title_input('title',osc_current_user_locale(), osc_esc_html( bender_item_title() )); ?>
with
<?php ItemForm::title_input('title',osc_current_user_locale(), osc_esc_html( removeunderline(bender_item_title()) )); ?>
and
<?php ItemForm::description_textarea('description',osc_current_user_locale(), osc_esc_html( bender_item_description() )); ?>
with
<?php ItemForm::description_textarea('description',osc_current_user_locale(), osc_esc_html( removeunderline(bender_item_description()) )); ?>
- /oc-includes/osclass/gui/item-post.php
Replace
<?php ItemForm::title_input('title',osc_current_user_locale(), osc_esc_html( bender_item_title() )); ?>
with
<?php ItemForm::title_input('title',osc_current_user_locale(), osc_esc_html( removeunderline(bender_item_title()) )); ?>
and
<?php ItemForm::description_textarea('description',osc_current_user_locale(), osc_esc_html( bender_item_description() )); ?>
with
<?php ItemForm::description_textarea('description',osc_current_user_locale(), osc_esc_html( removeunderline(bender_item_description()) )); ?>
- NO NEED TO MAKE THESE CHANGES IF YOU MAKE THE MODIFICATIONS FROM POINT 13!
/oc-content/themes/bender/item.php
Replace
osc_item_title()
with
removeunderline(osc_item_title())
and
osc_item_description()
with
removeunderline(osc_item_description())
4. /oc-content/themes/bender/search.php
Replace
if(osc_count_items() == 0) {?>
<p class="empty" ><?php printf(__('There are no results matching "%s"', 'bender'), osc_search_pattern()) ; ?></p>
with
if(osc_count_items() == 0) {?>
<p class="empty" ><?php printf(__('There are no results matching "%s"', 'bender'), removeunderline(osc_search_pattern())); ?></p>
5. /oc-includes/osclass/helpers/hSearch.php
After:
/**
* Gets current search pattern
*
* @return string
*/
function osc_search_pattern() {
if(View::newInstance()->_exists('search_pattern')) {
return View::newInstance()->_get('search_pattern');
} else {
return '';
}
}
Add this:
/**
* transforms all words with under 4 characters to 4 characters ones
*
* @return string
*/
function addunderline($t)
{
if(count($t))
{
$words = explode(" ", $t);
for ($i=0; $i<count($words); $i++)
{ $ln=strlen($words[$i]);
if($ln==1) $words[$i]=$words[$i].='___';
if($ln==2) $words[$i]=$words[$i].='__';
if($ln==3) $words[$i]=$words[$i].='_';
}
return implode(" ", $words);
}
else { return $t;
}
}
/**
* Removes '_' from the end of the 4 characters words
*
* @return string
*/
function removeunderline($t)
{
if(count($t))
{
$words = explode(" ", $t);
for ($i=0; $i<count($words); $i++)
{
if(strlen($words[$i])==4) $words[$i]=chop($words[$i],"_");
}
return implode(" ", $words);
}
else { return $t;
}
}
6. /oc-content/themes/bender/search-sidebar.php
Replace
<input class="input-text" type="text" name="sPattern" id="query" value="<?php echo osc_esc_html(osc_search_pattern()); ?>" />
with
<input class="input-text" type="text" name="sPattern" id="query" value="<?php echo removeunderline(osc_esc_html(osc_search_pattern())); ?>" />
7. /oc-includes/osclass/controller/search.php
Replace
$p_sPattern = trim(strip_tags(Params::getParam('sPattern')));
with
$p_sPattern = addunderline(trim(strip_tags(Params::getParam('sPattern'))));
8. /oc-content/themes/bender/functions.php
Add to the end of the file (do not leave empty lines at the end of file)
<?php
function cust_filter_title_description($aItem) {
foreach(@$aItem['title'] as $key => $value) {
$aItem['title'][$key] = addunderline($value);
}
foreach(@$aItem['description'] as $key => $value) {
$aItem['description'][$key] = addunderline($value);
}
return $aItem;
}
osc_add_filter('item_add_prepare_data', 'cust_filter_title_description');
osc_add_filter('item_edit_prepare_data', 'cust_filter_title_description');
?>
9. /oc-includes/osclass/classes/Breadcrumb.php
Replace
$pattern = osc_search_pattern();
with
$pattern = removeunderline(osc_search_pattern());
NO NEED TO MAKE THESE CHANGES[the ones below with osc_item_title()] IF YOU MAKE THE MODIFICATIONS FROM POINT 13!
and all
osc_item_title()
with
removeunderline(osc_item_title())
10. /oc-content/themes/bender/common/head.php
Replace
<title><?php echo meta_title() ; ?></title>
with
<title><?php echo removeunderline(meta_title()) ; ?></title>
11.NO NEED TO MAKE THESE CHANGES IF YOU MAKE THE MODIFICATIONS FROM POINT 13!
/oc-content/themes/bender/loop-single.php
Replace all
osc_item_title()
with
removeunderline(osc_item_title())
and
osc_item_description()
with
removeunderline(osc_item_description())
12.NO NEED TO MAKE THESE CHANGES IF YOU MAKE THE MODIFICATIONS FROM POINT 14!
/oc-content/themes/bender/loop-single-premium.php
Replace
osc_premium_description()
with
removeunderline(osc_premium_description())
and all
osc_premium_title()
with
removeunderline(osc_premium_title())
13. /oc-includes/osclass/helpers/hItems.php
Replace
/**
* Gets title from current item, if $locale is unspecified $locale is current user locale
*
* @param string $locale
* @return string
*/
function osc_item_title($locale = "") {
if ($locale == "") $locale = osc_current_user_locale();
$title = osc_item_field("s_title", $locale);
if($title=='') {
$title = osc_item_field("s_title", osc_language());
if($title=='') {
$aLocales = osc_get_locales();
foreach($aLocales as $locale) {
$title = osc_item_field("s_title", @$locale['pk_c_code']);
if($title!='') {
break;
}
}
}
}
return (string) $title;
}
with
/**
* Gets title from current item, if $locale is unspecified $locale is current user locale
*
* @param string $locale
* @return string
*/
function osc_item_title($locale = "") {
if ($locale == "") $locale = osc_current_user_locale();
$title = osc_item_field("s_title", $locale);
if($title=='') {
$title = osc_item_field("s_title", osc_language());
if($title=='') {
$aLocales = osc_get_locales();
foreach($aLocales as $locale) {
$title = osc_item_field("s_title", @$locale['pk_c_code']);
if($title!='') {
break;
}
}
}
}
return (string) removeunderline($title);
}
and
/**
* Gets description from current item, if $locale is unspecified $locale is current user locale
*
* @param string $locale
* @return string $desc
*/
function osc_item_description($locale = "") {
if ($locale == "") $locale = osc_current_user_locale();
$desc = osc_item_field("s_description", $locale);
if($desc=='') {
$desc = osc_item_field("s_description", osc_language());
if($desc=='') {
$aLocales = osc_get_locales();
foreach($aLocales as $locale) {
$desc = osc_item_field("s_description", @$locale['pk_c_code']);
if($desc!='') {
break;
}
}
}
}
return (string) $desc;
}
with
/**
* Gets description from current item, if $locale is unspecified $locale is current user locale
*
* @param string $locale
* @return string $desc
*/
function osc_item_description($locale = "") {
if ($locale == "") $locale = osc_current_user_locale();
$desc = osc_item_field("s_description", $locale);
if($desc=='') {
$desc = osc_item_field("s_description", osc_language());
if($desc=='') {
$aLocales = osc_get_locales();
foreach($aLocales as $locale) {
$desc = osc_item_field("s_description", @$locale['pk_c_code']);
if($desc!='') {
break;
}
}
}
}
return (string) removeunderline($desc);
}
14. /oc-includes/osclass/helpers/hPremium.php
Replace
/**
* Gets title from current premium, if $locale is unspecified $locale is current user locale
*
* @param string $locale
* @return string
*/
function osc_premium_title($locale = "") {
if ($locale == "") $locale = osc_current_user_locale();
$title = osc_premium_field("s_title", $locale);
if($title=='') {
$title = osc_premium_field("s_title", osc_language());
if($title=='') {
$aLocales = osc_get_locales();
foreach($aLocales as $locale) {
$title = osc_premium_field("s_title", $locale);
if($title!='') {
break;
}
}
}
}
return (string) $title;
}
with
/**
* Gets title from current premium, if $locale is unspecified $locale is current user locale
*
* @param string $locale
* @return string
*/
function osc_premium_title($locale = "") {
if ($locale == "") $locale = osc_current_user_locale();
$title = osc_premium_field("s_title", $locale);
if($title=='') {
$title = osc_premium_field("s_title", osc_language());
if($title=='') {
$aLocales = osc_get_locales();
foreach($aLocales as $locale) {
$title = osc_premium_field("s_title", $locale);
if($title!='') {
break;
}
}
}
}
return (string) removeunderline($title);
}
and
/**
* Gets description from current premium, if $locale is unspecified $locale is current user locale
*
* @param string $locale
* @return string $desc
*/
function osc_premium_description($locale = "") {
if ($locale == "") $locale = osc_current_user_locale();
$desc = osc_premium_field("s_description", $locale);
if($desc=='') {
$desc = osc_premium_field("s_description", osc_language());
if($desc=='') {
$aLocales = osc_get_locales();
foreach($aLocales as $locale) {
$desc = osc_premium_field("s_description", $locale);
if($desc!='') {
break;
}
}
}
}
return (string) $desc;
}
with
/**
* Gets description from current premium, if $locale is unspecified $locale is current user locale
*
* @param string $locale
* @return string $desc
*/
function osc_premium_description($locale = "") {
if ($locale == "") $locale = osc_current_user_locale();
$desc = osc_premium_field("s_description", $locale);
if($desc=='') {
$desc = osc_premium_field("s_description", osc_language());
if($desc=='') {
$aLocales = osc_get_locales();
foreach($aLocales as $locale) {
$desc = osc_premium_field("s_description", $locale);
if($desc!='') {
break;
}
}
}
}
return (string) removeunderline($desc);
}
End.
Now you must edit and save al the listings from your website or wait for all the listings to be edited and saved by their autors.(or regenerate the fulltext search tables in DB - you can find details about that online).
Obs. This makes: - urls, item_titles and item_description remain with underscores in automatic generated emails , - urls remain with underscores in seo friendly urls - item_title and item_description remain with underscores in osclass admin area(only for admin, not for signed in users). -words like bmw x5, become bmw_ x5,_ in the database, so you need to make modifications to add and remove underline functions to solve punctuation problem, without using <> characters in regex, because osclass transforms them into <; and >; and if the item gets edited and saved those characters are beeing multiplied with each edit-save action. I resolved this with an obs. for users not to use <> characters.
EDIT.
The item link can be resolved like this:
/oc-includes/osclass/helpers/hDefines.php
put
$url = str_replace('{ITEM_TITLE}', osc_sanitizeString(removeunderline($item['s_title'])), $url);
instead of
$url = str_replace('{ITEM_TITLE}', osc_sanitizeString($item['s_title']), $url);
For {ITEM_TITLE} and {ITEM_DESCRIPTION}:
/oc-includes/osclass/emails.php
Put removeunderline() at {ITEM_TITLE} and {ITEM_DESCRIPTION} values.
Example:
$words = array();
$words[] = array(
'{ITEM_DESCRIPTION_ALL_LANGUAGES}',
'{ITEM_DESCRIPTION}',
'{ITEM_COUNTRY}',
'{ITEM_PRICE}',
'{ITEM_REGION}',
'{ITEM_CITY}',
'{ITEM_ID}',
'{USER_NAME}',
'{USER_EMAIL}',
'{ITEM_TITLE}',
'{ITEM_URL}',
'{ITEM_LINK}',
'{VALIDATION_LINK}',
'{VALIDATION_URL}',
'{EDIT_LINK}',
'{EDIT_URL}',
'{DELETE_LINK}',
'{DELETE_URL}'
);
$words[] = array(
$all,
removeunderline($item['s_description']), // here
$item['s_country'],
osc_format_price($item['i_price']),
$item['s_region'],
$item['s_city'],
$item['pk_i_id'],
$item['s_contact_name'],
$item['s_contact_email'],
removeunderline($item['s_title']), // here
$item_url,
$item_link,
'<a href="' . $validation_url . '" >' . $validation_url . '</a>',
$validation_url,
'<a href="' . $edit_url . '">' . $edit_url . '</a>',
$edit_url,
'<a href="' . $delete_url . '">' . $delete_url . '</a>',
$delete_url
);
Do the same for all {ITEM_TITLE} in this file (10 replacements).
Do the same for all {ITEM_DESCRIPTION} in this file (3 replacements).
来源:https://stackoverflow.com/questions/43015613/how-to-modify-ft-min-word-len-4-to-ft-min-word-len-1-so-that-osclass-3-7-1-can-s