1a730bc47ed516487a88783785cdf4bafe2e78ac
[lhc/web/wiklou.git] / includes / content / AbstractContent.php
1 <?php
2 /**
3 * A content object represents page content, e.g. the text to show on a page.
4 * Content objects have no knowledge about how they relate to Wiki pages.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @since 1.21
22 *
23 * @file
24 * @ingroup Content
25 *
26 * @author Daniel Kinzler
27 */
28 abstract class AbstractContent implements Content {
29
30 /**
31 * Name of the content model this Content object represents.
32 * Use with CONTENT_MODEL_XXX constants
33 *
34 * @var string $model_id
35 */
36 protected $model_id;
37
38 /**
39 * @param String $model_id
40 */
41 public function __construct( $model_id = null ) {
42 $this->model_id = $model_id;
43 }
44
45 /**
46 * @see Content::getModel()
47 */
48 public function getModel() {
49 return $this->model_id;
50 }
51
52 /**
53 * Throws an MWException if $model_id is not the id of the content model
54 * supported by this Content object.
55 *
56 * @param $model_id int the model to check
57 *
58 * @throws MWException
59 */
60 protected function checkModelID( $model_id ) {
61 if ( $model_id !== $this->model_id ) {
62 throw new MWException( "Bad content model: " .
63 "expected {$this->model_id} " .
64 "but got $model_id." );
65 }
66 }
67
68 /**
69 * @see Content::getContentHandler()
70 */
71 public function getContentHandler() {
72 return ContentHandler::getForContent( $this );
73 }
74
75 /**
76 * @see Content::getDefaultFormat()
77 */
78 public function getDefaultFormat() {
79 return $this->getContentHandler()->getDefaultFormat();
80 }
81
82 /**
83 * @see Content::getSupportedFormats()
84 */
85 public function getSupportedFormats() {
86 return $this->getContentHandler()->getSupportedFormats();
87 }
88
89 /**
90 * @see Content::isSupportedFormat()
91 */
92 public function isSupportedFormat( $format ) {
93 if ( !$format ) {
94 return true; // this means "use the default"
95 }
96
97 return $this->getContentHandler()->isSupportedFormat( $format );
98 }
99
100 /**
101 * Throws an MWException if $this->isSupportedFormat( $format ) doesn't
102 * return true.
103 *
104 * @param $format
105 * @throws MWException
106 */
107 protected function checkFormat( $format ) {
108 if ( !$this->isSupportedFormat( $format ) ) {
109 throw new MWException( "Format $format is not supported for content model " .
110 $this->getModel() );
111 }
112 }
113
114 /**
115 * @see Content::serialize
116 */
117 public function serialize( $format = null ) {
118 return $this->getContentHandler()->serializeContent( $this, $format );
119 }
120
121 /**
122 * @see Content::isEmpty()
123 */
124 public function isEmpty() {
125 return $this->getSize() === 0;
126 }
127
128 /**
129 * @see Content::isValid()
130 */
131 public function isValid() {
132 return true;
133 }
134
135 /**
136 * @see Content::equals()
137 */
138 public function equals( Content $that = null ) {
139 if ( is_null( $that ) ) {
140 return false;
141 }
142
143 if ( $that === $this ) {
144 return true;
145 }
146
147 if ( $that->getModel() !== $this->getModel() ) {
148 return false;
149 }
150
151 return $this->getNativeData() === $that->getNativeData();
152 }
153
154
155 /**
156 * Returns a list of DataUpdate objects for recording information about this
157 * Content in some secondary data store.
158 *
159 * This default implementation calls
160 * $this->getParserOutput( $content, $title, null, null, false ),
161 * and then calls getSecondaryDataUpdates( $title, $recursive ) on the
162 * resulting ParserOutput object.
163 *
164 * Subclasses may override this to determine the secondary data updates more
165 * efficiently, preferrably without the need to generate a parser output object.
166 *
167 * @see Content::getSecondaryDataUpdates()
168 *
169 * @param $title Title The context for determining the necessary updates
170 * @param $old Content|null An optional Content object representing the
171 * previous content, i.e. the content being replaced by this Content
172 * object.
173 * @param $recursive boolean Whether to include recursive updates (default:
174 * false).
175 * @param $parserOutput ParserOutput|null Optional ParserOutput object.
176 * Provide if you have one handy, to avoid re-parsing of the content.
177 *
178 * @return Array. A list of DataUpdate objects for putting information
179 * about this content object somewhere.
180 *
181 * @since 1.21
182 */
183 public function getSecondaryDataUpdates( Title $title,
184 Content $old = null,
185 $recursive = true, ParserOutput $parserOutput = null
186 ) {
187 if ( !$parserOutput ) {
188 $parserOutput = $this->getParserOutput( $title, null, null, false );
189 }
190
191 return $parserOutput->getSecondaryDataUpdates( $title, $recursive );
192 }
193
194
195 /**
196 * @see Content::getRedirectChain()
197 */
198 public function getRedirectChain() {
199 global $wgMaxRedirects;
200 $title = $this->getRedirectTarget();
201 if ( is_null( $title ) ) {
202 return null;
203 }
204 // recursive check to follow double redirects
205 $recurse = $wgMaxRedirects;
206 $titles = array( $title );
207 while ( --$recurse > 0 ) {
208 if ( $title->isRedirect() ) {
209 $page = WikiPage::factory( $title );
210 $newtitle = $page->getRedirectTarget();
211 } else {
212 break;
213 }
214 // Redirects to some special pages are not permitted
215 if ( $newtitle instanceOf Title && $newtitle->isValidRedirectTarget() ) {
216 // The new title passes the checks, so make that our current
217 // title so that further recursion can be checked
218 $title = $newtitle;
219 $titles[] = $newtitle;
220 } else {
221 break;
222 }
223 }
224 return $titles;
225 }
226
227 /**
228 * @see Content::getRedirectTarget()
229 */
230 public function getRedirectTarget() {
231 return null;
232 }
233
234 /**
235 * @see Content::getUltimateRedirectTarget()
236 * @note: migrated here from Title::newFromRedirectRecurse
237 */
238 public function getUltimateRedirectTarget() {
239 $titles = $this->getRedirectChain();
240 return $titles ? array_pop( $titles ) : null;
241 }
242
243 /**
244 * @see Content::isRedirect()
245 *
246 * @since 1.21
247 *
248 * @return bool
249 */
250 public function isRedirect() {
251 return $this->getRedirectTarget() !== null;
252 }
253
254 /**
255 * @see Content::updateRedirect()
256 *
257 * This default implementation always returns $this.
258 *
259 * @since 1.21
260 *
261 * @return Content $this
262 */
263 public function updateRedirect( Title $target ) {
264 return $this;
265 }
266
267 /**
268 * @see Content::getSection()
269 */
270 public function getSection( $sectionId ) {
271 return null;
272 }
273
274 /**
275 * @see Content::replaceSection()
276 */
277 public function replaceSection( $section, Content $with, $sectionTitle = '' ) {
278 return null;
279 }
280
281 /**
282 * @see Content::preSaveTransform()
283 */
284 public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
285 return $this;
286 }
287
288 /**
289 * @see Content::addSectionHeader()
290 */
291 public function addSectionHeader( $header ) {
292 return $this;
293 }
294
295 /**
296 * @see Content::preloadTransform()
297 */
298 public function preloadTransform( Title $title, ParserOptions $popts ) {
299 return $this;
300 }
301
302 /**
303 * @see Content::prepareSave()
304 */
305 public function prepareSave( WikiPage $page, $flags, $baseRevId, User $user ) {
306 if ( $this->isValid() ) {
307 return Status::newGood();
308 } else {
309 return Status::newFatal( "invalid-content-data" );
310 }
311 }
312
313 /**
314 * @see Content::getDeletionUpdates()
315 *
316 * @since 1.21
317 *
318 * @param $page \WikiPage the deleted page
319 * @param $parserOutput null|\ParserOutput optional parser output object
320 * for efficient access to meta-information about the content object.
321 * Provide if you have one handy.
322 *
323 * @return array A list of DataUpdate instances that will clean up the
324 * database after deletion.
325 */
326 public function getDeletionUpdates( WikiPage $page,
327 ParserOutput $parserOutput = null )
328 {
329 return array(
330 new LinksDeletionUpdate( $page ),
331 );
332 }
333
334 /**
335 * @see Content::matchMagicWord()
336 *
337 * This default implementation always returns false. Subclasses may override this to supply matching logic.
338 *
339 * @param MagicWord $word
340 *
341 * @return bool
342 */
343 public function matchMagicWord( MagicWord $word ) {
344 return false;
345 }
346 }