<commit_before>'use strict';

var LAMPApp = require('./LAMPApp');
var configFiles = require('./wordpress/config');
var constants = require('./constants');


class WordPressApp extends LAMPApp {


  /**
   * Options (used by this task):
   *   @param {object} container - An instantiated and configured Container object.
   *   @param {object} options - A hash of configuration options specific to this task.
   *   @param {string} options.devDomain - The url of the dev site. This is replaced by the probo url in the db.
   *   @param {string} options.devHome - The homepage url of the dev site (including the domain). This is replaced by
   *      the probo url in the db.
   *   @param {string} options.database - The filename of the database to import if specified. Note that this database
   *      *must be added to the assets array separately*.
   *   @param {string} [options.databaseName] - The name of the database. Defaults to 'wordpress'.
   *   @param {boolean} [options.databaseGzipped] - Whether the database was sent gzipped and whether it should therefore
   *      be gunzipped before importing.
   *   @param {string} [options.subDirectory] - The directory of the actual web root (defaults to 'docroot').
   *   @param {boolean} [options.flushCaches] - Whether to flush the cache.
   *   @augments LAMPApp
   */
  constructor(container, options) {
    super(container, options);
    this.databaseName = options.databaseName || constants.WORDPRESS_DATABASE_NAME;

    this.options.siteFolder = options.siteFolder || 'default';
    this.options.profileName = options.profileName || 'standard';
    this.options.flushCaches = (options.flushCaches || typeof options.flushCaches === 'undefined');
    this.options.devHome = options.devHome;
    this.options.devDomain = options.devDomain;
    // TODO: Add some kind of validation.

    // Filter out secret strings
    options.secrets = [
    ];

    // Allow for subdirectory to be explicitly set to "" without being overwritten for being falsy.
    this.subDirectory = options.subDirectory || 'docroot';
    this.script = [];
    this.populateScriptArray();
    this.setScript(this.script);
  }

  description() {
    return `${this.plugin} 'Provisioning WordPress!'`;
  }

  /**
   *
   */
  populateScriptArray() {
    this.addScriptSetup();
    this.addScriptSymlinks();
    this.addScriptCreateDatbase();
    this.addScriptImportDatabase();
    this.addScriptAppendWPConfigSettings();
    this.addScriptReplaceDomain();
    if (this.options.flushCaches) {
      this.addScriptFlushCaches();
    }
    this.addScriptApachePhp();
  }

  addScriptAppendWPConfigSettings() {
    this.script = this.script.concat([
      // create a wp-config file if needed and insert the snippet at the correct line number
      `if [ ! -a "/var/www/html/wp-config.php" ] ; then`,
      `  echo "${configFiles.wpDefaultConfig}" > /var/www/html/wp-config.php`,
      'fi',
      // Prepend the probo-config.php file to the wp-config file.
      `sed -i "1i <?php require('probo-config.php'); ?>" /var/www/html/wp-config.php`,

      // Create the probo-config file to override any user defined settings.
      `echo "<?php`,
      `define('DB_NAME', '${this.databaseName}');`,
      `define('DB_USER', '${constants.DATABASE_USER}');`,
      `define('DB_PASSWORD', '${constants.DATABASE_PASSWORD}');`,
      `define('DB_HOST', 'localhost');`,
      `?>" >> /var/www/html/probo-config.php;`,
    ]);
  }

  addScriptUpdatePlugins() {
    this.script.push('cd $SRC_DIR ; wp plugin update --allow-root');
  }

  addScriptFlushCaches() {
    this.script.push('cd $SRC_DIR ; wp cache flush --allow-root');
  }

  addScriptReplaceDomain() {
    this.script.push(`export DEV_HOME=${this.options.devHome}`);
    this.script.push(`export DEV_DOMAIN=${this.options.devDomain}`);

    // flatten home page url first, so that it points to probo root

    this.script.push(this.replaceOption('$BUILD_DOMAIN', 'home'));
    this.script.push(this.replaceTextDb('$DEV_HOME', '$BUILD_DOMAIN'));
<commit_msg>Ensure the uploads directory is writable.<commit_after>'use strict';

var LAMPApp = require('./LAMPApp');
var configFiles = require('./wordpress/config');
var constants = require('./constants');


class WordPressApp extends LAMPApp {


  /**
   * Options (used by this task):
   *   @param {object} container - An instantiated and configured Container object.
   *   @param {object} options - A hash of configuration options specific to this task.
   *   @param {string} options.devDomain - The url of the dev site. This is replaced by the probo url in the db.
   *   @param {string} options.devHome - The homepage url of the dev site (including the domain). This is replaced by
   *      the probo url in the db.
   *   @param {string} options.database - The filename of the database to import if specified. Note that this database
   *      *must be added to the assets array separately*.
   *   @param {string} [options.databaseName] - The name of the database. Defaults to 'wordpress'.
   *   @param {boolean} [options.databaseGzipped] - Whether the database was sent gzipped and whether it should therefore
   *      be gunzipped before importing.
   *   @param {string} [options.subDirectory] - The directory of the actual web root (defaults to 'docroot').
   *   @param {boolean} [options.flushCaches] - Whether to flush the cache.
   *   @augments LAMPApp
   */
  constructor(container, options) {
    super(container, options);
    this.databaseName = options.databaseName || constants.WORDPRESS_DATABASE_NAME;

    this.options.siteFolder = options.siteFolder || 'default';
    this.options.profileName = options.profileName || 'standard';
    this.options.flushCaches = (options.flushCaches || typeof options.flushCaches === 'undefined');
    this.options.devHome = options.devHome;
    this.options.devDomain = options.devDomain;
    // TODO: Add some kind of validation.

    // Filter out secret strings
    options.secrets = [
    ];

    // Allow for subdirectory to be explicitly set to "" without being overwritten for being falsy.
    this.subDirectory = options.subDirectory || 'docroot';
    this.script = [];
    this.populateScriptArray();
    this.setScript(this.script);
  }

  description() {
    return `${this.plugin} 'Provisioning WordPress!'`;
  }

  /**
   *
   */
  populateScriptArray() {
    this.addScriptSetup();
    this.addScriptSymlinks();
    this.addScriptCreateDatbase();
    this.addScriptImportDatabase();
    this.addScriptAppendWPConfigSettings();
    this.addScriptFixFilePerms();
    this.addScriptReplaceDomain();
    if (this.options.flushCaches) {
      this.addScriptFlushCaches();
    }
    this.addScriptApachePhp();
  }

  addScriptAppendWPConfigSettings() {
    this.script = this.script.concat([
      // create a wp-config file if needed and insert the snippet at the correct line number
      `if [ ! -a "/var/www/html/wp-config.php" ] ; then`,
      `  echo "${configFiles.wpDefaultConfig}" > /var/www/html/wp-config.php`,
      'fi',
      // Prepend the probo-config.php file to the wp-config file.
      `sed -i "1i <?php require('probo-config.php'); ?>" /var/www/html/wp-config.php`,

      // Create the probo-config file to override any user defined settings.
      `echo "<?php`,
      `define('DB_NAME', '${this.databaseName}');`,
      `define('DB_USER', '${constants.DATABASE_USER}');`,
      `define('DB_PASSWORD', '${constants.DATABASE_PASSWORD}');`,
      `define('DB_HOST', 'localhost');`,
      `?>" >> /var/www/html/probo-config.php;`,
    ]);
  }

  addScriptUpdatePlugins() {
    this.script.push('cd $SRC_DIR ; wp plugin update --allow-root');
  }

  addScriptFlushCaches() {
    this.script.push('cd $SRC_DIR ; wp cache flush --allow-root');
  }

  addScriptFixFilePerms() {
    this.script = this.script.concat([
      // Ensure we have an uploads directory and that it is writable.
      `mkdir -p /var/www/html/wp-content/uploads`,
      `chown www-data:www-data /var/www/html/wp-content/uploads`,
      `chmod 755 /var/www/html/wp-content/uploads`,
    ]);
  }

  addScriptReplaceDomain() {
    // flatten home page url first, so that it points to probo root

    this.script.push(this.replaceOption('$BUILD_DOMAIN', 'home'));
    this.script.push(this.replaceTextDb('$DEV_HOME', '$BUILD_DOMAIN'));

{'fraction_non_alphanumeric': 0.13341346153846154, 'fraction_numerical': 0.0006009615384615385, 'mean_word_length': 3.2936016511867905, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 13, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4425579', 'n_tokens_mistral': 2516, 'n_tokens_neox': 2475, 'n_words': 673}