Example #1
0
 /**
  * @param Content|null $def_content The default value to return
  *
  * @return Content|null Content on success, $def_content for invalid sections
  *
  * @since 1.21
  */
 protected function getContentObject($def_content = null)
 {
     global $wgOut, $wgRequest, $wgUser, $wgContLang;
     wfProfileIn(__METHOD__);
     $content = false;
     // For message page not locally set, use the i18n message.
     // For other non-existent articles, use preload text if any.
     if (!$this->mTitle->exists() || $this->section == 'new') {
         if ($this->mTitle->getNamespace() == NS_MEDIAWIKI && $this->section != 'new') {
             # If this is a system message, get the default text.
             $msg = $this->mTitle->getDefaultMessageText();
             $content = $this->toEditContent($msg);
         }
         if ($content === false) {
             # If requested, preload some text.
             $preload = $wgRequest->getVal('preload', $this->section === 'new' ? 'MediaWiki:addsection-preload' : '');
             $params = $wgRequest->getArray('preloadparams', array());
             $content = $this->getPreloadedContent($preload, $params);
         }
         // For existing pages, get text based on "undo" or section parameters.
     } else {
         if ($this->section != '') {
             // Get section edit text (returns $def_text for invalid sections)
             $orig = $this->getOriginalContent($wgUser);
             $content = $orig ? $orig->getSection($this->section) : null;
             if (!$content) {
                 $content = $def_content;
             }
         } else {
             $undoafter = $wgRequest->getInt('undoafter');
             $undo = $wgRequest->getInt('undo');
             if ($undo > 0 && $undoafter > 0) {
                 $undorev = Revision::newFromId($undo);
                 $oldrev = Revision::newFromId($undoafter);
                 # Sanity check, make sure it's the right page,
                 # the revisions exist and they were not deleted.
                 # Otherwise, $content will be left as-is.
                 if (!is_null($undorev) && !is_null($oldrev) && !$undorev->isDeleted(Revision::DELETED_TEXT) && !$oldrev->isDeleted(Revision::DELETED_TEXT)) {
                     $content = $this->mArticle->getUndoContent($undorev, $oldrev);
                     if ($content === false) {
                         # Warn the user that something went wrong
                         $undoMsg = 'failure';
                     } else {
                         $oldContent = $this->mArticle->getPage()->getContent(Revision::RAW);
                         $popts = ParserOptions::newFromUserAndLang($wgUser, $wgContLang);
                         $newContent = $content->preSaveTransform($this->mTitle, $wgUser, $popts);
                         if ($newContent->equals($oldContent)) {
                             # Tell the user that the undo results in no change,
                             # i.e. the revisions were already undone.
                             $undoMsg = 'nochange';
                             $content = false;
                         } else {
                             # Inform the user of our success and set an automatic edit summary
                             $undoMsg = 'success';
                             # If we just undid one rev, use an autosummary
                             $firstrev = $oldrev->getNext();
                             if ($firstrev && $firstrev->getId() == $undo) {
                                 $userText = $undorev->getUserText();
                                 if ($userText === '') {
                                     $undoSummary = wfMessage('undo-summary-username-hidden', $undo)->inContentLanguage()->text();
                                 } else {
                                     $undoSummary = wfMessage('undo-summary', $undo, $userText)->inContentLanguage()->text();
                                 }
                                 if ($this->summary === '') {
                                     $this->summary = $undoSummary;
                                 } else {
                                     $this->summary = $undoSummary . wfMessage('colon-separator')->inContentLanguage()->text() . $this->summary;
                                 }
                                 $this->undidRev = $undo;
                             }
                             $this->formtype = 'diff';
                         }
                     }
                 } else {
                     // Failed basic sanity checks.
                     // Older revisions may have been removed since the link
                     // was created, or we may simply have got bogus input.
                     $undoMsg = 'norev';
                 }
                 // Messages: undo-success, undo-failure, undo-norev, undo-nochange
                 $class = ($undoMsg == 'success' ? '' : 'error ') . "mw-undo-{$undoMsg}";
                 $this->editFormPageTop .= $wgOut->parse("<div class=\"{$class}\">" . wfMessage('undo-' . $undoMsg)->plain() . '</div>', true, true);
             }
             if ($content === false) {
                 $content = $this->getOriginalContent($wgUser);
             }
         }
     }
     wfProfileOut(__METHOD__);
     return $content;
 }