Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 53
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
text_reparser
0.00% covered (danger)
0.00%
0 / 53
0.00% covered (danger)
0.00%
0 / 4
156
0.00% covered (danger)
0.00%
0 / 1
 depends_on
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 effectively_installed
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 update_data
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
2
 reparse
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 1
90
1<?php
2/**
3 *
4 * This file is part of the phpBB Forum Software package.
5 *
6 * @copyright (c) phpBB Limited <https://www.phpbb.com>
7 * @license GNU General Public License, version 2 (GPL-2.0)
8 *
9 * For full copyright and license information, please see
10 * the docs/CREDITS.txt file.
11 *
12 */
13
14namespace phpbb\db\migration\data\v320;
15
16use phpbb\textreparser\manager;
17use phpbb\textreparser\reparser_interface;
18
19class text_reparser extends \phpbb\db\migration\container_aware_migration
20{
21    public static function depends_on()
22    {
23        return array(
24            '\phpbb\db\migration\data\v310\contact_admin_form',
25            '\phpbb\db\migration\data\v320\allowed_schemes_links',
26        );
27    }
28
29    public function effectively_installed()
30    {
31        return isset($this->config['reparse_lock']);
32    }
33
34    public function update_data()
35    {
36        return array(
37            array('config.add', array('reparse_lock', 0, true)),
38            array('config.add', array('text_reparser.pm_text_cron_interval', 10)),
39            array('config.add', array('text_reparser.pm_text_last_cron', 0)),
40            array('config.add', array('text_reparser.poll_option_cron_interval', 10)),
41            array('config.add', array('text_reparser.poll_option_last_cron', 0)),
42            array('config.add', array('text_reparser.poll_title_cron_interval', 10)),
43            array('config.add', array('text_reparser.poll_title_last_cron', 0)),
44            array('config.add', array('text_reparser.post_text_cron_interval', 10)),
45            array('config.add', array('text_reparser.post_text_last_cron', 0)),
46            array('config.add', array('text_reparser.user_signature_cron_interval', 10)),
47            array('config.add', array('text_reparser.user_signature_last_cron', 0)),
48            array('custom', array(array($this, 'reparse'))),
49        );
50    }
51
52    public function reparse($resume_data)
53    {
54        /** @var manager $reparser_manager */
55        $reparser_manager = $this->container->get('text_reparser.manager');
56
57        if (!is_array($resume_data))
58        {
59            /** @var reparser_interface[] $reparsers */
60            $reparsers = $this->container->get('text_reparser_collection');
61
62            // Initialize all reparsers
63            foreach ($reparsers as $name => $reparser)
64            {
65                $reparser_manager->update_resume_data($name, 1, $reparser->get_max_id(), 100);
66            }
67        }
68
69        // Sometimes a cron job is too much
70        $limit = 100;
71        $fast_reparsers = array(
72            'text_reparser.contact_admin_info',
73            'text_reparser.forum_description',
74            'text_reparser.forum_rules',
75            'text_reparser.group_description',
76        );
77
78        if (!is_array($resume_data))
79        {
80            $resume_data = array(
81                'reparser'    => 0,
82                'current'    => $this->container->get($fast_reparsers[0])->get_max_id(),
83            );
84        }
85
86        $fast_reparsers_size = count($fast_reparsers);
87        $processed_records = 0;
88        while ($processed_records < $limit && $resume_data['reparser'] < $fast_reparsers_size)
89        {
90            $reparser = $this->container->get($fast_reparsers[$resume_data['reparser']]);
91
92            // New reparser
93            if ($resume_data['current'] === 0)
94            {
95                $resume_data['current'] = $reparser->get_max_id();
96            }
97
98            $start = max(1, $resume_data['current'] + 1 - ($limit - $processed_records));
99            $end = max(1, $resume_data['current']);
100            $reparser->reparse_range($start, $end);
101
102            $processed_records += $end - $start + 1;
103            $resume_data['current'] = $start - 1;
104
105            if ($start === 1)
106            {
107                // Prevent CLI command from running these reparsers again
108                $reparser_manager->update_resume_data($fast_reparsers[$resume_data['reparser']], 1, 0, $limit);
109
110                $resume_data['reparser']++;
111            }
112        }
113
114        if ($resume_data['reparser'] === $fast_reparsers_size)
115        {
116            return true;
117        }
118
119        return $resume_data;
120    }
121}