- name: 'Install packages: php{{ php_version }}-intl, php{{ php_version }}-mbstring'
  package:
    name:
      - "php{{ php_version }}-intl"
      - "php{{ php_version }}-mbstring"
    state: present

- name: Download {{ mediawiki_download_base_url }}/{{ mediawiki_src }} to {{ downloads_dir }}
  get_url:
    url: "{{ mediawiki_download_base_url }}/{{ mediawiki_src }}"
    dest: "{{ downloads_dir }}"    # /opt/iiab/downloads
    timeout: "{{ download_timeout }}"
    #force: yes
    #backup: yes
  when: internet_available

- name: Unarchive (unpack) it to permanent location {{ mediawiki_abs_path }} ({{ apache_user }}:{{ apache_user }}, u+rw,g+r,o+r)
  unarchive:
    src: "{{ downloads_dir }}/{{ mediawiki_src }}"
    dest: "{{ mediawiki_install_path }}"    # /library
    owner: "{{ apache_user }}"
    group: "{{ apache_user }}"
    mode: u+rw,g+r,o+r    # '0755' forced executable bits on files
    keep_newer: yes

- name: Symlink {{ doc_root }}/{{ mediawiki_symlink }} -> {{ mediawiki_abs_path }}
  file:
    src: "{{ mediawiki_abs_path }}"    # /library/mediawiki-1.XY.Z
    path: "{{ doc_root }}/{{ mediawiki_symlink }}"    # /library/www/html/w
    state: link

- name: Start MySQL systemd service ({{ mysql_service }}) so we can create db
  systemd:
    name: "{{ mysql_service }}"
    state: started

- name: Create MySQL database {{ mediawiki_db_name }}
  mysql_db:
    name: "{{ mediawiki_db_name }}"    # iiab_mediawiki
    #state: present

- name: Create MySQL database user {{ mediawiki_db_user }} with password, and permissions to above db
  mysql_user:
    name: "{{ mediawiki_db_user }}"    # iiab_mediawiki_user
    password: "{{ mediawiki_db_user_password }}"
    priv: "{{ mediawiki_db_name }}.*:ALL,GRANT"
    #state: present

- name: Configure MediaWiki, if {{ mediawiki_abs_path }}/LocalSettings.php doesn't exist
  shell: >
    php '{{ mediawiki_abs_path }}/maintenance/install.php'
    --dbname={{ mediawiki_db_name }}
    --dbserver="localhost"
    --installdbuser={{ mediawiki_db_user }}
    --installdbpass={{ mediawiki_db_user_password }}
    --dbuser={{ mediawiki_db_user }}
    --dbpass={{ mediawiki_db_user_password }}
    --scriptpath=/{{ mediawiki_symlink }}
    --lang=en
    --pass={{ mediawiki_admin_user_password }}
    "{{ mediawiki_site_name }}"
    "{{ mediawiki_admin_user }}"
  args:
    chdir: "{{ mediawiki_abs_path }}"    # /library/mediawiki-1.XY.Z
    creates: "{{ mediawiki_abs_path }}/LocalSettings.php"

- name: Configure wgArticlePath variable in {{ mediawiki_abs_path }}/LocalSettings.php
  lineinfile:
    path: "{{ mediawiki_abs_path }}/LocalSettings.php"    # /library/mediawiki-1.XY.Z
    line: '$wgArticlePath = "/wiki/$1";'

- name: Configure $wgUsePathInfo variable in {{ mediawiki_abs_path }}/LocalSettings.php
  lineinfile:
    path: "{{ mediawiki_abs_path }}/LocalSettings.php"    # /library/mediawiki-1.XY.Z
    line: '$wgUsePathInfo = true;'

- name: Configure $wgServer variable in {{ mediawiki_abs_path }}/LocalSettings.php
  lineinfile:
    path: "{{ mediawiki_abs_path }}/LocalSettings.php"    # /library/mediawiki-1.XY.Z
    regexp: '^\$wgServer ='
    line: '$wgServer = "//" . $_SERVER["HTTP_HOST"];'

- name: 'Install from template: /etc/{{ apache_conf_dir }}/mediawiki.conf -- for http://box{{ mediawiki_url }}'
  template:
    src: mediawiki.conf.j2
    dest: "/etc/{{ apache_conf_dir }}/mediawiki.conf"    # apache2/sites-available
  when: apache_installed is defined


# RECORD MediaWiki AS INSTALLED

- name: "Set 'mediawiki_installed: True'"
  set_fact:
    mediawiki_installed: True

- name: "Add 'mediawiki_installed: True' to {{ iiab_state_file }}"
  lineinfile:
    path: "{{ iiab_state_file }}"    # /etc/iiab/iiab_state.yml
    regexp: '^mediawiki_installed'
    line: 'mediawiki_installed: True'
