@@ -53,6 +53,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
5353
5454 $ this ->fixBadForm_1 ($ input , $ output );
5555 $ this ->fixBadForm_2 ($ input , $ output );
56+ $ this ->fixBadForm_3 ($ input , $ output );
5657
5758 $ output ->writeln ('<info>Done.</info> ' );
5859 return 0 ;
@@ -102,7 +103,7 @@ protected function fixBadForm_1(InputInterface $input, OutputInterface $output)
102103 return 0 ;
103104 }
104105
105- $ output ->write ("<info>-> Found $ count tickets to clean</info> " );
106+ $ output ->write ("<info>-> Found $ count tickets to clean (double encoded < and > signs) </info> " );
106107 $ output ->writeln ("" );
107108 $ output ->write ("<info>-> Cleaning tickets...</info> " );
108109 $ output ->writeln ("" );
@@ -130,7 +131,7 @@ protected function fixBadForm_1(InputInterface $input, OutputInterface $output)
130131 }
131132
132133 /**
133- * remove HTML tag <br />
134+ * replace litteral HTML tag <br /> with <br />
134135 *
135136 * @param InputInterface $input
136137 * @param OutputInterface $output
@@ -142,7 +143,7 @@ protected function fixBadForm_2(InputInterface $input, OutputInterface $output)
142143 // Search tickets having HTML tags <br />
143144 $ itemTicketTable = Item_Ticket::getTable ();
144145 $ ticketTable = Ticket::getTable ();
145- $ pattern = 'br / ' ;
146+ $ pattern = '< br /> ' ;
146147 $ tickets = $ DB ->request ([
147148 'SELECT ' => [$ ticketTable => [Ticket::getIndexName (), 'content ' ]],
148149 'FROM ' => $ ticketTable ,
@@ -158,7 +159,7 @@ protected function fixBadForm_2(InputInterface $input, OutputInterface $output)
158159 ],
159160 ],
160161 'WHERE ' => [
161- "$ ticketTable.content " => ['LIKE ' , '% ' . $ pattern . '% ' ], // Matches bad encoding for '< '
162+ "$ ticketTable.content " => ['LIKE ' , '% ' . $ pattern . '% ' ], // Matches bad encoding for 'br / '
162163 ],
163164 ]);
164165
@@ -169,17 +170,110 @@ protected function fixBadForm_2(InputInterface $input, OutputInterface $output)
169170 return 0 ;
170171 }
171172
172- $ output ->write ("<info>-> Found $ count tickets to clean</info> " );
173+ $ output ->write ("<info>-> Found $ count tickets to clean (literal BR tag) </info> " );
173174 $ output ->writeln ("" );
174175 $ output ->write ("<info>-> Cleaning tickets...</info> " );
175176 $ output ->writeln ("" );
176177 foreach ($ tickets as $ row ) {
177178 $ pattern = [
178179 '<br /> ' ,
179180 ];
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+ ' > ' ,
182260 ];
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+ }
183277 $ row ['content ' ] = str_replace ($ pattern , $ replace , $ row ['content ' ]);
184278 // Direct write to the table to avoid alteration of other fields
185279 $ DB ->update (
0 commit comments