Merge "Begin exposing SiteConfiguration via site contexts"
[mediawiki.git] / includes / content / TextContentHandler.php
blob94b5c578654319ebe73917a13f830eb33c876b7c
1 <?php
2 /**
3 * Base content handler class for flat text contents.
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.
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.
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
20 * @since 1.21
22 * @file
23 * @ingroup Content
26 /**
27 * Base content handler implementation for flat text contents.
29 * @ingroup Content
31 class TextContentHandler extends ContentHandler {
32 // @codingStandardsIgnoreStart bug 57585
33 public function __construct( $modelId = CONTENT_MODEL_TEXT,
34 $formats = array( CONTENT_FORMAT_TEXT )
35 ) {
36 parent::__construct( $modelId, $formats );
38 // @codingStandardsIgnoreEnd
40 /**
41 * Returns the content's text as-is.
43 * @param $content Content
44 * @param $format string|null
45 * @return mixed
47 public function serializeContent( Content $content, $format = null ) {
48 $this->checkFormat( $format );
50 return $content->getNativeData();
53 /**
54 * Attempts to merge differences between three versions. Returns a new
55 * Content object for a clean merge and false for failure or a conflict.
57 * All three Content objects passed as parameters must have the same
58 * content model.
60 * This text-based implementation uses wfMerge().
62 * @param $oldContent Content|string String
63 * @param $myContent Content|string String
64 * @param $yourContent Content|string String
66 * @return Content|Bool
68 public function merge3( Content $oldContent, Content $myContent, Content $yourContent ) {
69 $this->checkModelID( $oldContent->getModel() );
70 $this->checkModelID( $myContent->getModel() );
71 $this->checkModelID( $yourContent->getModel() );
73 $format = $this->getDefaultFormat();
75 $old = $this->serializeContent( $oldContent, $format );
76 $mine = $this->serializeContent( $myContent, $format );
77 $yours = $this->serializeContent( $yourContent, $format );
79 $ok = wfMerge( $old, $mine, $yours, $result );
81 if ( !$ok ) {
82 return false;
85 if ( !$result ) {
86 return $this->makeEmptyContent();
89 $mergedContent = $this->unserializeContent( $result, $format );
91 return $mergedContent;
94 /**
95 * Unserializes a Content object of the type supported by this ContentHandler.
97 * @since 1.21
99 * @param $text string serialized form of the content
100 * @param $format null|String the format used for serialization
102 * @return Content the TextContent object wrapping $text
104 public function unserializeContent( $text, $format = null ) {
105 $this->checkFormat( $format );
107 return new TextContent( $text );
111 * Creates an empty TextContent object.
113 * @since 1.21
115 * @return Content
117 public function makeEmptyContent() {
118 return new TextContent( '' );