Исходный код вики MySQL Encoding Check
Редактировал(а) Dmitriy Rybakov 05.08.2025, 13:07
Последние авторы
| author | version | line-number | content |
|---|---|---|---|
| 1 | {{groovy}} | ||
| 2 | |||
| 3 | |||
| 4 | def context = xcontext.getContext(); | ||
| 5 | def store = xwiki.getXWiki().getStore().getStore(); | ||
| 6 | if (services.wiki) { | ||
| 7 | wikilist = [] | ||
| 8 | services.wiki.getAll().each { wikilist.add(it.id) } | ||
| 9 | } else if (services.wikimanager) { | ||
| 10 | wikilist = services.wikimanager.getAllWikis() | ||
| 11 | } else { | ||
| 12 | wikilist = [xcontext.getDatabase()] | ||
| 13 | } | ||
| 14 | |||
| 15 | def indexes = xwiki.getDocument("Admin.CheckIndexesSQL").getContent().split("\n") | ||
| 16 | |||
| 17 | println "== General db settings ==" | ||
| 18 | |||
| 19 | try { | ||
| 20 | store.beginTransaction(context); | ||
| 21 | def session = store.getSession(context); | ||
| 22 | def connection = session.connection(); | ||
| 23 | def stmt = connection.createStatement(); | ||
| 24 | stmt.execute("show variables like 'character_set_%'"); | ||
| 25 | def rset = stmt.getResultSet(); | ||
| 26 | def result = null; | ||
| 27 | |||
| 28 | while (rset.next()) { | ||
| 29 | def varname = rset.getString(1); | ||
| 30 | def varvalue = rset.getString(2); | ||
| 31 | if (varname!="character_sets_dir") { | ||
| 32 | println "* MYSQL encoding setting ${varname}: ${varvalue}" | ||
| 33 | } | ||
| 34 | } | ||
| 35 | stmt.close(); | ||
| 36 | } catch (Exception e) { | ||
| 37 | println "* {{error}}Failed to check mysql encoding:{{/error}} " + e.getMessage() | ||
| 38 | } finally { | ||
| 39 | store.endTransaction(context, false); | ||
| 40 | } | ||
| 41 | |||
| 42 | println "== General db collation settings ==" | ||
| 43 | |||
| 44 | try { | ||
| 45 | store.beginTransaction(context); | ||
| 46 | def session = store.getSession(context); | ||
| 47 | def connection = session.connection(); | ||
| 48 | def stmt = connection.createStatement(); | ||
| 49 | stmt.execute("show variables like 'collation%'"); | ||
| 50 | def rset = stmt.getResultSet(); | ||
| 51 | def result = null; | ||
| 52 | |||
| 53 | while (rset.next()) { | ||
| 54 | def varname = rset.getString(1); | ||
| 55 | def varvalue = rset.getString(2); | ||
| 56 | println "* MYSQL collation setting ${varname}: ${varvalue}" | ||
| 57 | } | ||
| 58 | stmt.close(); | ||
| 59 | } catch (Exception e) { | ||
| 60 | println "* {{error}}Failed to check mysql encoding:{{/error}} " + e.getMessage() | ||
| 61 | } finally { | ||
| 62 | store.endTransaction(context, false); | ||
| 63 | } | ||
| 64 | |||
| 65 | println "== Encoding settings by wiki ==" | ||
| 66 | |||
| 67 | for(wiki in wikilist) { | ||
| 68 | |||
| 69 | // check database encoding | ||
| 70 | try { | ||
| 71 | store.beginTransaction(context); | ||
| 72 | def session = store.getSession(context); | ||
| 73 | def connection = session.connection(); | ||
| 74 | def stmt = connection.createStatement(); | ||
| 75 | stmt.execute("show create database ${wiki}"); | ||
| 76 | def rset = stmt.getResultSet(); | ||
| 77 | def result = null; | ||
| 78 | try { | ||
| 79 | rset.first(); | ||
| 80 | result = rset.getString(2); | ||
| 81 | } catch (Exception e2) { println '' } | ||
| 82 | |||
| 83 | // println result | ||
| 84 | def i1 = result.indexOf("DEFAULT CHARACTER SET") | ||
| 85 | def i2 = result.indexOf("*", i1) | ||
| 86 | if (result!=null && i1!=-1) { | ||
| 87 | def enc = result.substring(i1 + 22, i2); | ||
| 88 | println "* database encoding of ${wiki} is ${enc}" | ||
| 89 | } else { | ||
| 90 | println "* {{error}}Could not find xwikidoc encoding for wiki ${wiki}{{/error}}" | ||
| 91 | } | ||
| 92 | stmt.close(); | ||
| 93 | } catch (Exception e) { | ||
| 94 | println "* {{error}}Failed to check xwikidoc encoding for wiki ${wiki}: {{/error}} " + e.getMessage() | ||
| 95 | } finally { | ||
| 96 | store.endTransaction(context, false); | ||
| 97 | } | ||
| 98 | |||
| 99 | |||
| 100 | |||
| 101 | // check xwikidoc table encoding | ||
| 102 | try { | ||
| 103 | store.beginTransaction(context); | ||
| 104 | def session = store.getSession(context); | ||
| 105 | def connection = session.connection(); | ||
| 106 | def stmt = connection.createStatement(); | ||
| 107 | stmt.execute("show create table ${wiki}.xwikidoc"); | ||
| 108 | def rset = stmt.getResultSet(); | ||
| 109 | def result = null; | ||
| 110 | try { | ||
| 111 | rset.first(); | ||
| 112 | result = rset.getString(2); | ||
| 113 | } catch (Exception e2) {} | ||
| 114 | |||
| 115 | // println result | ||
| 116 | def i1 = -1; | ||
| 117 | def i2 = -1; | ||
| 118 | i1 = result.indexOf("DEFAULT CHARSET="); | ||
| 119 | if (i1!=-1) { | ||
| 120 | i2 = result.indexOf(" ", i1 + 16); | ||
| 121 | } | ||
| 122 | if (result!=null && i1!=-1) { | ||
| 123 | def enc = (i2==-1) ? result.substring(i1 + 16) : result.substring(i1 + 8, i2); | ||
| 124 | println "* encoding of xwikidoc of ${wiki} is ${enc}" | ||
| 125 | } else { | ||
| 126 | println "* {{error}}Could not find xwikidoc encoding for wiki ${wiki}{{/error}}" | ||
| 127 | } | ||
| 128 | |||
| 129 | i1 = result.indexOf("COLLATE=") | ||
| 130 | if (i1!=-1) { | ||
| 131 | i2 = result.indexOf(" ", i1+8) | ||
| 132 | } | ||
| 133 | if (result!=null && i1!=-1) { | ||
| 134 | def enc = (i2==-1) ? result.substring(i1 + 8) : result.substring(i1 + 8, i2); | ||
| 135 | println "* collation of xwikidoc of ${wiki} is ${enc}" | ||
| 136 | } else { | ||
| 137 | println "* {{error}}Could not find xwikidoc collation for wiki ${wiki}{{/error}}" | ||
| 138 | } | ||
| 139 | |||
| 140 | stmt.close(); | ||
| 141 | } catch (Exception e) { | ||
| 142 | println "* {{error}}Failed to check xwikidoc encoding or collation for wiki ${wiki}{{/error}}: " + e.getMessage() | ||
| 143 | } finally { | ||
| 144 | store.endTransaction(context, false); | ||
| 145 | } | ||
| 146 | |||
| 147 | |||
| 148 | |||
| 149 | } | ||
| 150 | |||
| 151 | {{/groovy}} |