1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the SVN-Buddy library. |
4
|
|
|
* For the full copyright and license information, please view |
5
|
|
|
* the LICENSE file that was distributed with this source code. |
6
|
|
|
* |
7
|
|
|
* @copyright Alexander Obuhovich <[email protected]> |
8
|
|
|
* @link https://github.com/console-helpers/svn-buddy |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace ConsoleHelpers\SVNBuddy\Repository\RevisionLog\Plugin; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
use Aura\Sql\ExtendedPdoInterface; |
15
|
|
|
use ConsoleHelpers\SVNBuddy\Repository\RevisionLog\RepositoryFiller; |
16
|
|
|
use ConsoleHelpers\SVNBuddy\Repository\RevisionLog\RevisionLog; |
17
|
|
|
|
18
|
|
|
abstract class AbstractPlugin implements IPlugin |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Database. |
23
|
|
|
* |
24
|
|
|
* @var ExtendedPdoInterface |
25
|
|
|
*/ |
26
|
|
|
protected $database; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Repository filler. |
30
|
|
|
* |
31
|
|
|
* @var RepositoryFiller |
32
|
|
|
*/ |
33
|
|
|
protected $repositoryFiller; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Revision log. |
37
|
|
|
* |
38
|
|
|
* @var RevisionLog |
39
|
|
|
*/ |
40
|
|
|
protected $revisionLog; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Parsing statistics. |
44
|
|
|
* |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
private $_statistics = array(); |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Creates plugin instance. |
51
|
|
|
* |
52
|
|
|
* @param ExtendedPdoInterface $database Database. |
53
|
|
|
* @param RepositoryFiller $repository_filler Repository filler. |
54
|
|
|
*/ |
55
|
117 |
|
public function __construct(ExtendedPdoInterface $database, RepositoryFiller $repository_filler) |
56
|
|
|
{ |
57
|
117 |
|
$this->database = $database; |
58
|
117 |
|
$this->repositoryFiller = $repository_filler; |
59
|
|
|
|
60
|
117 |
|
$this->_resetParsingStatistics(); |
61
|
117 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Resets parsing statistics. |
65
|
|
|
* |
66
|
|
|
* @return void |
67
|
|
|
*/ |
68
|
117 |
|
private function _resetParsingStatistics() |
69
|
|
|
{ |
70
|
117 |
|
$this->_statistics = array(); |
71
|
|
|
|
72
|
117 |
|
foreach ( $this->defineStatisticTypes() as $parsing_statistic_type ) { |
73
|
108 |
|
$this->_statistics[$parsing_statistic_type] = 0; |
74
|
117 |
|
} |
75
|
117 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Hook, that is called before "RevisionLog::refresh" method call. |
79
|
|
|
* |
80
|
|
|
* @return void |
81
|
|
|
*/ |
82
|
72 |
|
public function whenDatabaseReady() |
83
|
|
|
{ |
84
|
|
|
|
85
|
72 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Records parsing statistics. |
89
|
|
|
* |
90
|
|
|
* @param string $type Type. |
91
|
|
|
* @param integer $to_add Number to add. |
92
|
|
|
* |
93
|
|
|
* @return void |
94
|
|
|
*/ |
95
|
30 |
|
protected function recordStatistic($type, $to_add = 1) |
96
|
|
|
{ |
97
|
30 |
|
$this->_statistics[$type] += $to_add; |
98
|
30 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Returns parsing statistics. |
102
|
|
|
* |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
21 |
|
public function getStatistics() |
106
|
|
|
{ |
107
|
21 |
|
return $this->_statistics; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Adds results for missing revisions. |
112
|
|
|
* |
113
|
|
|
* @param array $revisions Revisions. |
114
|
|
|
* @param array $results Results. |
115
|
|
|
* |
116
|
|
|
* @return array |
117
|
|
|
*/ |
118
|
4 |
|
protected function addMissingResults(array $revisions, array $results) |
119
|
|
|
{ |
120
|
4 |
|
foreach ( $this->_getMissingRevisions($revisions, $results) as $missing_revision ) { |
121
|
4 |
|
$results[$missing_revision] = array(); |
122
|
4 |
|
} |
123
|
|
|
|
124
|
4 |
|
return $results; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Adds results for missing revisions. |
129
|
|
|
* |
130
|
|
|
* @param array $revisions Revisions. |
131
|
|
|
* @param array $results Results. |
132
|
|
|
* |
133
|
|
|
* @return void |
134
|
|
|
* @throws \InvalidArgumentException When some revisions are missing in results. |
135
|
|
|
*/ |
136
|
4 |
|
protected function assertNoMissingRevisions(array $revisions, array $results) |
137
|
|
|
{ |
138
|
4 |
|
$missing_revisions = $this->_getMissingRevisions($revisions, $results); |
139
|
|
|
|
140
|
4 |
|
if ( !$missing_revisions ) { |
141
|
2 |
|
return; |
142
|
|
|
} |
143
|
|
|
|
144
|
2 |
|
throw new \InvalidArgumentException(sprintf( |
145
|
2 |
|
'Revision(-s) "%s" not found by "%s" plugin.', |
146
|
2 |
|
implode('", "', $missing_revisions), |
147
|
2 |
|
$this->getName() |
148
|
2 |
|
)); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Returns revisions, that are missing in results. |
153
|
|
|
* |
154
|
|
|
* @param array $revisions Revisions. |
155
|
|
|
* @param array $results Results. |
156
|
|
|
* |
157
|
|
|
* @return array |
158
|
|
|
*/ |
159
|
8 |
|
private function _getMissingRevisions(array $revisions, array $results) |
160
|
|
|
{ |
161
|
8 |
|
return array_diff($revisions, array_keys($results)); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Returns last revision processed by plugin. |
166
|
|
|
* |
167
|
|
|
* @return integer |
168
|
|
|
*/ |
169
|
49 |
|
public function getLastRevision() |
170
|
|
|
{ |
171
|
|
|
$sql = 'SELECT LastRevision |
172
|
|
|
FROM PluginData |
173
|
49 |
|
WHERE Name = :name'; |
174
|
49 |
|
$last_revision = $this->database->fetchValue($sql, array('name' => $this->getName())); |
175
|
|
|
|
176
|
49 |
|
return $last_revision !== false ? $last_revision : 0; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Sets last revision processed by plugin. |
181
|
|
|
* |
182
|
|
|
* @param integer $last_revision Last revision. |
183
|
|
|
* |
184
|
|
|
* @return void |
185
|
|
|
*/ |
186
|
38 |
|
protected function setLastRevision($last_revision) |
187
|
|
|
{ |
188
|
|
|
$sql = 'REPLACE INTO PluginData (Name, LastRevision) |
189
|
38 |
|
VALUES (:name, :last_revision)'; |
190
|
38 |
|
$this->database->perform($sql, array('name' => $this->getName(), 'last_revision' => $last_revision)); |
191
|
38 |
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Finds project by path. |
195
|
|
|
* |
196
|
|
|
* @param string $path Path. |
197
|
|
|
* |
198
|
|
|
* @return integer |
199
|
|
|
* @throws \InvalidArgumentException When project can't be found. |
200
|
|
|
*/ |
201
|
35 |
|
protected function getProject($path) |
202
|
|
|
{ |
203
|
|
|
$sql = 'SELECT Id |
204
|
|
|
FROM Projects |
205
|
35 |
|
WHERE Path = :path'; |
206
|
35 |
|
$project_id = $this->database->fetchValue($sql, array('path' => $path)); |
207
|
|
|
|
208
|
35 |
|
if ( $project_id === false ) { |
209
|
5 |
|
throw new \InvalidArgumentException('The project with "' . $path . '" path not found.'); |
210
|
|
|
} |
211
|
|
|
|
212
|
30 |
|
return $project_id; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Automatically free memory, when >200MB is used. |
217
|
|
|
* |
218
|
|
|
* @return void |
219
|
|
|
* |
220
|
|
|
* @codeCoverageIgnore |
221
|
|
|
*/ |
222
|
|
|
protected function freeMemoryAutomatically() |
223
|
|
|
{ |
224
|
|
|
$memory_usage = memory_get_usage(); |
225
|
|
|
|
226
|
|
|
if ( $memory_usage > 200 * 1024 * 1024 ) { |
227
|
|
|
$this->freeMemoryManually(); |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Frees consumed memory manually. |
233
|
|
|
* |
234
|
|
|
* @return void |
235
|
|
|
* |
236
|
|
|
* @codeCoverageIgnore |
237
|
|
|
*/ |
238
|
|
|
protected function freeMemoryManually() |
239
|
|
|
{ |
240
|
|
|
$profiler = $this->database->getProfiler(); |
241
|
|
|
|
242
|
|
|
if ( is_object($profiler) && $profiler->isActive() ) { |
243
|
|
|
$profiler->resetProfiles(); |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Sets reference to revision log. |
249
|
|
|
* |
250
|
|
|
* @param RevisionLog $revision_log Revision log. |
251
|
|
|
* |
252
|
|
|
* @return void |
253
|
|
|
*/ |
254
|
2 |
|
public function setRevisionLog(RevisionLog $revision_log) |
255
|
|
|
{ |
256
|
2 |
|
$this->revisionLog = $revision_log; |
257
|
2 |
|
} |
258
|
|
|
|
259
|
|
|
} |
260
|
|
|
|