poked at adding diff stuff but gave up and just left some todos
[mediawiki.git] / includes / Content.php
blobd094b55cd51b47e79f0f545100bd8dcfabe6aaed
1 <?php
3 /**
4 * A content object represents page content, e.g. the text to show on a page.
5 * Content objects have no knowledge about how they relate to Wiki pages.
7 * @since 1.WD
8 */
9 abstract class Content {
11 /**
12 * Name of the content model this Content object represents.
13 * Use with CONTENT_MODEL_XXX constants
15 * @var String $model_name
17 protected $modelName;
19 /**
20 * @since WD.1
22 * @return String a string representing the content in a way useful for building a full text search index.
23 * If no useful representation exists, this method returns an empty string.
25 public abstract function getTextForSearchIndex( );
27 /**
28 * @since WD.1
30 * @return String the wikitext to include when another page includes this content, or false if the content is not
31 * includable in a wikitext page.
33 * @TODO: allow native handling, bypassing wikitext representation, like for includable special pages.
34 * @TODO: use in parser, etc!
36 public abstract function getWikitextForTransclusion( );
38 /**
39 * Returns a textual representation of the content suitable for use in edit summaries and log messages.
41 * @since WD.1
43 * @param int $maxlength maximum length of the summary text
44 * @return String the summary text
46 public abstract function getTextForSummary( $maxlength = 250 );
48 /**
49 * Returns native represenation of the data. Interpretation depends on the data model used,
50 * as given by getDataModel().
52 * @since WD.1
54 * @return mixed the native representation of the content. Could be a string, a nested array
55 * structure, an object, a binary blob... anything, really.
57 * @NOTE: review all calls carefully, caller must be aware of content model!
59 public abstract function getNativeData( );
61 /**
62 * returns the content's nominal size in bogo-bytes.
64 * @return int
66 public abstract function getSize( );
68 /**
69 * @param $model_name
71 public function __construct( $modelName = null ) {
72 $this->modelName = $modelName;
75 /**
76 * Returns the name of the content model used by this content objects.
77 * Corresponds to the CONTENT_MODEL_XXX constants.
79 * @since WD.1
81 * @return String the model name
83 public function getModelName() {
84 return $this->modelName;
87 /**
88 * Throws an MWException if $model_name is not the name of the content model
89 * supported by this Content object.
91 * @param String $modelName the model to check
93 protected function checkModelName( $modelName ) {
94 if ( $modelName !== $this->modelName ) {
95 throw new MWException( "Bad content model: expected " . $this->modelName . " but got found " . $modelName );
99 /**
100 * Conveniance method that returns the ContentHandler singleton for handling the content
101 * model this Content object uses.
103 * Shorthand for ContentHandler::getForContent( $this )
105 * @since WD.1
107 * @return ContentHandler
109 public function getContentHandler() {
110 return ContentHandler::getForContent( $this );
114 * Conveniance method that returns the default serialization format for the content model
115 * model this Content object uses.
117 * Shorthand for $this->getContentHandler()->getDefaultFormat()
119 * @since WD.1
121 * @return ContentHandler
123 public function getDefaultFormat() {
124 return $this->getContentHandler()->getDefaultFormat();
128 * Conveniance method that returns the list of serialization formats supported
129 * for the content model model this Content object uses.
131 * Shorthand for $this->getContentHandler()->getSupportedFormats()
133 * @since WD.1
135 * @return array of supported serialization formats
137 public function getSupportedFormats() {
138 return $this->getContentHandler()->getSupportedFormats();
142 * Returns true if $format is a supported serialization format for this Content object,
143 * false if it isn't.
145 * Note that this will always return true if $format is null, because null stands for the
146 * default serialization.
148 * Shorthand for $this->getContentHandler()->isSupportedFormat( $format )
150 * @since WD.1
152 * @param String $format the format to check
153 * @return bool whether the format is supported
155 public function isSupportedFormat( $format ) {
156 if ( !$format ) {
157 return true; // this means "use the default"
160 return $this->getContentHandler()->isSupportedFormat( $format );
164 * Throws an MWException if $this->isSupportedFormat( $format ) doesn't return true.
166 * @param $format
167 * @throws MWException
169 protected function checkFormat( $format ) {
170 if ( !$this->isSupportedFormat( $format ) ) {
171 throw new MWException( "Format $format is not supported for content model " . $this->getModelName() );
176 * Conveniance method for serializing this Content object.
178 * Shorthand for $this->getContentHandler()->serializeContent( $this, $format )
180 * @since WD.1
182 * @param null|String $format the desired serialization format (or null for the default format).
183 * @return String serialized form of this Content object
185 public function serialize( $format = null ) {
186 return $this->getContentHandler()->serializeContent( $this, $format );
190 * Returns true if this Content object represents empty content.
192 * @since WD.1
194 * @return bool whether this Content object is empty
196 public function isEmpty() {
197 return $this->getSize() == 0;
201 * Returns if the content is valid.
202 * It needs to be valid before it can be saved.
204 * @since WD.1
206 * @return boolean
208 public function isValid() {
209 // TODO
210 return true;
214 * Diff the content object with what is currently stored in the database.
215 * If it is not currently stored, it will be diffed with an empty object.
217 * @since WD.diff
219 * @return ContentDiff
221 public function diffToDatabase() {
222 // TODO
226 * Returns true if this Content objects is conceptually equivalent to the given Content object.
228 * Will returns false if $that is null.
229 * Will return true if $that === $this.
230 * Will return false if $that->getModleName() != $this->getModelName().
231 * Will return false if $that->getNativeData() is not equal to $this->getNativeData(),
232 * where the meaning of "equal" depends on the actual data model.
234 * Implementations should be careful to make equals() transitive and reflexive:
236 * * $a->equals( $b ) <=> $b->equals( $b )
237 * * $a->equals( $b ) && $b->equals( $c ) ==> $a->equals( $c )
239 * @since WD.1
241 * @param Content $that the Content object to compare to
242 * @return bool true if this Content object is euqual to $that, false otherwise.
244 public function equals( Content $that = null ) {
245 if ( is_null( $that ) ){
246 return false;
249 if ( $that === $this ) {
250 return true;
253 if ( $that->getModelName() !== $this->getModelName() ) {
254 return false;
257 return $this->getNativeData() === $that->getNativeData();
261 * Return a copy of this Content object. The following must be true for the object returned
262 * if $copy = $original->copy()
264 * * get_class($original) === get_class($copy)
265 * * $original->getModelName() === $copy->getModelName()
266 * * $original->equals( $copy )
268 * If and only if the Content object is imutable, the copy() method can and should
269 * return $this. That is, $copy === $original may be true, but only for imutable content
270 * objects.
272 * @since WD.1
274 * @return Content. A copy of this object
276 public abstract function copy( );
279 * Returns true if this content is countable as a "real" wiki page, provided
280 * that it's also in a countable location (e.g. a current revision in the main namespace).
282 * @since WD.1
284 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
285 * to avoid redundant parsing to find out.
286 * @return boolean
288 public abstract function isCountable( $hasLinks = null ) ;
291 * @param IContextSource $context
292 * @param null $revId
293 * @param null|ParserOptions $options
294 * @param Boolean $generateHtml whether to generate Html (default: true). If false,
295 * the result of calling getText() on the ParserOutput object returned by
296 * this method is undefined.
298 * @since WD.1
300 * @return ParserOutput
302 public abstract function getParserOutput( IContextSource $context, $revId = null, ParserOptions $options = NULL, $generateHtml = true );
305 * Construct the redirect destination from this content and return an
306 * array of Titles, or null if this content doesn't represent a redirect.
307 * The last element in the array is the final destination after all redirects
308 * have been resolved (up to $wgMaxRedirects times).
310 * @since WD.1
312 * @return Array of Titles, with the destination last
314 public function getRedirectChain() {
315 return null;
319 * Construct the redirect destination from this content and return an
320 * array of Titles, or null if this content doesn't represent a redirect.
321 * This will only return the immediate redirect target, useful for
322 * the redirect table and other checks that don't need full recursion.
324 * @since WD.1
326 * @return Title: The corresponding Title
328 public function getRedirectTarget() {
329 return null;
333 * Construct the redirect destination from this content and return the
334 * Title, or null if this content doesn't represent a redirect.
335 * This will recurse down $wgMaxRedirects times or until a non-redirect target is hit
336 * in order to provide (hopefully) the Title of the final destination instead of another redirect.
338 * @since WD.1
340 * @return Title
342 public function getUltimateRedirectTarget() {
343 return null;
347 * @since WD.1
349 * @return bool
351 public function isRedirect() {
352 return $this->getRedirectTarget() !== null;
356 * Returns the section with the given id.
358 * The default implementation returns null.
360 * @since WD.1
362 * @param String $sectionId the section's id, given as a numeric string. The id "0" retrieves the section before
363 * the first heading, "1" the text between the first heading (inluded) and the second heading (excluded), etc.
364 * @return Content|Boolean|null the section, or false if no such section exist, or null if sections are not supported
366 public function getSection( $sectionId ) {
367 return null;
371 * Replaces a section of the content and returns a Content object with the section replaced.
373 * @since WD.1
375 * @param $section empty/null/false or a section number (0, 1, 2, T1, T2...), or "new"
376 * @param $with Content: new content of the section
377 * @param $sectionTitle String: new section's subject, only if $section is 'new'
378 * @return string Complete article text, or null if error
380 public function replaceSection( $section, Content $with, $sectionTitle = '' ) {
381 return null;
385 * Returns a Content object with pre-save transformations applied (or this object if no transformations apply).
387 * @since WD.1
389 * @param Title $title
390 * @param User $user
391 * @param null|ParserOptions $popts
392 * @return Content
394 public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
395 return $this;
399 * Returns a new WikitextContent object with the given section heading prepended, if supported.
400 * The default implementation just returns this Content object unmodified, ignoring the section header.
402 * @since WD.1
404 * @param $header String
405 * @return Content
407 public function addSectionHeader( $header ) {
408 return $this;
412 * Returns a Content object with preload transformations applied (or this object if no transformations apply).
414 * @since WD.1
416 * @param Title $title
417 * @param null|ParserOptions $popts
418 * @return Content
420 public function preloadTransform( Title $title, ParserOptions $popts ) {
421 return $this;
424 # TODO: handle ImagePage and CategoryPage
425 # TODO: make sure we cover lucene search / wikisearch.
426 # TODO: make sure ReplaceTemplates still works
427 # FUTURE: nice&sane integration of GeSHi syntax highlighting
428 # [11:59] <vvv> Hooks are ugly; make CodeHighlighter interface and a config to set the class which handles syntax highlighting
429 # [12:00] <vvv> And default it to a DummyHighlighter
431 # TODO: make sure we cover the external editor interface (does anyone actually use that?!)
433 # TODO: tie into API to provide contentModel for Revisions
434 # TODO: tie into API to provide serialized version and contentFormat for Revisions
435 # TODO: tie into API edit interface
436 # FUTURE: make EditForm plugin for EditPage
438 # FUTURE: special type for redirects?!
439 # FUTURE: MultipartMultipart < WikipageContent (Main + Links + X)
440 # FUTURE: LinksContent < LanguageLinksContent, CategoriesContent
443 * Content object implementation for representing flat text.
445 * TextContent instances are imutable
447 * @since WD.1
449 abstract class TextContent extends Content {
451 public function __construct( $text, $model_name = null ) {
452 parent::__construct( $model_name );
454 $this->mText = $text;
457 public function copy() {
458 return $this; #NOTE: this is ok since TextContent are imutable.
461 public function getTextForSummary( $maxlength = 250 ) {
462 global $wgContLang;
464 $text = $this->getNativeData();
466 $truncatedtext = $wgContLang->truncate(
467 preg_replace( "/[\n\r]/", ' ', $text ),
468 max( 0, $maxlength ) );
470 return $truncatedtext;
474 * returns the text's size in bytes.
476 * @return int the size
478 public function getSize( ) {
479 $text = $this->getNativeData( );
480 return strlen( $text );
484 * Returns true if this content is not a redirect, and $wgArticleCountMethod is "any".
486 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
487 * to avoid redundant parsing to find out.
489 * @return bool true if the content is countable
491 public function isCountable( $hasLinks = null ) {
492 global $wgArticleCountMethod;
494 if ( $this->isRedirect( ) ) {
495 return false;
498 if ( $wgArticleCountMethod === 'any' ) {
499 return true;
502 return false;
506 * Returns the text represented by this Content object, as a string.
508 * @return String the raw text
510 public function getNativeData( ) {
511 $text = $this->mText;
512 return $text;
516 * Returns the text represented by this Content object, as a string.
518 * @return String the raw text
520 public function getTextForSearchIndex( ) {
521 return $this->getNativeData();
525 * Returns the text represented by this Content object, as a string.
527 * @return String the raw text
529 public function getWikitextForTransclusion( ) {
530 return $this->getNativeData();
534 * Returns a generic ParserOutput object, wrapping the HTML returned by getHtml().
536 * @return ParserOutput representing the HTML form of the text
538 public function getParserOutput( IContextSource $context, $revId = null, ParserOptions $options = null, $generateHtml = true ) {
539 # generic implementation, relying on $this->getHtml()
541 if ( $generateHtml ) $html = $this->getHtml( $options );
542 else $html = '';
544 $po = new ParserOutput( $html );
546 return $po;
549 protected abstract function getHtml( );
554 * @since WD.1
556 class WikitextContent extends TextContent {
558 public function __construct( $text ) {
559 parent::__construct($text, CONTENT_MODEL_WIKITEXT);
562 protected function getHtml( ) {
563 throw new MWException( "getHtml() not implemented for wikitext. Use getParserOutput()->getText()." );
567 * Returns a ParserOutput object resulting from parsing the content's text using $wgParser.
569 * @since WikiData1
571 * @param IContextSource|null $context
572 * @param null $revId
573 * @param null|ParserOptions $options
574 * @param bool $generateHtml
576 * @return ParserOutput representing the HTML form of the text
578 public function getParserOutput( IContextSource $context, $revId = null, ParserOptions $options = null, $generateHtml = true ) {
579 global $wgParser;
581 if ( !$options ) {
582 $options = ParserOptions::newFromUserAndLang( $context->getUser(), $context->getLanguage() );
585 $po = $wgParser->parse( $this->mText, $context->getTitle(), $options, true, true, $revId );
587 return $po;
591 * Returns the section with the given id.
593 * @param String $sectionId the section's id
594 * @return Content|false|null the section, or false if no such section exist, or null if sections are not supported
596 public function getSection( $section ) {
597 global $wgParser;
599 $text = $this->getNativeData();
600 $sect = $wgParser->getSection( $text, $section, false );
602 return new WikitextContent( $sect );
606 * Replaces a section in the wikitext
608 * @param $section empty/null/false or a section number (0, 1, 2, T1, T2...), or "new"
609 * @param $with Content: new content of the section
610 * @param $sectionTitle String: new section's subject, only if $section is 'new'
611 * @return Content Complete article content, or null if error
613 public function replaceSection( $section, Content $with, $sectionTitle = '' ) {
614 wfProfileIn( __METHOD__ );
616 $myModelName = $this->getModelName();
617 $sectionModelName = $with->getModelName();
619 if ( $sectionModelName != $myModelName ) {
620 throw new MWException( "Incompatible content model for section: document uses $myModelName, section uses $sectionModelName." );
623 $oldtext = $this->getNativeData();
624 $text = $with->getNativeData();
626 if ( $section === '' ) {
627 return $with; #XXX: copy first?
628 } if ( $section == 'new' ) {
629 # Inserting a new section
630 $subject = $sectionTitle ? wfMsgForContent( 'newsectionheaderdefaultlevel', $sectionTitle ) . "\n\n" : '';
631 if ( wfRunHooks( 'PlaceNewSection', array( $this, $oldtext, $subject, &$text ) ) ) {
632 $text = strlen( trim( $oldtext ) ) > 0
633 ? "{$oldtext}\n\n{$subject}{$text}"
634 : "{$subject}{$text}";
636 } else {
637 # Replacing an existing section; roll out the big guns
638 global $wgParser;
640 $text = $wgParser->replaceSection( $oldtext, $section, $text );
643 $newContent = new WikitextContent( $text );
645 wfProfileOut( __METHOD__ );
646 return $newContent;
650 * Returns a new WikitextContent object with the given section heading prepended.
652 * @param $header String
653 * @return Content
655 public function addSectionHeader( $header ) {
656 $text = wfMsgForContent( 'newsectionheaderdefaultlevel', $header ) . "\n\n" . $this->getNativeData();
658 return new WikitextContent( $text );
662 * Returns a Content object with pre-save transformations applied (or this object if no transformations apply).
664 * @param Title $title
665 * @param User $user
666 * @param ParserOptions $popts
667 * @return Content
669 public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
670 global $wgParser, $wgConteLang;
672 $text = $this->getNativeData();
673 $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
675 return new WikitextContent( $pst );
679 * Returns a Content object with preload transformations applied (or this object if no transformations apply).
681 * @param Title $title
682 * @param ParserOptions $popts
683 * @return Content
685 public function preloadTransform( Title $title, ParserOptions $popts ) {
686 global $wgParser, $wgConteLang;
688 $text = $this->getNativeData();
689 $plt = $wgParser->getPreloadText( $text, $title, $popts );
691 return new WikitextContent( $plt );
694 public function getRedirectChain() {
695 $text = $this->getNativeData();
696 return Title::newFromRedirectArray( $text );
699 public function getRedirectTarget() {
700 $text = $this->getNativeData();
701 return Title::newFromRedirect( $text );
704 public function getUltimateRedirectTarget() {
705 $text = $this->getNativeData();
706 return Title::newFromRedirectRecurse( $text );
710 * Returns true if this content is not a redirect, and this content's text is countable according to
711 * the criteria defiend by $wgArticleCountMethod.
713 * @param Bool $hasLinks if it is known whether this content contains links, provide this information here,
714 * to avoid redundant parsing to find out.
715 * @param IContextSource $context context for parsing if necessary
717 * @return bool true if the content is countable
719 public function isCountable( $hasLinks = null, IContextSource $context = null ) {
720 global $wgArticleCountMethod, $wgRequest;
722 if ( $this->isRedirect( ) ) {
723 return false;
726 $text = $this->getNativeData();
728 switch ( $wgArticleCountMethod ) {
729 case 'any':
730 return true;
731 case 'comma':
732 return strpos( $text, ',' ) !== false;
733 case 'link':
734 if ( $hasLinks === null ) { # not known, find out
735 if ( !$context ) { # make dummy context
736 //XXX: caller of this method often knows the title, but not a context...
737 $context = new RequestContext( $wgRequest );
740 $po = $this->getParserOutput( $context, null, null, false );
741 $links = $po->getLinks();
742 $hasLinks = !empty( $links );
745 return $hasLinks;
749 public function getTextForSummary( $maxlength = 250 ) {
750 $truncatedtext = parent::getTextForSummary( $maxlength );
752 #clean up unfinished links
753 #XXX: make this optional? wasn't there in autosummary, but required for deletion summary.
754 $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
756 return $truncatedtext;
762 * @since WD.1
764 class MessageContent extends TextContent {
765 public function __construct( $msg_key, $params = null, $options = null ) {
766 parent::__construct(null, CONTENT_MODEL_WIKITEXT); #XXX: messages may be wikitext, html or plain text! and maybe even something else entirely.
768 $this->mMessageKey = $msg_key;
770 $this->mParameters = $params;
772 if ( is_null( $options ) ) {
773 $options = array();
775 elseif ( is_string( $options ) ) {
776 $options = array( $options );
779 $this->mOptions = $options;
781 $this->mHtmlOptions = null;
785 * Returns the message as rendered HTML, using the options supplied to the constructor plus "parse".
787 protected function getHtml( ) {
788 $opt = array_merge( $this->mOptions, array('parse') );
790 return wfMsgExt( $this->mMessageKey, $this->mParameters, $opt );
795 * Returns the message as raw text, using the options supplied to the constructor minus "parse" and "parseinline".
797 public function getNativeData( ) {
798 $opt = array_diff( $this->mOptions, array('parse', 'parseinline') );
800 return wfMsgExt( $this->mMessageKey, $this->mParameters, $opt );
806 * @since WD.1
808 class JavaScriptContent extends TextContent {
809 public function __construct( $text ) {
810 parent::__construct($text, CONTENT_MODEL_JAVASCRIPT);
813 protected function getHtml( ) {
814 $html = "";
815 $html .= "<pre class=\"mw-code mw-js\" dir=\"ltr\">\n";
816 $html .= htmlspecialchars( $this->getNativeData() );
817 $html .= "\n</pre>\n";
819 return $html;
825 * @since WD.1
827 class CssContent extends TextContent {
828 public function __construct( $text ) {
829 parent::__construct($text, CONTENT_MODEL_CSS);
832 protected function getHtml( ) {
833 $html = "";
834 $html .= "<pre class=\"mw-code mw-css\" dir=\"ltr\">\n";
835 $html .= htmlspecialchars( $this->getNativeData() );
836 $html .= "\n</pre>\n";
838 return $html;