/**
  * Appends a preview of the actual form, when a page in the "Form"
  * namespace is previewed.
  *
  * @author Solitarius
  * @since 2.4
  *
  * @param EditPage $editpage
  * @param WebRequest $request
  *
  * @return true
  */
 public static function showFormPreview(EditPage $editpage, WebRequest $request)
 {
     global $wgOut, $wgParser, $sfgFormPrinter;
     wfDebug(__METHOD__ . ": enter.\n");
     // Exit if we're not in preview mode.
     if (!$editpage->preview) {
         return true;
     }
     // Exit if we aren't in the "Form" namespace.
     if ($editpage->getArticle()->getTitle()->getNamespace() != SF_NS_FORM) {
         return true;
     }
     $editpage->previewTextAfterContent .= Html::element('h2', null, wfMessage('sf-preview-header')->text()) . "\n" . '<div class="previewnote" style="font-weight: bold">' . $wgOut->parse(wfMessage('sf-preview-note')->text()) . "</div>\n<hr />\n";
     $form_definition = StringUtils::delimiterReplace('<noinclude>', '</noinclude>', '', $editpage->textbox1);
     list($form_text, $javascript_text, $data_text, $form_page_title, $generated_page_name) = $sfgFormPrinter->formHTML($form_definition, null, false, null, null, "Semantic Forms form preview dummy title", null);
     $parserOutput = $wgParser->getOutput();
     if (method_exists($wgOut, 'addParserOutputMetadata')) {
         $wgOut->addParserOutputMetadata($parserOutput);
     } else {
         $wgOut->addParserOutputNoText($parserOutput);
     }
     SFUtils::addFormRLModules();
     $editpage->previewTextAfterContent .= '<div style="margin-top: 15px">' . $form_text . "</div>";
     return true;
 }
    function printPage($form_name, $embedded = false)
    {
        global $wgOut, $wgRequest, $sfgFormPrinter, $wgParser, $sfgRunQueryFormAtTop;
        global $wgUser;
        // Get contents of form-definition page.
        $form_title = Title::makeTitleSafe(SF_NS_FORM, $form_name);
        if (!$form_title || !$form_title->exists()) {
            if ($form_name === '') {
                $text = Html::element('p', array('class' => 'error'), wfMessage('sf_runquery_badurl')->text()) . "\n";
            } else {
                $text = Html::rawElement('p', array('class' => 'error'), wfMessage('sf_formstart_badform', SFUtils::linkText(SF_NS_FORM, $form_name))->parse()) . "\n";
            }
            $wgOut->addHTML($text);
            return;
        }
        // Initialize variables.
        $form_definition = SFUtils::getPageText($form_title);
        if ($embedded) {
            $run_query = false;
            $content = null;
            $raw = false;
        } else {
            $run_query = $wgRequest->getCheck('wpRunQuery');
            $content = $wgRequest->getVal('wpTextbox1');
            $raw = $wgRequest->getBool('raw', false);
        }
        $form_submitted = $run_query;
        if ($raw) {
            $wgOut->setArticleBodyOnly(true);
        }
        // If user already made some action, ignore the edited
        // page and just get data from the query string.
        if (!$embedded && $wgRequest->getVal('query') == 'true') {
            $edit_content = null;
            $is_text_source = false;
        } elseif ($content != null) {
            $edit_content = $content;
            $is_text_source = true;
        } else {
            $edit_content = null;
            $is_text_source = true;
        }
        list($form_text, $javascript_text, $data_text, $form_page_title) = $sfgFormPrinter->formHTML($form_definition, $form_submitted, $is_text_source, $form_title->getArticleID(), $edit_content, null, null, true, $embedded);
        $text = "";
        // Get the text of the results.
        $resultsText = '';
        if ($form_submitted) {
            // @TODO - fix RunQuery's parsing so that this check
            // isn't needed.
            if ($wgParser->getOutput() == null) {
                $headItems = array();
            } else {
                $headItems = $wgParser->getOutput()->getHeadItems();
            }
            foreach ($headItems as $key => $item) {
                $wgOut->addHeadItem($key, "\t\t" . $item . "\n");
            }
            $wgParser->mOptions = ParserOptions::newFromUser($wgUser);
            $resultsText = $wgParser->parse($data_text, $this->getTitle(), $wgParser->mOptions)->getText();
        }
        // Get the full text of the form.
        $fullFormText = '';
        $additionalQueryHeader = '';
        $dividerText = '';
        if (!$raw) {
            // Create the "additional query" header, and the
            // divider text - one of these (depending on whether
            // the query form is at the top or bottom) is displayed
            // if the form has already been submitted.
            if ($form_submitted) {
                $additionalQueryHeader = "\n" . Html::element('h2', null, wfMessage('sf_runquery_additionalquery')->text()) . "\n";
                $dividerText = "\n<hr style=\"margin: 15px 0;\" />\n";
            }
            $action = htmlspecialchars($this->getTitle($form_name)->getLocalURL());
            $fullFormText .= <<<END
\t<form id="sfForm" name="createbox" action="{$action}" method="post" class="createbox">

END;
            $fullFormText .= Html::hidden('query', 'true');
            $fullFormText .= $form_text;
        }
        // Either don't display a query form at all, or display the
        // query form at the top, and the results at the bottom, or the
        // other way around, depending on the settings.
        if ($wgRequest->getVal('additionalquery') == 'false') {
            $text .= $resultsText;
        } elseif ($sfgRunQueryFormAtTop) {
            $text .= Html::openElement('div', array('class' => 'sf-runquery-formcontent'));
            $text .= $fullFormText;
            $text .= $dividerText;
            $text .= Html::closeElement('div');
            $text .= $resultsText;
        } else {
            $text .= $resultsText;
            $text .= Html::openElement('div', array('class' => 'sf-runquery-formcontent'));
            $text .= $additionalQueryHeader;
            $text .= $fullFormText;
            $text .= Html::closeElement('div');
        }
        if ($embedded) {
            $text = "<div class='runQueryEmbedded'>{$text}</div>";
        }
        // Armor against doBlockLevels()
        $text = preg_replace('/^ +/m', '', $text);
        // Now write everything to the screen.
        $wgOut->addHTML($text);
        SFUtils::addFormRLModules($embedded ? $wgParser : null);
        $script = "\t\t" . '<script type="text/javascript">' . "\n" . $javascript_text . '</script>' . "\n";
        if ($embedded) {
            if (method_exists('ResourceLoader', 'makeInlineScript')) {
                // MW 1.25+
                $wgParser->getOutput()->addHeadItem(ResourceLoader::makeInlineScript($javascript_text));
            } else {
                $wgParser->getOutput()->addHeadItem($script);
            }
        } else {
            if (method_exists('ResourceLoader', 'makeInlineScript')) {
                // MW 1.25+
                $wgOut->addScript(ResourceLoader::makeInlineScript($javascript_text));
            } else {
                $wgOut->addScript($script);
            }
            $po = $wgParser->getOutput();
            if ($po) {
                // addParserOutputMetadata was introduced in 1.24 when addParserOutputNoText was deprecated
                if (method_exists($wgOut, 'addParserOutputMetadata')) {
                    $wgOut->addParserOutputMetadata($po);
                } else {
                    $wgOut->addParserOutputNoText($po);
                }
            }
        }
        // Finally, set the page title - previously, this had to be
        // called after addParserOutputNoText() for it to take effect;
        // now the order doesn't matter.
        if (!$embedded) {
            if ($form_page_title != null) {
                $wgOut->setPageTitle($form_page_title);
            } else {
                $s = wfMessage('sf_runquery_title', $form_title->getText())->text();
                $wgOut->setPageTitle($s);
            }
        }
    }
 function printForm($form_name, $targetName, $alt_forms = array())
 {
     $out = $this->getOutput();
     $req = $this->getRequest();
     $module = new SFAutoeditAPI(new ApiMain(), 'sfautoedit');
     $module->setOption('form', $form_name);
     $module->setOption('target', $targetName);
     if ($req->getCheck('wpSave') || $req->getCheck('wpPreview') || $req->getCheck('wpDiff')) {
         // If the page was submitted, form data should be
         // complete => do not preload (unless it's a partial
         // form).
         if ($req->getCheck('partial')) {
             $module->setOption('preload', true);
         } else {
             $module->setOption('preload', false);
         }
     } else {
         if (!empty($targetName) && Title::newFromText($targetName)->exists()) {
             // If target page exists, do not overwrite it with
             // preload data; just preload the page's data.
             $module->setOption('preload', true);
         } else {
             if ($req->getCheck('preload')) {
                 // if page does not exist and preload parameter is set, pass that on
                 $module->setOption('preload', $req->getText('preload'));
             } else {
                 // nothing set, so do not set preload
             }
         }
     }
     $module->execute();
     $text = '';
     // if action was successful and action was a Save, return
     if ($module->getStatus() === 200) {
         if ($module->getAction() === SFAutoeditAPI::ACTION_SAVE) {
             return;
         }
     } else {
         $resultData = $module->getResultData();
         if (array_key_exists('errors', $resultData)) {
             foreach ($resultData['errors'] as $error) {
                 // FIXME: This should probably not be hard-coded to WARNING but put into a setting
                 if ($error['level'] <= SFAutoeditAPI::WARNING) {
                     $text .= Html::rawElement('p', array('class' => 'error'), $error['message']) . "\n";
                 }
             }
         }
     }
     // Override the default title for this page if a title was
     // specified in the form.
     $result = $module->getOptions();
     $targetTitle = Title::newFromText($result['target']);
     // Set page title depending on whether an explicit title was
     // specified in the form definition.
     if (array_key_exists('formtitle', $result)) {
         // set page title depending on whether the target page exists
         if (empty($targetName)) {
             $pageTitle = $result['formtitle'];
         } else {
             $pageTitle = $result['formtitle'] . ': ' . $targetName;
         }
     } elseif ($result['form'] !== '') {
         // Set page title depending on whether the target page
         // exists.
         if (empty($targetName)) {
             $pageTitle = wfMessage('sf_formedit_createtitlenotarget', $result['form'])->text();
         } elseif ($targetTitle->exists()) {
             $pageTitle = wfMessage('sf_formedit_edittitle', $result['form'], $targetName)->text();
         } else {
             $pageTitle = wfMessage('sf_formedit_createtitle', $result['form'], $targetName)->text();
         }
     } elseif (count($alt_forms) > 0) {
         // We use the 'creating' message here, instead of
         // 'sf_formedit_createtitlenotarget', to differentiate
         // between a page with no (default) form, and one with
         // no target; in English they'll show up as
         // "Creating ..." and "Create ...", respectively.
         // Does this make any difference? Who knows.
         $pageTitle = wfMessage('creating', $targetName)->text();
     } elseif ($result['form'] == '') {
         //FIXME: This looks weird; a simple else should be enough, right?
         // display error message if the form is not specified in the URL
         $pageTitle = wfMessage('formedit')->text();
         $text .= Html::element('p', array('class' => 'error'), wfMessage('sf_formedit_badurl')->text()) . "\n";
         $out->addHTML($text);
     }
     $out->setPageTitle($pageTitle);
     if (count($alt_forms) > 0) {
         $text .= '<div class="infoMessage">';
         if ($result['form'] != '') {
             $text .= wfMessage('sf_formedit_altforms')->escaped();
         } else {
             $text .= wfMessage('sf_formedit_altformsonly')->escaped();
         }
         $text .= ' ' . $this->printAltFormsList($alt_forms, $targetName);
         $text .= "</div>\n";
     }
     $text .= '<form name="createbox" id="sfForm" method="post" class="createbox">';
     $pre_form_html = '';
     Hooks::run('sfHTMLBeforeForm', array(&$targetTitle, &$pre_form_html));
     $text .= $pre_form_html;
     if (isset($result['formHTML'])) {
         $text .= $result['formHTML'];
     }
     SFUtils::addFormRLModules();
     if (isset($result['formJS'])) {
         if (method_exists('ResourceLoader', 'makeInlineScript')) {
             // MW 1.25+
             $out->addScript(ResourceLoader::makeInlineScript($result['formJS']));
         } else {
             $out->addScript('		<script type="text/javascript">' . "\n{$result['formJS']}\n" . '</script>' . "\n");
         }
     }
     $out->addHTML($text);
     return null;
 }