MediaWiki  master
AbstractContent.php
Go to the documentation of this file.
00001 <?php
00028 abstract class AbstractContent implements Content {
00029 
00038         protected $model_id;
00039 
00045         public function __construct( $modelId = null ) {
00046                 $this->model_id = $modelId;
00047         }
00048 
00054         public function getModel() {
00055                 return $this->model_id;
00056         }
00057 
00068         protected function checkModelID( $modelId ) {
00069                 if ( $modelId !== $this->model_id ) {
00070                         throw new MWException(
00071                                 "Bad content model: " .
00072                                 "expected {$this->model_id}  " .
00073                                 "but got $modelId."
00074                         );
00075                 }
00076         }
00077 
00083         public function getContentHandler() {
00084                 return ContentHandler::getForContent( $this );
00085         }
00086 
00092         public function getDefaultFormat() {
00093                 return $this->getContentHandler()->getDefaultFormat();
00094         }
00095 
00101         public function getSupportedFormats() {
00102                 return $this->getContentHandler()->getSupportedFormats();
00103         }
00104 
00114         public function isSupportedFormat( $format ) {
00115                 if ( !$format ) {
00116                         return true; // this means "use the default"
00117                 }
00118 
00119                 return $this->getContentHandler()->isSupportedFormat( $format );
00120         }
00121 
00131         protected function checkFormat( $format ) {
00132                 if ( !$this->isSupportedFormat( $format ) ) {
00133                         throw new MWException(
00134                                 "Format $format is not supported for content model " .
00135                                 $this->getModel()
00136                         );
00137                 }
00138         }
00139 
00149         public function serialize( $format = null ) {
00150                 return $this->getContentHandler()->serializeContent( $this, $format );
00151         }
00152 
00160         public function isEmpty() {
00161                 return $this->getSize() === 0;
00162         }
00163 
00171         public function isValid() {
00172                 return true;
00173         }
00174 
00184         public function equals( Content $that = null ) {
00185                 if ( is_null( $that ) ) {
00186                         return false;
00187                 }
00188 
00189                 if ( $that === $this ) {
00190                         return true;
00191                 }
00192 
00193                 if ( $that->getModel() !== $this->getModel() ) {
00194                         return false;
00195                 }
00196 
00197                 return $this->getNativeData() === $that->getNativeData();
00198         }
00199 
00200 
00229         public function getSecondaryDataUpdates( Title $title,
00230                 Content $old = null,
00231                 $recursive = true, ParserOutput $parserOutput = null
00232         ) {
00233                 if ( !$parserOutput ) {
00234                         $parserOutput = $this->getParserOutput( $title, null, null, false );
00235                 }
00236 
00237                 return $parserOutput->getSecondaryDataUpdates( $title, $recursive );
00238         }
00239 
00240 
00246         public function getRedirectChain() {
00247                 global $wgMaxRedirects;
00248                 $title = $this->getRedirectTarget();
00249                 if ( is_null( $title ) ) {
00250                         return null;
00251                 }
00252                 // recursive check to follow double redirects
00253                 $recurse = $wgMaxRedirects;
00254                 $titles = array( $title );
00255                 while ( --$recurse > 0 ) {
00256                         if ( $title->isRedirect() ) {
00257                                 $page = WikiPage::factory( $title );
00258                                 $newtitle = $page->getRedirectTarget();
00259                         } else {
00260                                 break;
00261                         }
00262                         // Redirects to some special pages are not permitted
00263                         if ( $newtitle instanceOf Title && $newtitle->isValidRedirectTarget() ) {
00264                                 // The new title passes the checks, so make that our current
00265                                 // title so that further recursion can be checked
00266                                 $title = $newtitle;
00267                                 $titles[] = $newtitle;
00268                         } else {
00269                                 break;
00270                         }
00271                 }
00272                 return $titles;
00273         }
00274 
00280         public function getRedirectTarget() {
00281                 return null;
00282         }
00283 
00290         public function getUltimateRedirectTarget() {
00291                 $titles = $this->getRedirectChain();
00292                 return $titles ? array_pop( $titles ) : null;
00293         }
00294 
00302         public function isRedirect() {
00303                 return $this->getRedirectTarget() !== null;
00304         }
00305 
00317         public function updateRedirect( Title $target ) {
00318                 return $this;
00319         }
00320 
00326         public function getSection( $sectionId ) {
00327                 return null;
00328         }
00329 
00335         public function replaceSection( $section, Content $with, $sectionTitle = ''  ) {
00336                 return null;
00337         }
00338 
00344         public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
00345                 return $this;
00346         }
00347 
00353         public function addSectionHeader( $header ) {
00354                 return $this;
00355         }
00356 
00362         public function preloadTransform( Title $title, ParserOptions $popts ) {
00363                 return $this;
00364         }
00365 
00371         public function prepareSave( WikiPage $page, $flags, $baseRevId, User $user ) {
00372                 if ( $this->isValid() ) {
00373                         return Status::newGood();
00374                 } else {
00375                         return Status::newFatal( "invalid-content-data" );
00376                 }
00377         }
00378 
00392         public function getDeletionUpdates( WikiPage $page,
00393                 ParserOutput $parserOutput = null )
00394         {
00395                 return array(
00396                         new LinksDeletionUpdate( $page ),
00397                 );
00398         }
00399 
00411         public function matchMagicWord( MagicWord $word ) {
00412                 return false;
00413         }
00414 
00428         public function convert( $toModel, $lossy = '' ) {
00429                 if ( $this->getModel() === $toModel ) {
00430                         //nothing to do, shorten out.
00431                         return $this;
00432                 }
00433 
00434                 $lossy = ( $lossy === 'lossy' ); // string flag, convert to boolean for convenience
00435                 $result = false;
00436 
00437                 wfRunHooks( 'ConvertContent', array( $this, $toModel, $lossy, &$result ) );
00438                 return $result;
00439         }
00440 }