izartu API

Bookmark extends Crud
in package

A bookmark: a typed record plus its own persistence (Active Record).

Table of Contents

Properties

$created  : int|null
Creation time, as epoch seconds.
$hlink  : string
$id  : int|null
$modified  : int|null
Last-modification time, as epoch seconds.
$text  : string
$title  : string
$user  : int
$username  : string
Owner's display name, read via JOIN; never written back.
$visibility  : Visibility

Methods

behind()  : bool
Whether the database lacks migrations the code has.
connect()  : PDO
Open a connection to the database the `DB_*` settings name.
delete()  : void
Delete this bookmark and, through the foreign key's cascade, its tag links.
deleteTags()  : void
Unlink every tag from this bookmark.
dsn()  : string
Build a MySQL DSN.
find()  : self|null
Find a bookmark by id.
options()  : array<int, bool|int|string>
The PDO options of a connection.
orderByDate()  : array{bookmarks: list, pages: int}
Read one page of bookmarks ordered by modification date.
pageWindow()  : array<int, int|null>
The page numbers a pager should display: first, last, and a window around the current page, with null marking each gap (an ellipsis).
parseTags()  : array<int, string>
Split a comma-separated tag string into normalised tag names.
save()  : void
Insert this bookmark when it is new, or update it when it has an id.
saveWithTags()  : void
Save this bookmark and replace its tags, in one transaction.
sslConflict()  : string
The `DB_SSL_*` settings another one leaves without effect, as a warning for the operator.
sslHint()  : string
What to check when a connection over TLS fails.
tagsFor()  : array<int, array<int, string>>
The tag names of several bookmarks in a single query (one page's worth).
visibleTo()  : bool
Whether a viewer may see this bookmark.
db()  : PDO
The shared database connection, opened on first use.
read()  : array<int, array<string, mixed>>
Prepare and run a read query, then return its rows.
count()  : int
Count the bookmarks the current listing can see.
filter()  : array{0: string, 1: list|false}
Build the WHERE clause a listing needs (visibility and tag filters).
hydrate()  : self
Build a Bookmark from a database row.
saveTags()  : void
Replace this bookmark's tags, inside `saveWithTags()`'s transaction.
select()  : array<int, array{id: int, title: string, hlink: string, text: string, user: int, visibility: string, created: int, modified: int, username: string|null}>
Read bookmark rows matching an SQL condition.

Properties

$created

Creation time, as epoch seconds.

public int|null $created = \null

$modified

Last-modification time, as epoch seconds.

public int|null $modified = \null

$username

Owner's display name, read via JOIN; never written back.

public string $username = ''

Methods

behind()

Whether the database lacks migrations the code has.

public static behind() : bool
Tags
throws
RuntimeException

When the database cannot be checked (see Migration::version()).

Return values
bool

connect()

Open a connection to the database the `DB_*` settings name.

public static connect() : PDO

The charset is utf8mb4 and the timezone is the server's.

Tags
throws
PDOException

When the database cannot be reached; when TLS may be why, the message says what to check (sslHint()).

Return values
PDO —

The connection, with options().

delete()

Delete this bookmark and, through the foreign key's cascade, its tag links.

public delete() : void

deleteTags()

Unlink every tag from this bookmark.

public deleteTags() : void

dsn()

Build a MySQL DSN.

public static dsn(string $host, int $port, string $name) : string
Parameters
$host : string

Host name or IP address, or the path of a Unix socket (it starts with /).

$port : int

TCP port, left out of the DSN when it is MySQL's default (3306) and ignored for a socket.

$name : string

Database name.

Return values
string —

The PDO DSN, with the utf8mb4 charset.

find()

Find a bookmark by id.

public static find(int $id) : self|null
Parameters
$id : int

The bookmark id.

Return values
self|null —

The hydrated bookmark, or null if none has that id.

options()

The PDO options of a connection.

public static options(string $host, bool $ssl, bool $verify, string $ca) : array<int, bool|int|string>

Errors throw, and prepared statements are native, so integer columns come back as int. Over TCP the connection is encrypted, unless $ssl is off; over a Unix socket TLS does not work.

Parameters
$host : string

Host name or IP address, or a socket path; localhost connects over the default socket.

$ssl : bool

Whether to encrypt over TCP.

$verify : bool

Whether to verify the server's certificate, name included.

$ca : string

CA file to verify it against; '' for the CAs the system trusts.

Return values
array<int, bool|int|string> —

The options.

orderByDate()

Read one page of bookmarks ordered by modification date.

public final orderByDate([int|null $viewer = null ][, int $page = 1 ][, array<int, string> $tags = [] ][, string|null $username = null ][, bool $order = false ]) : array{bookmarks: list, pages: int}
Parameters
$viewer : int|null = null

The viewer's user id (Auth::id()), or null for anonymous. A viewer sees public bookmarks plus their own private ones.

$page : int = 1

1-based page number; each page holds PAGE_SIZE bookmarks.

$tags : array<int, string> = []

Only bookmarks carrying ALL these tag names; empty for all.

$username : string|null = null

Only bookmarks added by this user, or null for all.

$order : bool = false

true for ascending order, false (default) for descending.

Return values
array{bookmarks: list, pages: int} —

The page's bookmarks (newest first by default) and the total number of pages (at least 1).

pageWindow()

The page numbers a pager should display: first, last, and a window around the current page, with null marking each gap (an ellipsis).

public static pageWindow(int $page, int $pages[, int $radius = 2 ]) : array<int, int|null>
Parameters
$page : int

Current 1-based page number.

$pages : int

Total number of pages.

$radius : int = 2

How many pages to show on each side of the current one.

Return values
array<int, int|null> —

Page numbers in order, null where pages are skipped.

parseTags()

Split a comma-separated tag string into normalised tag names.

public static parseTags(string $tags) : array<int, string>
Parameters
$tags : string

Comma-separated tag names, as typed by the user.

Return values
array<int, string> —

Lower-case, trimmed names with inner whitespace collapsed, deduplicated; empties dropped.

save()

Insert this bookmark when it is new, or update it when it has an id.

public save() : void

created and modified are left to the database, so both come from one clock; this object does not learn their values until it is read back.

saveWithTags()

Save this bookmark and replace its tags, in one transaction.

public saveWithTags(string $tags) : void

A failure leaves the bookmark and its tags as they were. A deadlock rolls the save back and it runs again, three attempts at most.

Parameters
$tags : string

Comma-separated tag names, as typed by the user.

Tags
throws
PDOException

When a new tag has no id left, the database fails, or the third attempt deadlocks.

sslConflict()

The `DB_SSL_*` settings another one leaves without effect, as a warning for the operator.

public static sslConflict(string $host, bool $ssl, bool $verify, string $ca) : string
Parameters
$host : string

Host name or IP address, or a socket path, as for options().

$ssl : bool

Whether to encrypt over TCP.

$verify : bool

Whether to verify the server's certificate.

$ca : string

CA file to verify it against.

Return values
string —

The warning; '' when every setting takes effect.

sslHint()

What to check when a connection over TLS fails.

public static sslHint(string $error, array<int, bool|int|string> $options) : string

mysqlnd gives one message for every certificate that does not verify, readable CA or not, and reports a server without TLS as gone away.

Parameters
$error : string

The connection's error message.

$options : array<int, bool|int|string>

Its options, options()'s.

Return values
string —

The setting to check, as a sentence; '' when TLS is not why.

tagsFor()

The tag names of several bookmarks in a single query (one page's worth).

public tagsFor(array<int, int> $ids) : array<int, array<int, string>>
Parameters
$ids : array<int, int>

Bookmark ids.

Return values
array<int, array<int, string>> —

Tag names (alphabetical) per bookmark id; ids without tags are absent.

visibleTo()

Whether a viewer may see this bookmark.

public visibleTo(int|null $viewer) : bool

Public bookmarks are visible to everyone; private ones only to their owner (no role sees another user's private bookmarks).

Parameters
$viewer : int|null

The viewer's user id, or null for anonymous.

Return values
bool

db()

The shared database connection, opened on first use.

protected final static db() : PDO

Building, validating or formatting a model therefore needs no database. With DEBUG on, statements are created as DebugStatement so every query execution is timed.

Tags
throws
DatabaseUnavailable

When the database cannot be reached.

Return values
PDO —

The shared connection, opened by connect().

read()

Prepare and run a read query, then return its rows.

protected final read(string $sql[, array<int, array{0: string, 1: mixed, 2: int, 3: int}>|false $param = false ]) : array<int, array<string, mixed>>
Parameters
$sql : string

SQL query with bind placeholders.

$param : array<int, array{0: string, 1: mixed, 2: int, 3: int}>|false = false

Bind parameters (each: [name, value, PDO type, length]), or false for none.

Return values
array<int, array<string, mixed>> —

The result rows.

count()

Count the bookmarks the current listing can see.

private count(int|null $viewer[, array<int, string> $tags = [] ][, string|null $username = null ]) : int
Parameters
$viewer : int|null

The viewer's user id, or null for anonymous.

$tags : array<int, string> = []

Only bookmarks carrying ALL these tag names; empty for all.

$username : string|null = null

Only bookmarks added by this user, or null for all.

Return values
int —

The bookmark count.

filter()

Build the WHERE clause a listing needs (visibility and tag filters).

private filter(int|null $viewer[, array<int, string> $tags = [] ][, string|null $username = null ]) : array{0: string, 1: list|false}
Parameters
$viewer : int|null

The viewer's user id, or null for anonymous.

$tags : array<int, string> = []

Only bookmarks carrying ALL these tag names; empty for all.

$username : string|null = null

Only bookmarks added by this user, or null for all.

Return values
array{0: string, 1: list|false} —

The WHERE clause (never empty) and its bind parameters.

hydrate()

Build a Bookmark from a database row.

private static hydrate(array{id: int, title: string, hlink: string, text: string, user: int, visibility: string, created: int, modified: int, username: string|null} $row) : self
Parameters
$row : array{id: int, title: string, hlink: string, text: string, user: int, visibility: string, created: int, modified: int, username: string|null}

A bookmark row (see select).

Return values
self —

The hydrated bookmark.

saveTags()

Replace this bookmark's tags, inside `saveWithTags()`'s transaction.

private saveTags(string $tags) : void

The names missing from tag are created, and every name is linked in bookmark_tag by name, so a name that exists spends no id and two spellings of one tag (the as_ci collation: é as one character or as e and an accent) make one link.

Parameters
$tags : string

Comma-separated tag names, as typed by the user.

Tags
throws
PDOException

When a new tag has no id left, or the database fails.

select()

Read bookmark rows matching an SQL condition.

private select(string|false $cond, array<int, array{0: string, 1: mixed, 2: int, 3: int}>|false $param) : array<int, array{id: int, title: string, hlink: string, text: string, user: int, visibility: string, created: int, modified: int, username: string|null}>
Parameters
$cond : string|false

Extra SQL appended to the base SELECT, or false.

$param : array<int, array{0: string, 1: mixed, 2: int, 3: int}>|false

Bind parameters for $cond (each: [name, value, PDO type, length]), or false.

Return values
array<int, array{id: int, title: string, hlink: string, text: string, user: int, visibility: string, created: int, modified: int, username: string|null}> —

One raw row per bookmark.

On this page

Search results