Merge "Remove needless leading dash on Main Page interlanguage links"
[lhc/web/wiklou.git] / includes / content / TextContentHandler.php
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 public function __construct( $modelId = CONTENT_MODEL_TEXT,
33 $formats = array( CONTENT_FORMAT_TEXT )
34 ) {
35 parent::__construct( $modelId, $formats );
36 }
37
38 /**
39 * Returns the content's text as-is.
40 *
41 * @param $content Content
42 * @param $format string|null
43 * @return mixed
44 */
45 public function serializeContent( Content $content, $format = null ) {
46 $this->checkFormat( $format );
47
48 return $content->getNativeData();
49 }
50
51 /**
52 * Attempts to merge differences between three versions. Returns a new
53 * Content object for a clean merge and false for failure or a conflict.
54 *
55 * All three Content objects passed as parameters must have the same
56 * content model.
57 *
58 * This text-based implementation uses wfMerge().
59 *
60 * @param $oldContent Content|string String
61 * @param $myContent Content|string String
62 * @param $yourContent Content|string String
63 *
64 * @return Content|Bool
65 */
66 public function merge3( Content $oldContent, Content $myContent, Content $yourContent ) {
67 $this->checkModelID( $oldContent->getModel() );
68 $this->checkModelID( $myContent->getModel() );
69 $this->checkModelID( $yourContent->getModel() );
70
71 $format = $this->getDefaultFormat();
72
73 $old = $this->serializeContent( $oldContent, $format );
74 $mine = $this->serializeContent( $myContent, $format );
75 $yours = $this->serializeContent( $yourContent, $format );
76
77 $ok = wfMerge( $old, $mine, $yours, $result );
78
79 if ( !$ok ) {
80 return false;
81 }
82
83 if ( !$result ) {
84 return $this->makeEmptyContent();
85 }
86
87 $mergedContent = $this->unserializeContent( $result, $format );
88
89 return $mergedContent;
90 }
91
92 /**
93 * Unserializes a Content object of the type supported by this ContentHandler.
94 *
95 * @since 1.21
96 *
97 * @param $text string serialized form of the content
98 * @param $format null|String the format used for serialization
99 *
100 * @return Content the TextContent object wrapping $text
101 */
102 public function unserializeContent( $text, $format = null ) {
103 $this->checkFormat( $format );
104
105 return new TextContent( $text );
106 }
107
108 /**
109 * Creates an empty TextContent object.
110 *
111 * @since 1.21
112 *
113 * @return Content
114 */
115 public function makeEmptyContent() {
116 return new TextContent( '' );
117 }
118 }