Merge "Fix doc/hooks.txt for EditPage::showStandardInputs:options"
[lhc/web/wiklou.git] / includes / content / TextContentHandler.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 /**
22 * @since 1.21
23 */
24 class TextContentHandler extends ContentHandler {
25
26 public function __construct( $modelId = CONTENT_MODEL_TEXT, $formats = array( CONTENT_FORMAT_TEXT ) ) {
27 parent::__construct( $modelId, $formats );
28 }
29
30 /**
31 * Returns the content's text as-is.
32 *
33 * @param $content Content
34 * @param $format string|null
35 * @return mixed
36 */
37 public function serializeContent( Content $content, $format = null ) {
38 $this->checkFormat( $format );
39 return $content->getNativeData();
40 }
41
42 /**
43 * Attempts to merge differences between three versions. Returns a new
44 * Content object for a clean merge and false for failure or a conflict.
45 *
46 * All three Content objects passed as parameters must have the same
47 * content model.
48 *
49 * This text-based implementation uses wfMerge().
50 *
51 * @param $oldContent Content|string String
52 * @param $myContent Content|string String
53 * @param $yourContent Content|string String
54 *
55 * @return Content|Bool
56 */
57 public function merge3( Content $oldContent, Content $myContent, Content $yourContent ) {
58 $this->checkModelID( $oldContent->getModel() );
59 $this->checkModelID( $myContent->getModel() );
60 $this->checkModelID( $yourContent->getModel() );
61
62 $format = $this->getDefaultFormat();
63
64 $old = $this->serializeContent( $oldContent, $format );
65 $mine = $this->serializeContent( $myContent, $format );
66 $yours = $this->serializeContent( $yourContent, $format );
67
68 $ok = wfMerge( $old, $mine, $yours, $result );
69
70 if ( !$ok ) {
71 return false;
72 }
73
74 if ( !$result ) {
75 return $this->makeEmptyContent();
76 }
77
78 $mergedContent = $this->unserializeContent( $result, $format );
79 return $mergedContent;
80 }
81
82 /**
83 * Unserializes a Content object of the type supported by this ContentHandler.
84 *
85 * @since 1.21
86 *
87 * @param $text string serialized form of the content
88 * @param $format null|String the format used for serialization
89 *
90 * @return Content the TextContent object wrapping $text
91 */
92 public function unserializeContent( $text, $format = null ) {
93 $this->checkFormat( $format );
94
95 return new TextContent( $text );
96 }
97
98 /**
99 * Creates an empty TextContent object.
100 *
101 * @since 1.21
102 *
103 * @return Content
104 */
105 public function makeEmptyContent() {
106 return new TextContent( '' );
107 }
108 }