-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcron.php
More file actions
383 lines (308 loc) · 11.8 KB
/
cron.php
File metadata and controls
383 lines (308 loc) · 11.8 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
<?php
/**
* This software is intended for use with Oxwall Free Community Software http://www.oxwall.org/ and is
* licensed under The BSD license.
* ---
* Copyright (c) 2011, Oxwall Foundation
* All rights reserved.
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
* following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this list of conditions and
* the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* - Neither the name of the Oxwall Foundation nor the names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* Forum cron job.
*
* @author Egor Bulgakov <egor.bulgakov@gmail.com>
* @package ow.ow_plugins.forum
* @since 1.0
*/
class FORUM_Cron extends OW_Cron
{
const TOPICS_DELETE_LIMIT = 5;
const MEDIA_DELETE_LIMIT = 10;
const UPDATE_SEARCH_INDEX_LIFE_TIME = 3600;
const UPDATE_SEARCH_INDEX_MAX_TIME = 10;
public function __construct()
{
parent::__construct();
$this->addJob('topicsDeleteProcess', 1);
$this->addJob('tempTopicsDeleteProcess', 60);
$this->addJob('updateSearchIndex', 1);
}
public function run()
{
}
/**
* Get update search index dao
*
* @return FORUM_BOL_UpdateSearchIndexDao
*/
private function getUpdateSearchIndexDao()
{
return FORUM_BOL_UpdateSearchIndexDao::getInstance();
}
/**
* Update search index
*
* @throws Exception
* @return void
*/
public function updateSearchIndex()
{
$config = OW::getConfig();
$cronBusyValue = (int) $config->getValue('forum', 'update_search_index_cron_busy');
// stop cron task execution
if ( time() < $cronBusyValue)
{
return;
}
// in process
$config->saveConfig('forum', 'update_search_index_cron_busy', time() + self::UPDATE_SEARCH_INDEX_LIFE_TIME);
$maxExecutionTime = time() + self::UPDATE_SEARCH_INDEX_MAX_TIME;
while ( time() < $maxExecutionTime )
{
// do we have any queue
if ( null == ($firstQueue = $this->getUpdateSearchIndexDao()->findFirstQueue()) )
{
break;
}
// process queue
switch ($firstQueue->type)
{
// delete topic
case FORUM_BOL_UpdateSearchIndexDao::DELETE_TOPIC :
$result = $this->deleteTopicFromSearchIndex($firstQueue->entityId);
break;
// update topic
case FORUM_BOL_UpdateSearchIndexDao::UPDATE_TOPIC :
$result = $this->updateTopicInSearchIndex($firstQueue->entityId);
break;
// update topic posts
case FORUM_BOL_UpdateSearchIndexDao::UPDATE_TOPIC_POSTS :
$result = $this->
updateTopicPostsInSearchIndex($firstQueue->entityId, $firstQueue, $maxExecutionTime);
break;
// delete group
case FORUM_BOL_UpdateSearchIndexDao::DELETE_GROUP :
$result = $this->deleteGroupFromSearchIndex($firstQueue->entityId);
break;
// update group
case FORUM_BOL_UpdateSearchIndexDao::UPDATE_GROUP :
$result = $this->
updateGroupInSearchIndex($firstQueue->entityId, $firstQueue, $maxExecutionTime);
break;
default :
$result = true;
}
if ( !$result )
{
break;
}
// task successfully completed
$this->getUpdateSearchIndexDao()->delete($firstQueue);
}
// check the plugin state
$pluginForum = BOL_PluginDao::getInstance()->findPluginByKey('forum');
if ( !$pluginForum || !$pluginForum->isActive )
{
$this->getTextSearchService()->deactivateEntities();
}
// finished
$config->saveConfig('forum', 'update_search_index_cron_busy', 0);
}
/**
* Update group
*
* @param integer $groupId
* @param FORUM_BOL_UpdateSearchIndex $firstQueue
* @param integer $maxExecutionTime
* @return boolean
*/
private function updateGroupInSearchIndex( $groupId, FORUM_BOL_UpdateSearchIndex $firstQueue, $maxExecutionTime )
{
$forumService = FORUM_BOL_ForumService::getInstance();
// get the group info
$group = $forumService->findGroupById($groupId);
if ( $group )
{
$topicPage = 1;
$lastEntityId = $firstQueue->lastEntityId;
// get group's topics
while ( time() < $maxExecutionTime )
{
if ( null == ($topics = $forumService->
getSimpleGroupTopicList($group->id, $topicPage, $lastEntityId)) )
{
return true;
}
// add topics into the group
foreach ($topics as $topic)
{
$this->getTextSearchService()->addTopic($topic);
// remember the last post id
$firstQueue->lastEntityId = $topic->id;
$this->getUpdateSearchIndexDao()->save($firstQueue);
// update topic's posts
FORUM_BOL_UpdateSearchIndexDao::getInstance()->addQueue($topic->id,
FORUM_BOL_UpdateSearchIndexDao::UPDATE_TOPIC_POSTS, FORUM_BOL_UpdateSearchIndexDao::HIGH_PRIORITY);
}
$topicPage++;
}
return false;
}
return true;
}
/**
* Delete group from the search index
*
* @param integer $groupId
* @return boolean
*/
private function deleteGroupFromSearchIndex( $groupId )
{
$this->getTextSearchService()->deleteGroup($groupId);
FORUM_BOL_UpdateSearchIndexDao::getInstance()->addQueue($groupId,
FORUM_BOL_UpdateSearchIndexDao::UPDATE_GROUP, FORUM_BOL_UpdateSearchIndexDao::HIGH_PRIORITY);
return true;
}
/**
* Delete topic from the search index
*
* @param integer $topicId
* @return boolean
*/
private function deleteTopicFromSearchIndex( $topicId )
{
$this->getTextSearchService()->deleteTopic($topicId);
FORUM_BOL_UpdateSearchIndexDao::getInstance()->addQueue($topicId,
FORUM_BOL_UpdateSearchIndexDao::UPDATE_TOPIC, FORUM_BOL_UpdateSearchIndexDao::HIGH_PRIORITY);
return true;
}
/**
* Update topic
*
* @param integer $topicId
* @return boolean
*/
private function updateTopicInSearchIndex( $topicId )
{
$forumService = FORUM_BOL_ForumService::getInstance();
// get the topic info
$topic = $forumService->findTopicById($topicId);
if ( $topic )
{
// add the topic
$this->getTextSearchService()->addTopic($topic);
FORUM_BOL_UpdateSearchIndexDao::getInstance()->addQueue($topicId,
FORUM_BOL_UpdateSearchIndexDao::UPDATE_TOPIC_POSTS, FORUM_BOL_UpdateSearchIndexDao::HIGH_PRIORITY);
}
return true;
}
/**
* Update topic posts
*
* @param integer $topicId
* @param FORUM_BOL_UpdateSearchIndex $firstQueue
* @param integer $maxExecutionTime
* @return boolean
*/
private function updateTopicPostsInSearchIndex( $topicId, FORUM_BOL_UpdateSearchIndex $firstQueue, $maxExecutionTime )
{
$forumService = FORUM_BOL_ForumService::getInstance();
// get the topic info
$topic = $forumService->findTopicById($topicId);
if ( $topic )
{
$postPage = 1;
$lastEntityId = $firstQueue->lastEntityId;
// get topic's post list
while ( time() < $maxExecutionTime )
{
if ( null == ($posts = $forumService->
getSimpleTopicPostList($topic->id, $postPage, $lastEntityId)) )
{
return true;
}
// add posts into the topic
foreach ($posts as $post)
{
$this->getTextSearchService()->saveOrUpdatePost($post, true);
// remember the last post id
$firstQueue->lastEntityId = $post->id;
$this->getUpdateSearchIndexDao()->save($firstQueue);
}
$postPage++;
}
return false;
}
return true;
}
/**
* Get text search service
*
* @return FORUM_BOL_TextSearchService
*/
private function getTextSearchService()
{
return FORUM_BOL_TextSearchService::getInstance();
}
public function tempTopicsDeleteProcess()
{
$forumService = FORUM_BOL_ForumService::getInstance();
$tmpTopics = $forumService->findTemporaryTopics(self::TOPICS_DELETE_LIMIT);
if ( !$tmpTopics )
{
return;
}
foreach ( $tmpTopics as $topic )
{
$forumService->deleteTopic($topic['id']);
}
}
public function topicsDeleteProcess()
{
$config = OW::getConfig();
// check if uninstall is in progress
if ( !$config->getValue('forum', 'uninstall_inprogress') )
{
return;
}
// check if cron queue is not busy
if ( $config->getValue('forum', 'uninstall_cron_busy') )
{
return;
}
if ( !$config->getValue('forum', 'delete_search_index_cron') )
{
$this->getTextSearchService()->deleteAllEntities();
$config->saveConfig('forum', 'delete_search_index_cron', 1);
}
$config->saveConfig('forum', 'uninstall_cron_busy', 1);
$forumService = FORUM_BOL_ForumService::getInstance();
$forumService->deleteTopics(self::TOPICS_DELETE_LIMIT);
$mediaPanelService = BOL_MediaPanelService::getInstance();
$mediaPanelService->deleteImages('forum', self::MEDIA_DELETE_LIMIT);
$config->saveConfig('forum', 'uninstall_cron_busy', 0);
if ( (int) $forumService->countAllTopics() + (int) $mediaPanelService->countGalleryImages('forum') == 0 )
{
$config->saveConfig('forum', 'uninstall_inprogress', 0);
BOL_PluginService::getInstance()->uninstall('forum');
FORUM_BOL_ForumService::getInstance()->setMaintenanceMode(false);
}
}
}