Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 65
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Hooks
0.00% covered (danger)
0.00%
0 / 65
0.00% covered (danger)
0.00%
0 / 4
210
0.00% covered (danger)
0.00%
0 / 1
 onLoadExtensionSchemaUpdates
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 1
56
 onMakeGlobalVariablesScript
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 onRenameUserSQL
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 onUserMergeAccountFields
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace MediaWiki\Extension\CodeReview;
4
5use DatabaseUpdater;
6use MediaWiki\Extension\Renameuser\RenameuserSQL;
7use OutputPage;
8
9/**
10 * Class containing hooked functions used by the CodeReview extension.
11 * All functions are public and static.
12 *
13 * @file
14 */
15class Hooks {
16
17    /**
18     * Performs database updates (initial table creation on first install,
19     * addition of new tables/fields/indexes for old installs that are being
20     * upgraded) when the user runs the core MediaWiki updater script,
21     * /maintenance/update.php.
22     *
23     * Only MySQL(/MariaDB) and SQLite are supported at the moment.
24     *
25     * @param DatabaseUpdater $updater
26     */
27    public static function onLoadExtensionSchemaUpdates( DatabaseUpdater $updater ) {
28        $base = __DIR__ . '/..';
29        switch ( $updater->getDB()->getType() ) {
30        case 'mysql':
31            // Initial install tables
32            $updater->addExtensionTable( 'code_rev', "$base/sql/codereview.sql" );
33
34            $updater->addExtensionField( 'code_rev', 'cr_diff',
35                "$base/archives/codereview-cr_diff.sql" );
36            $updater->addExtensionIndex( 'code_relations', 'repo_to_from',
37                "$base/archives/code_relations_index.sql" );
38
39            if ( !$updater->updateRowExists( 'make cr_status varchar' ) ) {
40                $updater->modifyExtensionField( 'code_rev', 'cr_status',
41                    "$base/archives/codereview-cr_status_varchar.sql" );
42            }
43
44            $updater->addExtensionTable( 'code_bugs', "$base/archives/code_bugs.sql" );
45
46            $updater->addExtensionTable( 'code_signoffs', "$base/archives/code_signoffs.sql" );
47
48            $updater->addExtensionField( 'code_signoffs', 'cs_user',
49                "$base/archives/code_signoffs_userid.sql" );
50            $updater->addExtensionField( 'code_signoffs', 'cs_timestamp_struck',
51                "$base/archives/code_signoffs_timestamp_struck.sql" );
52
53            $updater->addExtensionIndex( 'code_comment', 'cc_author',
54                "$base/archives/code_comment_author-index.sql" );
55
56            $updater->addExtensionIndex( 'code_prop_changes', 'cpc_author',
57                "$base/archives/code_prop_changes_author-index.sql" );
58
59            if ( !$updater->updateRowExists( 'make cp_action char' ) ) {
60                $updater->modifyExtensionField( 'code_paths', 'cp_action',
61                    "$base/archives/codereview-cp_action_char.sql" );
62            }
63
64            if ( !$updater->updateRowExists( 'make cpc_attrib varchar' ) ) {
65                $updater->modifyExtensionField( 'code_prop_changes', 'cpc_attrib',
66                    "$base/archives/codereview-cpc_attrib_varchar.sql" );
67            }
68
69            $updater->addExtensionIndex( 'code_paths', 'repo_path',
70                "$base/archives/codereview-repopath.sql" );
71
72            $updater->addExtensionIndex( 'code_rev', 'cr_repo_status_author',
73                "$base/archives/code_revs_status_author-index.sql" );
74
75            $updater->dropExtensionField( 'code_comment', 'cc_review',
76                "$base/archives/code_drop_cc_review.sql" );
77
78            $updater->dropExtensionTable( 'code_test_suite',
79                "$base/archives/code_drop_test.sql" );
80
81            $updater->addExtensionField( 'code_authors', 'ca_user',
82                "$base/archives/code_authors_add_ca_user.sql" );
83
84            $updater->dropExtensionIndex(
85                'code_authors',
86                'ca_repo_author',
87                "$base/archives/code_author-drop-ca_repo_author.sql"
88            );
89            break;
90        case 'sqlite':
91            $updater->addExtensionTable( 'code_rev', "$base/sql/codereview.sql" );
92            $updater->addExtensionTable( 'code_signoffs', "$base/archives/code_signoffs.sql" );
93            $updater->addExtensionField( 'code_signoffs', 'cs_user',
94                "$base/archives/code_signoffs_userid-sqlite.sql" );
95            $updater->addExtensionField( 'code_signoffs', 'cs_timestamp_struck',
96                "$base/archives/code_signoffs_timestamp_struck.sql" );
97            $updater->addExtensionIndex( 'code_paths', 'repo_path',
98                "$base/archives/codereview-repopath.sql" );
99            $updater->addExtensionField( 'code_authors', 'ca_user',
100                "$base/archives/code_authors_add_ca_user.sql" );
101            break;
102        case 'postgres':
103            // Initial install tables
104            $updater->addExtensionTable( 'code_rev', "$base/sql/codereview.pg.sql" );
105            $updater->addExtensionTable( 'code_signoffs', "$base/archives/code_signoffs.pg.sql" );
106            break;
107        }
108    }
109
110    /**
111     * Sets the wgCodeReviewRepository JavaScript variable to the name of the
112     * current repository when we're on Special:Code, or to be more specific,
113     * a subpage of a repository on Special:Code.
114     *
115     * @param array &$values
116     * @param OutputPage $out
117     */
118    public static function onMakeGlobalVariablesScript( &$values, $out ) {
119        # Bleugh, this is horrible
120        $title = $out->getTitle();
121        if ( $title->isSpecial( 'Code' ) ) {
122            $bits = explode( '/', $title->getText() );
123            if ( isset( $bits[1] ) ) {
124                $values['wgCodeReviewRepository'] = $bits[1];
125            }
126        }
127    }
128
129    /**
130     * For integration with the Renameuser extension.
131     *
132     * @param RenameuserSQL $renameUserSQL
133     */
134    public static function onRenameUserSQL( $renameUserSQL ) {
135        foreach ( self::$userTables as $table => $fields ) {
136            $renameUserSQL->tables[$table] = $fields;
137        }
138    }
139
140    /**
141     * @var string[][]
142     */
143    private static $userTables = [
144        'code_authors' => [ 'ca_user_text', 'ca_user' ],
145        'code_comment' => [ 'cc_user_text', 'cc_user' ],
146        'code_prop_changes' => [ 'cpc_user_text', 'cpc_user' ],
147        'code_signoffs' => [ 'cs_user_text', 'cs_user' ]
148    ];
149
150    /**
151     * For integration with the UserMerge extension.
152     *
153     * @param array &$updateFields
154     */
155    public static function onUserMergeAccountFields( &$updateFields ) {
156        // array( tableName, idField, textField )
157        foreach ( self::$userTables as $table => $fields ) {
158            $updateFields[] = [ $table, $fields[1], $fields[0] ];
159        }
160    }
161}