Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
9 / 9
CRAP
100.00% covered (success)
100.00%
1 / 1
WikiLambdaMode
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
9 / 9
12
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 readFlag
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 isRepo
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isClient
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isClientOffline
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isAbstract
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isAbstractClient
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isAbstractClientIntegration
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 isRepoOrAbstract
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * WikiLambda feature-mode value object
4 *
5 * @file
6 * @ingroup Extensions
7 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
8 * @license MIT
9 */
10
11namespace MediaWiki\Extension\WikiLambda;
12
13use MediaWiki\Config\Config;
14
15/**
16 * Resolves WikiLambda's independently-toggled feature modes once, from config,
17 * and answers "which mode am I in?" behind a small interface.
18 *
19 * This is the single seam for the ~75 runtime mode checks that were previously
20 * raw `$config->get( 'WikiLambdaEnable…' )` reads scattered across four
21 * inconsistent access paths. It deliberately does NOT cover the two
22 * extension-bootstrap gating sites — RepoHooks::registerExtension() (which runs
23 * before the service container and reads $wg globals) and the
24 * LoadExtensionSchemaUpdates installer blocks (has()-guarded, pre-registry) —
25 * as those are structural registration, not runtime predicate reads.
26 *
27 * The flags are fixed for the lifetime of a request, so they are read once at
28 * construction and the object is immutable thereafter.
29 */
30class WikiLambdaMode {
31
32    private bool $repo;
33    private bool $client;
34    private bool $clientOffline;
35    private bool $abstract;
36    private bool $abstractClient;
37    private bool $abstractClientIntegration;
38
39    public function __construct( Config $config ) {
40        $this->repo = self::readFlag( $config, 'WikiLambdaEnableRepoMode' );
41        $this->client = self::readFlag( $config, 'WikiLambdaEnableClientMode' );
42        $this->clientOffline = self::readFlag( $config, 'WikiLambdaClientModeOffline' );
43        $this->abstract = self::readFlag( $config, 'WikiLambdaEnableAbstractMode' );
44        $this->abstractClient = self::readFlag( $config, 'WikiLambdaEnableAbstractClientMode' );
45        $this->abstractClientIntegration = self::readFlag( $config, 'WikiLambdaEnableAbstractClientModeIntegration' );
46    }
47
48    /**
49     * Read a boolean mode flag, treating an absent key as false (fail-closed: a
50     * mode that isn't configured is off). In production during normal operations,
51     * every flag is at least its extension.json default or over-ridden, so the
52     * key is always present; the has() guard is for partial Configs (e.g. a
53     * HashConfig in a test) and mirrors the defensive config-presence checks used
54     * elsewhere in the extension for e.g. very early reads during MediaWiki's
55     * start-up.
56     */
57    private static function readFlag( Config $config, string $key ): bool {
58        return $config->has( $key ) && (bool)$config->get( $key );
59    }
60
61    /**
62     * Repo mode: this wiki hosts ZObjects in the main namespace (Wikifunctions).
63     * The DB-backed ZObjectStore exists only when this is true.
64     */
65    public function isRepo(): bool {
66        return $this->repo;
67    }
68
69    /**
70     * Client mode: this wiki calls a remote Wikifunctions repo via {{#function:…}}.
71     */
72    public function isClient(): bool {
73        return $this->client;
74    }
75
76    /**
77     * Client-offline modifier: emit cached/disabled state instead of running new
78     * function calls. Only meaningful inside client-mode code paths.
79     */
80    public function isClientOffline(): bool {
81        return $this->clientOffline;
82    }
83
84    /**
85     * Abstract mode: this wiki hosts Abstract Wikipedia content (an Abstract Repo).
86     */
87    public function isAbstract(): bool {
88        return $this->abstract;
89    }
90
91    /**
92     * Abstract Client mode: this wiki displays rendered Abstract Articles in place
93     * of missing local ones (an Abstract Client).
94     */
95    public function isAbstractClient(): bool {
96        return $this->abstractClient;
97    }
98
99    /**
100     * Whether Abstract Client integration is actually live. The integration flag
101     * is a kill-switch that is meaningless unless Abstract Client mode is also on,
102     * so it is only ever the two AND-ed together.
103     */
104    public function isAbstractClientIntegration(): bool {
105        return $this->abstractClient && $this->abstractClientIntegration;
106    }
107
108    /**
109     * Whether repo or abstract mode is on — the union used by the page-rendering
110     * hooks to decide whether a request concerns a locally-hosted ZObject or
111     * Abstract Article at all. (Client modes render content too, but do not host
112     * it, so they are deliberately excluded here.)
113     */
114    public function isRepoOrAbstract(): bool {
115        return $this->repo || $this->abstract;
116    }
117}