| 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 | |
| 29 | namespace MediaWiki\Content; |
| 30 | |
| 31 | use LogicException; |
| 32 | use MediaWiki\HookContainer\HookRunner; |
| 33 | use MediaWiki\MediaWikiServices; |
| 34 | use MediaWiki\Parser\MagicWord; |
| 35 | use MediaWiki\Title\Title; |
| 36 | use MWException; |
| 37 | use Psr\Container\ContainerInterface; |
| 38 | use Wikimedia\JsonCodec\JsonClassCodec; |
| 39 | use Wikimedia\JsonCodec\JsonCodecable; |
| 40 | use Wikimedia\JsonCodec\JsonCodecInterface; |
| 41 | |
| 42 | /** |
| 43 | * Base implementation for content objects. |
| 44 | * |
| 45 | * @stable to extend |
| 46 | * |
| 47 | * @ingroup Content |
| 48 | */ |
| 49 | abstract class AbstractContent implements Content, JsonCodecable { |
| 50 | /** |
| 51 | * Name of the content model this Content object represents. |
| 52 | * Use with CONTENT_MODEL_XXX constants |
| 53 | * |
| 54 | * @since 1.21 |
| 55 | * |
| 56 | * @var string |
| 57 | */ |
| 58 | protected $model_id; |
| 59 | |
| 60 | /** |
| 61 | * @stable to call |
| 62 | * |
| 63 | * @param string|null $modelId |
| 64 | * |
| 65 | * @since 1.21 |
| 66 | */ |
| 67 | public function __construct( $modelId = null ) { |
| 68 | $this->model_id = $modelId; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @since 1.21 |
| 73 | * |
| 74 | * @see Content::getModel |
| 75 | * @return string |
| 76 | */ |
| 77 | public function getModel() { |
| 78 | return $this->model_id; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @since 1.21 |
| 83 | * |
| 84 | * @param string $modelId The model to check |
| 85 | * |
| 86 | * @throws MWException If the provided ID is not the ID of the content model supported by this |
| 87 | * Content object. |
| 88 | */ |
| 89 | protected function checkModelID( $modelId ) { |
| 90 | if ( $modelId !== $this->model_id ) { |
| 91 | throw new MWException( |
| 92 | "Bad content model: " . |
| 93 | "expected {$this->model_id} " . |
| 94 | "but got $modelId." |
| 95 | ); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @since 1.21 |
| 101 | * |
| 102 | * @see Content::getContentHandler |
| 103 | * @return ContentHandler |
| 104 | */ |
| 105 | public function getContentHandler() { |
| 106 | return $this->getContentHandlerFactory()->getContentHandler( $this->getModel() ); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * @return IContentHandlerFactory |
| 111 | */ |
| 112 | protected function getContentHandlerFactory(): IContentHandlerFactory { |
| 113 | return MediaWikiServices::getInstance()->getContentHandlerFactory(); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * @since 1.21 |
| 118 | * |
| 119 | * @see Content::getDefaultFormat |
| 120 | * @return string |
| 121 | */ |
| 122 | public function getDefaultFormat() { |
| 123 | return $this->getContentHandler()->getDefaultFormat(); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * @since 1.21 |
| 128 | * |
| 129 | * @see Content::getSupportedFormats |
| 130 | * @return string[] |
| 131 | */ |
| 132 | public function getSupportedFormats() { |
| 133 | return $this->getContentHandler()->getSupportedFormats(); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * @since 1.21 |
| 138 | * |
| 139 | * @param string $format |
| 140 | * |
| 141 | * @return bool |
| 142 | * |
| 143 | * @see Content::isSupportedFormat |
| 144 | */ |
| 145 | public function isSupportedFormat( $format ) { |
| 146 | if ( !$format ) { |
| 147 | return true; // this means "use the default" |
| 148 | } |
| 149 | |
| 150 | return $this->getContentHandler()->isSupportedFormat( $format ); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * @since 1.21 |
| 155 | * |
| 156 | * @param string $format The serialization format to check. |
| 157 | * |
| 158 | * @throws MWException If the format is not supported by this content handler. |
| 159 | */ |
| 160 | protected function checkFormat( $format ) { |
| 161 | if ( !$this->isSupportedFormat( $format ) ) { |
| 162 | throw new MWException( |
| 163 | "Format $format is not supported for content model " . |
| 164 | $this->getModel() |
| 165 | ); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * @stable to override |
| 171 | * @since 1.21 |
| 172 | * |
| 173 | * @param string|null $format |
| 174 | * |
| 175 | * @return string |
| 176 | * |
| 177 | * @see Content::serialize |
| 178 | */ |
| 179 | public function serialize( $format = null ) { |
| 180 | return $this->getContentHandler()->serializeContent( $this, $format ); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Returns native representation of the data. Interpretation depends on |
| 185 | * the data model used, as given by getDataModel(). |
| 186 | * |
| 187 | * @stable to override |
| 188 | * @since 1.21 |
| 189 | * |
| 190 | * @deprecated since 1.33. Use getText() for TextContent instances. |
| 191 | * For other content models, use specialized getters. |
| 192 | * Emitting deprecation warnings since 1.41. |
| 193 | * |
| 194 | * @return mixed The native representation of the content. Could be a |
| 195 | * string, a nested array structure, an object, a binary blob... |
| 196 | * anything, really. |
| 197 | * @throws LogicException |
| 198 | * |
| 199 | * @note Caller must be aware of content model! |
| 200 | */ |
| 201 | public function getNativeData() { |
| 202 | wfDeprecated( __METHOD__, '1.33' ); |
| 203 | throw new LogicException( __METHOD__ . ': not implemented' ); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * @stable to override |
| 208 | * @since 1.21 |
| 209 | * |
| 210 | * @return bool |
| 211 | * |
| 212 | * @see Content::isEmpty |
| 213 | */ |
| 214 | public function isEmpty() { |
| 215 | return $this->getSize() === 0; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Subclasses may override this to implement (light weight) validation. |
| 220 | * |
| 221 | * @stable to override |
| 222 | * @since 1.21 |
| 223 | * |
| 224 | * @return bool Always true. |
| 225 | * |
| 226 | * @see Content::isValid |
| 227 | */ |
| 228 | public function isValid() { |
| 229 | return true; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Decides whether two Content objects are equal. |
| 234 | * Two Content objects MUST not be considered equal if they do not share the same content model. |
| 235 | * Two Content objects that are equal SHOULD have the same serialization. |
| 236 | * |
| 237 | * This default implementation relies on equalsInternal() to determine whether the |
| 238 | * Content objects are logically equivalent. Subclasses that need to implement a custom |
| 239 | * equality check should consider overriding equalsInternal(). Subclasses that override |
| 240 | * equals() itself MUST make sure that the implementation returns false for $that === null, |
| 241 | * and true for $that === this. It MUST also return false if $that does not have the same |
| 242 | * content model. |
| 243 | * |
| 244 | * @stable to override |
| 245 | * @since 1.21 |
| 246 | * |
| 247 | * @param Content|null $that |
| 248 | * |
| 249 | * @return bool |
| 250 | * |
| 251 | * @see Content::equals |
| 252 | */ |
| 253 | public function equals( ?Content $that = null ) { |
| 254 | if ( $that === null ) { |
| 255 | return false; |
| 256 | } |
| 257 | |
| 258 | if ( $that === $this ) { |
| 259 | return true; |
| 260 | } |
| 261 | |
| 262 | if ( $that->getModel() !== $this->getModel() ) { |
| 263 | return false; |
| 264 | } |
| 265 | |
| 266 | // For type safety. Needed for odd cases like non-TextContents using CONTENT_MODEL_WIKITEXT |
| 267 | if ( get_class( $that ) !== get_class( $this ) ) { |
| 268 | return false; |
| 269 | } |
| 270 | |
| 271 | return $this->equalsInternal( $that ); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Checks whether $that is logically equal to this Content object. |
| 276 | * |
| 277 | * This method can be overwritten by subclasses that need to implement custom |
| 278 | * equality checks. |
| 279 | * |
| 280 | * This default implementation checks whether the serializations |
| 281 | * of $this and $that are the same: $this->serialize() === $that->serialize() |
| 282 | * |
| 283 | * Implementors can assume that $that is an instance of the same class |
| 284 | * as the present Content object, as long as equalsInternal() is only called |
| 285 | * by the standard implementation of equals(). |
| 286 | * |
| 287 | * @note Do not call this method directly, call equals() instead. |
| 288 | * |
| 289 | * @stable to override |
| 290 | * |
| 291 | * @param Content $that |
| 292 | * @return bool |
| 293 | */ |
| 294 | protected function equalsInternal( Content $that ) { |
| 295 | return $this->serialize() === $that->serialize(); |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Subclasses that implement redirects should override this. |
| 300 | * |
| 301 | * @stable to override |
| 302 | * @since 1.21 |
| 303 | * |
| 304 | * @return Title|null |
| 305 | * |
| 306 | * @see Content::getRedirectTarget |
| 307 | */ |
| 308 | public function getRedirectTarget() { |
| 309 | return null; |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * @since 1.21 |
| 314 | * |
| 315 | * @return bool |
| 316 | * |
| 317 | * @see Content::isRedirect |
| 318 | */ |
| 319 | public function isRedirect() { |
| 320 | return $this->getRedirectTarget() !== null; |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * This default implementation always returns $this. |
| 325 | * Subclasses that implement redirects should override this. |
| 326 | * |
| 327 | * @stable to override |
| 328 | * @since 1.21 |
| 329 | * |
| 330 | * @param Title $target |
| 331 | * |
| 332 | * @return Content $this |
| 333 | * |
| 334 | * @see Content::updateRedirect |
| 335 | */ |
| 336 | public function updateRedirect( Title $target ) { |
| 337 | return $this; |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * @stable to override |
| 342 | * @since 1.21 |
| 343 | * |
| 344 | * @param string|int $sectionId |
| 345 | * @return null |
| 346 | * |
| 347 | * @see Content::getSection |
| 348 | */ |
| 349 | public function getSection( $sectionId ) { |
| 350 | return null; |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * @stable to override |
| 355 | * @since 1.21 |
| 356 | * |
| 357 | * @param string|int|null|false $sectionId |
| 358 | * @param Content $with |
| 359 | * @param string $sectionTitle |
| 360 | * @return null |
| 361 | * |
| 362 | * @see Content::replaceSection |
| 363 | */ |
| 364 | public function replaceSection( $sectionId, Content $with, $sectionTitle = '' ) { |
| 365 | return null; |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * @stable to override |
| 370 | * @since 1.21 |
| 371 | * |
| 372 | * @param string $header |
| 373 | * @return Content $this |
| 374 | * |
| 375 | * @see Content::addSectionHeader |
| 376 | */ |
| 377 | public function addSectionHeader( $header ) { |
| 378 | return $this; |
| 379 | } |
| 380 | |
| 381 | /** |
| 382 | * This default implementation always returns false. Subclasses may override |
| 383 | * this to supply matching logic. |
| 384 | * |
| 385 | * @stable to override |
| 386 | * @since 1.21 |
| 387 | * |
| 388 | * @param MagicWord $word |
| 389 | * |
| 390 | * @return bool Always false. |
| 391 | * |
| 392 | * @see Content::matchMagicWord |
| 393 | */ |
| 394 | public function matchMagicWord( MagicWord $word ) { |
| 395 | return false; |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * This base implementation calls the hook ConvertContent to enable custom conversions. |
| 400 | * Subclasses may override this to implement conversion for "their" content model. |
| 401 | * |
| 402 | * @stable to override |
| 403 | * |
| 404 | * @param string $toModel |
| 405 | * @param string $lossy |
| 406 | * |
| 407 | * @return Content|false |
| 408 | * |
| 409 | * @see Content::convert() |
| 410 | */ |
| 411 | public function convert( $toModel, $lossy = '' ) { |
| 412 | if ( $this->getModel() === $toModel ) { |
| 413 | // nothing to do, shorten out. |
| 414 | return $this; |
| 415 | } |
| 416 | |
| 417 | $lossy = ( $lossy === 'lossy' ); // string flag, convert to boolean for convenience |
| 418 | $result = false; |
| 419 | |
| 420 | ( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) ) |
| 421 | ->onConvertContent( $this, $toModel, $lossy, $result ); |
| 422 | |
| 423 | return $result; |
| 424 | } |
| 425 | |
| 426 | public static function jsonClassCodec( |
| 427 | JsonCodecInterface $codec, |
| 428 | ContainerInterface $serviceContainer |
| 429 | ): JsonClassCodec { |
| 430 | return $serviceContainer->get( 'ContentJsonCodec' ); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | /** @deprecated class alias since 1.43 */ |
| 435 | class_alias( AbstractContent::class, 'AbstractContent' ); |