update to latest snapshot
[mediawiki-setup] / mw-setup-script
1 #!/bin/bash
2 # Copyright (C) 2016 Ian Kelling
3 # This program is under GPL v. 3 or later, see <http://www.gnu.org/licenses/>
4 # <source lang="bash">
5 # identify if this is a debian based distro
6 isdeb() { command -v apt &>/dev/null; }
7 # tee unique. append each stdin line if it does not exist in the file
8 teeu () {
9 local MAPFILE
10 mapfile -t
11 for line in "${MAPFILE[@]}"; do
12 grep -xFq "$line" "$1" &>/dev/null || tee -a "$1" <<<"$line"
13 done
14 }
15
16 # get and reset an extension/skin repository, and enable it
17 mw-clone() {
18 local url=$1
19 local original_pwd="$PWD"
20 local name
21 local re='[^/]*/[^/]*$'
22 [[ $url =~ $re ]] ||:
23 target=$mw/${BASH_REMATCH[0]}
24 if [[ ! -e $target/.git ]]; then
25 git clone $url $target
26 fi
27 if ! cd $target; then
28 echo "mw-ext error: failed cd $target";
29 exit 1
30 fi
31 git fetch
32 git checkout -qf origin/$mw_branch || git checkout -qf origin/master
33 git clean -xffd
34 cd "$original_pwd"
35
36 }
37 mw-ext () {
38 local ext
39 for ext in "$@"; do
40 mw-clone https://gerrit.wikimedia.org/r/p/mediawiki/extensions/$ext
41 if [[ -e $mw/ext/$ext/extension.json ]]; then
42 # new style extension. remove old style declaration
43 sed -i '#^require_once( "\\\$IP/extensions/\$ext/\$ext\.php" );#d' $mwc
44 teeu $mwc <<EOF
45 wfLoadExtension( '$ext' );
46 EOF
47 else
48 teeu $mwc <<EOF
49 require_once( "\$IP/extensions/$ext/$ext.php" );
50 EOF
51 fi
52 done
53 # --quick is quicker than default flags,
54 # but still add a sleep to make sure everything works right
55 sudo -u $apache_user php $mw/maintenance/update.php -q --quick; sleep 1
56 }
57 mw-skin() {
58 local skin=$1
59 mw-clone https://gerrit.wikimedia.org/r/p/mediawiki/skins/$skin
60 sed -i '/^wfLoadSkin/d' $mwc
61 sed -i '/^\$wgDefaultSkin/d' $mwc
62 teeu $mwc <<EOF
63 \$wgDefaultSkin = "${skin,,*}";
64 wfLoadSkin( '$skin' );
65 EOF
66 sudo -u $apache_user php $mw/maintenance/update.php -q --quick; sleep 1
67 }
68
69 if command -v apt &>/dev/null; then
70 apache_user=www-data
71 else
72 apache_user=apache
73 fi
74
75 # </source>
76 # <source lang="bash">
77 # From here on out, exit if a command fails.
78 # This will prevent us from not noticing an important failure.
79 # We recommend setting this for the entire installation session.
80 # If you are running commands interactively, it might be best to
81 # put it in your ~/.bashrc temporarily.
82 set -eE -o pipefail
83 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
84 source ~/mw_vars
85
86 if isdeb; then
87 # main reference:
88 # https://www.mediawiki.org/wiki/Manual:Running_MediaWiki_on_Ubuntu
89 apt-get update
90 apt-get install -y imagemagick php-mbstring
91 if apt-get install -s mediawiki &>/dev/null; then
92 # in debian wheezy time-frame distros, mediawiki was packaged.
93 apt-get -y install php-apc mediawiki
94 else
95 # https://www.mediawiki.org/wiki/Manual:Installation_requirements
96 if apt-get install -s php7.0 &>/dev/null; then
97 # note, 7.0 is untested by the editor here, since it's not
98 # available in debian 8. it's listed as supported
99 # in the mediawiki page.
100 # noninteractive to avoid mysql password prompt
101 DEBIAN_FRONTEND=noninteractive apt-get install -y apache2 mysql-server \
102 php7.0 php7.0-mysql libapache2-mod-php7.0 php7.0-xml \
103 php7.0-apcu
104 else
105 DEBIAN_FRONTEND=noninteractive apt-get install -y apache2 mysql-server \
106 php5 php5-mysql libapache2-mod-php5 php5-apcu
107 fi
108 fi
109 service apache2 restart
110 else
111 # note
112 # fedora deps are missing a database, so some is translated from debian packages
113 yum -y install mediawiki ImageMagick php-mysqlnd php-pecl-apcu mariadb-server
114
115 systemctl restart mariadb.service
116 systemctl enable mariadb.service
117 systemctl enable httpd.service
118 systemctl restart httpd.service
119 fi
120
121
122 # slightly different depending on if we already set the root pass
123 if echo exit|mysql -u root -p"$dbpass"; then
124 # answer interactive prompts:
125 # mysql root pass, change pass? no, remove anon users? (default, yes)
126 # disallow remote root (default, yes), reload? (default, yes)
127 echo -e "$dbpass\nn\n\n\n\n" | mysql_secure_installation
128 else
129 # I had 1 less newline at the start when doing ubuntu 14.04,
130 # compared to debian 8, so can't say this is especially portable.
131 # It won't hurt if it fails.
132 echo -e "\n\n$dbpass\n$dbpass\n\n\n\n\n" | mysql_secure_installation
133 fi
134 # </source>
135 # <source lang="bash">
136 mkdir -p $mw
137 cd $mw
138 # this will just fail if it already exists which is fine
139 if [[ ! -e .git ]]; then
140 git clone https://gerrit.wikimedia.org/r/p/mediawiki/core.git .
141 fi
142 # to see available branches: https://www.mediawiki.org/wiki/Version_lifecycle
143 # and
144 # git branch -r
145 git checkout -f origin/$mw_branch
146 git clean -ffxd
147 # Get the php libraries wmf uses. Based on:
148 # https://www.mediawiki.org/wiki/Download_from_Git#Fetch_external_libraries
149 if [[ ! -e vendor/.git ]]; then
150 git clone https://gerrit.wikimedia.org/r/p/mediawiki/vendor.git
151 fi
152 cd vendor
153 git checkout -f origin/$mw_branch
154 cd ..
155
156 # Drop any previous database which may have been installed while testing.
157 # If upgrading, we should have a db backup which will get restored.
158 # https://www.mediawiki.org/wiki/Manual:Upgrading
159 mysql -u root -p$dbpass <<'EOF' ||:
160 drop database my_wiki;
161 exit
162 EOF
163 php $mw/maintenance/install.php --pass $wikipass --scriptpath /w \
164 --dbuser root --dbpass $dbpass "$mwdescription" "$wikiuser"
165 teeu $mwc <<'EOF'
166 # lock down the wiki to only the initial owner until anti-spam measures are put in place
167 # limit edits to registered users
168 $wgGroupPermissions['*']['edit'] = false;
169 # don't allow any account creation
170 $wgGroupPermissions['*']['createaccount'] = false;
171 EOF
172 # </source>
173 # <source lang="bash">
174 temp=$(mktemp -d)
175 cd $temp
176 git_site=https://iankelling.org/git
177 git clone $git_site/acme-tiny-wrapper
178 l=$mw/../../logs
179 mkdir -p $l
180
181 acme-tiny-wrapper/acme-tiny-wrapper $mwdomain
182
183 git clone $git_site/basic-https-conf
184 { cat <<EOF
185 ServerAdmin $mw_email
186 RewriteEngine On
187 # make the site's root url go to our main page
188 RewriteRule ^/?wiki(/.*)?\$ %{DOCUMENT_ROOT}/w/index.php [L]
189 # use short urls https://www.mediawiki.org/wiki/Manual:Short_URL
190 RewriteRule ^/*\$ %{DOCUMENT_ROOT}/w/index.php [L]
191 EOF
192 find -L $(readlink -f $mw) -name .htaccess \
193 | while read line; do
194 echo -e "<Directory ${line%/.htaccess}>\n $(< $line)\n</Directory>";
195 done
196 } | basic-https-conf/apache-site -r ${mw%/*} - $mwdomain
197 cd
198 rm -rf $temp
199 # </source>
200 # <source lang="bash">
201 dd of=$mw/../robots.txt <<'EOF'
202 User-agent: *
203 Disallow: /w/
204 User-agent: ia_archiver
205 Allow: /*&action=raw
206 EOF
207 mw-skin Vector
208 # </source>
209 # <source lang="bash">
210 teeu $mwc<<EOF
211 \$wgServer = "https://$mwdomain";
212 \$wgDBserver = "localhost";
213 \$wgRightsUrl = "$mw_RightsUrl";
214 \$wgRightsText = "$mw_RightsText";
215 \$wgRightsIcon = "$mw_RightsIcon";
216 EOF
217 # </source>
218 # <source lang="bash">
219 teeu $mwc<<EOF
220 \$wgPasswordSender = "$mw_email";
221 \$wgEmergencyContact = "$mw_email";
222 \$wgEnotifUserTalk = true; # UPO
223 \$wgEnotifWatchlist = true; # UPO
224 \$wgMainCacheType = CACHE_ACCEL;
225 \$wgEnableUploads = true;
226 \$wgUseInstantCommons = true;
227 EOF
228 # </source>
229 # <source lang="bash">
230 teeu $mwc <<'EOF'
231 # from https://www.mediawiki.org/wiki/Manual:Short_URL
232 $wgArticlePath = "/wiki/$1";
233
234 # https://www.mediawiki.org/wiki/Manual:Combating_spam
235 # check that url if our precautions don't work
236 # not using nofollow is good practice, as long as we avoid spam.
237 $wgNoFollowLinks = false;
238 # Allow user customization.
239 $wgAllowUserJs = true;
240 $wgAllowUserCss = true;
241
242 # use imagemagick over GD
243 $wgUseImageMagick = true;
244 EOF
245
246
247 # https://www.mediawiki.org/wiki/Manual:Configuring_file_uploads
248 # Increase from default of 2M to 100M.
249 # This will at least allow high res pics etc.
250 php_ini=$(php -r 'echo(php_ini_loaded_file());')
251 sed -i 's/^\(upload_max_filesize\|post_max_size\)\b.*/\1 = 100M/' $php_ini
252 if isdeb; then
253 service apache2 restart
254 else
255 systemctl restart httpd.service
256 fi
257
258 # if you were to install as a normal user, you would need this for images
259 # sudo usermod -aG $apache_user $USER
260
261 # this doesn't propogate right away
262 chgrp -R $apache_user $mw/images
263 chmod -R g+w $mw/images
264 # </source>
265 # <source lang="bash">
266 teeu $mwc <<'EOF'
267 $wgLogo = null;
268 #$wgFooterIcons = null;
269 EOF
270 # Make the toolbox go into the drop down.
271 cd $mw/skins/Vector
272 if ! git remote show ian-kelling &>/dev/null; then
273 git remote add ian-kelling https://iankelling.org/git/Vector
274 fi
275 git fetch ian-kelling
276 git checkout ian-kelling/REL1_27-toolbox-in-dropdown
277 # </source>
278 # <source lang="bash">
279 mw-ext Cite CiteThisPage CSS Echo Gadgets ImageMap Interwiki News \
280 Nuke ParserFunctions Poem SyntaxHighlight_GeSHi Variables
281 # </source>
282 # <source lang="bash">
283 mw-ext AntiSpoof
284 # recommended setup script to account for existing users
285 sudo -u $apache_user php $mw/extensions/AntiSpoof/maintenance/batchAntiSpoof.php
286 # </source>
287 # <source lang="bash">
288 mw-ext CheckUser
289 sudo -u $apache_user php $mw/extensions/CheckUser/install.php; sleep 1
290 # </source>
291 # <source lang="bash">
292 if isdeb; then
293 apt-get -y install php-wikidiff2
294 teeu $mwc <<'EOF'
295 $wgExternalDiffEngine = 'wikidiff2';
296 EOF
297 dir=$(dirname $(php -r 'echo(php_ini_loaded_file());'))/../apache2/conf.d
298 ln -sf ../../mods-available/wikidiff2.ini $dir
299 service apache2 restart
300 fi
301 # </source>
302 # <source lang="bash">
303 mw-ext Math
304 # php5-curl according to Math readme
305 if isdeb; then
306 curl_pkg=php7.0-curl
307 if ! apt-get -s install $curl_pkg &>/dev/null; then
308 curl_pkg=php5-curl
309 fi
310 apt-get -y install latex-cjk-all texlive-latex-extra texlive-latex-base \
311 ghostscript imagemagick ocaml $curl_pkg make
312 else
313 # todo, php5-curl equivalent on fedora
314 yum -y install texlive-cjk ghostscript ImageMagick texlive ocaml
315 fi
316 dir=$(dirname $(php -r 'echo(php_ini_loaded_file());'))/../apache2/conf.d
317 ln -sf ../../mods-available/curl.ini $dir
318 service apache2 restart
319
320 cd $mw/extensions/Math/math; make # makes texvc
321 cd $mw/extensions/Math/texvccheck; make
322
323 teeu $mwc <<'EOF'
324 # Enable MathJax as rendering option
325 $wgUseMathJax = true;
326 # Enable LaTeXML as rendering option
327 $wgMathValidModes[] = 'latexml';
328 # Set LaTeXML as default rendering option, because it is nicest
329 $wgDefaultUserOptions['math'] = 'latexml';
330 EOF
331 # </source>
332 # <source lang="bash">
333 mw-ext SpamBlacklist
334 if ! grep -F '$wgSpamBlacklistFiles = array(' $mwc &>/dev/null; then
335 tee -a $mwc <<'EOF'
336 $wgEnableDnsBlacklist = true;
337 $wgDnsBlacklistUrls = array( 'xbl.spamhaus.org', 'dnsbl.tornevall.org' );
338
339 ini_set( 'pcre.backtrack_limit', '10M' );
340 $wgSpamBlacklistFiles = array(
341 "[[m:Spam blacklist]]",
342 "http://en.wikipedia.org/wiki/MediaWiki:Spam-blacklist"
343 );
344 EOF
345 fi
346 # </source>
347 # <source lang="bash">
348 mw-ext TitleBlacklist
349 if ! grep -F '$wgTitleBlacklistSources = array(' $mwc &>/dev/null; then
350 tee -a $mwc <<'EOF'
351 $wgTitleBlacklistSources = array(
352 array(
353 'type' => 'local',
354 'src' => 'MediaWiki:Titleblacklist',
355 ),
356 array(
357 'type' => 'url',
358 'src' => 'http://meta.wikimedia.org/w/index.php?title=Title_blacklist&action=raw',
359 ),
360 );
361 EOF
362 fi
363 # </source>
364 # <source lang="bash">
365 mw-ext WikiEditor
366 teeu $mwc <<'EOF'
367 # Enable Wikieditor by default
368 $wgDefaultUserOptions['usebetatoolbar'] = 1;
369 $wgDefaultUserOptions['usebetatoolbar-cgd'] = 1;
370
371 # Display the Preview and Changes tabs
372 $wgDefaultUserOptions['wikieditor-preview'] = 1;
373 EOF
374 # </source>
375 # <source lang="bash">
376 mw-ext CategoryTree
377 teeu $mwc <<'EOF'
378 # Mediawiki setting dependency for CategoryTree
379 $wgUseAjax = true;
380 EOF
381 # </source>
382 # <source lang="bash">
383 mw-ext AbuseFilter
384 teeu $mwc<<'EOF'
385 $wgGroupPermissions['sysop']['abusefilter-modify'] = true;
386 $wgGroupPermissions['*']['abusefilter-log-detail'] = true;
387 $wgGroupPermissions['*']['abusefilter-view'] = true;
388 $wgGroupPermissions['*']['abusefilter-log'] = true;
389 $wgGroupPermissions['sysop']['abusefilter-private'] = true;
390 $wgGroupPermissions['sysop']['abusefilter-modify-restricted'] = true;
391 $wgGroupPermissions['sysop']['abusefilter-revert'] = true;
392 EOF
393 # </source>
394 # <source lang="bash">
395 mw-ext ConfirmEdit
396 captchaArray
397 teeu $mwc <<'EOF'
398 wfLoadExtension( 'ConfirmEdit/QuestyCaptcha' );
399 $wgCaptchaClass = 'QuestyCaptcha';
400 # only captcha on registration
401 $wgGroupPermissions['user' ]['skipcaptcha'] = true;
402 $wgGroupPermissions['autoconfirmed']['skipcaptcha'] = true;
403 EOF
404 if ! grep -Fx 'foreach ( $localSettingsQuestyQuestions as $key => $value ) {' $mwc; then
405 tee -a $mwc <<'EOF'
406 foreach ( $localSettingsQuestyQuestions as $key => $value ) {
407 $wgCaptchaQuestions[] = array( 'question' => $key, 'answer' => $value );
408 }
409 EOF
410 fi
411 # </source>
412 # <source lang="bash">
413 sed -i "/\\\$wgGroupPermissions\\['\\*'\\]\\['createaccount'\\] = false;/d" $mwc
414 # </source>
415 # <source lang="bash">
416 # get repo
417 if [[ ! -e ~/pywikibot/.git ]]; then
418 git clone --recursive \
419 https://gerrit.wikimedia.org/r/pywikibot/core.git ~/pywikibot
420 fi
421 cd ~/pywikibot
422 #updating
423 git pull --all
424 git submodule update
425 # </source>
426 # <source lang="bash">
427 cd $HOME/pywikibot
428 dd of=user-config.py <<EOF
429 mylang = 'en'
430 usernames["$mwfamily"]['en'] = u'$wikiuser'
431 family = "$mwfamily"
432 console_encoding = 'utf-8'
433 password_file = "secretsfile"
434 EOF
435
436 dd of=secretsfile <<EOF
437 ("$wikiuser", "$wikipass")
438 EOF
439
440 # it won't overrwrite an existing file. Remove if if one exists
441 rm -f pywikibot/families/${mwfamily}_family.py
442 if isdeb; then
443 apt-get install -y python-requests
444 else
445 yum -y install python-requests
446 fi
447
448 python generate_family_file.py https://$mwdomain/wiki/Main_Page "$mwfamily"
449
450 # Note, this needed only for ssl site
451 tee -a pywikibot/families/${mwfamily}_family.py<<'EOF'
452 def protocol(self, code):
453 return 'https'
454 EOF
455 # </source>
456 # <source lang="bash">
457 cd "$HOME/pywikibot"
458
459 dd of=scripts/${mwfamily}_setup.py<<EOF
460 import pywikibot
461 import time
462 import sys
463 site = pywikibot.Site()
464 def x(p, t=""):
465 page = pywikibot.Page(site, p)
466 page.text = t
467 #force is for some anti-bot thing, not necessary in my testing, but might as well include it
468 page.save(force=True)
469
470 # Small/medium noncommercial wiki should be fine with no privacy policy
471 # based on https://www.mediawiki.org/wiki/Manual:Footer
472 x("MediaWiki:Privacy")
473
474 # licenses for uploads. Modified from the mediawiki's wiki
475 x("MediaWiki:Licenses", u"""* Same as this wiki's text (preferred)
476 ** CC BY-SA or GFDL| Creative Commons Attribution ShareAlike or GNU Free Documentation License
477 * Others:
478 ** Unknown_copyright|I don't know exactly
479 ** PD|PD: public domain
480 ** CC BY|Creative Commons Attribution
481 ** CC BY-SA|Creative Commons Attribution ShareAlike
482 ** GFDL|GFDL: GNU Free Documentation License
483 ** GPL|GPL: GNU General Public License
484 ** LGPL|LGPL: GNU Lesser General Public License""")
485 x("MediaWiki:Copyright", '$mw_license')
486 x("MediaWiki:Mainpage-description", "$mwdescription")
487
488
489
490 # The rest of the settings are for the site style
491
492 # Remove various clutter
493 x("MediaWiki:Lastmodifiedat")
494 x("MediaWiki:Disclaimers")
495 x("MediaWiki:Viewcount")
496 x("MediaWiki:Aboutsite")
497 # remove these lines from sidebar
498 # ** recentchanges-url|recentchanges
499 # ** randompage-url|randompage
500 # ** helppage|help
501 x("MediaWiki:Sidebar", """* navigation
502 ** mainpage|mainpage-description
503 * SEARCH
504 * TOOLBOX
505 * LANGUAGES""")
506
507 # remove side panel
508 # helpfull doc: https://www.mediawiki.org/wiki/Manual:Interface/Sidebar
509 x("mediawiki:Common.css", """/* adjust sidebar to just be home link and up top */
510 /* panel width increased to fit full wiki name. */
511 div#mw-panel { top: 10px; padding-top: 0em; width: 20em }
512 div#footer, #mw-head-base, div#content { margin-left: 1em; }
513 #left-navigation { margin-left: 1em; }
514
515
516 /* logo, and toolbar hidden */
517 #p-logo, #p-tb.portal {
518 display:none;
519 }
520
521 /* make the font size smaller for the misc stuff */
522 #p-personal {
523 font-size: 0.8em;
524 }
525
526 #footer-info {
527 font-size: 0.8em;
528 }
529 div#mw-content-text {
530 max-width: 720px;
531 }
532 """)
533 EOF
534
535 python pwb.py ${mwfamily}_setup
536 # </source>
537 # <source lang="bash">
538 s=/etc/cron.daily/mediawiki_update
539 dd of=$s<<'EOF'
540 #!/bin/bash
541 source ~/mw_vars
542 cd $mw
543 git fetch --all
544 git checkout origin/$mw_branch
545 git rebase ian/REL1_23-toolbox-in-dropdown
546 cd extensions
547 for x in *; do
548 if [[ -d $x ]]; then
549 cd $x
550 git fetch --all
551 git checkout origin/$mw_branch || git checkout -qf origin/master
552 cd ..
553 fi
554 done
555 php $mw/maintenance/update.php -q
556 EOF
557 chmod +x $s
558 # </source>