Coverage for quibble/mediawiki/maintenance.py: 88%

87 statements  

« prev     ^ index     » next       coverage.py v7.10.7, created at 2026-08-07 07:21 +0000

1# Copyright 2018, Antoine "hashar" Musso 

2# Copyright 2018, Wikimedia Foundation Inc. 

3# 

4# Licensed under the Apache License, Version 2.0 (the "License"); 

5# you may not use this file except in compliance with the License. 

6# You may obtain a copy of the License at 

7# 

8# http://www.apache.org/licenses/LICENSE-2.0 

9# 

10# Unless required by applicable law or agreed to in writing, software 

11# distributed under the License is distributed on an "AS IS" BASIS, 

12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

13# See the License for the specific language governing permissions and 

14# limitations under the License. 

15 

16import logging 

17import os 

18import subprocess 

19 

20 

21# Compatibility with MW < 1.40; to remove once 1.39 is not CI-tested 

22def getMaintenanceScript(script, args=[]): 

23 """ 

24 Return a sequence of command arguments to run a maintenance script 

25 

26 Since MediaWiki 1.40, instead of invoking maintenance script directly, one 

27 should use `maintenance/run.php` and pass the basename of the script: 

28 

29 php maintenance/run.php update 

30 

31 Will run `maintenance/update.php`. This command takes care of back 

32 compatibility with older MediaWiki versions which do not have 

33 `maintenance/run.php`. 

34 """ 

35 subprocess.Popen 

36 (basename, ext) = os.path.splitext(script) 

37 

38 if isinstance(args, str): 

39 args = [args] 

40 

41 if os.path.exists('maintenance/run.php'): 

42 cmd = ['php', 'maintenance/run.php', basename] 

43 else: 

44 if ext == '': 

45 cmd = ['php', 'maintenance/%s.php' % basename] 

46 else: 

47 cmd = ['php', 'maintenance/%s' % script] 

48 cmd.extend(args) 

49 return cmd 

50 

51 

52def update(mwdir=None, args=[]): 

53 log = logging.getLogger('mw.maintenance.update') 

54 

55 cmd = getMaintenanceScript('update', '--quick') 

56 cmd.extend(args) 

57 log.info(' '.join(cmd)) 

58 

59 update_env = {} 

60 update_env.update(os.environ) 

61 if mwdir is not None: 

62 update_env['MW_INSTALL_PATH'] = mwdir 

63 

64 p = subprocess.Popen(cmd, cwd=mwdir, env=update_env) 

65 p.communicate() 

66 if p.returncode > 0: 

67 raise Exception('Update failed with exit code: %s' % p.returncode) 

68 

69 

70def install(args, mwdir=None): 

71 log = logging.getLogger('mw.maintenance.install') 

72 

73 cmd = getMaintenanceScript('install') 

74 cmd.extend(args) 

75 cmd.extend( 

76 [ 

77 '--with-extensions', # T189567 

78 '--pass=testwikijenkinspass', 

79 'TestWiki', 

80 'WikiAdmin', 

81 ] 

82 ) 

83 log.info(' '.join(cmd)) 

84 

85 install_env = {} 

86 install_env.update(os.environ) 

87 

88 # LANG is passed to $wgShellLocale 

89 install_env.update({'LANG': 'C.UTF-8'}) 

90 

91 p = subprocess.Popen(cmd, cwd=mwdir, env=install_env) 

92 p.communicate() 

93 if p.returncode > 0: 93 ↛ 94line 93 didn't jump to line 94 because the condition on line 93 was never true

94 raise Exception('Install failed with exit code: %s' % p.returncode) 

95 

96 

97def rebuildLocalisationCache(lang=['en'], mwdir=None): 

98 log = logging.getLogger('mw.maintenance.rebuildLocalisationCache') 

99 

100 cmd = getMaintenanceScript('rebuildLocalisationCache') 

101 cmd.extend(['--lang', ','.join(lang)]) 

102 log.info(' '.join(cmd)) 

103 

104 p = subprocess.Popen(cmd, cwd=mwdir) 

105 p.communicate() 

106 if p.returncode > 0: 

107 raise Exception( 

108 'rebuildLocalisationCache failed with exit code: %s' 

109 % (p.returncode) 

110 ) 

111 

112 

113def addSite(args, mwdir=None): 

114 log = logging.getLogger('mw.maintenance.addSite') 

115 

116 cmd = getMaintenanceScript('addSite') 

117 cmd.extend(args) 

118 log.info(' '.join(cmd)) 

119 

120 addSite_env = {} 

121 addSite_env.update(os.environ) 

122 if mwdir is not None: 122 ↛ 123line 122 didn't jump to line 123 because the condition on line 122 was never true

123 addSite_env['MW_INSTALL_PATH'] = mwdir 

124 

125 p = subprocess.Popen(cmd, cwd=mwdir, env=addSite_env) 

126 p.communicate() 

127 if p.returncode > 0: 127 ↛ 128line 127 didn't jump to line 128 because the condition on line 127 was never true

128 raise Exception('addSite failed with exit code: %s' % p.returncode) 

129 

130 

131def updateSearchIndexConfig(mwdir=None): 

132 log = logging.getLogger('mw.maintenance.UpdateSearchIndexConfig') 

133 

134 cmd = [ 

135 'php', 

136 'maintenance/run.php', 

137 'CirrusSearch\\Maintenance\\UpdateSearchIndexConfig', 

138 '--startOver', 

139 ] 

140 log.info(' '.join(cmd)) 

141 

142 env = {} 

143 env.update(os.environ) 

144 if mwdir is not None: 144 ↛ 145line 144 didn't jump to line 145 because the condition on line 144 was never true

145 env['MW_INSTALL_PATH'] = mwdir 

146 

147 p = subprocess.Popen(cmd, cwd=mwdir, env=env) 

148 p.communicate() 

149 if p.returncode > 0: 149 ↛ 150line 149 didn't jump to line 150 because the condition on line 149 was never true

150 raise Exception( 

151 'updateSearchIndexConfig failed with exit code: %s' % p.returncode 

152 ) 

153 

154 

155def forceSearchIndex(mwdir=None): 

156 log = logging.getLogger('mw.maintenance.ForceSearchIndex') 

157 

158 cmd = [ 

159 'php', 

160 'maintenance/run.php', 

161 'CirrusSearch\\Maintenance\\ForceSearchIndex', 

162 ] 

163 log.info(' '.join(cmd)) 

164 

165 env = {} 

166 env.update(os.environ) 

167 if mwdir is not None: 167 ↛ 168line 167 didn't jump to line 168 because the condition on line 167 was never true

168 env['MW_INSTALL_PATH'] = mwdir 

169 

170 p = subprocess.Popen(cmd, cwd=mwdir, env=env) 

171 p.communicate() 

172 if p.returncode > 0: 172 ↛ 173line 172 didn't jump to line 173 because the condition on line 172 was never true

173 raise Exception( 

174 'forceSearchIndex failed with exit code: %s' % p.returncode 

175 )