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