Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 55
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
CargoGanttData
0.00% covered (danger)
0.00%
0 / 55
0.00% covered (danger)
0.00%
0 / 2
272
0.00% covered (danger)
0.00%
0 / 1
 getTableSchema
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
12
 storeGanttValues
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 1
182
1<?php
2
3/**
4 * Static functions for dealing with the "_ganttData" table, which stores data
5 * produced by the Flex Diagrams extension.
6 *
7 * @author Yaron Koren
8 */
9class CargoGanttData {
10
11    /**
12     * Set the schema.
13     */
14    public static function getTableSchema() {
15        $fieldTypes = [];
16        $fieldTypes['_localID'] = [ 'String', false ];
17        $fieldTypes['_name'] = [ 'String', false ];
18        $fieldTypes['_startDate'] = [ 'Date', false ];
19        $fieldTypes['_endDate'] = [ 'Date', false ];
20        $fieldTypes['_progress'] = [ 'Float', false ];
21        $fieldTypes['_parent'] = [ 'String', false ];
22        $fieldTypes['_linksToBB'] = [ 'String', true ];
23        $fieldTypes['_linksToBF'] = [ 'String', true ];
24        $fieldTypes['_linksToFB'] = [ 'String', true ];
25        $fieldTypes['_linksToFF'] = [ 'String', true ];
26
27        $tableSchema = new CargoTableSchema();
28        foreach ( $fieldTypes as $field => $fieldVals ) {
29            list( $type, $isList ) = $fieldVals;
30            $fieldDesc = new CargoFieldDescription();
31            $fieldDesc->mType = $type;
32            if ( $isList ) {
33                $fieldDesc->mIsList = true;
34                $fieldDesc->setDelimiter( '|' );
35            }
36            $tableSchema->mFieldDescriptions[$field] = $fieldDesc;
37        }
38
39        return $tableSchema;
40    }
41
42    public static function storeGanttValues( $title, $createReplacement ) {
43        if ( $title == null ) {
44            return;
45        }
46
47        $tableName = $createReplacement ? '_ganttData__NEXT' : '_ganttData';
48
49        // If this table does not exist, getTableSchemas() will
50        // throw an error.
51        try {
52            $tableSchemas = CargoUtils::getTableSchemas( [ $tableName ] );
53        } catch ( MWException $e ) {
54            return;
55        }
56
57        $revisionRecord = MediaWiki\MediaWikiServices::getInstance()->getRevisionLookup()->getRevisionByTitle( $title );
58        $role = MediaWiki\Revision\SlotRecord::MAIN;
59        $pageText = $revisionRecord->getContent( $role )->getText();
60        $data = json_decode( $pageText );
61
62        $allGanttValues = [];
63        foreach ( $data->data as $task ) {
64            $ganttValues = [
65                '_localID' => $task->id,
66                '_name' => $task->text,
67                '_startDate' => $task->start_date,
68                '_endDate' => $task->end_date,
69                '_progress' => $task->progress
70            ];
71            if ( $task->parent != 0 ) {
72                $ganttValues['_parent'] = $task->parent;
73            }
74            $allGanttValues[] = $ganttValues;
75        }
76
77        foreach ( $data->links as $link ) {
78            $sourceID = $link->source;
79            foreach ( $allGanttValues as $i => $ganttValues ) {
80                if ( $ganttValues['_localID'] == $sourceID ) {
81                    if ( $link->type == 0 ) {
82                        $allGanttValues[$i]['_linksToBB'] = $link->target;
83                    } elseif ( $link->type == 1 ) {
84                        $allGanttValues[$i]['_linksToFF'] = $link->target;
85                    } elseif ( $link->type == 2 ) {
86                        $allGanttValues[$i]['_linksToFB'] = $link->target;
87                    } else { // if ( $link->type == 3 ) {
88                        $allGanttValues[$i]['_linksToBF'] = $link->target;
89                    }
90                }
91            }
92        }
93
94        foreach ( $allGanttValues as $ganttValues ) {
95            CargoStore::storeAllData( $title, $tableName, $ganttValues, $tableSchemas[$tableName] );
96        }
97    }
98
99}