示例#1
0
 /**
  * Get/set the ParserOptions object to use for wikitext parsing
  *
  * @param ParserOptions|null $options Either the ParserOption to use or null to only get the
  *   current ParserOption object
  * @return ParserOptions
  */
 public function parserOptions($options = null)
 {
     if ($options !== null && !empty($options->isBogus)) {
         // Someone is trying to set a bogus pre-$wgUser PO. Check if it has
         // been changed somehow, and keep it if so.
         $anonPO = ParserOptions::newFromAnon();
         $anonPO->setEditSection(false);
         if (!$options->matches($anonPO)) {
             wfLogWarning(__METHOD__ . ': Setting a changed bogus ParserOptions: ' . wfGetAllCallers(5));
             $options->isBogus = false;
         }
     }
     if (!$this->mParserOptions) {
         if (!$this->getContext()->getUser()->isSafeToLoad()) {
             // $wgUser isn't unstubbable yet, so don't try to get a
             // ParserOptions for it. And don't cache this ParserOptions
             // either.
             $po = ParserOptions::newFromAnon();
             $po->setEditSection(false);
             $po->isBogus = true;
             if ($options !== null) {
                 $this->mParserOptions = empty($options->isBogus) ? $options : null;
             }
             return $po;
         }
         $this->mParserOptions = ParserOptions::newFromContext($this->getContext());
         $this->mParserOptions->setEditSection(false);
     }
     if ($options !== null && !empty($options->isBogus)) {
         // They're trying to restore the bogus pre-$wgUser PO. Do the right
         // thing.
         return wfSetVar($this->mParserOptions, null, true);
     } else {
         return wfSetVar($this->mParserOptions, $options);
     }
 }
 /**
  * Attempt to cache PST content and corresponding parser output in passing
  *
  * This method can be called when the output was already generated for other
  * reasons. Parsing should not be done just to call this method, however.
  * $pstOpts must be that of the user doing the edit preview. If $pOpts does
  * not match the options of WikiPage::makeParserOptions( 'canonical' ), this
  * will do nothing. Provided the values are cacheable, they will be stored
  * in memcached so that final edit submission might make use of them.
  *
  * @param Article|WikiPage $page Page title
  * @param Content $content Proposed page content
  * @param Content $pstContent The result of preSaveTransform() on $content
  * @param ParserOutput $pOut The result of getParserOutput() on $pstContent
  * @param ParserOptions $pstOpts Options for $pstContent (MUST be for prospective author)
  * @param ParserOptions $pOpts Options for $pOut
  * @param string $timestamp TS_MW timestamp of parser output generation
  * @return bool Success
  */
 public static function stashEditFromPreview(Page $page, Content $content, Content $pstContent, ParserOutput $pOut, ParserOptions $pstOpts, ParserOptions $pOpts, $timestamp)
 {
     global $wgMemc;
     // getIsPreview() controls parser function behavior that references things
     // like user/revision that don't exists yet. The user/text should already
     // be set correctly by callers, just double check the preview flag.
     if (!$pOpts->getIsPreview()) {
         return false;
         // sanity
     } elseif ($pOpts->getIsSectionPreview()) {
         return false;
         // short-circuit (need the full content)
     }
     // PST parser options are for the user (handles signatures, etc...)
     $user = $pstOpts->getUser();
     // Get a key based on the source text, format, and user preferences
     $key = self::getStashKey($page->getTitle(), $content, $user);
     // Parser output options must match cannonical options.
     // Treat some options as matching that are different but don't matter.
     $canonicalPOpts = $page->makeParserOptions('canonical');
     $canonicalPOpts->setIsPreview(true);
     // force match
     $canonicalPOpts->setTimestamp($pOpts->getTimestamp());
     // force match
     if (!$pOpts->matches($canonicalPOpts)) {
         wfDebugLog('StashEdit', "Uncacheable preview output for key '{$key}' (options).");
         return false;
     }
     // Build a value to cache with a proper TTL
     list($stashInfo, $ttl) = self::buildStashValue($pstContent, $pOut, $timestamp);
     if (!$stashInfo) {
         wfDebugLog('StashEdit', "Uncacheable parser output for key '{$key}' (rev/TTL).");
         return false;
     }
     $ok = $wgMemc->set($key, $stashInfo, $ttl);
     if (!$ok) {
         wfDebugLog('StashEdit', "Failed to cache preview parser output for key '{$key}'.");
     } else {
         wfDebugLog('StashEdit', "Cached preview output for key '{$key}'.");
     }
     return $ok;
 }