Example #1
0
 /**
  * @author Władysław Bodzek <*****@*****.**>
  * @modify Piotr Molski <*****@*****.**>
  */
 public static function do_clean_math_table(DatabaseUpdater $updater)
 {
     $db = $updater->getDB();
     $table = 'math';
     $primaryKey = 'math_inputhash';
     $fields = array('math_inputhash', 'math_outputhash', 'math_html', 'math_mathml');
     $updater->output("Checking {$table} table and removing rows with different encoding than utf8...\n");
     if ($db->tableExists($table)) {
         $updater->output("...scanning table...");
         // Read the whole table
         $allFields = array_unique(array_merge($fields, array($primaryKey)));
         $res = $db->select(" `{$table}`", $allFields, '', __METHOD__);
         // scan for all rows containing text which is not in utf8 encoding
         $wrong = array();
         while ($row = $db->fetchRow($res)) {
             foreach ($fields as $field) {
                 if (!self::is_valid_utf8_text($row[$field])) {
                     $wrong[] = $row[$primaryKey];
                     break;
                 }
             }
         }
         $db->freeResult($res);
         $count = count($wrong);
         $updater->output("ok (found " . count($wrong) . " rows)\n");
         // and finally remove all the malformed rows
         if ($count > 0) {
             $updater->output("...removing malformed rows...");
             $pos = 0;
             $chunkSize = 500;
             while ($pos < $count) {
                 $removing = array_slice($wrong, $pos, $chunkSize);
                 $res = $db->delete($table, array($primaryKey => $removing), __METHOD__);
                 $pos += $chunkSize;
             }
             $updater->output("ok\n");
         }
     }
 }