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