|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * |
| 4 | + * Ideas extension for 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 | + */ |
| 10 | + |
| 11 | +namespace phpbb\ideas\migrations; |
| 12 | + |
| 13 | +class m11_reparse_old_ideas extends \phpbb\db\migration\container_aware_migration |
| 14 | +{ |
| 15 | + /** |
| 16 | + * {@inheritDoc} |
| 17 | + */ |
| 18 | + public static function depends_on() |
| 19 | + { |
| 20 | + return [ |
| 21 | + '\phpbb\ideas\migrations\m1_initial_schema', |
| 22 | + '\phpbb\ideas\migrations\m6_migrate_old_tables', |
| 23 | + '\phpbb\ideas\migrations\m7_drop_old_tables', |
| 24 | + '\phpbb\ideas\migrations\m8_implemented_version', |
| 25 | + '\phpbb\ideas\migrations\m10_update_idea_schema', |
| 26 | + ]; |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * {@inheritDoc} |
| 31 | + */ |
| 32 | + public function effectively_installed() |
| 33 | + { |
| 34 | + /** @var \phpbb\textreparser\manager $reparser_manager */ |
| 35 | + $reparser_manager = $this->container->get('text_reparser.manager'); |
| 36 | + |
| 37 | + return !empty($reparser_manager->get_resume_data('phpbb.ideas.text_reparser.clean_old_ideas') || !$this->bbcode_exists('idea')); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * @inheritDoc |
| 42 | + */ |
| 43 | + public function update_data() |
| 44 | + { |
| 45 | + return [ |
| 46 | + ['custom', [[$this, 'reparse']]], |
| 47 | + ]; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Run the clean old ideas reparser |
| 52 | + * |
| 53 | + * @param int $current An idea identifier |
| 54 | + * @return bool|int An idea identifier or true if finished |
| 55 | + */ |
| 56 | + public function reparse($current = 0) |
| 57 | + { |
| 58 | + /** @var \phpbb\textreparser\manager $reparser_manager */ |
| 59 | + $reparser_manager = $this->container->get('text_reparser.manager'); |
| 60 | + |
| 61 | + /** @var \phpbb\textformatter\s9e\utils $text_formatter_utils */ |
| 62 | + $text_formatter_utils = $this->container->get('text_formatter.utils'); |
| 63 | + |
| 64 | + $reparser = new \phpbb\ideas\textreparser\plugins\clean_old_ideas( |
| 65 | + $this->db, |
| 66 | + $text_formatter_utils, |
| 67 | + $this->container->getParameter('tables.posts'), |
| 68 | + $this->container->getParameter('tables.topics'), |
| 69 | + $this->container->getParameter('core.table_prefix') . 'ideas_ideas' |
| 70 | + ); |
| 71 | + |
| 72 | + if (empty($current)) |
| 73 | + { |
| 74 | + $current = $reparser->get_max_id(); |
| 75 | + } |
| 76 | + |
| 77 | + $limit = 50; // lets keep the reparsing conservative |
| 78 | + $start = max(1, $current + 1 - $limit); |
| 79 | + $end = max(1, $current); |
| 80 | + |
| 81 | + $reparser->reparse_range($start, $end); |
| 82 | + |
| 83 | + $current = $start - 1; |
| 84 | + |
| 85 | + if ($current === 0) |
| 86 | + { |
| 87 | + // Prevent CLI command from running this reparser again |
| 88 | + $reparser_manager->update_resume_data('phpbb.ideas.text_reparser.clean_old_ideas', 1, 0, $limit); |
| 89 | + |
| 90 | + return true; |
| 91 | + } |
| 92 | + |
| 93 | + return $current; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Check if a bbcode exists |
| 98 | + * |
| 99 | + * @param string $tag BBCode's tag |
| 100 | + * @return bool True if bbcode exists, false if not |
| 101 | + */ |
| 102 | + public function bbcode_exists($tag) |
| 103 | + { |
| 104 | + $sql = 'SELECT bbcode_id |
| 105 | + FROM ' . $this->table_prefix . "bbcodes |
| 106 | + WHERE LOWER(bbcode_tag) = '" . $this->db->sql_escape(strtolower($tag)) . "' |
| 107 | + OR LOWER(bbcode_tag) = '" . $this->db->sql_escape(strtolower($tag)) . "='"; |
| 108 | + $result = $this->db->sql_query_limit($sql, 1); |
| 109 | + $bbcode_id = $this->db->sql_fetchfield('bbcode_id'); |
| 110 | + $this->db->sql_freeresult($result); |
| 111 | + |
| 112 | + return $bbcode_id !== false; |
| 113 | + } |
| 114 | +} |
0 commit comments