database_connect.php
\file \brief Connects to the database and check its state.
PHP version 8.1
Tags
Table of Contents
Functions
- do_mysqli_query() : mysqli_result|true
- Do a SQL query to the database.
- runsql() : string
- Run a SQL query, you can specify its behavior and error message.
- get_first_value() : float|int|string|null
- Return the record "value" in the first line of the database if found.
- prepare_textdata() : string
- Replace Windows line return ("\r\n") by Linux ones ("\n").
- prepare_textdata_js() : string
- convert_string_to_sqlsyntax() : string
- Prepares a string to be properly recognized as a string by SQL.
- convert_string_to_sqlsyntax_nonull() : string
- Prepares a string to be properly recognized as a string by SQL.
- convert_string_to_sqlsyntax_notrim_nonull() : string
- Prepares a string to be properly recognized as a string by SQL.
- convert_regexp_to_sqlsyntax() : string
- validateLang() : string
- Validate a language ID
- validateText() : string
- Validate a text ID
- validateTag() : mixed
- validateArchTextTag() : mixed
- validateTextTag() : mixed
- getSettingZeroOrOne() : int
- Convert a setting to 0 or 1
- getSetting() : string
- Get a setting from the database. It can also check for its validity.
- getSettingWithDefault() : string
- Get the settings value for a specific key. Return a default value when possible
- saveSetting() : string
- Save the setting identified by a key with a specific value.
- LWTTableCheck() : void
- Check if the _lwtgeneral table exists, create it if not.
- LWTTableSet() : void
- LWTTableGet() : string
- adjust_autoincr() : void
- Adjust the auto-incrementation in the database.
- optimizedb() : void
- Optimize the database.
- update_japanese_word_count() : void
- Update the word count for Japanese language (using MeCab only).
- init_word_count() : void
- Initiate the number of words in terms for all languages.
- set_word_count() : void
- Initiate the number of words in terms for all languages
- parse_japanese_text() : null|array<string|int, string>
- Parse a Japanese text using MeCab and add it to the database.
- save_processed_text_with_sql() : void
- Insert a processed text in the data in pure SQL way.
- parse_standard_text() : null|array<string|int, string>
- Parse a text using the default tools. It is a not-japanese text.
- prepare_text_parsing() : null|array<string|int, string>
- Pre-parse the input text before a definitive parsing by a specialized parser.
- check_text_valid() : void
- Echo the sentences in a text. Prepare JS data for words and word count.
- registerSentencesTextItems() : void
- Append sentences and text items in the database.
- update_default_values() : void
- Append sentences and text items in the database.
- displayTextStatistics() : void
- Display statistics about a text.
- check_text() : void
- Check a text and display statistics about it.
- checkExpressions() : void
- Check a language that contains expressions.
- check_text_with_expressions() : string
- Check a language that contains expressions.
- splitCheckText() : null|array<string|int, string>
- Parse the input text.
- reparse_all_texts() : void
- Reparse all texts in order.
- update_database() : void
- Update the database if it is using an outdate version.
- prefixSQLQuery() : mixed
- Add a prefix to table in a SQL query string.
- check_update_db() : void
- Check and/or update the database.
- connect_to_database() : mysqli
- Make the connection to the database.
- getDatabasePrefix() : array<string|int, mixed>
- Get the prefixes for the database.
- get_database_prefixes() : 0|1
- Get the prefixes for the database.
Functions
do_mysqli_query()
Do a SQL query to the database.
do_mysqli_query(string $sql) : mysqli_result|true
It is a wrapper for mysqli_query function.
Parameters
- $sql : string
-
Query using SQL syntax
Tags
Return values
mysqli_result|truerunsql()
Run a SQL query, you can specify its behavior and error message.
runsql(string $sql, string $m[, bool $sqlerrdie = true ]) : string
Parameters
- $sql : string
-
MySQL query
- $m : string
-
Success phrase to prepend to the number of affected rows
- $sqlerrdie : bool = true
-
To die on errors (default = TRUE)
Return values
string —Error message if failure, or the number of affected rows
get_first_value()
Return the record "value" in the first line of the database if found.
get_first_value(string $sql) : float|int|string|null
Parameters
- $sql : string
-
MySQL query
Tags
Return values
float|int|string|null —Any returned value from the database.
prepare_textdata()
Replace Windows line return ("\r\n") by Linux ones ("\n").
prepare_textdata(string $s) : string
Parameters
- $s : string
-
Input string
Return values
string —Adapted string.
prepare_textdata_js()
prepare_textdata_js(mixed $s) : string
Parameters
- $s : mixed
Return values
stringconvert_string_to_sqlsyntax()
Prepares a string to be properly recognized as a string by SQL.
convert_string_to_sqlsyntax(string $data) : string
Parameters
- $data : string
-
Input string
Tags
Return values
string —Properly escaped and trimmed string. "NULL" if the input string is empty.
convert_string_to_sqlsyntax_nonull()
Prepares a string to be properly recognized as a string by SQL.
convert_string_to_sqlsyntax_nonull(string $data) : string
Parameters
- $data : string
-
Input string
Return values
string —Properly escaped and trimmed string
convert_string_to_sqlsyntax_notrim_nonull()
Prepares a string to be properly recognized as a string by SQL.
convert_string_to_sqlsyntax_notrim_nonull(string $data) : string
Parameters
- $data : string
-
Input string
Return values
string —Properly escaped string
convert_regexp_to_sqlsyntax()
convert_regexp_to_sqlsyntax(mixed $input) : string
Parameters
- $input : mixed
Return values
stringvalidateLang()
Validate a language ID
validateLang(string $currentlang) : string
Parameters
- $currentlang : string
-
Language ID to validate
Tags
Return values
string —'' if the language is not valid, $currentlang otherwise
validateText()
Validate a text ID
validateText(string $currenttext) : string
Parameters
- $currenttext : string
-
Text ID to validate
Tags
Return values
stringvalidateTag()
validateTag(mixed $currenttag, mixed $currentlang) : mixed
Parameters
- $currenttag : mixed
- $currentlang : mixed
validateArchTextTag()
validateArchTextTag(mixed $currenttag, mixed $currentlang) : mixed
Parameters
- $currenttag : mixed
- $currentlang : mixed
validateTextTag()
validateTextTag(mixed $currenttag, mixed $currentlang) : mixed
Parameters
- $currenttag : mixed
- $currentlang : mixed
getSettingZeroOrOne()
Convert a setting to 0 or 1
getSettingZeroOrOne(string $key, string|int $dft) : int
Parameters
- $key : string
-
The input value
- $dft : string|int
-
Default value to use, should be convertible to string
Tags
Return values
intgetSetting()
Get a setting from the database. It can also check for its validity.
getSetting(string $key) : string
Parameters
- $key : string
-
Setting key. If $key is 'currentlanguage' or 'currenttext', we validate language/text.
Tags
Return values
string —$val Value in the database if found, or an empty string
getSettingWithDefault()
Get the settings value for a specific key. Return a default value when possible
getSettingWithDefault(string $key) : string
Parameters
- $key : string
-
Settings key
Tags
Return values
string —Requested setting, or default value, or ''
saveSetting()
Save the setting identified by a key with a specific value.
saveSetting(string $k, mixed $v) : string
Parameters
- $k : string
-
Setting key
- $v : mixed
-
Setting value, will get converted to string
Tags
Return values
string —Success message (starts by "OK: "), or error message
LWTTableCheck()
Check if the _lwtgeneral table exists, create it if not.
LWTTableCheck() : void
LWTTableSet()
LWTTableSet(mixed $key, mixed $val) : void
Parameters
- $key : mixed
- $val : mixed
LWTTableGet()
LWTTableGet(mixed $key) : string
Parameters
- $key : mixed
Return values
stringadjust_autoincr()
Adjust the auto-incrementation in the database.
adjust_autoincr(mixed $table, mixed $key) : void
Parameters
- $table : mixed
- $key : mixed
Tags
optimizedb()
Optimize the database.
optimizedb() : void
Tags
update_japanese_word_count()
Update the word count for Japanese language (using MeCab only).
update_japanese_word_count(int $japid) : void
Parameters
- $japid : int
-
Japanese language ID
Tags
init_word_count()
Initiate the number of words in terms for all languages.
init_word_count() : void
Only terms with a word count set to 0 are changed.
Tags
set_word_count()
Initiate the number of words in terms for all languages
set_word_count() : void
Only terms with a word count set to 0 are changed.
Tags
parse_japanese_text()
Parse a Japanese text using MeCab and add it to the database.
parse_japanese_text(string $text, int $id) : null|array<string|int, string>
Parameters
- $text : string
-
Text to parse.
- $id : int
-
Text ID. If $id = -1 print results, if $id = -2 return splitted texts
Tags
Return values
null|array<string|int, string> —Splitted sentence if $id = -2
save_processed_text_with_sql()
Insert a processed text in the data in pure SQL way.
save_processed_text_with_sql(string $text, int $id) : void
Parameters
- $text : string
-
Preprocessed text to insert
- $id : int
-
Text ID
Tags
parse_standard_text()
Parse a text using the default tools. It is a not-japanese text.
parse_standard_text(string $text, int $id, int $lid) : null|array<string|int, string>
Parameters
- $text : string
-
Text to parse
- $id : int
-
Text ID. If $id == -2, only split the text.
- $lid : int
-
Language ID.
Tags
Return values
null|array<string|int, string> —If $id == -2 return a splitted version of the text.
prepare_text_parsing()
Pre-parse the input text before a definitive parsing by a specialized parser.
prepare_text_parsing(string $text, int $id, int $lid) : null|array<string|int, string>
Parameters
- $text : string
-
Text to parse
- $id : int
-
Text ID
- $lid : int
-
Language ID
Tags
Return values
null|array<string|int, string> —If $id = -2 return a splitted version of the text
check_text_valid()
Echo the sentences in a text. Prepare JS data for words and word count.
check_text_valid(int $lid) : void
Parameters
- $lid : int
-
Language ID
Tags
registerSentencesTextItems()
Append sentences and text items in the database.
registerSentencesTextItems(int $tid, int $lid, bool $hasmultiword) : void
Parameters
- $tid : int
-
ID of text from which insert data
- $lid : int
-
ID of the language of the text
- $hasmultiword : bool
-
Set to true to insert multi-words as well.
Tags
update_default_values()
Append sentences and text items in the database.
update_default_values(int $id, int $lid, string $_sql) : void
Parameters
- $id : int
-
New default text ID
- $lid : int
-
New default language ID
- $_sql : string
-
Unnused since 2.10.0. Will be removed in 3.0.0.
Tags
displayTextStatistics()
Display statistics about a text.
displayTextStatistics(int $lid, bool $rtlScript, bool $multiwords) : void
Parameters
- $lid : int
-
Language ID
- $rtlScript : bool
-
true if language is right-to-left
- $multiwords : bool
-
Display if text has multi-words
Tags
check_text()
Check a text and display statistics about it.
check_text(string $sql, bool $rtlScript, array<string|int, int> $wl) : void
Parameters
- $sql : string
- $rtlScript : bool
-
true if language is right-to-left
- $wl : array<string|int, int>
-
Words lengths
Tags
checkExpressions()
Check a language that contains expressions.
checkExpressions(array<string|int, int> $wl) : void
Parameters
- $wl : array<string|int, int>
-
All the different expression length in the language.
Tags
check_text_with_expressions()
Check a language that contains expressions.
check_text_with_expressions(int $id, int $lid, array<string|int, int> $wl, int $wl_max, string $mw_sql) : string
Parameters
- $id : int
-
Text ID
- $lid : int
-
Language ID
- $wl : array<string|int, int>
-
Word length
- $wl_max : int
-
Maximum word length
- $mw_sql : string
-
SQL-formatted string
Tags
Return values
string —SQL-formatted query string
splitCheckText()
Parse the input text.
splitCheckText(string $text, string|int $lid, int $id) : null|array<string|int, string>
Parameters
- $text : string
-
Text to parse
- $lid : string|int
-
Language ID (LgID from languages table)
- $id : int
-
References whether the text is new to the database $id = -1 => Check, return protocol $id = -2 => Only return sentence array $id = TextID => Split: insert sentences/textitems entries in DB
Tags
Return values
null|array<string|int, string> —The sentence array if $id = -2
reparse_all_texts()
Reparse all texts in order.
reparse_all_texts() : void
Tags
update_database()
Update the database if it is using an outdate version.
update_database(string $dbname) : void
Parameters
- $dbname : string
-
Name of the database
Tags
prefixSQLQuery()
Add a prefix to table in a SQL query string.
prefixSQLQuery(string $sql_line, string $prefix) : mixed
Parameters
- $sql_line : string
-
SQL string to prefix.
- $prefix : string
-
Prefix to add
check_update_db()
Check and/or update the database.
check_update_db(mixed $debug, mixed $tbpref, mixed $dbname) : void
Parameters
- $debug : mixed
- $tbpref : mixed
- $dbname : mixed
Tags
connect_to_database()
Make the connection to the database.
connect_to_database(string $server, string $userid, string $passwd, string $dbname[, string $socket = "" ]) : mysqli
Parameters
- $server : string
-
Server name
- $userid : string
-
Database user ID
- $passwd : string
-
User password
- $dbname : string
-
Database name
- $socket : string = ""
-
Database socket
Tags
Return values
mysqli —Connection to the database
getDatabasePrefix()
Get the prefixes for the database.
getDatabasePrefix(mixed $dbconnection) : array<string|int, mixed>
Is $tbpref set in connect.inc.php? Take it and $fixed_tbpref=1. If not: $fixed_tbpref=0. Is it set in table "_lwtgeneral"? Take it. If not: Use $tbpref = '' (no prefix, old/standard behaviour).
Parameters
- $dbconnection : mixed
Return values
array<string|int, mixed> —Table prefix, and if table prefix should be fixed
get_database_prefixes()
Get the prefixes for the database.
get_database_prefixes(string|null &$tbpref) : 0|1
Is $tbpref set in connect.inc.php? Take it and $fixed_tbpref=1. If not: $fixed_tbpref=0. Is it set in table "_lwtgeneral"? Take it. If not: Use $tbpref = '' (no prefix, old/standard behaviour).
Parameters
- $tbpref : string|null
-
Temporary database table prefix
Tags
Return values
0|1 —Table Prefix is fixed, no changes possible.