Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 41 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| PostRevisionStorage | |
0.00% |
0 / 41 |
|
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 / 20 |
|
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 | $queryBuilder = $dbw->newInsertQueryBuilder() |
| 59 | ->insertInto( $this->joinTable() ) |
| 60 | ->rows( $this->preprocessNestedSqlArray( $trees ) ) |
| 61 | ->caller( __METHOD__ ); |
| 62 | DbStorage::maybeSetInsertIgnore( $queryBuilder ); |
| 63 | $queryBuilder->execute(); |
| 64 | } |
| 65 | |
| 66 | // If this is a brand new root revision it needs to be added to the tree |
| 67 | // If it has a rev_parent_id then its already a part of the tree |
| 68 | foreach ( $rows as $row ) { |
| 69 | if ( $row['rev_parent_id'] === null ) { |
| 70 | $this->treeRepo->insert( |
| 71 | UUID::create( $row['tree_rev_descendant_id'] ), |
| 72 | UUID::create( $row['tree_parent_id'] ) |
| 73 | ); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | return $rows; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Topic split will primarily be done through the TreeRepository directly, but |
| 82 | * we will need to accept updates to the denormalized tree_parent_id field for |
| 83 | * the new root post |
| 84 | * @param array $changes |
| 85 | * @param array $old |
| 86 | * @return array |
| 87 | */ |
| 88 | protected function updateRelated( array $changes, array $old ) { |
| 89 | $treeChanges = $this->splitUpdate( $changes, 'tree' ); |
| 90 | |
| 91 | // no changes to be performed |
| 92 | if ( !$treeChanges ) { |
| 93 | return $changes; |
| 94 | } |
| 95 | |
| 96 | $dbw = $this->dbFactory->getDB( DB_PRIMARY ); |
| 97 | $dbw->newUpdateQueryBuilder() |
| 98 | ->update( $this->joinTable() ) |
| 99 | ->set( $this->preprocessSqlArray( $treeChanges ) ) |
| 100 | ->where( [ 'tree_rev_id' => $old['tree_rev_id'] ] ) |
| 101 | ->caller( __METHOD__ ) |
| 102 | ->execute(); |
| 103 | |
| 104 | return $changes; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * this doesn't delete the whole post, it just deletes the revision. |
| 109 | * The post will *always* exist in the tree structure, its just a tree |
| 110 | * and we aren't going to re-parent its children; |
| 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 | } |