Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 40 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
PostRevisionStorage | |
0.00% |
0 / 40 |
|
0.00% |
0 / 7 |
182 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
joinTable | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
joinField | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getRevType | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
insertRelated | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
42 | |||
updateRelated | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
6 | |||
removeRelated | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace Flow\Data\Storage; |
4 | |
5 | use Flow\DbFactory; |
6 | use Flow\Model\UUID; |
7 | use Flow\Repository\TreeRepository; |
8 | |
9 | /** |
10 | * SQL storage and query for PostRevision instances |
11 | */ |
12 | class PostRevisionStorage extends RevisionStorage { |
13 | /** |
14 | * @var TreeRepository |
15 | */ |
16 | protected $treeRepo; |
17 | |
18 | /** |
19 | * @param DbFactory $dbFactory |
20 | * @param array|false $externalStore List of external store servers available for insert |
21 | * or false to disable. See $wgFlowExternalStore. |
22 | * @param TreeRepository $treeRepo |
23 | */ |
24 | public function __construct( DbFactory $dbFactory, $externalStore, TreeRepository $treeRepo ) { |
25 | parent::__construct( $dbFactory, $externalStore ); |
26 | $this->treeRepo = $treeRepo; |
27 | } |
28 | |
29 | protected function joinTable() { |
30 | return 'flow_tree_revision'; |
31 | } |
32 | |
33 | protected function joinField() { |
34 | return 'tree_rev_id'; |
35 | } |
36 | |
37 | protected function getRevType() { |
38 | return 'post'; |
39 | } |
40 | |
41 | /** |
42 | * @param array|array[] $rows |
43 | * @return array[] |
44 | */ |
45 | protected function insertRelated( array $rows ) { |
46 | if ( !is_array( reset( $rows ) ) ) { |
47 | $rows = [ $rows ]; |
48 | } |
49 | '@phan-var array[] $rows'; |
50 | |
51 | $trees = []; |
52 | foreach ( $rows as $key => $row ) { |
53 | $trees[$key] = $this->splitUpdate( $row, 'tree' ); |
54 | } |
55 | |
56 | if ( $trees ) { |
57 | $dbw = $this->dbFactory->getDB( DB_PRIMARY ); |
58 | $dbw->newInsertQueryBuilder() |
59 | ->insertInto( $this->joinTable() ) |
60 | ->rows( $this->preprocessNestedSqlArray( $trees ) ) |
61 | ->caller( __METHOD__ ) |
62 | ->execute(); |
63 | } |
64 | |
65 | // If this is a brand new root revision it needs to be added to the tree |
66 | // If it has a rev_parent_id then its already a part of the tree |
67 | foreach ( $rows as $row ) { |
68 | if ( $row['rev_parent_id'] === null ) { |
69 | $this->treeRepo->insert( |
70 | UUID::create( $row['tree_rev_descendant_id'] ), |
71 | UUID::create( $row['tree_parent_id'] ) |
72 | ); |
73 | } |
74 | } |
75 | |
76 | return $rows; |
77 | } |
78 | |
79 | /** |
80 | * Topic split will primarily be done through the TreeRepository directly, but |
81 | * we will need to accept updates to the denormalized tree_parent_id field for |
82 | * the new root post |
83 | * @param array $changes |
84 | * @param array $old |
85 | * @return array |
86 | */ |
87 | protected function updateRelated( array $changes, array $old ) { |
88 | $treeChanges = $this->splitUpdate( $changes, 'tree' ); |
89 | |
90 | // no changes to be performed |
91 | if ( !$treeChanges ) { |
92 | return $changes; |
93 | } |
94 | |
95 | $dbw = $this->dbFactory->getDB( DB_PRIMARY ); |
96 | $dbw->newUpdateQueryBuilder() |
97 | ->update( $this->joinTable() ) |
98 | ->set( $this->preprocessSqlArray( $treeChanges ) ) |
99 | ->where( [ 'tree_rev_id' => $old['tree_rev_id'] ] ) |
100 | ->caller( __METHOD__ ) |
101 | ->execute(); |
102 | |
103 | return $changes; |
104 | } |
105 | |
106 | /** |
107 | * this doesn't delete the whole post, it just deletes the revision. |
108 | * The post will *always* exist in the tree structure, its just a tree |
109 | * and we aren't going to re-parent its children; |
110 | * @param array $row |
111 | */ |
112 | protected function removeRelated( array $row ) { |
113 | $this->dbFactory->getDB( DB_PRIMARY )->newDeleteQueryBuilder() |
114 | ->deleteFrom( $this->joinTable() ) |
115 | ->where( $this->preprocessSqlArray( [ $this->joinField() => $row['rev_id'] ] ) ) |
116 | ->caller( __METHOD__ ) |
117 | ->execute(); |
118 | } |
119 | } |