#4117·YOURLS

Deprecate `yourls_get_db()` context param

Author: ozhCreated Jun 1, 2026Updated Jul 19, 2026

Since 1.10.3 yourls_get_db() needs a $context param to indicate either a read- something or a write- something action.

php
function yourls_get_db($context = '') {
    // Allow plugins to short-circuit the whole function
    [... shunt...]

    // Validate context and raise notice if missing or malformed
    if ($context == '' || !preg_match('/^(read|write)-[a-z0-9_]+$/', $context)) {
        $db = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
        $file = $db[0]['file'];
        $line = $db[0]['line'];

        if ($context == '') {
            $msg = 'Undefined yourls_get_db() context';
        } else {
            $msg = 'Improperly formatted yourls_get_db() context ("' . $context . '")';
        }

        trigger_error( $msg . ' at <b>' . $file . ':' . $line .'</b>', E_USER_NOTICE );
    }

    [... rest of the func...]

I introduced this parameter in 1.10.3 to allow segregating read from write queries and allow, for instance, a DB with a master/slave setup -- https://gist.github.com/ozh/553c2519878984271584939355e91b8e

But this is mostly cosmetic and targets a very specific audience.

Since 1.10.4 we have much better : each query is filtered. It should be "easy" to route queries based on the actual query rather than a short description context.

Not completely trivial because the flow is "first, get the DB instance, then, perform the query" (yourls_get_db('read-something')->fetchSomething('SQL query');), so when we get the DB we don't know the query yet.

Needs some thinking :)