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