[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/includes/content/ -> TextContentHandler.php (source)

   1  <?php
   2  /**
   3   * Base content handler class for flat text contents.
   4   *
   5   * This program is free software; you can redistribute it and/or modify
   6   * it under the terms of the GNU General Public License as published by
   7   * the Free Software Foundation; either version 2 of the License, or
   8   * (at your option) any later version.
   9   *
  10   * This program is distributed in the hope that it will be useful,
  11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13   * GNU General Public License for more details.
  14   *
  15   * You should have received a copy of the GNU General Public License along
  16   * with this program; if not, write to the Free Software Foundation, Inc.,
  17   * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18   * http://www.gnu.org/copyleft/gpl.html
  19   *
  20   * @since 1.21
  21   *
  22   * @file
  23   * @ingroup Content
  24   */
  25  
  26  /**
  27   * Base content handler implementation for flat text contents.
  28   *
  29   * @ingroup Content
  30   */
  31  class TextContentHandler extends ContentHandler {
  32  
  33      // @codingStandardsIgnoreStart bug 57585
  34  	public function __construct( $modelId = CONTENT_MODEL_TEXT,
  35          $formats = array( CONTENT_FORMAT_TEXT ) ) {
  36          parent::__construct( $modelId, $formats );
  37      }
  38      // @codingStandardsIgnoreEnd
  39  
  40      /**
  41       * Returns the content's text as-is.
  42       *
  43       * @param Content $content
  44       * @param string $format The serialization format to check
  45       *
  46       * @return mixed
  47       */
  48  	public function serializeContent( Content $content, $format = null ) {
  49          $this->checkFormat( $format );
  50  
  51          return $content->getNativeData();
  52      }
  53  
  54      /**
  55       * Attempts to merge differences between three versions. Returns a new
  56       * Content object for a clean merge and false for failure or a conflict.
  57       *
  58       * All three Content objects passed as parameters must have the same
  59       * content model.
  60       *
  61       * This text-based implementation uses wfMerge().
  62       *
  63       * @param Content $oldContent The page's previous content.
  64       * @param Content $myContent One of the page's conflicting contents.
  65       * @param Content $yourContent One of the page's conflicting contents.
  66       *
  67       * @return Content|bool
  68       */
  69  	public function merge3( Content $oldContent, Content $myContent, Content $yourContent ) {
  70          $this->checkModelID( $oldContent->getModel() );
  71          $this->checkModelID( $myContent->getModel() );
  72          $this->checkModelID( $yourContent->getModel() );
  73  
  74          $format = $this->getDefaultFormat();
  75  
  76          $old = $this->serializeContent( $oldContent, $format );
  77          $mine = $this->serializeContent( $myContent, $format );
  78          $yours = $this->serializeContent( $yourContent, $format );
  79  
  80          $ok = wfMerge( $old, $mine, $yours, $result );
  81  
  82          if ( !$ok ) {
  83              return false;
  84          }
  85  
  86          if ( !$result ) {
  87              return $this->makeEmptyContent();
  88          }
  89  
  90          $mergedContent = $this->unserializeContent( $result, $format );
  91  
  92          return $mergedContent;
  93      }
  94  
  95      /**
  96       * Returns the name of the associated Content class, to
  97       * be used when creating new objects. Override expected
  98       * by subclasses.
  99       *
 100       * @since 1.24
 101       *
 102       * @return string
 103       */
 104  	protected function getContentClass() {
 105          return 'TextContent';
 106      }
 107  
 108      /**
 109       * Unserializes a Content object of the type supported by this ContentHandler.
 110       *
 111       * @since 1.21
 112       *
 113       * @param string $text Serialized form of the content
 114       * @param string $format The format used for serialization
 115       *
 116       * @return Content The TextContent object wrapping $text
 117       */
 118  	public function unserializeContent( $text, $format = null ) {
 119          $this->checkFormat( $format );
 120  
 121          $class = $this->getContentClass();
 122          return new $class( $text );
 123      }
 124  
 125      /**
 126       * Creates an empty TextContent object.
 127       *
 128       * @since 1.21
 129       *
 130       * @return Content A new TextContent object with empty text.
 131       */
 132  	public function makeEmptyContent() {
 133          $class = $this->getContentClass();
 134          return new $class( '' );
 135      }
 136  
 137  }


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