Properly deprecate srprop=score|hasrelated
[mediawiki.git] / includes / content / TextContentHandler.php
blobffe1acbd202963ea835e1bb565dde7690284951e
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 {
33 // @codingStandardsIgnoreStart bug 57585
34 public function __construct( $modelId = CONTENT_MODEL_TEXT,
35 $formats = array( CONTENT_FORMAT_TEXT ) ) {
36 parent::__construct( $modelId, $formats );
38 // @codingStandardsIgnoreEnd
40 /**
41 * Returns the content's text as-is.
43 * @param Content $content
44 * @param string $format The serialization format to check
46 * @return mixed
48 public function serializeContent( Content $content, $format = null ) {
49 $this->checkFormat( $format );
51 return $content->getNativeData();
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.
58 * All three Content objects passed as parameters must have the same
59 * content model.
61 * This text-based implementation uses wfMerge().
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.
67 * @return Content|bool
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() );
74 $format = $this->getDefaultFormat();
76 $old = $this->serializeContent( $oldContent, $format );
77 $mine = $this->serializeContent( $myContent, $format );
78 $yours = $this->serializeContent( $yourContent, $format );
80 $ok = wfMerge( $old, $mine, $yours, $result );
82 if ( !$ok ) {
83 return false;
86 if ( !$result ) {
87 return $this->makeEmptyContent();
90 $mergedContent = $this->unserializeContent( $result, $format );
92 return $mergedContent;
95 /**
96 * Returns the name of the associated Content class, to
97 * be used when creating new objects. Override expected
98 * by subclasses.
100 * @since 1.24
102 * @return string
104 protected function getContentClass() {
105 return 'TextContent';
109 * Unserializes a Content object of the type supported by this ContentHandler.
111 * @since 1.21
113 * @param string $text Serialized form of the content
114 * @param string $format The format used for serialization
116 * @return Content The TextContent object wrapping $text
118 public function unserializeContent( $text, $format = null ) {
119 $this->checkFormat( $format );
121 $class = $this->getContentClass();
122 return new $class( $text );
126 * Creates an empty TextContent object.
128 * @since 1.21
130 * @return Content A new TextContent object with empty text.
132 public function makeEmptyContent() {
133 $class = $this->getContentClass();
134 return new $class( '' );