Example #1
0
 /**
  * Get the rendered text for previewing.
  * @throws MWException
  * @return string
  */
 function getPreviewText()
 {
     global $wgOut, $wgUser, $wgRawHtml, $wgLang;
     global $wgAllowUserCss, $wgAllowUserJs;
     wfProfileIn(__METHOD__);
     if ($wgRawHtml && !$this->mTokenOk) {
         // Could be an offsite preview attempt. This is very unsafe if
         // HTML is enabled, as it could be an attack.
         $parsedNote = '';
         if ($this->textbox1 !== '') {
             // Do not put big scary notice, if previewing the empty
             // string, which happens when you initially edit
             // a category page, due to automatic preview-on-open.
             $parsedNote = $wgOut->parse("<div class='previewnote'>" . wfMessage('session_fail_preview_html')->text() . "</div>", true, true);
         }
         wfProfileOut(__METHOD__);
         return $parsedNote;
     }
     $note = '';
     try {
         $content = $this->toEditContent($this->textbox1);
         $previewHTML = '';
         if (!wfRunHooks('AlternateEditPreview', array($this, &$content, &$previewHTML, &$this->mParserOutput))) {
             wfProfileOut(__METHOD__);
             return $previewHTML;
         }
         # provide a anchor link to the editform
         $continueEditing = '<span class="mw-continue-editing">' . '[[#' . self::EDITFORM_ID . '|' . $wgLang->getArrow() . ' ' . wfMessage('continue-editing')->text() . ']]</span>';
         if ($this->mTriedSave && !$this->mTokenOk) {
             if ($this->mTokenOkExceptSuffix) {
                 $note = wfMessage('token_suffix_mismatch')->plain();
             } else {
                 $note = wfMessage('session_fail_preview')->plain();
             }
         } elseif ($this->incompleteForm) {
             $note = wfMessage('edit_form_incomplete')->plain();
         } else {
             $note = wfMessage('previewnote')->plain() . ' ' . $continueEditing;
         }
         $parserOptions = $this->mArticle->makeParserOptions($this->mArticle->getContext());
         $parserOptions->setEditSection(false);
         $parserOptions->setIsPreview(true);
         $parserOptions->setIsSectionPreview(!is_null($this->section) && $this->section !== '');
         # don't parse non-wikitext pages, show message about preview
         if ($this->mTitle->isCssJsSubpage() || $this->mTitle->isCssOrJsPage()) {
             if ($this->mTitle->isCssJsSubpage()) {
                 $level = 'user';
             } elseif ($this->mTitle->isCssOrJsPage()) {
                 $level = 'site';
             } else {
                 $level = false;
             }
             if ($content->getModel() == CONTENT_MODEL_CSS) {
                 $format = 'css';
                 if ($level === 'user' && !$wgAllowUserCss) {
                     $format = false;
                 }
             } elseif ($content->getModel() == CONTENT_MODEL_JAVASCRIPT) {
                 $format = 'js';
                 if ($level === 'user' && !$wgAllowUserJs) {
                     $format = false;
                 }
             } else {
                 $format = false;
             }
             # Used messages to make sure grep find them:
             # Messages: usercsspreview, userjspreview, sitecsspreview, sitejspreview
             if ($level && $format) {
                 $note = "<div id='mw-{$level}{$format}preview'>" . wfMessage("{$level}{$format}preview")->text() . ' ' . $continueEditing . "</div>";
             }
         }
         # If we're adding a comment, we need to show the
         # summary as the headline
         if ($this->section === "new" && $this->summary !== "") {
             $content = $content->addSectionHeader($this->summary);
         }
         $hook_args = array($this, &$content);
         ContentHandler::runLegacyHooks('EditPageGetPreviewText', $hook_args);
         wfRunHooks('EditPageGetPreviewContent', $hook_args);
         $parserOptions->enableLimitReport();
         # For CSS/JS pages, we should have called the ShowRawCssJs hook here.
         # But it's now deprecated, so never mind
         $content = $content->preSaveTransform($this->mTitle, $wgUser, $parserOptions);
         $parserOutput = $content->getParserOutput($this->getArticle()->getTitle(), null, $parserOptions);
         $previewHTML = $parserOutput->getText();
         $this->mParserOutput = $parserOutput;
         $wgOut->addParserOutputMetadata($parserOutput);
         if (count($parserOutput->getWarnings())) {
             $note .= "\n\n" . implode("\n\n", $parserOutput->getWarnings());
         }
     } catch (MWContentSerializationException $ex) {
         $m = wfMessage('content-failed-to-parse', $this->contentModel, $this->contentFormat, $ex->getMessage());
         $note .= "\n\n" . $m->parse();
         $previewHTML = '';
     }
     if ($this->isConflict) {
         $conflict = '<h2 id="mw-previewconflict">' . wfMessage('previewconflict')->escaped() . "</h2>\n";
     } else {
         $conflict = '<hr />';
     }
     $previewhead = "<div class='previewnote'>\n" . '<h2 id="mw-previewheader">' . wfMessage('preview')->escaped() . "</h2>" . $wgOut->parse($note, true, true) . $conflict . "</div>\n";
     $pageViewLang = $this->mTitle->getPageViewLanguage();
     $attribs = array('lang' => $pageViewLang->getHtmlCode(), 'dir' => $pageViewLang->getDir(), 'class' => 'mw-content-' . $pageViewLang->getDir());
     $previewHTML = Html::rawElement('div', $attribs, $previewHTML);
     wfProfileOut(__METHOD__);
     return $previewhead . $previewHTML . $this->previewTextAfterContent;
 }
Example #2
0
 /**
  * Get the rendered text for previewing.
  * @return string
  */
 function getPreviewText()
 {
     global $wgOut, $wgUser, $wgParser, $wgRawHtml, $wgLang;
     wfProfileIn(__METHOD__);
     if ($wgRawHtml && !$this->mTokenOk) {
         // Could be an offsite preview attempt. This is very unsafe if
         // HTML is enabled, as it could be an attack.
         $parsedNote = '';
         if ($this->textbox1 !== '') {
             // Do not put big scary notice, if previewing the empty
             // string, which happens when you initially edit
             // a category page, due to automatic preview-on-open.
             $parsedNote = $wgOut->parse("<div class='previewnote'>" . wfMessage('session_fail_preview_html')->text() . "</div>", true, true);
         }
         wfProfileOut(__METHOD__);
         return $parsedNote;
     }
     if ($this->mTriedSave && !$this->mTokenOk) {
         if ($this->mTokenOkExceptSuffix) {
             $note = wfMessage('token_suffix_mismatch')->plain();
         } else {
             $note = wfMessage('session_fail_preview')->plain();
         }
     } elseif ($this->incompleteForm) {
         $note = wfMessage('edit_form_incomplete')->plain();
     } else {
         $note = wfMessage('previewnote')->plain() . ' [[#' . self::EDITFORM_ID . '|' . $wgLang->getArrow() . ' ' . wfMessage('continue-editing')->text() . ']]';
     }
     $parserOptions = $this->mArticle->makeParserOptions($this->mArticle->getContext());
     $parserOptions->setEditSection(false);
     $parserOptions->setIsPreview(true);
     $parserOptions->setIsSectionPreview(!is_null($this->section) && $this->section !== '');
     # don't parse non-wikitext pages, show message about preview
     if ($this->mTitle->isCssJsSubpage() || !$this->mTitle->isWikitextPage()) {
         if ($this->mTitle->isCssJsSubpage()) {
             $level = 'user';
         } elseif ($this->mTitle->isCssOrJsPage()) {
             $level = 'site';
         } else {
             $level = false;
         }
         # Used messages to make sure grep find them:
         # Messages: usercsspreview, userjspreview, sitecsspreview, sitejspreview
         $class = 'mw-code';
         if ($level) {
             if (preg_match("/\\.css\$/", $this->mTitle->getText())) {
                 $previewtext = "<div id='mw-{$level}csspreview'>\n" . wfMessage("{$level}csspreview")->text() . "\n</div>";
                 $class .= " mw-css";
             } elseif (preg_match("/\\.js\$/", $this->mTitle->getText())) {
                 $previewtext = "<div id='mw-{$level}jspreview'>\n" . wfMessage("{$level}jspreview")->text() . "\n</div>";
                 $class .= " mw-js";
             } else {
                 throw new MWException('A CSS/JS (sub)page but which is not css nor js!');
             }
             $parserOutput = $wgParser->parse($previewtext, $this->mTitle, $parserOptions);
             $previewHTML = $parserOutput->getText();
         } else {
             $previewHTML = '';
         }
         $previewHTML .= "<pre class=\"{$class}\" dir=\"ltr\">\n" . htmlspecialchars($this->textbox1) . "\n</pre>\n";
     } else {
         $toparse = $this->textbox1;
         # If we're adding a comment, we need to show the
         # summary as the headline
         if ($this->section == "new" && $this->summary != "") {
             $toparse = wfMessage('newsectionheaderdefaultlevel', $this->summary)->inContentLanguage()->text() . "\n\n" . $toparse;
         }
         wfRunHooks('EditPageGetPreviewText', array($this, &$toparse));
         $toparse = $wgParser->preSaveTransform($toparse, $this->mTitle, $wgUser, $parserOptions);
         $parserOutput = $wgParser->parse($toparse, $this->mTitle, $parserOptions);
         $rt = Title::newFromRedirectArray($this->textbox1);
         if ($rt) {
             $previewHTML = $this->mArticle->viewRedirect($rt, false);
         } else {
             $previewHTML = $parserOutput->getText();
         }
         $this->mParserOutput = $parserOutput;
         $wgOut->addParserOutputNoText($parserOutput);
         if (count($parserOutput->getWarnings())) {
             $note .= "\n\n" . implode("\n\n", $parserOutput->getWarnings());
         }
     }
     if ($this->isConflict) {
         $conflict = '<h2 id="mw-previewconflict">' . wfMessage('previewconflict')->escaped() . "</h2>\n";
     } else {
         $conflict = '<hr />';
     }
     $previewhead = "<div class='previewnote'>\n" . '<h2 id="mw-previewheader">' . wfMessage('preview')->escaped() . "</h2>" . $wgOut->parse($note, true, true) . $conflict . "</div>\n";
     $pageLang = $this->mTitle->getPageLanguage();
     $attribs = array('lang' => $pageLang->getCode(), 'dir' => $pageLang->getDir(), 'class' => 'mw-content-' . $pageLang->getDir());
     $previewHTML = Html::rawElement('div', $attribs, $previewHTML);
     wfProfileOut(__METHOD__);
     return $previewhead . $previewHTML . $this->previewTextAfterContent;
 }