Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
CargoTableDiagram
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 3
156
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 1
110
 getGroupName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Displays a sort of "database diagram" showing the relationships between
4 * Cargo tables.
5 *
6 * @author Yaron Koren
7 * @ingroup Cargo
8 */
9
10class CargoTableDiagram extends IncludableSpecialPage {
11    public function __construct() {
12        parent::__construct( 'CargoTableDiagram' );
13    }
14
15    public function execute( $subpage = null ) {
16        $out = $this->getOutput();
17
18        $this->setHeaders();
19
20        $out->addModules( 'ext.cargo.diagram' );
21
22        $tableNames = CargoUtils::getTables();
23        if ( count( $tableNames ) == 0 ) {
24            return;
25        }
26
27        $userDefinedTables = [];
28        foreach ( $tableNames as $tableName ) {
29            if ( substr( $tableName, 0, 1 ) !== '_' ) {
30                $userDefinedTables[] = $tableName;
31            }
32        }
33
34        // Create a minimal array of schema data, since we only need
35        // a small fraction of the overall schema information.
36        $tableSchemas = CargoUtils::getTableSchemas( $userDefinedTables );
37        $tableSchemaData = [];
38        foreach ( $tableSchemas as $tableName => $tableSchema ) {
39            $curTableSchemaData = [];
40            foreach ( $tableSchema->mFieldDescriptions as $fieldName => $fieldDesc ) {
41                $typeString = $fieldDesc->mType;
42                if ( $fieldDesc->mIsList ) {
43                    // @todo - i18n this?
44                    $typeString = "List of $typeString";
45                }
46                $curTableSchemaData[$fieldName] = [
47                    'type' => $typeString
48                ];
49            }
50            $tableSchemaData[$tableName] = $curTableSchemaData;
51        }
52        $tableSchemasJSON = json_encode( $tableSchemaData );
53
54        $allParentTables = [];
55        foreach ( $userDefinedTables as $tableName ) {
56            $parentTables = CargoUtils::getParentTables( $tableName );
57            if ( is_array( $parentTables ) && count( $parentTables ) > 0 ) {
58                $allParentTables[$tableName] = $parentTables;
59            }
60        }
61        $allParentTablesJSON = json_encode( $allParentTables );
62
63        $text = "<div class=\"cargo-table-diagram\" data-table-schemas='$tableSchemasJSON' data-parent-tables='$allParentTablesJSON'><svg class=\"cargo-table-svg\"></svg></div>";
64
65        $out->addHTML( $text );
66
67        return true;
68    }
69
70    protected function getGroupName() {
71        return 'cargo';
72    }
73}