Move remaining LoadBalancer classes to Rdbms
[mediawiki.git] / includes / content / AbstractContent.php
blob811b241798ee29434b192d28a57aee8e96530cd4
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.
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.
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.
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
21 * @since 1.21
23 * @file
24 * @ingroup Content
26 * @author Daniel Kinzler
29 /**
30 * Base implementation for content objects.
32 * @ingroup Content
34 abstract class AbstractContent implements Content {
35 /**
36 * Name of the content model this Content object represents.
37 * Use with CONTENT_MODEL_XXX constants
39 * @since 1.21
41 * @var string $model_id
43 protected $model_id;
45 /**
46 * @param string $modelId
48 * @since 1.21
50 public function __construct( $modelId = null ) {
51 $this->model_id = $modelId;
54 /**
55 * @since 1.21
57 * @see Content::getModel
59 public function getModel() {
60 return $this->model_id;
63 /**
64 * @since 1.21
66 * @param string $modelId The model to check
68 * @throws MWException If the provided ID is not the ID of the content model supported by this
69 * Content object.
71 protected function checkModelID( $modelId ) {
72 if ( $modelId !== $this->model_id ) {
73 throw new MWException(
74 "Bad content model: " .
75 "expected {$this->model_id} " .
76 "but got $modelId."
81 /**
82 * @since 1.21
84 * @see Content::getContentHandler
86 public function getContentHandler() {
87 return ContentHandler::getForContent( $this );
90 /**
91 * @since 1.21
93 * @see Content::getDefaultFormat
95 public function getDefaultFormat() {
96 return $this->getContentHandler()->getDefaultFormat();
99 /**
100 * @since 1.21
102 * @see Content::getSupportedFormats
104 public function getSupportedFormats() {
105 return $this->getContentHandler()->getSupportedFormats();
109 * @since 1.21
111 * @param string $format
113 * @return bool
115 * @see Content::isSupportedFormat
117 public function isSupportedFormat( $format ) {
118 if ( !$format ) {
119 return true; // this means "use the default"
122 return $this->getContentHandler()->isSupportedFormat( $format );
126 * @since 1.21
128 * @param string $format The serialization format to check.
130 * @throws MWException If the format is not supported by this content handler.
132 protected function checkFormat( $format ) {
133 if ( !$this->isSupportedFormat( $format ) ) {
134 throw new MWException(
135 "Format $format is not supported for content model " .
136 $this->getModel()
142 * @since 1.21
144 * @param string $format
146 * @return string
148 * @see Content::serialize
150 public function serialize( $format = null ) {
151 return $this->getContentHandler()->serializeContent( $this, $format );
155 * @since 1.21
157 * @return bool
159 * @see Content::isEmpty
161 public function isEmpty() {
162 return $this->getSize() === 0;
166 * Subclasses may override this to implement (light weight) validation.
168 * @since 1.21
170 * @return bool Always true.
172 * @see Content::isValid
174 public function isValid() {
175 return true;
179 * @since 1.21
181 * @param Content $that
183 * @return bool
185 * @see Content::equals
187 public function equals( Content $that = null ) {
188 if ( is_null( $that ) ) {
189 return false;
192 if ( $that === $this ) {
193 return true;
196 if ( $that->getModel() !== $this->getModel() ) {
197 return false;
200 return $this->getNativeData() === $that->getNativeData();
204 * Returns a list of DataUpdate objects for recording information about this
205 * Content in some secondary data store.
207 * This default implementation returns a LinksUpdate object and calls the
208 * SecondaryDataUpdates hook.
210 * Subclasses may override this to determine the secondary data updates more
211 * efficiently, preferably without the need to generate a parser output object.
212 * They should however make sure to call SecondaryDataUpdates to give extensions
213 * a chance to inject additional updates.
215 * @since 1.21
217 * @param Title $title
218 * @param Content $old
219 * @param bool $recursive
220 * @param ParserOutput $parserOutput
222 * @return DataUpdate[]
224 * @see Content::getSecondaryDataUpdates()
226 public function getSecondaryDataUpdates( Title $title, Content $old = null,
227 $recursive = true, ParserOutput $parserOutput = null
229 if ( $parserOutput === null ) {
230 $parserOutput = $this->getParserOutput( $title, null, null, false );
233 $updates = [
234 new LinksUpdate( $title, $parserOutput, $recursive )
237 Hooks::run( 'SecondaryDataUpdates', [ $title, $old, $recursive, $parserOutput, &$updates ] );
239 return $updates;
243 * @since 1.21
245 * @return Title[]|null
247 * @see Content::getRedirectChain
249 public function getRedirectChain() {
250 global $wgMaxRedirects;
251 $title = $this->getRedirectTarget();
252 if ( is_null( $title ) ) {
253 return null;
255 // recursive check to follow double redirects
256 $recurse = $wgMaxRedirects;
257 $titles = [ $title ];
258 while ( --$recurse > 0 ) {
259 if ( $title->isRedirect() ) {
260 $page = WikiPage::factory( $title );
261 $newtitle = $page->getRedirectTarget();
262 } else {
263 break;
265 // Redirects to some special pages are not permitted
266 if ( $newtitle instanceof Title && $newtitle->isValidRedirectTarget() ) {
267 // The new title passes the checks, so make that our current
268 // title so that further recursion can be checked
269 $title = $newtitle;
270 $titles[] = $newtitle;
271 } else {
272 break;
276 return $titles;
280 * Subclasses that implement redirects should override this.
282 * @since 1.21
284 * @return Title|null
286 * @see Content::getRedirectTarget
288 public function getRedirectTarget() {
289 return null;
293 * @note Migrated here from Title::newFromRedirectRecurse.
295 * @since 1.21
297 * @return Title|null
299 * @see Content::getUltimateRedirectTarget
301 public function getUltimateRedirectTarget() {
302 $titles = $this->getRedirectChain();
304 return $titles ? array_pop( $titles ) : null;
308 * @since 1.21
310 * @return bool
312 * @see Content::isRedirect
314 public function isRedirect() {
315 return $this->getRedirectTarget() !== null;
319 * This default implementation always returns $this.
320 * Subclasses that implement redirects should override this.
322 * @since 1.21
324 * @param Title $target
326 * @return Content $this
328 * @see Content::updateRedirect
330 public function updateRedirect( Title $target ) {
331 return $this;
335 * @since 1.21
337 * @return null
339 * @see Content::getSection
341 public function getSection( $sectionId ) {
342 return null;
346 * @since 1.21
348 * @return null
350 * @see Content::replaceSection
352 public function replaceSection( $sectionId, Content $with, $sectionTitle = '' ) {
353 return null;
357 * @since 1.21
359 * @return Content $this
361 * @see Content::preSaveTransform
363 public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
364 return $this;
368 * @since 1.21
370 * @return Content $this
372 * @see Content::addSectionHeader
374 public function addSectionHeader( $header ) {
375 return $this;
379 * @since 1.21
381 * @return Content $this
383 * @see Content::preloadTransform
385 public function preloadTransform( Title $title, ParserOptions $popts, $params = [] ) {
386 return $this;
390 * @since 1.21
392 * @return Status
394 * @see Content::prepareSave
396 public function prepareSave( WikiPage $page, $flags, $parentRevId, User $user ) {
397 if ( $this->isValid() ) {
398 return Status::newGood();
399 } else {
400 return Status::newFatal( "invalid-content-data" );
405 * @since 1.21
407 * @param WikiPage $page
408 * @param ParserOutput $parserOutput
410 * @return LinksDeletionUpdate[]
412 * @see Content::getDeletionUpdates
414 public function getDeletionUpdates( WikiPage $page, ParserOutput $parserOutput = null ) {
415 return [
416 new LinksDeletionUpdate( $page ),
421 * This default implementation always returns false. Subclasses may override
422 * this to supply matching logic.
424 * @since 1.21
426 * @param MagicWord $word
428 * @return bool Always false.
430 * @see Content::matchMagicWord
432 public function matchMagicWord( MagicWord $word ) {
433 return false;
437 * This base implementation calls the hook ConvertContent to enable custom conversions.
438 * Subclasses may override this to implement conversion for "their" content model.
440 * @param string $toModel
441 * @param string $lossy
443 * @return Content|bool
445 * @see Content::convert()
447 public function convert( $toModel, $lossy = '' ) {
448 if ( $this->getModel() === $toModel ) {
449 // nothing to do, shorten out.
450 return $this;
453 $lossy = ( $lossy === 'lossy' ); // string flag, convert to boolean for convenience
454 $result = false;
456 Hooks::run( 'ConvertContent', [ $this, $toModel, $lossy, &$result ] );
458 return $result;
462 * Returns a ParserOutput object containing information derived from this content.
463 * Most importantly, unless $generateHtml was false, the return value contains an
464 * HTML representation of the content.
466 * Subclasses that want to control the parser output may override this, but it is
467 * preferred to override fillParserOutput() instead.
469 * Subclasses that override getParserOutput() itself should take care to call the
470 * ContentGetParserOutput hook.
472 * @since 1.24
474 * @param Title $title Context title for parsing
475 * @param int|null $revId Revision ID (for {{REVISIONID}})
476 * @param ParserOptions|null $options Parser options
477 * @param bool $generateHtml Whether or not to generate HTML
479 * @return ParserOutput Containing information derived from this content.
481 public function getParserOutput( Title $title, $revId = null,
482 ParserOptions $options = null, $generateHtml = true
484 if ( $options === null ) {
485 $options = $this->getContentHandler()->makeParserOptions( 'canonical' );
488 $po = new ParserOutput();
490 if ( Hooks::run( 'ContentGetParserOutput',
491 [ $this, $title, $revId, $options, $generateHtml, &$po ] ) ) {
493 // Save and restore the old value, just in case something is reusing
494 // the ParserOptions object in some weird way.
495 $oldRedir = $options->getRedirectTarget();
496 $options->setRedirectTarget( $this->getRedirectTarget() );
497 $this->fillParserOutput( $title, $revId, $options, $generateHtml, $po );
498 $options->setRedirectTarget( $oldRedir );
501 Hooks::run( 'ContentAlterParserOutput', [ $this, $title, $po ] );
503 return $po;
507 * Fills the provided ParserOutput with information derived from the content.
508 * Unless $generateHtml was false, this includes an HTML representation of the content.
510 * This is called by getParserOutput() after consulting the ContentGetParserOutput hook.
511 * Subclasses are expected to override this method (or getParserOutput(), if need be).
512 * Subclasses of TextContent should generally override getHtml() instead.
514 * This placeholder implementation always throws an exception.
516 * @since 1.24
518 * @param Title $title Context title for parsing
519 * @param int|null $revId Revision ID (for {{REVISIONID}})
520 * @param ParserOptions $options Parser options
521 * @param bool $generateHtml Whether or not to generate HTML
522 * @param ParserOutput &$output The output object to fill (reference).
524 * @throws MWException
526 protected function fillParserOutput( Title $title, $revId,
527 ParserOptions $options, $generateHtml, ParserOutput &$output
529 // Don't make abstract, so subclasses that override getParserOutput() directly don't fail.
530 throw new MWException( 'Subclasses of AbstractContent must override fillParserOutput!' );