LocalDictionaryService
in package
Service class for managing local dictionaries.
Handles CRUD operations for local dictionaries and entries, as well as term lookups.
Tags
Table of Contents
Constants
- BATCH_SIZE : mixed = 1000
- Batch size for bulk inserts.
- MAX_POS_LENGTH : mixed = 50
- Character capacity of `LePartOfSpeech` (VARCHAR(50)).
- MAX_READING_LENGTH : mixed = 250
- Character capacity of `LeReading` (VARCHAR(250)).
- MAX_TERM_LENGTH : mixed = 250
- Character capacity of `LeTerm` / `LeTermLc` (both VARCHAR(250)).
Methods
- addEntriesBatch() : array{added: int, skipped: int}
- Add multiple entries to a dictionary in batches.
- addEntry() : int
- Add a single entry to a dictionary.
- autoEnableLocalDictMode() : void
- Auto-enable local dictionary mode if currently set to online-only.
- clearEntries() : int
- Delete all entries from a dictionary.
- create() : int
- Create a new local dictionary.
- createVocabularyFromEntries() : int
- Create vocabulary terms (status 1) from dictionary entries.
- delete() : bool
- Delete a dictionary and all its entries.
- deleteEntry() : bool
- Delete a single entry.
- getAllForLanguage() : array<string|int, LocalDictionary>
- Get all dictionaries for a language (including disabled).
- getById() : LocalDictionary|null
- Get a dictionary by ID.
- getEntries() : array{entries: array, total: int, page: int, perPage: int}
- Get entries for a dictionary (paginated).
- getEntryCount() : int
- Get entry count for a dictionary.
- getForLanguage() : array<string|int, LocalDictionary>
- Get all dictionaries for a language.
- getLocalDictMode() : int
- Get the local dictionary mode for a language.
- hasLocalDictionaries() : bool
- Check if a language has any local dictionaries.
- lookup() : array<string|int, array{term: string, definition: string, reading: ?string, pos: ?string, dictionary: string}>
- Look up a term in local dictionaries for a language.
- lookupPrefix() : array<string|int, array{term: string, definition: string}>
- Look up a term with prefix matching (for autocomplete).
- update() : bool
- Update a dictionary.
- updateEntry() : bool
- Update a single entry.
- assertOwnsDictionary() : void
- Reject operations that target a dictionary the current user does not own.
- buildRow() : array<string, string|int|null>|null
- Build an insertable row from a parsed dictionary entry.
- clip() : string|null
- Truncate an optional metadata value to a column's character capacity.
- hydrateFromRecord() : LocalDictionary
- Hydrate a LocalDictionary entity from a database record.
- insertBatch() : void
- Insert a batch of entries.
- updateEntryCount() : void
- Update the entry count for a dictionary.
Constants
BATCH_SIZE
Batch size for bulk inserts.
private
mixed
BATCH_SIZE
= 1000
MAX_POS_LENGTH
Character capacity of `LePartOfSpeech` (VARCHAR(50)).
private
mixed
MAX_POS_LENGTH
= 50
MAX_READING_LENGTH
Character capacity of `LeReading` (VARCHAR(250)).
private
mixed
MAX_READING_LENGTH
= 250
MAX_TERM_LENGTH
Character capacity of `LeTerm` / `LeTermLc` (both VARCHAR(250)).
private
mixed
MAX_TERM_LENGTH
= 250
Entries with a longer headword are skipped rather than allowed to abort
the import: under STRICT_ALL_TABLES an over-long value fails the whole
multi-row INSERT, so a single bad entry used to lose every other entry in
its batch and end the import (issue #250 — one 293-character headword in
FreeDict German-English killed all 517,534 entries). 250 is also LWT's
own term length (words.WoText), so a longer headword could not become a
usable term anyway.
Methods
addEntriesBatch()
Add multiple entries to a dictionary in batches.
public
addEntriesBatch(int $dictId, iterable<string|int, array{term: string, definition: string, reading?: ?string, pos?: ?string}> $entries) : array{added: int, skipped: int}
Entries whose headword exceeds the column's capacity are skipped and
counted rather than inserted. Under STRICT_ALL_TABLES an over-long value
aborts the entire multi-row INSERT, so without this a single oversized
headword discarded its whole batch and ended the import (issue #250).
reading and pos are descriptive metadata, so those are truncated
instead — losing a part-of-speech label beats losing the entry.
Parameters
- $dictId : int
-
Dictionary ID
- $entries : iterable<string|int, array{term: string, definition: string, reading?: ?string, pos?: ?string}>
-
Entries to add
Tags
Return values
array{added: int, skipped: int} —Entries inserted, and entries skipped for an unstorable headword
addEntry()
Add a single entry to a dictionary.
public
addEntry(int $dictId, string $term, string $definition[, string|null $reading = null ][, string|null $pos = null ]) : int
Parameters
- $dictId : int
-
Dictionary ID
- $term : string
-
Term/headword
- $definition : string
-
Definition
- $reading : string|null = null
-
Pronunciation/reading
- $pos : string|null = null
-
Part of speech
Return values
int —Entry ID
autoEnableLocalDictMode()
Auto-enable local dictionary mode if currently set to online-only.
public
autoEnableLocalDictMode(int $languageId) : void
When a dictionary is imported, if the language's local dict mode is 0 (online only), upgrade it to 1 (local first, online fallback).
Parameters
- $languageId : int
-
Language ID
clearEntries()
Delete all entries from a dictionary.
public
clearEntries(int $dictId) : int
Parameters
- $dictId : int
-
Dictionary ID
Return values
int —Number of entries deleted
create()
Create a new local dictionary.
public
create(int $languageId, string $name[, string $sourceFormat = 'csv' ][, string|null $description = null ]) : int
Parameters
- $languageId : int
-
Language ID
- $name : string
-
Dictionary name
- $sourceFormat : string = 'csv'
-
Source format (csv, json, stardict)
- $description : string|null = null
-
Optional description
Return values
int —The new dictionary ID
createVocabularyFromEntries()
Create vocabulary terms (status 1) from dictionary entries.
public
createVocabularyFromEntries(int $dictId, int $languageId) : int
Uses INSERT IGNORE to skip terms that already exist in the vocabulary for this language. Sets WoStatus = 1 (new/unknown) and WoTranslation from the dictionary definition.
Parameters
- $dictId : int
-
Dictionary ID
- $languageId : int
-
Language ID
Return values
int —Number of vocabulary terms created
delete()
Delete a dictionary and all its entries.
public
delete(int $dictId) : bool
Parameters
- $dictId : int
-
Dictionary ID
Return values
bool —Success
deleteEntry()
Delete a single entry.
public
deleteEntry(int $entryId) : bool
Parameters
- $entryId : int
-
Entry ID
Return values
bool —Success
getAllForLanguage()
Get all dictionaries for a language (including disabled).
public
getAllForLanguage(int $languageId) : array<string|int, LocalDictionary>
Parameters
- $languageId : int
-
Language ID
Return values
array<string|int, LocalDictionary>getById()
Get a dictionary by ID.
public
getById(int $dictId) : LocalDictionary|null
Parameters
- $dictId : int
-
Dictionary ID
Return values
LocalDictionary|nullgetEntries()
Get entries for a dictionary (paginated).
public
getEntries(int $dictId[, int $page = 1 ][, int $perPage = 50 ]) : array{entries: array, total: int, page: int, perPage: int}
Parameters
- $dictId : int
-
Dictionary ID
- $page : int = 1
-
Page number (1-based)
- $perPage : int = 50
-
Entries per page
Return values
array{entries: array, total: int, page: int, perPage: int}getEntryCount()
Get entry count for a dictionary.
public
getEntryCount(int $dictId) : int
Parameters
- $dictId : int
-
Dictionary ID
Return values
intgetForLanguage()
Get all dictionaries for a language.
public
getForLanguage(int $languageId) : array<string|int, LocalDictionary>
Parameters
- $languageId : int
-
Language ID
Return values
array<string|int, LocalDictionary>getLocalDictMode()
Get the local dictionary mode for a language.
public
getLocalDictMode(int $languageId) : int
Parameters
- $languageId : int
-
Language ID
Return values
int —Mode (0=online only, 1=local first, 2=local only, 3=combined)
hasLocalDictionaries()
Check if a language has any local dictionaries.
public
hasLocalDictionaries(int $languageId) : bool
Parameters
- $languageId : int
-
Language ID
Return values
boollookup()
Look up a term in local dictionaries for a language.
public
lookup(int $languageId, string $term) : array<string|int, array{term: string, definition: string, reading: ?string, pos: ?string, dictionary: string}>
Parameters
- $languageId : int
-
Language ID
- $term : string
-
Term to look up
Return values
array<string|int, array{term: string, definition: string, reading: ?string, pos: ?string, dictionary: string}>lookupPrefix()
Look up a term with prefix matching (for autocomplete).
public
lookupPrefix(int $languageId, string $prefix[, int $limit = 10 ]) : array<string|int, array{term: string, definition: string}>
Parameters
- $languageId : int
-
Language ID
- $prefix : string
-
Term prefix
- $limit : int = 10
-
Maximum results
Return values
array<string|int, array{term: string, definition: string}>update()
Update a dictionary.
public
update(LocalDictionary $dictionary) : bool
Parameters
- $dictionary : LocalDictionary
-
Dictionary entity
Return values
bool —Success
updateEntry()
Update a single entry.
public
updateEntry(int $entryId, string $term, string $definition[, string|null $reading = null ][, string|null $pos = null ]) : bool
Parameters
- $entryId : int
-
Entry ID
- $term : string
-
Term
- $definition : string
-
Definition
- $reading : string|null = null
-
Reading
- $pos : string|null = null
-
Part of speech
Return values
bool —Success
assertOwnsDictionary()
Reject operations that target a dictionary the current user does not own.
private
assertOwnsDictionary(int $dictId) : void
local_dictionary_entries is not auto-scoped (it has no LdUsID column),
so every entry-level call has to bounce through getById — which IS
user-scoped via QueryBuilder — to confirm ownership before touching the
row. Single-user mode is a no-op.
Parameters
- $dictId : int
Tags
buildRow()
Build an insertable row from a parsed dictionary entry.
private
static buildRow(int $dictId, array{term: string, definition: string, reading?: ?string, pos?: ?string} $entry) : array<string, string|int|null>|null
Parameters
- $dictId : int
-
Dictionary ID
- $entry : array{term: string, definition: string, reading?: ?string, pos?: ?string}
-
Parsed entry
Return values
array<string, string|int|null>|null —The row, or null when the headword cannot be stored and the entry has to be skipped
clip()
Truncate an optional metadata value to a column's character capacity.
private
static clip(string|null $value, int $maxLength) : string|null
Parameters
- $value : string|null
-
Value to clip, or null
- $maxLength : int
-
Maximum characters the column accepts
Return values
string|null —The value, shortened if it was over-long
hydrateFromRecord()
Hydrate a LocalDictionary entity from a database record.
private
hydrateFromRecord(array<string, mixed> $record) : LocalDictionary
Parameters
- $record : array<string, mixed>
-
Database record
Return values
LocalDictionaryinsertBatch()
Insert a batch of entries.
private
insertBatch(array<string|int, array<string, mixed>> $batch) : void
Parameters
- $batch : array<string|int, array<string, mixed>>
-
Batch of entry data
updateEntryCount()
Update the entry count for a dictionary.
private
updateEntryCount(int $dictId) : void
Parameters
- $dictId : int
-
Dictionary ID