error_reporting(E_ALL);
require_once dirname(dirname(dirname(__FILE__))) . "/maintenance/commandLine.inc";
if (!$wgFundraiserPortalDirectory) {
    echo "\$wgFundraiserProtalDirectory isn't set -- we're not configured to build static templates.";
    die;
}
if (isset($options['help'])) {
    echo "Rebuild all static button templates\n";
    echo "Usage:\n";
    echo "  php extensions/FundraiserPortal/rebuildButtons.php [-o|Output to disk]\n";
} else {
    echo "Rebuilding button templates ...\n";
    $builder = new DonateButton();
    $js = $builder->getJsOutput();
    if (isset($options['o'])) {
        $lang = $wgLang->getCode();
        $outputDir = "{$wgFundraiserPortalDirectory}/wikipedia/{$lang}";
        if (wfMkDirParents($outputDir, null, __METHOD__)) {
            $outputFile = "{$outputDir}/fundraiserportal.js";
            $ok = file_put_contents($outputFile, $js);
            if (!$ok) {
                echo "FAILED to write {$outputFile}!\n";
            }
        } else {
            echo "FAILED to create {$outputDir}!\n";
        }
    } else {
        echo $js;
    }
}
	/**
	 * Render the LaTeX equation as PNG and store it into
	 * $wgTrustedMathDirectory under a two level hash path. Will not re-render
	 * if the file already exists
	 *
	 * @param bool $force Force re-rendering the file
	 * @return Status Status object with $value set to the relative path of
	 * the rendered equation on success.
	 */
	public function render( $force = false ) {
		// Determine output path
		$hash = $this->getHash();
		$hashPath = $this->getHashPath();
		$filePath = "{$this->dir}/$hashPath/$hash.png";

		// Check equation existence
		if ( !$force && file_exists( $filePath ) &&
				filesize( $filePath ) > 0 ) {
			return Status::newGood( "$hashPath/$hash.png" );
		}

		// Create the hash path
		wfSuppressWarnings();
		if ( !wfMkDirParents( "{$this->dir}/$hashPath", null, __METHOD__ ) ) {
			wfRestoreWarnings();
			return Status::newFatal( 'trustedmath-path-error', $hashPath );
		}
		wfRestoreWarnings();

		// Create a random file, wrap the equation in LaTeX and store it
		$file = "{$this->dir}/$hash" . wfBaseConvert( mt_rand(), 10, 36 );
		file_put_contents( "$file.tex", self::wrapEquation( $this->getText() ) );

		$retval = null;

		// Render the LaTeX file as DVI
		$output = wfShellExec( wfEscapeShellArg( $this->latex,
			 '-halt-on-error', '-output-directory',
			$this->dir, "$file.tex" ) . ' 2>&1', $retval, $this->environ );
		if ( !file_exists( "$file.dvi" ) ) {
			// Something went wrong, return the output of the latex command
			$this->cleanup( $file );
			return Status::newFatal( 'trustedmath-convert-error',
				$this->latex, $output );
		}

		// Render the DVI file as PNG
		$output = wfShellExec( wfEscapeShellArg( $this->dvipng ) .
			' -D 150 -T tight -v -o ' .
			wfEscapeShellArg( $filePath, "$file.dvi" ), $retval, $this->environ );
		if ( !file_exists( $filePath ) ) {
			// Something went wrong, return the output of the dvipng command
			$this->cleanup( $file );
			return Status::newFatal( 'trustedmath-convert-error',
				$this->dvipng, $output );
		}

		// Everything ok, return the path
		$this->cleanup( $file );
		return Status::newGood( "$hashPath/$hash.png" );
	}