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

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

- name: Unpack it to permanent location {{ mediawiki_abs_path }}
  unarchive:
    src: "{{ downloads_dir }}/{{ mediawiki_src }}"
    dest: "{{ mediawiki_install_path }}"
    owner: root
    group: "{{ apache_user }}"
    mode: 0755
    keep_newer: yes

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

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

- name: Create MySQL database user {{ mediawiki_db_user }}
  mysql_user:
    name: "{{ mediawiki_db_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
    --lang=en
    --pass={{ mediawiki_admin_user_password }}
    "{{ mediawiki_site_name }}"
    "{{ mediawiki_admin_user }}"
  args:
    chdir: "{{ mediawiki_abs_path }}"
    creates: "{{ mediawiki_abs_path }}/LocalSettings.php"

- name: Install /etc/{{ apache_config_dir }}/mediawiki.conf from template, for http://box{{ mediawiki_url }}
  template:
    src: mediawiki.conf.j2
    dest: "/etc/{{ apache_config_dir }}/mediawiki.conf"

- name: Create softlink mediawiki.conf from sites-enabled to sites-available, if mediawiki_enabled (debuntu)
  file:
    src: /etc/apache2/sites-available/mediawiki.conf
    dest: /etc/apache2/sites-enabled/mediawiki.conf
    state: link
  when: mediawiki_enabled and is_debuntu

- name: Remove mediawiki.conf if not mediawiki_enabled (debuntu)
  file:
    path: /etc/apache2/sites-enabled/mediawiki.conf
    state: absent
  when: not mediawiki_enabled and is_debuntu

- name: Restart Apache service ({{ apache_service }}) to enable/disable http://box{{ mediawiki_url }}
  systemd:
    name: "{{ apache_service }}"
    state: restarted

- name: Add 'mediawiki' variable values to {{ iiab_ini_file }}
  ini_file:
    path: "{{ iiab_ini_file }}"
    section: mediawiki
    option: "{{ item.option }}"
    value: "{{ item.value }}"
  with_items:
    - option: name
      value: mediawiki
    - option: description
      value: '"mediawiki is a blog and web site management application."'
    - option: mediawiki_src
      value: "{{ mediawiki_src }}"
    - option: mediawiki_abs_path
      value: "{{ mediawiki_abs_path }}"
    - option: mediawiki_db_name
      value: "{{ mediawiki_db_name }}"
    - option: mediawiki_db_user
      value: "{{ mediawiki_db_user }}"
    - option: mediawiki_url
      value: "{{ mediawiki_url }}"
    - option: mediawiki_full_url
      value: "{{ mediawiki_full_url }}"
    - option: mediawiki_enabled
      value: "{{ mediawiki_enabled }}"
