Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
PostRevisionStorage
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 7
156
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 joinTable
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 joinField
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getRevType
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 insertRelated
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
30
 updateRelated
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
6
 removeRelated
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace Flow\Data\Storage;
4
5use Flow\DbFactory;
6use Flow\Model\UUID;
7use Flow\Repository\TreeRepository;
8
9/**
10 * SQL storage and query for PostRevision instances
11 */
12class 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        $dbw = $this->dbFactory->getDB( DB_PRIMARY );
57        $dbw->insert(
58            $this->joinTable(),
59            $this->preprocessNestedSqlArray( $trees ),
60            __METHOD__
61        );
62
63        // If this is a brand new root revision it needs to be added to the tree
64        // If it has a rev_parent_id then its already a part of the tree
65        foreach ( $rows as $row ) {
66            if ( $row['rev_parent_id'] === null ) {
67                $this->treeRepo->insert(
68                    UUID::create( $row['tree_rev_descendant_id'] ),
69                    UUID::create( $row['tree_parent_id'] )
70                );
71            }
72        }
73
74        return $rows;
75    }
76
77    /**
78     * Topic split will primarily be done through the TreeRepository directly,  but
79     * we will need to accept updates to the denormalized tree_parent_id field for
80     * the new root post
81     * @param array $changes
82     * @param array $old
83     * @return array
84     */
85    protected function updateRelated( array $changes, array $old ) {
86        $treeChanges = $this->splitUpdate( $changes, 'tree' );
87
88        // no changes to be performed
89        if ( !$treeChanges ) {
90            return $changes;
91        }
92
93        $dbw = $this->dbFactory->getDB( DB_PRIMARY );
94        $dbw->update(
95            $this->joinTable(),
96            $this->preprocessSqlArray( $treeChanges ),
97            [ 'tree_rev_id' => $old['tree_rev_id'] ],
98            __METHOD__
99        );
100
101        return $changes;
102    }
103
104    /**
105     * this doesn't delete the whole post, it just deletes the revision.
106     * The post will *always* exist in the tree structure, its just a tree
107     * and we aren't going to re-parent its children;
108     * @param array $row
109     * @return bool|\Wikimedia\Rdbms\IResultWrapper
110     */
111    protected function removeRelated( array $row ) {
112        return $this->dbFactory->getDB( DB_PRIMARY )->delete(
113            $this->joinTable(),
114            $this->preprocessSqlArray( [ $this->joinField() => $row['rev_id'] ] ),
115            __METHOD__
116        );
117    }
118}