@@ -53,6 +53,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
53
53
54
54
$ this ->fixBadForm_1 ($ input , $ output );
55
55
$ this ->fixBadForm_2 ($ input , $ output );
56
+ $ this ->fixBadForm_3 ($ input , $ output );
56
57
57
58
$ output ->writeln ('<info>Done.</info> ' );
58
59
return 0 ;
@@ -102,7 +103,7 @@ protected function fixBadForm_1(InputInterface $input, OutputInterface $output)
102
103
return 0 ;
103
104
}
104
105
105
- $ output ->write ("<info>-> Found $ count tickets to clean</info> " );
106
+ $ output ->write ("<info>-> Found $ count tickets to clean (double encoded < and > signs) </info> " );
106
107
$ output ->writeln ("" );
107
108
$ output ->write ("<info>-> Cleaning tickets...</info> " );
108
109
$ output ->writeln ("" );
@@ -130,7 +131,7 @@ protected function fixBadForm_1(InputInterface $input, OutputInterface $output)
130
131
}
131
132
132
133
/**
133
- * remove HTML tag <br />
134
+ * replace litteral HTML tag <br /> with <br />
134
135
*
135
136
* @param InputInterface $input
136
137
* @param OutputInterface $output
@@ -142,7 +143,7 @@ protected function fixBadForm_2(InputInterface $input, OutputInterface $output)
142
143
// Search tickets having HTML tags <br />
143
144
$ itemTicketTable = Item_Ticket::getTable ();
144
145
$ ticketTable = Ticket::getTable ();
145
- $ pattern = 'br / ' ;
146
+ $ pattern = '< br /> ' ;
146
147
$ tickets = $ DB ->request ([
147
148
'SELECT ' => [$ ticketTable => [Ticket::getIndexName (), 'content ' ]],
148
149
'FROM ' => $ ticketTable ,
@@ -158,7 +159,7 @@ protected function fixBadForm_2(InputInterface $input, OutputInterface $output)
158
159
],
159
160
],
160
161
'WHERE ' => [
161
- "$ ticketTable.content " => ['LIKE ' , '% ' . $ pattern . '% ' ], // Matches bad encoding for '< '
162
+ "$ ticketTable.content " => ['LIKE ' , '% ' . $ pattern . '% ' ], // Matches bad encoding for 'br / '
162
163
],
163
164
]);
164
165
@@ -169,17 +170,110 @@ protected function fixBadForm_2(InputInterface $input, OutputInterface $output)
169
170
return 0 ;
170
171
}
171
172
172
- $ output ->write ("<info>-> Found $ count tickets to clean</info> " );
173
+ $ output ->write ("<info>-> Found $ count tickets to clean (literal BR tag) </info> " );
173
174
$ output ->writeln ("" );
174
175
$ output ->write ("<info>-> Cleaning tickets...</info> " );
175
176
$ output ->writeln ("" );
176
177
foreach ($ tickets as $ row ) {
177
178
$ pattern = [
178
179
'<br /> ' ,
179
180
];
180
- $ replace = [
181
- '<br /> ' ,
181
+ // Determine if we must use legacy or new encoding
182
+ // @see Sanitizer::sanitize()
183
+ $ replace = null ;
184
+ if (strpos ($ row ['content ' ], '< ' ) !== false && strpos ($ row ['content ' ], '#60; ' ) === false ) {
185
+ $ replace = [
186
+ '<br /> ' ,
187
+ ];
188
+ } else if (strpos ($ row ['content ' ], '#60 ' ) !== false && strpos ($ row ['content ' ], '< ' ) === false ) {
189
+ $ replace = [
190
+ '<br /> ' ,
191
+ ];
192
+ }
193
+ if ($ replace === null ) {
194
+ $ output ->write ("<error>-> Unable to determine the encoding type of ticket ID: " . $ row ['id ' ]. "</error> " );
195
+ continue ;
196
+ }
197
+ $ row ['content ' ] = str_replace ($ pattern , $ replace , $ row ['content ' ]);
198
+ // Direct write to the table to avoid alteration of other fields
199
+ $ DB ->update (
200
+ $ ticketTable ,
201
+ [
202
+ 'content ' => $ DB ->escape ($ row ['content ' ])
203
+ ],
204
+ [
205
+ 'id ' => $ row ['id ' ],
206
+ ]
207
+ );
208
+ }
209
+ }
210
+
211
+ /**
212
+ * replace litteral HTML tag > with #38;
213
+ * This may happen when a question gives the path to an item of a CommonTreeObject
214
+ * entities, locations, ...
215
+ *
216
+ * @param InputInterface $input
217
+ * @param OutputInterface $output
218
+ * @return void
219
+ */
220
+ protected function fixBadForm_3 (InputInterface $ input , OutputInterface $ output ) {
221
+ global $ DB ;
222
+
223
+ // Search tickets having HTML tags <br />
224
+ $ itemTicketTable = Item_Ticket::getTable ();
225
+ $ ticketTable = Ticket::getTable ();
226
+ $ pattern = ' > ' ; // greater than sign with a space before and after
227
+ $ tickets = $ DB ->request ([
228
+ 'SELECT ' => [$ ticketTable => [Ticket::getIndexName (), 'content ' ]],
229
+ 'FROM ' => $ ticketTable ,
230
+ 'INNER JOIN ' => [
231
+ $ itemTicketTable => [
232
+ 'FKEY ' => [
233
+ $ ticketTable => Ticket::getIndexName (),
234
+ $ itemTicketTable => Ticket::getForeignKeyField (),
235
+ ],
236
+ 'AND ' => [
237
+ "$ itemTicketTable.itemtype " => PluginFormcreatorFormAnswer::getType (),
238
+ ]
239
+ ],
240
+ ],
241
+ 'WHERE ' => [
242
+ "$ ticketTable.content " => ['LIKE ' , '% ' . $ pattern . '% ' ],
243
+ ],
244
+ ]);
245
+
246
+ $ count = $ tickets ->count ();
247
+ if ($ count < 1 ) {
248
+ $ output ->writeln ('<info>-> No ticket to fix.</info> ' );
249
+ $ output ->writeln ("" );
250
+ return 0 ;
251
+ }
252
+
253
+ $ output ->write ("<info>-> Found $ count tickets to clean (litteral > sign)</info> " );
254
+ $ output ->writeln ("" );
255
+ $ output ->write ("<info>-> Cleaning tickets...</info> " );
256
+ $ output ->writeln ("" );
257
+ foreach ($ tickets as $ row ) {
258
+ $ pattern = [
259
+ ' > ' ,
182
260
];
261
+ // Determine if we must use legacy or new encoding
262
+ // @see Sanitizer::sanitize()
263
+ $ replace = null ;
264
+ if (strpos ($ row ['content ' ], '< ' ) !== false && strpos ($ row ['content ' ], '#60; ' ) === false ) {
265
+ $ replace = [
266
+ ' > ' ,
267
+ ];
268
+ } else if (strpos ($ row ['content ' ], '#60 ' ) !== false && strpos ($ row ['content ' ], '< ' ) === false ) {
269
+ $ replace = [
270
+ ' & ' ,
271
+ ];
272
+ }
273
+ if ($ replace === null ) {
274
+ $ output ->write ("<error>-> Unable to determine the encoding type of ticket ID: " . $ row ['id ' ]. "</error> " );
275
+ continue ;
276
+ }
183
277
$ row ['content ' ] = str_replace ($ pattern , $ replace , $ row ['content ' ]);
184
278
// Direct write to the table to avoid alteration of other fields
185
279
$ DB ->update (
0 commit comments