public function execute($args = array())
 {
     $this->logger->message('Starting import.', 10);
     try {
         $parser = new ParserController(array('logger' => $this->logger, 'db' => &$this->db, 'import_data_dir' => $this->options['d']));
         // TODO: Do edition stuff
         $edition_args = array();
         $edition_args['edition_option'] = 'existing';
         $edition = $parser->get_current_edition();
         $edition_args['edition'] = $edition->id;
         /*
          * Step through each parser method.
          */
         if ($parser->test_environment() !== FALSE) {
             echo 'Environment test succeeded<br />';
             if ($parser->populate_db() !== FALSE) {
                 $edition_errors = $parser->handle_editions($edition_args);
                 if (count($edition_errors) > 0) {
                     throw new Exception(join("\n", $edition_errors), E_ERROR);
                 } else {
                     $parser->clear_cache();
                     $parser->clear_edition($edition->id);
                     /*
                      * We should only continue if parsing was successful.
                      */
                     if ($parser->parse()) {
                         $parser->build_permalinks();
                         $parser->write_api_key();
                         $parser->export();
                         $parser->generate_sitemap();
                         $parser->index_laws();
                         $parser->structural_stats_generate();
                         $parser->prune_views();
                         $parser->finish_import();
                     }
                 }
             }
         }
         /*
          * Attempt to purge Varnish's cache. (Fails silently if Varnish isn't installed or running.)
          */
         $varnish = new Varnish();
         $varnish->purge();
         $this->logger->message('Done.', 10);
     } catch (Exception $e) {
         exit(1);
     }
 }
Example #2
0
function show_admin_forms($args = array())
{
    $parser = new ParserController($args);
    $editions = $parser->get_editions();
    if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) {
        $base_url = 'https://';
    } else {
        $edition_url_base = 'http://';
    }
    $edition_url_base .= $_SERVER['SERVER_NAME'];
    if ($_SERVER['SERVER_PORT'] != 80) {
        $edition_url_base .= ':' . $_SERVER['SERVER_PORT'];
    }
    $edition_url_base .= '/';
    $body = '<p>What do you want to do?</p>

	<form method="post" action="/admin/?page=parse&noframe=1">
		<h3>Import Data</h3>';
    if (isset($args['import_errors'])) {
        $body .= '<div class="errors">
			Please fix the following errors:
			<ul>';
        foreach ($args['import_errors'] as $error) {
            $body .= '<li>' . $error . '</li>';
        }
        $body .= '</div>';
    }
    $body .= '<p>This will import all data files from your data directory:
		<span class="code">' . IMPORT_DATA_DIR . '</span></p>
		<input type="hidden" name="action" value="parse" />
			<div class="option">
				<input type="radio" class="radio" name="edition_option"
					id="edition_option_new" value="new"';
    if (!$editions || $args['edition_option'] == 'existing') {
        $body .= 'checked="checked"';
    }
    $body .= '/>
				<label for="edition_option_new">I want to create a new edition of the laws.</label>

				<div class="suboption">
					<label for="new_edition_name">I want to call this edition</label>
					<div>
						<input type="text" class="text" name="new_edition_name"
							id="new_edition_name" placeholder="' . date('Y-m') . '"
							value="' . (isset($args['new_edition_name']) ? $args['new_edition_name'] : '') . '"
							/>
					</div>
				</div>
				<div class="suboption">
					<label for="new_edition_slug">.&thinsp;.&thinsp;.</option> and the URL for this edition will be</label>
					<div class="edition_url">' . $edition_url_base . '
						<input type="text" class="text" name="new_edition_slug"
							id="new_edition_slug" placeholder="' . date('Y-m') . '"
							value="' . (isset($args['new_edition_slug']) ? $args['new_edition_slug'] : '') . '"
							/> /
					</div>
					<p class="note">Note: In general, try to only use letters, numbers, and hyphens,
					and avoid using anything that might conflict with a section number or structure
					name.</p>
				</div>
			</div>
			<div class="option">
				<input type="radio" class="radio" name="edition_option"
					id="edition_option_existing" value="existing"';
    if (!isset($editions) || $editions === FALSE) {
        $body .= 'disabled="disabled"';
    } elseif ($args['edition_option'] == 'existing') {
        $body .= 'checked="checked"';
    }
    $body .= '/><label for="edition_option_existing">I want to update an existing edition of the laws.</label>';
    if ($editions !== FALSE) {
        $body .= '<div class="suboption">
					<select name="edition" value="edition">
						<option value="">Choose Edition .&thinsp;.&thinsp;.</option>';
        foreach ($editions as $edition) {
            $body .= '<option value="' . $edition->id . '"';
            if ($args['edition'] == $edition->id) {
                $body .= ' select="selected"';
            }
            $body .= '>' . $edition->name . '</option>';
        }
        $body .= '</select>
			</div>';
    } else {
        $body .= '<div class="suboption">
			You don’t have any editions yet, you’ll need to create a new one.
		</div>';
    }
    $body .= '</div>
		<div class="option">
			<input type="checkbox" class="checkbox" name="make_current"
				id="make_current" value="1"';
    if (!$editions) {
        $body .= 'checked="checked"';
    }
    $body .= ' />
			<label for="make_current">Make this edition current.</label>
		</div>
		<input type="submit" value="Import" />
	</form>

	<form method="post" action="/admin/?page=parse&noframe=1">
		<h3>Empty the Database</h3>
		<p>Remove all data from the database. (This leaves database tables intact.)</p>

		<input type="hidden" name="action" value="empty" />
		<input type="submit" value="Empty the Database" />
	</form>

	<form method="post" action="/admin/?page=parse&noframe=1">
		<h3>Rebuild Permalinks</h3>
		<p>Completely rebuild all permalinks for all editions of code.</p>

		<input type="hidden" name="action" value="permalinks" />
		<input type="submit" value="Rebuild Permalinks" />
	</form>

	<form method="post" action="/admin/?page=parse&noframe=1">
		<h3>Test Your Environment</h3>
		<p>Verifies that the State Decoded will run on the current server
			environment.</p>

		<input type="hidden" name="action" value="test_environment" />
		<input type="submit" value="Test Environment" />
	</form>';
    /*
     * If APC is running, provide an option to clear the cache.
     */
    if (APC_RUNNING === TRUE) {
        $body .= '
			<form method="post" action="/admin/?page=parse&noframe=1">
				<h3>Clear the APC Cache</h3>
				<p>Delete all data currently stored in APC.</p>
				<input type="hidden" name="action" value="apc" />
				<input type="submit" value="Clear APC Cache" />
			</form>';
    }
    return $body;
}
Example #3
0
 * Require that the user log in.
 */
if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) || $_SERVER['PHP_AUTH_USER'] != ADMIN_USERNAME || $_SERVER['PHP_AUTH_PW'] != ADMIN_PASSWORD) {
    Header('WWW-Authenticate: Basic realm="The State Decoded Admin"');
    Header('HTTP/1.0 401 Unauthorized');
    echo '<html><body>
        <h1>Rejected</h1>
        <big>Wrong Username or Password</big>
        </body></html>';
    exit;
}
/*
 * Create a new parser controller.
 */
global $db;
$parser = new ParserController(array('logger' => $logger, 'db' => &$db, 'import_data_dir' => IMPORT_DATA_DIR));
if (isset($_GET['noframe'])) {
    /*
     * Begin the flush immediately, by sending the content type header.
     */
    header('Content-type: text/html; charset=utf-8');
    /*
     * Then we get the template (which contains just the head)
     * much earlier than usual.
     */
    $content = new Content();
    $template = Template::create('admin_frame');
    $template->parse($content);
}
/*
 * When first loading the page, show options.