get latest fix
[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 --follow-symlinks '#^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 --follow-symlinks '/^wfLoadSkin/d' $mwc
61 sed -i --follow-symlinks '/^\$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 --follow-symlinks '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 service apache2 restart
317
318 cd $mw/extensions/Math/math; make # makes texvc
319 cd $mw/extensions/Math/texvccheck; make
320
321 teeu $mwc <<'EOF'
322 # Enable MathJax as rendering option
323 $wgUseMathJax = true;
324 # Enable LaTeXML as rendering option
325 $wgMathValidModes[] = 'latexml';
326 # Set LaTeXML as default rendering option, because it is nicest
327 $wgDefaultUserOptions['math'] = 'latexml';
328 EOF
329 # </source>
330 # <source lang="bash">
331 mw-ext SpamBlacklist
332 if ! grep -F '$wgSpamBlacklistFiles = array(' $mwc &>/dev/null; then
333 tee -a $mwc <<'EOF'
334 $wgEnableDnsBlacklist = true;
335 $wgDnsBlacklistUrls = array( 'xbl.spamhaus.org', 'dnsbl.tornevall.org' );
336
337 ini_set( 'pcre.backtrack_limit', '10M' );
338 $wgSpamBlacklistFiles = array(
339 "[[m:Spam blacklist]]",
340 "http://en.wikipedia.org/wiki/MediaWiki:Spam-blacklist"
341 );
342 EOF
343 fi
344 # </source>
345 # <source lang="bash">
346 mw-ext TitleBlacklist
347 if ! grep -F '$wgTitleBlacklistSources = array(' $mwc &>/dev/null; then
348 tee -a $mwc <<'EOF'
349 $wgTitleBlacklistSources = array(
350 array(
351 'type' => 'local',
352 'src' => 'MediaWiki:Titleblacklist',
353 ),
354 array(
355 'type' => 'url',
356 'src' => 'http://meta.wikimedia.org/w/index.php?title=Title_blacklist&action=raw',
357 ),
358 );
359 EOF
360 fi
361 # </source>
362 # <source lang="bash">
363 mw-ext WikiEditor
364 teeu $mwc <<'EOF'
365 # Enable Wikieditor by default
366 $wgDefaultUserOptions['usebetatoolbar'] = 1;
367 $wgDefaultUserOptions['usebetatoolbar-cgd'] = 1;
368
369 # Display the Preview and Changes tabs
370 $wgDefaultUserOptions['wikieditor-preview'] = 1;
371 EOF
372 # </source>
373 # <source lang="bash">
374 mw-ext CategoryTree
375 teeu $mwc <<'EOF'
376 # Mediawiki setting dependency for CategoryTree
377 $wgUseAjax = true;
378 EOF
379 # </source>
380 # <source lang="bash">
381 mw-ext AbuseFilter
382 teeu $mwc<<'EOF'
383 $wgGroupPermissions['sysop']['abusefilter-modify'] = true;
384 $wgGroupPermissions['*']['abusefilter-log-detail'] = true;
385 $wgGroupPermissions['*']['abusefilter-view'] = true;
386 $wgGroupPermissions['*']['abusefilter-log'] = true;
387 $wgGroupPermissions['sysop']['abusefilter-private'] = true;
388 $wgGroupPermissions['sysop']['abusefilter-modify-restricted'] = true;
389 $wgGroupPermissions['sysop']['abusefilter-revert'] = true;
390 EOF
391 # </source>
392 # <source lang="bash">
393 mw-ext ConfirmEdit
394 captchaArray
395 teeu $mwc <<'EOF'
396 wfLoadExtension( 'ConfirmEdit/QuestyCaptcha' );
397 $wgCaptchaClass = 'QuestyCaptcha';
398 # only captcha on registration
399 $wgGroupPermissions['user' ]['skipcaptcha'] = true;
400 $wgGroupPermissions['autoconfirmed']['skipcaptcha'] = true;
401 EOF
402 if ! grep -Fx 'foreach ( $localSettingsQuestyQuestions as $key => $value ) {' $mwc; then
403 tee -a $mwc <<'EOF'
404 foreach ( $localSettingsQuestyQuestions as $key => $value ) {
405 $wgCaptchaQuestions[] = array( 'question' => $key, 'answer' => $value );
406 }
407 EOF
408 fi
409 # </source>
410 # <source lang="bash">
411 sed -i --follow-symlinks "/\\\$wgGroupPermissions\\['\\*'\\]\\['createaccount'\\] = false;/d" $mwc
412 # </source>
413 # <source lang="bash">
414 # get repo
415 if [[ ! -e ~/pywikibot/.git ]]; then
416 git clone --recursive \
417 https://gerrit.wikimedia.org/r/pywikibot/core.git ~/pywikibot
418 fi
419 cd ~/pywikibot
420 #updating
421 git pull --all
422 git submodule update
423 # </source>
424 # <source lang="bash">
425 cd $HOME/pywikibot
426 dd of=user-config.py <<EOF
427 mylang = 'en'
428 usernames["$mwfamily"]['en'] = u'$wikiuser'
429 family = "$mwfamily"
430 console_encoding = 'utf-8'
431 password_file = "secretsfile"
432 EOF
433
434 dd of=secretsfile <<EOF
435 ("$wikiuser", "$wikipass")
436 EOF
437
438 # it won't overrwrite an existing file. Remove if if one exists
439 rm -f pywikibot/families/${mwfamily}_family.py
440 if isdeb; then
441 apt-get install -y python-requests
442 else
443 yum -y install python-requests
444 fi
445
446 python generate_family_file.py https://$mwdomain/wiki/Main_Page "$mwfamily"
447
448 # Note, this needed only for ssl site
449 tee -a pywikibot/families/${mwfamily}_family.py<<'EOF'
450 def protocol(self, code):
451 return 'https'
452 EOF
453 # </source>
454 # <source lang="bash">
455 cd "$HOME/pywikibot"
456
457 dd of=scripts/${mwfamily}_setup.py<<EOF
458 import pywikibot
459 import time
460 import sys
461 site = pywikibot.Site()
462 def x(p, t=""):
463 page = pywikibot.Page(site, p)
464 page.text = t
465 #force is for some anti-bot thing, not necessary in my testing, but might as well include it
466 page.save(force=True)
467
468 # Small/medium noncommercial wiki should be fine with no privacy policy
469 # based on https://www.mediawiki.org/wiki/Manual:Footer
470 x("MediaWiki:Privacy")
471
472 # licenses for uploads. Modified from the mediawiki's wiki
473 x("MediaWiki:Licenses", u"""* Same as this wiki's text (preferred)
474 ** CC BY-SA or GFDL| Creative Commons Attribution ShareAlike or GNU Free Documentation License
475 * Others:
476 ** Unknown_copyright|I don't know exactly
477 ** PD|PD: public domain
478 ** CC BY|Creative Commons Attribution
479 ** CC BY-SA|Creative Commons Attribution ShareAlike
480 ** GFDL|GFDL: GNU Free Documentation License
481 ** GPL|GPL: GNU General Public License
482 ** LGPL|LGPL: GNU Lesser General Public License""")
483 x("MediaWiki:Copyright", '$mw_license')
484 x("MediaWiki:Mainpage-description", "$mwdescription")
485
486
487
488 # The rest of the settings are for the site style
489
490 # Remove various clutter
491 x("MediaWiki:Lastmodifiedat")
492 x("MediaWiki:Disclaimers")
493 x("MediaWiki:Viewcount")
494 x("MediaWiki:Aboutsite")
495 # remove these lines from sidebar
496 # ** recentchanges-url|recentchanges
497 # ** randompage-url|randompage
498 # ** helppage|help
499 x("MediaWiki:Sidebar", """* navigation
500 ** mainpage|mainpage-description
501 * SEARCH
502 * TOOLBOX
503 * LANGUAGES""")
504
505 # remove side panel
506 # helpfull doc: https://www.mediawiki.org/wiki/Manual:Interface/Sidebar
507 x("mediawiki:Common.css", """/* adjust sidebar to just be home link and up top */
508 /* panel width increased to fit full wiki name. */
509 div#mw-panel { top: 10px; padding-top: 0em; width: 20em }
510 div#footer, #mw-head-base, div#content { margin-left: 1em; }
511 #left-navigation { margin-left: 1em; }
512
513
514 /* logo, and toolbar hidden */
515 #p-logo, #p-tb.portal {
516 display:none;
517 }
518
519 /* make the font size smaller for the misc stuff */
520 #p-personal {
521 font-size: 0.8em;
522 }
523
524 #footer-info {
525 font-size: 0.8em;
526 }
527 div#mw-content-text {
528 max-width: 720px;
529 }
530 """)
531 EOF
532
533 python pwb.py ${mwfamily}_setup
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 cd $mw
541 git fetch --all
542 git checkout origin/$mw_branch
543 git rebase ian/REL1_23-toolbox-in-dropdown
544 cd extensions
545 for x in *; do
546 if [[ -d $x ]]; then
547 cd $x
548 git fetch --all
549 git checkout origin/$mw_branch || git checkout -qf origin/master
550 cd ..
551 fi
552 done
553 php $mw/maintenance/update.php -q
554 EOF
555 chmod +x $s
556 # </source>