summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/SQLStore/ChangeOp/TableChangeOp.php
blob: 3923b770e838638a187ca3783ec9b4e9ea329c36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php

namespace SMW\SQLStore\ChangeOp;

/**
 * @license GNU GPL v2+
 * @since 2.4
 *
 * @author mwjames
 */
class TableChangeOp {

	const OP_INSERT = 'insert';
	const OP_DELETE = 'delete';

	/**
	 * @var string
	 */
	private $tableName;

	/**
	 * @var array
	 */
	private $changeOps;

	/**
	 * @since 2.4
	 *
	 * @param string $tableName
	 * @param array $changeOps
	 */
	public function __construct( $tableName, array $changeOps ) {
		$this->tableName = $tableName;
		$this->changeOps = $changeOps;
	}

	/**
	 * @since 2.4
	 *
	 * @return string
	 */
	public function getTableName() {
		return $this->tableName;
	}

	/**
	 * @since 2.4
	 *
	 * @return boolean
	 */
	public function isFixedPropertyOp() {
		return isset( $this->changeOps['property'] );
	}

	/**
	 * @since 3.0
	 *
	 * @param string $field
	 *
	 * @return null|string
	 */
	public function getFixedPropertyValByField( $field ) {

		if ( $this->isFixedPropertyOp() && isset( $this->changeOps['property'][$field] ) ) {
			return $this->changeOps['property'][$field];
		}

		return null;
	}

	/**
	 * @deprecated since 3.0, use TableChangeOp::getFixedPropertyValByField
	 * @since 2.4
	 *
	 * @param string $field
	 *
	 * @return null|string
	 */
	public function getFixedPropertyValueBy( $field ) {
		return $this->getFixedPropertyValByField( $field );
	}

	/**
	 * @since 2.4
	 *
	 * @param string $opType
	 *
	 * @return boolean
	 */
	public function hasChangeOp( $opType ) {
		return isset( $this->changeOps[$opType] );
	}

	/**
	 * @since 2.4
	 *
	 * @param string|null $opType
	 * @param array $filter
	 *
	 * @return FieldChangeOp[]|[]
	 */
	public function getFieldChangeOps( $opType = null, $filter = [] ) {

		if ( $opType !== null && !$this->hasChangeOp( $opType ) ) {
			return [];
		}

		$fieldOps = [];
		$changeOps = $this->changeOps;

		if ( $opType !== null ) {
			$changeOps = $this->changeOps[$opType];
		} elseif ( !isset( $this->changeOps[self::OP_DELETE] ) && !isset( $this->changeOps[self::OP_INSERT] ) )  {
			$changeOps = $this->changeOps;
		} else  {
			return array_merge(
				$this->getFieldChangeOps( self::OP_DELETE, $filter ),
				$this->getFieldChangeOps( self::OP_INSERT, $filter )
			);
		}

		unset( $changeOps['property'] );

		foreach ( $changeOps as $changeOp ) {

			// Filter defined as: [ 's_id' => [ 42 => true, 1001 => true ] ]
			if ( isset( $filter['s_id' ] ) && isset( $changeOp['s_id'] ) && isset( $filter['s_id'][$changeOp['s_id']] ) ) {
				continue;
			}

			if ( isset( $this->changeOps['property'] ) ) {
				$changeOp['p_id'] = $this->changeOps['property']['p_id'];
			}

			$fieldOps[] = new FieldChangeOp( $changeOp, $opType );
		}

		return $fieldOps;
	}

	/**
	 * @since 3.0
	 *
	 * @return string
	 */
	public function __toString() {
		return json_encode( $this->toArray() );
	}

	/**
	 * @since 3.0
	 *
	 * @return array
	 */
	public function toArray() {
		return [ $this->tableName => $this->changeOps ];
	}

}