Completed
Branch 3.2.x (01dc0a)
by Erwan
03:51
created

v2_0_0::update_data()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
/**
3
*
4
* phpBB Directory extension for the phpBB Forum Software package.
5
*
6
* @copyright (c) 2014 ErnadoO <http://www.phpbb-services.com>
7
* @license GNU General Public License, version 2 (GPL-2.0)
8
*
9
*/
10
11
namespace ernadoo\phpbbdirectory\migrations\v20x;
12
13
class v2_0_0 extends \phpbb\db\migration\container_aware_migration
14
{
15
	/**
16
	* @inheritDoc
17
	*/
18
	static public function depends_on()
19
	{
20
		return array(
21
			'\ernadoo\phpbbdirectory\migrations\v10x\v1_0_0',
22
		);
23
	}
24
25
	/**
26
	* @inheritDoc
27
	*/
28
	public function update_data()
29
	{
30
		return array(
31
			array('custom', array(array($this, 'reparse'))),
32
		);
33
	}
34
35
	public function reparse($resume_data)
36
	{
37
		$limit = 100;
38
		$fast_reparsers = array(
39
			array('\ernadoo\phpbbdirectory\textreparser\plugins\cat_description', 'directory_cats'),
40
			array('\ernadoo\phpbbdirectory\textreparser\plugins\comment_text', 'directory_comments'),
41
			array('\ernadoo\phpbbdirectory\textreparser\plugins\link_description', 'directory_links'),
42
		);
43
44
		if (!is_array($resume_data))
45
		{
46
			$default_reparser = new $fast_reparsers[0][0](
47
				$this->db,
48
				$this->container->getParameter('core.table_prefix') . $fast_reparsers[0][1]);
49
50
			$resume_data = array(
51
				'reparser'	=> 0,
52
				'current'	=> $default_reparser->get_max_id()
53
54
			);
55
		}
56
57
		$fast_reparsers_size = sizeof($fast_reparsers);
58
		$processed_records = 0;
59
		while ($processed_records < $limit && $resume_data['reparser'] < $fast_reparsers_size)
60
		{
61
			$reparser = new $fast_reparsers[$resume_data['reparser']][0](
62
				$this->db,
63
				$this->container->getParameter('core.table_prefix') . $fast_reparsers[$resume_data['reparser']][1]
64
				);
65
66
			// New reparser
67
			if ($resume_data['current'] === 0)
68
			{
69
				$resume_data['current'] = $reparser->get_max_id();
70
			}
71
72
			$start = max(1, $resume_data['current'] + 1 - ($limit - $processed_records));
73
			$end = max(1, $resume_data['current']);
74
			$reparser->reparse_range($start, $end);
75
76
			$processed_records = $end - $start + 1;
77
			$resume_data['current'] = $start - 1;
78
79
			if ($start === 1)
80
			{
81
				// Prevent CLI command from running these reparsers again
82
				$reparser_manager = $this->container->get('text_reparser.manager');
83
				$reparser_manager->update_resume_data($fast_reparsers[$resume_data['reparser']][0], 1, 0, $limit);
84
85
				$resume_data['reparser']++;
86
			}
87
		}
88
89
		if ($resume_data['reparser'] === $fast_reparsers_size)
90
		{
91
			return true;
92
		}
93
94
		return $resume_data;
95
	}
96
}
97