Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 90
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
CNDatabasePatcher
0.00% covered (danger)
0.00%
0 / 90
0.00% covered (danger)
0.00%
0 / 2
20
0.00% covered (danger)
0.00%
0 / 1
 onLoadExtensionSchemaUpdates
0.00% covered (danger)
0.00%
0 / 65
0.00% covered (danger)
0.00%
0 / 1
6
 doOnSchemaUpdatesPopulateKnownDevices
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * @file
4 * @license GPL-2.0-or-later
5 */
6
7use MediaWiki\Installer\Hook\LoadExtensionSchemaUpdatesHook;
8
9/**
10 * Maintenance helper class that updates the database schema when required.
11 *
12 * Apply patches with /maintenance/update.php
13 */
14class CNDatabasePatcher implements LoadExtensionSchemaUpdatesHook {
15    /**
16     * LoadExtensionSchemaUpdates hook handler
17     * This function makes sure that the database schema is up to date.
18     *
19     * @param DatabaseUpdater $updater
20     */
21    public function onLoadExtensionSchemaUpdates( $updater ) {
22        $base = __DIR__ . '/../sql';
23        $dbType = $updater->getDB()->getType();
24        $updater->addExtensionTable( 'cn_notices', "$base/$dbType/tables-generated.sql" );
25
26        if ( $dbType === 'mysql' ) {
27            // 1.35
28            // Adds geotargeted regions for notices and the corresponding log columns
29            $updater->addExtensionUpdate(
30                [
31                    'addTable', 'cn_notice_regions',
32                    $base . '/mysql/patch-notice_regions.sql', true
33                ]
34            );
35            $updater->addExtensionUpdate(
36                [
37                    'addField', 'cn_templates', 'tmp_is_template',
38                    $base . '/mysql/patch_template_banners.sql', true
39                ]
40            );
41            $updater->addExtensionUpdate(
42                [
43                    'modifyField', 'cn_templates', 'tmp_is_template',
44                    $base . '/mysql/patch_template_banners_field_update.sql', true
45                ]
46            );
47
48            // 1.36
49            $updater->addExtensionField(
50                'cn_notices',
51                'not_type',
52                $base . '/mysql/patch-notice_not_type.sql'
53            );
54            // This adds both notlog_begin_type and notlog_end_type fields
55            $updater->addExtensionField(
56                'cn_notice_log',
57                'notlog_begin_type',
58                $base . '/mysql/patch-notice-type-log.sql'
59            );
60
61            // 1.39
62            $updater->modifyExtensionField(
63                'cn_notices',
64                'not_end',
65                $base . '/mysql/patch-cn_notices-timestamps.sql'
66            );
67            $updater->modifyExtensionField(
68                'cn_notice_log',
69                'notlog_end_end',
70                $base . '/mysql/patch-cn_notice_log-timestamps.sql'
71            );
72        }
73
74        // 1.40
75        $updater->dropExtensionIndex(
76            'cn_notice_languages',
77            'nl_notice_id_language',
78            "$base/$dbType/patch-cn_notice_languages-unique-to-pk.sql"
79        );
80        $updater->dropExtensionIndex(
81            'cn_notice_projects',
82            'np_notice_id_project',
83            "$base/$dbType/patch-cn_notice_projects-unique-to-pk.sql"
84        );
85        $updater->dropExtensionIndex(
86            'cn_notice_countries',
87            'nc_notice_id_country',
88            "$base/$dbType/patch-cn_notice_countries-unique-to-pk.sql"
89        );
90        $updater->dropExtensionIndex(
91            'cn_notice_regions',
92            'nr_notice_id_region',
93            "$base/$dbType/patch-cn_notice_regions-unique-to-pk.sql"
94        );
95
96        $updater->addExtensionUpdate( [
97            [ __CLASS__, 'doOnSchemaUpdatesPopulateKnownDevices' ],
98        ] );
99    }
100
101    public static function doOnSchemaUpdatesPopulateKnownDevices( DatabaseUpdater $updater ): void {
102        $updateKey = 'populateKnownDevices-1.24';
103        if ( $updater->updateRowExists( $updateKey ) ) {
104            $updater->output( "...default known devices already added\n" );
105            return;
106        }
107
108        $updater->output( "Adding known devices...\n" );
109        $dbw = $updater->getDB();
110        $dbw->newInsertQueryBuilder()
111            ->insertInto( 'cn_known_devices' )
112            ->ignore()
113            ->rows( [
114                [ 'dev_id' => 1, 'dev_name' => 'desktop',
115                    'dev_display_label' => '{{int:centralnotice-devicetype-desktop}}' ],
116                // 1.24
117                [ 'dev_id' => 2, 'dev_name' => 'android',
118                    'dev_display_label' => '{{int:centralnotice-devicetype-android}}' ],
119                [ 'dev_id' => 3, 'dev_name' => 'iphone',
120                    'dev_display_label' => '{{int:centralnotice-devicetype-iphone}}' ],
121                [ 'dev_id' => 4, 'dev_name' => 'ipad',
122                    'dev_display_label' => '{{int:centralnotice-devicetype-ipad}}' ],
123                [ 'dev_id' => 5, 'dev_name' => 'unknown',
124                    'dev_display_label' => '{{int:centralnotice-devicetype-unknown}}' ],
125            ] )
126            ->caller( __METHOD__ )
127            ->execute();
128
129        $updater->output( "Done\n" );
130        $updater->insertUpdateRow( $updateKey );
131    }
132}