[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/includes/poolcounter/ -> PoolWorkArticleView.php (source)

   1  <?php
   2  /**
   3   * This program is free software; you can redistribute it and/or modify
   4   * it under the terms of the GNU General Public License as published by
   5   * the Free Software Foundation; either version 2 of the License, or
   6   * (at your option) any later version.
   7   *
   8   * This program is distributed in the hope that it will be useful,
   9   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11   * GNU General Public License for more details.
  12   *
  13   * You should have received a copy of the GNU General Public License along
  14   * with this program; if not, write to the Free Software Foundation, Inc.,
  15   * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16   * http://www.gnu.org/copyleft/gpl.html
  17   *
  18   * @file
  19   */
  20  
  21  class PoolWorkArticleView extends PoolCounterWork {
  22      /** @var Page */
  23      private $page;
  24  
  25      /** @var string */
  26      private $cacheKey;
  27  
  28      /** @var int */
  29      private $revid;
  30  
  31      /** @var ParserOptions */
  32      private $parserOptions;
  33  
  34      /** @var Content|null */
  35      private $content = null;
  36  
  37      /** @var ParserOutput|bool */
  38      private $parserOutput = false;
  39  
  40      /** @var bool */
  41      private $isDirty = false;
  42  
  43      /** @var Status|bool */
  44      private $error = false;
  45  
  46      /**
  47       * @param Page $page
  48       * @param ParserOptions $parserOptions ParserOptions to use for the parse
  49       * @param int $revid ID of the revision being parsed.
  50       * @param bool $useParserCache Whether to use the parser cache.
  51       *   operation.
  52       * @param Content|string $content Content to parse or null to load it; may
  53       *   also be given as a wikitext string, for BC.
  54       */
  55  	public function __construct( Page $page, ParserOptions $parserOptions,
  56          $revid, $useParserCache, $content = null
  57      ) {
  58          if ( is_string( $content ) ) { // BC: old style call
  59              $modelId = $page->getRevision()->getContentModel();
  60              $format = $page->getRevision()->getContentFormat();
  61              $content = ContentHandler::makeContent( $content, $page->getTitle(), $modelId, $format );
  62          }
  63  
  64          $this->page = $page;
  65          $this->revid = $revid;
  66          $this->cacheable = $useParserCache;
  67          $this->parserOptions = $parserOptions;
  68          $this->content = $content;
  69          $this->cacheKey = ParserCache::singleton()->getKey( $page, $parserOptions );
  70          parent::__construct( 'ArticleView', $this->cacheKey . ':revid:' . $revid );
  71      }
  72  
  73      /**
  74       * Get the ParserOutput from this object, or false in case of failure
  75       *
  76       * @return ParserOutput
  77       */
  78  	public function getParserOutput() {
  79          return $this->parserOutput;
  80      }
  81  
  82      /**
  83       * Get whether the ParserOutput is a dirty one (i.e. expired)
  84       *
  85       * @return bool
  86       */
  87  	public function getIsDirty() {
  88          return $this->isDirty;
  89      }
  90  
  91      /**
  92       * Get a Status object in case of error or false otherwise
  93       *
  94       * @return Status|bool
  95       */
  96  	public function getError() {
  97          return $this->error;
  98      }
  99  
 100      /**
 101       * @return bool
 102       */
 103  	public function doWork() {
 104          global $wgUseFileCache;
 105  
 106          // @todo several of the methods called on $this->page are not declared in Page, but present
 107          //        in WikiPage and delegated by Article.
 108  
 109          $isCurrent = $this->revid === $this->page->getLatest();
 110  
 111          if ( $this->content !== null ) {
 112              $content = $this->content;
 113          } elseif ( $isCurrent ) {
 114              // XXX: why use RAW audience here, and PUBLIC (default) below?
 115              $content = $this->page->getContent( Revision::RAW );
 116          } else {
 117              $rev = Revision::newFromTitle( $this->page->getTitle(), $this->revid );
 118  
 119              if ( $rev === null ) {
 120                  $content = null;
 121              } else {
 122                  // XXX: why use PUBLIC audience here (default), and RAW above?
 123                  $content = $rev->getContent();
 124              }
 125          }
 126  
 127          if ( $content === null ) {
 128              return false;
 129          }
 130  
 131          // Reduce effects of race conditions for slow parses (bug 46014)
 132          $cacheTime = wfTimestampNow();
 133  
 134          $time = - microtime( true );
 135          $this->parserOutput = $content->getParserOutput(
 136              $this->page->getTitle(),
 137              $this->revid,
 138              $this->parserOptions
 139          );
 140          $time += microtime( true );
 141  
 142          // Timing hack
 143          if ( $time > 3 ) {
 144              wfDebugLog( 'slow-parse', sprintf( "%-5.2f %s", $time,
 145                  $this->page->getTitle()->getPrefixedDBkey() ) );
 146          }
 147  
 148          if ( $this->cacheable && $this->parserOutput->isCacheable() && $isCurrent ) {
 149              ParserCache::singleton()->save(
 150                  $this->parserOutput, $this->page, $this->parserOptions, $cacheTime, $this->revid );
 151          }
 152  
 153          // Make sure file cache is not used on uncacheable content.
 154          // Output that has magic words in it can still use the parser cache
 155          // (if enabled), though it will generally expire sooner.
 156          if ( !$this->parserOutput->isCacheable() || $this->parserOutput->containsOldMagic() ) {
 157              $wgUseFileCache = false;
 158          }
 159  
 160          if ( $isCurrent ) {
 161              $this->page->doCascadeProtectionUpdates( $this->parserOutput );
 162          }
 163  
 164          return true;
 165      }
 166  
 167      /**
 168       * @return bool
 169       */
 170  	public function getCachedWork() {
 171          $this->parserOutput = ParserCache::singleton()->get( $this->page, $this->parserOptions );
 172  
 173          if ( $this->parserOutput === false ) {
 174              wfDebug( __METHOD__ . ": parser cache miss\n" );
 175              return false;
 176          } else {
 177              wfDebug( __METHOD__ . ": parser cache hit\n" );
 178              return true;
 179          }
 180      }
 181  
 182      /**
 183       * @return bool
 184       */
 185  	public function fallback() {
 186          $this->parserOutput = ParserCache::singleton()->getDirty( $this->page, $this->parserOptions );
 187  
 188          if ( $this->parserOutput === false ) {
 189              wfDebugLog( 'dirty', 'dirty missing' );
 190              wfDebug( __METHOD__ . ": no dirty cache\n" );
 191              return false;
 192          } else {
 193              wfDebug( __METHOD__ . ": sending dirty output\n" );
 194              wfDebugLog( 'dirty', "dirty output {$this->cacheKey}" );
 195              $this->isDirty = true;
 196              return true;
 197          }
 198      }
 199  
 200      /**
 201       * @param Status $status
 202       * @return bool
 203       */
 204  	public function error( $status ) {
 205          $this->error = $status;
 206          return false;
 207      }
 208  }


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1