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