Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
WikifunctionsClientUsageUpdateJob
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 2
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
2
 run
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3/**
4 * @file
5 * @ingroup Extensions
6 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
7 * @license MIT
8 */
9
10namespace MediaWiki\Extension\WikiLambda\Jobs;
11
12use MediaWiki\Config\Config;
13use MediaWiki\Extension\WikiLambda\WikifunctionsClientStore;
14use MediaWiki\Extension\WikiLambda\WikiLambdaServices;
15use MediaWiki\JobQueue\GenericParameterJob;
16use MediaWiki\JobQueue\Job;
17use MediaWiki\Logger\LoggerFactory;
18use MediaWiki\MediaWikiServices;
19use MediaWiki\Title\Title;
20use Psr\Log\LoggerInterface;
21
22/**
23 * Asynchronous job to record that a page is using a function in the database,
24 * which allows us to avoid a database write on an API GET.
25 */
26class WikifunctionsClientUsageUpdateJob extends Job implements GenericParameterJob {
27
28    private LoggerInterface $logger;
29    private WikifunctionsClientStore $wikifunctionsClientStore;
30    private Config $config;
31
32    private string $targetFunction;
33    private string $targetPageText;
34    private int $targetPageNamespace;
35
36    public function __construct( array $params ) {
37        parent::__construct( 'wikifunctionsClientUsageUpdate', $params );
38        $this->logger = LoggerFactory::getInstance( 'WikiLambdaClient' );
39        $this->wikifunctionsClientStore = WikiLambdaServices::getWikifunctionsClientStore();
40
41        $this->config = MediaWikiServices::getInstance()->getMainConfig();
42
43        $this->targetFunction = $params['targetFunction'];
44        $this->targetPageText = $params['targetPageText'];
45        $this->targetPageNamespace = $params['targetPageNamespace'];
46
47        $this->logger->debug(
48            __CLASS__ . ' created for {targetFunction} on {targetPageNS}:{targetPage}',
49            [
50                'targetFunction' => $this->targetFunction,
51                'targetPage' => $this->targetPageText,
52                'targetPageNS' => $this->targetPageNamespace,
53            ]
54        );
55    }
56
57    /**
58     * @return bool
59     */
60    public function run() {
61        $this->logger->debug(
62            __CLASS__ . ' initiated for {targetFunction} on {targetPageNS}:{targetPage}',
63            [
64                'targetFunction' => $this->targetFunction,
65                'targetPage' => $this->targetPageText,
66                'targetPageNS' => $this->targetPageNamespace,
67            ]
68        );
69
70        if (
71            // If the wgWikiLambdaEnableClientMode flag is not set, don't try to run anything
72            !$this->config->get( 'WikiLambdaEnableClientMode' ) &&
73            // … but don't do this if the server thinks the wiki isn't configured for WikiLambda at all, which will
74            // happen when one server thinks we're configured and one doesn't (i.e., we're admist a deployment)
75            $this->config->has( 'WikiLambdaEnableClientMode' )
76        ) {
77            $this->logger->warning(
78                __CLASS__ . ' triggered for {targetFunction} on {targetPageNS}:{targetPage}; not in client mode.',
79                [
80                    'targetFunction' => $this->targetFunction,
81                    'targetPage' => $this->targetPageText,
82                    'targetPageNS' => $this->targetPageNamespace,
83                ]
84            );
85
86            // Nothing for us to do.
87            return true;
88        }
89
90        // FIXME: Don't proceed but evict from page if cache job finds that parser object doesn't have our flag? We
91        // have set it (on the PC, not Title) via $extApi->getMetadata()->setExtensionData( 'wikilambda', 'present' );
92
93        $success = $this->wikifunctionsClientStore->insertWikifunctionsUsage(
94            $this->targetFunction,
95            Title::newFromText( $this->targetPageText, $this->targetPageNamespace )
96        );
97
98        if ( $success ) {
99            $this->logger->debug(
100                __CLASS__ . ' Updated usage table for {targetFunction} on {targetPageNS}:{targetPage}',
101                [
102                    'targetFunction' => $this->targetFunction,
103                    'targetPage' => $this->targetPageText,
104                    'targetPageNS' => $this->targetPageNamespace,
105                ]
106            );
107        } else {
108            $this->logger->info(
109                __CLASS__ . ' Didn\'t update usage for {targetFunction} on {targetPageNS}:{targetPage}; already there?',
110                [
111                    'targetFunction' => $this->targetFunction,
112                    'targetPage' => $this->targetPageText,
113                    'targetPageNS' => $this->targetPageNamespace,
114                ]
115            );
116
117        }
118
119        return true;
120    }
121}