ApiDeleteWatchlistGroup   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 195
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 21
lcom 0
cbo 0
dl 0
loc 195
ccs 0
cts 143
cp 0
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A execute() 0 21 5
D deleteGroup() 0 123 10
A getAllowedParams() 0 9 1
A getParamDescription() 0 5 1
A getDescription() 0 5 1
A getExamples() 0 5 1
A getVersion() 0 3 1
1
<?php
2
3
/**
4
 * API module to delete semantic watchlist groups.
5
 *
6
 * @since 0.1
7
 *
8
 * @file ApiDeleteWatchlistGroup.php
9
 * @ingroup SemanticWatchlist
10
 * @ingroup API
11
 *
12
 * @licence GNU GPL v3+
13
 * @author Jeroen De Dauw < [email protected] >
14
 * 
15
 * TODO: delete changes
16
 */
17
class ApiDeleteWatchlistGroup extends ApiBase {
18
	
19
	public function __construct( $main, $action ) {
20
		parent::__construct( $main, $action );
21
	}
22
	
23
	public function execute() {
24
		$user = $this->getUser();
25
		
26
		if ( !$user->isAllowed( 'semanticwatchgroups' ) || $user->isBlocked() ) {
27
			$this->dieUsageMsg( array( 'badaccess-groups' ) );
28
		}
29
		
30
		$params = $this->extractRequestParams();
31
		
32
		$everythingOk = true;
33
		
34
		foreach ( $params['ids'] as $id ) {
35
			$everythingOk = $this->deleteGroup( $id ) && $everythingOk;
36
		}
37
		
38
		$this->getResult()->addValue(
39
			null,
40
			'success',
41
			$everythingOk
42
		);
43
	}
44
	
45
	/**
46
	 * Delete the group with specified ID, and
47
	 * all linked data not used by other groups.
48
	 * 
49
	 * @since 0.1
50
	 * 
51
	 * @param integer $groupId
52
	 * 
53
	 * @return boolean Success indicator
54
	 */
55
	protected function deleteGroup( $groupId ) {
56
		$everythingOk = true;
57
		
58
		$dbr = wfGetDB( DB_REPLICA );
59
		
60
		$setsForGroup = $dbr->select(
61
			'swl_sets_per_group',
62
			array( 'spg_set_id' ),
63
			array(
64
				'spg_group_id' => $groupId,
65
			)
66
		);
67
		
68
		foreach ( $setsForGroup as $set ) {
69
			$changes = $dbr->select(
70
				'swl_changes',
71
				array( 'change_id' ),
72
				array( 'change_set_id' => $set->spg_set_id )
73
			);
74
			
75
			foreach ( $changes as $change ) {
76
				$dbr->select(
77
					'swl_changes',
78
					array( 'change_id' ),
79
					array( 'change_set_id' => $set->spg_set_id )
80
				);
81
			}
82
		}
83
		
84
		// Find all edits linked to this group.
85
		$editsForGroup = $dbr->select(
86
			array( 'swl_sets_per_group', 'swl_sets_per_edit' ),
87
			array( 'spe_edit_id' ),
88
			array(
89
				'spg_group_id' => $groupId,
90
			),
91
			'',
92
			array(),
93
			array(
94
				'swl_sets_per_edit' => array( 'INNER JOIN', array( 'spe_set_id=spg_set_id' ) ),
95
			)
96
		);
97
		
98
		$editsToDelete = array();
99
		
100
		// For each linked edit, find all linked groups, and save those with only one (this one).
101
		foreach ( $editsForGroup as $edit ) {
102
			$groupsForEdit = $dbr->select(
103
				array( 'swl_sets_per_edit', 'swl_sets_per_group', 'swl_groups' ),
104
				array( 'spg_group_id' ),
105
				array(
106
					'spe_edit_id' => $edit->spe_edit_id,
107
				),
108
				'',
109
				array(),
110
				array(
111
					'swl_sets_per_group' => array( 'INNER JOIN', array( 'spg_set_id=spe_set_id' ) ),
112
					'swl_groups' => array( 'INNER JOIN', array( 'group_id=spg_group_id' ) ),
113
				)
114
			);
115
			
116
			if ( $dbr->numRows( $groupsForEdit ) < 2 ) {
117
				$editsToDelete[] = $edit->spe_edit_id;
118
			}
119
		}
120
		
121
		$dbw = wfGetDB( DB_MASTER );
122
		$dbw->startAtomic( __METHOD__ );
123
		
124
		// Delete all edits and sets per edits only linked to this group.
125
		foreach ( $editsToDelete as $editId ) {
126
			$dbw->delete(
127
				'swl_edits',
128
				array( 'edit_id' => $editId )
129
			);
130
			
131
			$dbw->delete(
132
				'swl_sets_per_edit',
133
				array( 'spe_edit_id' => $editId )
134
			);
135
		}
136
		
137
		foreach ( $setsForGroup as $set ) {
138
			$dbw->delete(
139
				'swl_sets',
140
				array( 'set_id' => $set->spg_set_id )
141
			);
142
		}
143
		
144
		// Delete sets per group links for this group. 
145
		$result = $dbw->delete(
146
			'swl_sets_per_group',
147
			array( 'spg_group_id' => $groupId )
148
		);
149
150
		if ( $result === false ) {
151
			$everythingOk = false;
152
		}
153
		
154
		// Delete users per group links for this group.
155
		$result = $dbw->delete(
156
			'swl_users_per_group',
157
			array( 'upg_group_id' => $groupId )
158
		);
159
160
		if ( $result === false ) {
161
			$everythingOk = false;
162
		}
163
		
164
		// Delete the actual group.
165
		$result = $dbw->delete(
166
			'swl_groups',
167
			array( 'group_id' => $groupId )
168
		);
169
170
		if ( $result === false ) {
171
			$everythingOk = false;
172
		}
173
		
174
		$dbw->endAtomic( __METHOD__ );
175
176
		return $everythingOk; 
177
	}
178
179
	public function getAllowedParams() {
180
		return array(
181
			'ids' => array(
182
				ApiBase::PARAM_TYPE => 'integer',
183
				ApiBase::PARAM_REQUIRED => true,
184
				ApiBase::PARAM_ISMULTI => true,
185
			),
186
		);
187
	}
188
	
189
	public function getParamDescription() {
190
		return array(
191
			'ids' => 'The IDs of the watchlist groups to delete'
192
		);
193
	}
194
	
195
	public function getDescription() {
196
		return array(
197
			'API module to delete semantic watchlist groups.'
198
		);
199
	}
200
201
	protected function getExamples() {
202
		return array(
203
			'api.php?action=deleteswlgroup&ids=42|34',
204
		);
205
	}	
206
	
207
	public function getVersion() {
208
		return __CLASS__ . ': $Id$';
209
	}		
210
	
211
}
212