Learning with Texts - Fork  2.10.0-fork
Learn foreign languages with texts
Namespaces | Functions | Variables
database_connect.php File Reference

Connects to the database and check its state. More...

Namespaces

 Lwt
 

Functions

 do_mysqli_query ($sql)
 
 runsql ($sql, $m, $sqlerrdie=true)
 
 get_first_value ($sql)
 
 prepare_textdata ($s)
 
 prepare_textdata_js ($s)
 
 convert_string_to_sqlsyntax ($data)
 
 convert_string_to_sqlsyntax_nonull ($data)
 
 convert_string_to_sqlsyntax_notrim_nonull ($data)
 
 convert_regexp_to_sqlsyntax ($input)
 
 validateLang ($currentlang)
 
 validateText ($currenttext)
 
 validateTag ($currenttag, $currentlang)
 
 validateArchTextTag ($currenttag, $currentlang)
 
 validateTextTag ($currenttag, $currentlang)
 
 getSettingZeroOrOne ($key, $dft)
 
 getSetting ($key)
 
 getSettingWithDefault ($key)
 
 saveSetting ($k, $v)
 
 LWTTableCheck ()
 
 LWTTableSet ($key, $val)
 
 LWTTableGet ($key)
 
 adjust_autoincr ($table, $key)
 
 optimizedb ()
 
 update_japanese_word_count ($japid)
 
 set_word_count ()
 
 parse_japanese_text ($text, $id)
 
 save_processed_text_with_sql ($text, $id)
 
 parse_standard_text ($text, $id, $lid)
 
 prepare_text_parsing ($text, $id, $lid)
 
 check_text_valid ($lid)
 
 registerSentencesTextItems ($tid, $lid, $hasmultiword)
 
 update_default_values ($id, $lid, $_sql)
 
 displayTextStatistics ($lid, $rtlScript, $multiwords)
 
 check_text ($sql, $rtlScript, $wl)
 
 checkExpressions ($wl)
 
 check_text_with_expressions ($id, $lid, $wl, $wl_max, $mw_sql)
 
 splitCheckText ($text, $lid, $id)
 
 reparse_all_texts ()
 
 update_database ($dbname)
 
 prefixSQLQuery ($sql_line, $prefix)
 
 check_update_db ($debug, $tbpref, $dbname)
 
 connect_to_database ($server, $userid, $passwd, $dbname, $socket="")
 
 getDatabasePrefix ($dbconnection)
 
 get_database_prefixes (&$tbpref)
 

Variables

 $DBCONNECTION
 
 $tbpref = null
 
 $fixed_tbpref = null
 

Detailed Description

Connects to the database and check its state.

PHP version 8.1

Function Documentation

◆ adjust_autoincr()

adjust_autoincr (   $table,
  $key 
)

Adjust the auto-incrementation in the database.

@global string $tbpref Database table prefix

◆ check_text()

check_text (   $sql,
  $rtlScript,
  $wl 
)

Check a text and display statistics about it.

Parameters
string$sql
bool$rtlScripttrue if language is right-to-left
int[]$wlWords lengths
Returns
void
Deprecated:
Use displayTextStatistics instead. Will be removed in 3.0.0.

◆ check_text_valid()

check_text_valid (   $lid)

Echo the sentences in a text. Prepare JS data for words and word count.

Parameters
int$lidLanguage ID

@global string $tbpref Database table prefix

Returns
void

◆ check_text_with_expressions()

check_text_with_expressions (   $id,
  $lid,
  $wl,
  $wl_max,
  $mw_sql 
)

Check a language that contains expressions.

Parameters
int$idText ID
int$lidLanguage ID
int[]$wlWord length
int$wl_maxMaximum word length
string$mw_sqlSQL-formatted string
Returns
string SQL-formatted query string

@global string $tbpref Database table prefix

Deprecated:
Since 2.10.0-fork use checkExpressions. It does not modify SQL globals.

◆ check_update_db()

check_update_db (   $debug,
  $tbpref,
  $dbname 
)

Check and/or update the database.

@global mysqli $DBCONNECTION Connection to the database.

Since
2.10.0 Use confiduration files instead of containing all the data.

counter for cache rebuild

◆ checkExpressions()

checkExpressions (   $wl)

Check a language that contains expressions.

Parameters
int[]$wlAll the different expression length in the language.

@global string $tbpref Database table prefix

◆ connect_to_database()

connect_to_database (   $server,
  $userid,
  $passwd,
  $dbname,
  $socket = "" 
)

Make the connection to the database.

Parameters
string$serverServer name
string$useridDatabase user ID
string$passwdUser password
string$dbnameDatabase name
string$socketDatabase socket
Returns
mysqli Connection to the database

@psalm-suppress UndefinedDocblockClass

Since
2.6.0-fork Use mysqli_init and mysql_real_connect instead of deprecated mysql_connect
2.6.0-fork Tries to allow local infiles for the connection.
2.9.0 Can accept a $socket as an optional argument

◆ convert_string_to_sqlsyntax()

convert_string_to_sqlsyntax (   $data)

Prepares a string to be properly recognized as a string by SQL.

Parameters
string$dataInput string
Returns
string Properly escaped and trimmed string. "NULL" if the input string is empty.

@global $DBDONNECTION

◆ convert_string_to_sqlsyntax_nonull()

convert_string_to_sqlsyntax_nonull (   $data)

Prepares a string to be properly recognized as a string by SQL.

Parameters
string$dataInput string
Returns
string Properly escaped and trimmed string

◆ convert_string_to_sqlsyntax_notrim_nonull()

convert_string_to_sqlsyntax_notrim_nonull (   $data)

Prepares a string to be properly recognized as a string by SQL.

Parameters
string$dataInput string
Returns
string Properly escaped string

◆ displayTextStatistics()

displayTextStatistics (   $lid,
  $rtlScript,
  $multiwords 
)

Display statistics about a text.

Parameters
int$lidLanguage ID
bool$rtlScripttrue if language is right-to-left
bool$multiwordsDisplay if text has multi-words
Returns
void

@global $tbpref

◆ do_mysqli_query()

do_mysqli_query (   $sql)

Do a SQL query to the database. It is a wrapper for mysqli_query function.

Parameters
string$sqlQuery using SQL syntax

@global mysqli $DBCONNECTION Connection to the database

Returns
mysqli_result|true

◆ get_database_prefixes()

get_database_prefixes ( $tbpref)

Get the prefixes for the database.

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
string | null$tbprefTemporary database table prefix
Returns
0|1 Table Prefix is fixed, no changes possible.
Deprecated:
Since 2.10.0-fork, use getDatabasePrefix instead

◆ get_first_value()

get_first_value (   $sql)

Return the record "value" in the first line of the database if found.

Parameters
string$sqlMySQL query
Returns
float|int|string|null Any returned value from the database.
Since
2.6.0-fork Officially return numeric types.

◆ getDatabasePrefix()

getDatabasePrefix (   $dbconnection)

Get the prefixes for the database.

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
string | null$tbprefTemporary database table prefix
Returns
array Table prefix, and if table prefix should be fixed

◆ getSetting()

getSetting (   $key)

Get a setting from the database. It can also check for its validity.

Parameters
string$keySetting key. If $key is 'currentlanguage' or 'currenttext', we validate language/text.
Returns
string $val Value in the database if found, or an empty string @global string $tbpref Table name prefix

◆ getSettingWithDefault()

getSettingWithDefault (   $key)

Get the settings value for a specific key. Return a default value when possible

Parameters
string$keySettings key
Returns
string Requested setting, or default value, or ''

@global string $tbpref Table name prefix

◆ getSettingZeroOrOne()

getSettingZeroOrOne (   $key,
  $dft 
)

Convert a setting to 0 or 1

Parameters
string$keyThe input value
string | int$dftDefault value to use, should be convertible to string
Returns
int

@psalm-return 0|1

◆ LWTTableCheck()

LWTTableCheck ( )

Check if the _lwtgeneral table exists, create it if not.

◆ optimizedb()

optimizedb ( )

Optimize the database.

@global string $trbpref Table prefix

◆ parse_japanese_text()

parse_japanese_text (   $text,
  $id 
)

Parse a Japanese text using MeCab and add it to the database.

Parameters
string$textText to parse.
int$idText ID. If $id = -1 print results, if $id = -2 return splitted texts
Returns
null|string[] Splitted sentence if $id = -2
Since
2.5.1-fork Works even if LOAD DATA LOCAL INFILE operator is disabled.
2.6.0-fork Use PHP instead of SQL, slower but works better.

@global string $tbpref Database table prefix

@psalm-return non-empty-list<string>|null

◆ parse_standard_text()

parse_standard_text (   $text,
  $id,
  $lid 
)

Parse a text using the default tools. It is a not-japanese text.

Parameters
string$textText to parse
int$idText ID. If $id == -2, only split the text.
int$lidLanguage ID.
Returns
null|string[] If $id == -2 return a splitted version of the text.
Since
2.5.1-fork Works even if LOAD DATA LOCAL INFILE operator is disabled.

@global string $tbpref Database table prefix

@psalm-return non-empty-list<string>|null

◆ prefixSQLQuery()

prefixSQLQuery (   $sql_line,
  $prefix 
)

Add a prefix to table in a SQL query string.

Parameters
string$sql_lineSQL string to prefix.
string$prefixPrefix to add

◆ prepare_text_parsing()

prepare_text_parsing (   $text,
  $id,
  $lid 
)

Pre-parse the input text before a definitive parsing by a specialized parser.

Parameters
string$textText to parse
int$idText ID
int$lidLanguage ID
Returns
null|string[] If $id = -2 return a splitted version of the text

@global string $tbpref Database table prefix

@psalm-return non-empty-list<string>|null

◆ prepare_textdata()

prepare_textdata (   $s)

Replace Windows line return ("\r\n") by Linux ones ("\n").

Parameters
string$sInput string
Returns
string Adapted string.

◆ registerSentencesTextItems()

registerSentencesTextItems (   $tid,
  $lid,
  $hasmultiword 
)

Append sentences and text items in the database.

Parameters
int$tidID of text from which insert data
int$lidID of the language of the text
bool$hasmultiwordSet to true to insert multi-words as well.
Returns
void

@global string $tbpref Database table prefix

◆ reparse_all_texts()

reparse_all_texts ( )

Reparse all texts in order.

@global string $tbpref Database table prefix

◆ runsql()

runsql (   $sql,
  $m,
  $sqlerrdie = true 
)

Run a SQL query, you can specify its behavior and error message.

Parameters
string$sqlMySQL query
string$mSuccess phrase to prepend to the number of affected rows
bool$sqlerrdieTo die on errors (default = TRUE)
Returns
string Error message if failure, or the number of affected rows

◆ save_processed_text_with_sql()

save_processed_text_with_sql (   $text,
  $id 
)

Insert a processed text in the data in pure SQL way.

Parameters
string$textPreprocessed text to insert
int$idText ID
Returns
void

@global string $tbpref Database table prefix

◆ saveSetting()

saveSetting (   $k,
  $v 
)

Save the setting identified by a key with a specific value.

Parameters
string$kSetting key
mixed$vSetting value, will get converted to string

@global string $tbpref Table name prefix

Returns
string Success message (starts by "OK: "), or error message
Since
2.9.0 Success message starts by "OK: "

◆ set_word_count()

set_word_count ( )

Initiate the number of words in terms for all languages

Only terms with a word count set to 0 are changed.

Returns
void

@global string $tbpref Database table prefix

Deprecated:
Use init_word_count: same effect, but more logical name. Will be removed in version 3.0.0.

◆ splitCheckText()

splitCheckText (   $text,
  $lid,
  $id 
)

Parse the input text.

Parameters
string$textText to parse
string | int$lidLanguage ID (LgID from languages table)
int$idReferences 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

@global string $tbpref Database table prefix

Returns
null|string[] The sentence array if $id = -2

@psalm-return non-empty-list<string>|null

◆ update_database()

update_database (   $dbname)

Update the database if it is using an outdate version.

Parameters
string$dbnameName of the database

@global string $tbpref Database table prefix @global 0|1 $debug Output debug messages.

Returns
void
Since
2.10.0-fork Migrations are defined thourgh SQL, and not directly here

◆ update_default_values()

update_default_values (   $id,
  $lid,
  $_sql 
)

Append sentences and text items in the database.

Parameters
int$idNew default text ID
int$lidNew default language ID
string$_sqlUnnused since 2.10.0. Will be removed in 3.0.0.
Returns
void

@global string $tbpref Database table prefix

Deprecated:
Since 2.10.0, use registerSentencesTextItems instead

◆ update_japanese_word_count()

update_japanese_word_count (   $japid)

Update the word count for Japanese language (using MeCab only).

Parameters
int$japidJapanese language ID
Returns
void

@global string $tbpref Database table prefix.

◆ validateLang()

validateLang (   $currentlang)

Validate a language ID

Parameters
string$currentlangLanguage ID to validate
Returns
string '' if the language is not valid, $currentlang otherwise

@global string $tbpref Table name prefix

◆ validateText()

validateText (   $currenttext)

Validate a text ID

Parameters
string$currenttextText ID to validate

@global string '' if the text is not valid, $currenttext otherwise

@global string $tbpref Table name prefix

Variable Documentation

◆ $DBCONNECTION

$DBCONNECTION
Initial value:
$server, $userid, $passwd, $dbname, $socket ?? ""
)
connect_to_database($server, $userid, $passwd, $dbname, $socket="")
Definition: database_connect.php:1997