Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
28 / 28 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
row_based_plugin | |
100.00% |
28 / 28 |
|
100.00% |
5 / 5 |
7 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
get_columns | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
get_max_id | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
get_records_by_range | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
get_records_by_range_query | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
3 | |||
save_record | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 |
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 | |
14 | namespace phpbb\textreparser; |
15 | |
16 | abstract class row_based_plugin extends base |
17 | { |
18 | /** |
19 | * @var \phpbb\db\driver\driver_interface |
20 | */ |
21 | protected $db; |
22 | |
23 | /** |
24 | * @var string |
25 | */ |
26 | protected $table; |
27 | |
28 | /** |
29 | * Constructor |
30 | * |
31 | * @param \phpbb\db\driver\driver_interface $db Database connection |
32 | * @param string $table |
33 | */ |
34 | public function __construct(\phpbb\db\driver\driver_interface $db, $table) |
35 | { |
36 | $this->db = $db; |
37 | $this->table = $table; |
38 | } |
39 | |
40 | /** |
41 | * Return the name of the column that correspond to each field |
42 | * |
43 | * @return array |
44 | */ |
45 | abstract public function get_columns(); |
46 | |
47 | /** |
48 | * {@inheritdoc} |
49 | */ |
50 | public function get_max_id() |
51 | { |
52 | $columns = $this->get_columns(); |
53 | |
54 | $sql = 'SELECT MAX(' . $columns['id'] . ') AS max_id FROM ' . $this->table; |
55 | $result = $this->db->sql_query($sql); |
56 | $max_id = (int) $this->db->sql_fetchfield('max_id'); |
57 | $this->db->sql_freeresult($result); |
58 | |
59 | return $max_id; |
60 | } |
61 | |
62 | /** |
63 | * {@inheritdoc} |
64 | */ |
65 | protected function get_records_by_range($min_id, $max_id) |
66 | { |
67 | $sql = $this->get_records_by_range_query($min_id, $max_id); |
68 | $result = $this->db->sql_query($sql); |
69 | $records = $this->db->sql_fetchrowset($result); |
70 | $this->db->sql_freeresult($result); |
71 | |
72 | return $records; |
73 | } |
74 | |
75 | /** |
76 | * Generate the query that retrieves all records for given range |
77 | * |
78 | * @param integer $min_id Lower bound |
79 | * @param integer $max_id Upper bound |
80 | * @return string SQL query |
81 | */ |
82 | protected function get_records_by_range_query($min_id, $max_id) |
83 | { |
84 | $columns = $this->get_columns(); |
85 | $fields = array(); |
86 | foreach ($columns as $field_name => $column_name) |
87 | { |
88 | if ($column_name === $field_name) |
89 | { |
90 | $fields[] = $column_name; |
91 | } |
92 | else |
93 | { |
94 | $fields[] = $column_name . ' AS ' . $field_name; |
95 | } |
96 | } |
97 | |
98 | $sql = 'SELECT ' . implode(', ', $fields) . ' |
99 | FROM ' . $this->table . ' |
100 | WHERE ' . $columns['id'] . ' BETWEEN ' . $min_id . ' AND ' . $max_id; |
101 | |
102 | return $sql; |
103 | } |
104 | |
105 | /** |
106 | * {@inheritdoc} |
107 | */ |
108 | protected function save_record(array $record) |
109 | { |
110 | $columns = $this->get_columns(); |
111 | |
112 | $sql = 'UPDATE ' . $this->table . ' |
113 | SET ' . $columns['text'] . " = '" . $this->db->sql_escape($record['text']) . "' |
114 | WHERE " . $columns['id'] . ' = ' . $record['id']; |
115 | $this->db->sql_query($sql); |
116 | } |
117 | } |