Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 75
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
CargoBPMNData
0.00% covered (danger)
0.00%
0 / 75
0.00% covered (danger)
0.00%
0 / 2
1190
0.00% covered (danger)
0.00%
0 / 1
 getTableSchema
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
12
 storeBPMNValues
0.00% covered (danger)
0.00%
0 / 59
0.00% covered (danger)
0.00%
0 / 1
992
1<?php
2
3/**
4 * Static functions for dealing with the "_bpmnData" table, which stores data
5 * produced by the Flex Diagrams extension.
6 *
7 * @author Yaron Koren
8 */
9class CargoBPMNData {
10
11    /**
12     * Set the schema.
13     */
14    public static function getTableSchema() {
15        $fieldTypes = [];
16        $fieldTypes['_BPMNID'] = [ 'String', false ];
17        $fieldTypes['_name'] = [ 'String', false ];
18        $fieldTypes['_type'] = [ 'String', false ];
19        $fieldTypes['_connectsTo'] = [ 'String', true ];
20        $fieldTypes['_annotation'] = [ 'Text', false ];
21
22        $tableSchema = new CargoTableSchema();
23        foreach ( $fieldTypes as $field => $fieldVals ) {
24            [ $type, $isList ] = $fieldVals;
25            $fieldDesc = new CargoFieldDescription();
26            $fieldDesc->mType = $type;
27            if ( $isList ) {
28                $fieldDesc->mIsList = true;
29                $fieldDesc->setDelimiter( '|' );
30            }
31            $tableSchema->mFieldDescriptions[$field] = $fieldDesc;
32        }
33
34        return $tableSchema;
35    }
36
37    public static function storeBPMNValues( $title, $createReplacement ) {
38        if ( $title == null ) {
39            return;
40        }
41
42        $tableName = $createReplacement ? '_bpmnData__NEXT' : '_bpmnData';
43
44        // If this table does not exist, getTableSchemas() will
45        // throw an error.
46        try {
47            $tableSchemas = CargoUtils::getTableSchemas( [ $tableName ] );
48        } catch ( MWException $e ) {
49            return;
50        }
51
52        $revisionRecord = MediaWiki\MediaWikiServices::getInstance()->getRevisionLookup()->getRevisionByTitle( $title );
53        $role = MediaWiki\Revision\SlotRecord::MAIN;
54        $pageText = $revisionRecord->getContent( $role )->getText();
55        $xml = new SimpleXMLElement( $pageText );
56
57        $allBPMNValues = [];
58        $annotations = [];
59        $associations = [];
60        foreach ( $xml->children( 'bpmn', true ) as $key => $value ) {
61            if ( $key == 'process' ) {
62                foreach ( $value->children( 'bpmn', true ) as $k2 => $v2 ) {
63                    if ( in_array( $k2, [ 'task', 'exclusiveGateway', 'sequenceFlow', 'startEvent', 'endEvent' ] ) ) {
64                        $bpmnValues = [ '_type' => $k2, '_connectsTo' => [] ];
65                        foreach ( $v2->attributes() as $ak1 => $av1 ) {
66                            if ( $ak1 == 'id' ) {
67                                $bpmnValues['_BPMNID'] = (string)$av1;
68                            } elseif ( $ak1 == 'name' ) {
69                                $bpmnValues['_name'] = (string)$av1;
70                            } elseif ( $k2 == 'sequenceFlow' && $ak1 == 'targetRef' ) {
71                                $bpmnValues['_connectsTo'][] = (string)$av1;
72                            }
73                        }
74                        foreach ( $v2->children( 'bpmn', true ) as $k3 => $v3 ) {
75                            if ( $k3 == 'outgoing' ) {
76                                $bpmnValues['_connectsTo'][] = (string)$v3;
77                            }
78                        }
79                        $allBPMNValues[] = $bpmnValues;
80                    } elseif ( $k2 == 'textAnnotation' ) {
81                        $curAnnotation = [];
82                        foreach ( $v2->attributes() as $ak1 => $av1 ) {
83                            if ( $ak1 == 'id' ) {
84                                $curAnnotation['id'] = (string)$av1;
85                            }
86                        }
87                        foreach ( $v2->children( 'bpmn', true ) as $k3 => $v3 ) {
88                            if ( $k3 == 'text' ) {
89                                $curAnnotation['text'] = (string)$v3;
90                            }
91                        }
92                        $annotations[] = $curAnnotation;
93                    } elseif ( $k2 == 'association' ) {
94                        $curAssociation = [];
95                        foreach ( $v2->attributes() as $ak1 => $av1 ) {
96                            if ( $ak1 == 'sourceRef' ) {
97                                $curAssociation['sourceRef'] = (string)$av1;
98                            } elseif ( $ak1 == 'targetRef' ) {
99                                $curAssociation['targetRef'] = (string)$av1;
100                            }
101                        }
102                        $associations[] = $curAssociation;
103                    }
104                }
105            }
106        }
107
108        foreach ( $associations as $association ) {
109            // Find actual text.
110            foreach ( $annotations as $annotation ) {
111                if ( $association['targetRef'] == $annotation['id'] ) {
112                    $annotationText = $annotation['text'];
113                    break;
114                }
115            }
116            if ( $annotationText == null ) {
117                continue;
118            }
119            foreach ( $allBPMNValues as $i => $bpmnValues ) {
120                if ( $association['sourceRef'] == $bpmnValues['_BPMNID'] ) {
121                    $allBPMNValues[$i]['_annotation'] = $annotationText;
122                }
123            }
124        }
125
126        foreach ( $allBPMNValues as $bpmnValues ) {
127            $bpmnValues['_connectsTo'] = implode( '|', $bpmnValues['_connectsTo'] );
128            CargoStore::storeAllData( $title, $tableName, $bpmnValues, $tableSchemas[$tableName] );
129        }
130    }
131
132}