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.
6 * Content objects are imutable.
9 abstract class Content
{
11 public function __construct( $modelName = null ) {
12 $this->mModelName
= $modelName;
15 public function getModelName() {
16 return $this->mModelName
;
19 protected function checkModelName( $modelName ) {
20 if ( $modelName !== $this->mModelName
) {
21 throw new MWException( "Bad content model: expected " . $this->mModelName
. " but got found " . $modelName );
25 public function getContentHandler() {
26 return ContentHandler
::getForContent( $this );
29 public function getDefaultFormat() {
30 return $this->getContentHandler()->getDefaultFormat();
33 public function getSupportedFormats() {
34 return $this->getContentHandler()->getSupportedFormats();
37 public function isSupportedFormat( $format ) {
38 if ( !$format ) return true; # this means "use the default"
40 return $this->getContentHandler()->isSupportedFormat( $format );
43 protected function checkFormat( $format ) {
44 if ( !$this->isSupportedFormat( $format ) ) {
45 throw new MWException( "Format $format is not supported for content model " . $this->getModelName() );
49 public function serialize( $format = null ) {
50 return $this->getContentHandler()->serialize( $this, $format );
54 * @return String a string representing the content in a way useful for building a full text search index.
55 * If no useful representation exists, this method returns an empty string.
57 public abstract function getTextForSearchIndex( );
60 * @return String the wikitext to include when another page includes this content, or false if the content is not
61 * includable in a wikitext page.
63 #TODO: allow native handling, bypassing wikitext representation, like for includable special pages.
64 public abstract function getWikitextForTransclusion( ); #FIXME: use in parser, etc!
67 * Returns a textual representation of the content suitable for use in edit summaries and log messages.
69 * @param int $maxlength maximum length of the summary text
70 * @return String the summary text
72 public abstract function getTextForSummary( $maxlength = 250 );
75 * Returns native represenation of the data. Interpretation depends on the data model used,
76 * as given by getDataModel().
78 * @return mixed the native representation of the content. Could be a string, a nested array
79 * structure, an object, a binary blob... anything, really.
81 public abstract function getNativeData( ); #FIXME: review all calls carefully, caller must be aware of content model!
84 * returns the content's nominal size in bogo-bytes.
88 public abstract function getSize( );
90 public function isEmpty() {
91 return $this->getSize() == 0;
94 public function equals( Content
$that ) {
95 if ( empty( $that ) ) return false;
96 if ( $that === $this ) return true;
97 if ( $that->getModelName() !== $this->getModelName() ) return false;
99 return $this->getNativeData() == $that->getNativeData();
103 * Returns true if this content is countable as a "real" wiki page, provided
104 * that it's also in a countable location (e.g. a current revision in the main namespace).
106 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
107 * to avoid redundant parsing to find out.
109 public abstract function isCountable( $hasLinks = null ) ;
112 * @param null|Title $title
114 * @param null|ParserOptions $options
115 * @return ParserOutput
117 public abstract function getParserOutput( Title
$title = null, $revId = null, ParserOptions
$options = NULL );
120 * Construct the redirect destination from this content and return an
121 * array of Titles, or null if this content doesn't represent a redirect.
122 * The last element in the array is the final destination after all redirects
123 * have been resolved (up to $wgMaxRedirects times).
125 * @return Array of Titles, with the destination last
127 public function getRedirectChain() {
132 * Construct the redirect destination from this content and return an
133 * array of Titles, or null if this content doesn't represent a redirect.
134 * This will only return the immediate redirect target, useful for
135 * the redirect table and other checks that don't need full recursion.
137 * @return Title: The corresponding Title
139 public function getRedirectTarget() {
144 * Construct the redirect destination from this content and return the
145 * Title, or null if this content doesn't represent a redirect.
146 * This will recurse down $wgMaxRedirects times or until a non-redirect target is hit
147 * in order to provide (hopefully) the Title of the final destination instead of another redirect.
151 public function getUltimateRedirectTarget() {
155 public function isRedirect() {
156 return $this->getRedirectTarget() != null;
160 * Returns the section with the given id.
162 * The default implementation returns null.
164 * @param String $sectionId the section's id
165 * @return Content|Boolean|null the section, or false if no such section exist, or null if sections are not supported
167 public function getSection( $sectionId ) {
172 * Replaces a section of the content and returns a Content object with the section replaced.
174 * @param $section empty/null/false or a section number (0, 1, 2, T1, T2...), or "new"
175 * @param $with Content: new content of the section
176 * @param $sectionTitle String: new section's subject, only if $section is 'new'
177 * @return string Complete article text, or null if error
179 public function replaceSection( $section, Content
$with, $sectionTitle = '' ) {
184 * Returns a Content object with pre-save transformations applied (or this object if no transformations apply).
186 * @param Title $title
188 * @param null|ParserOptions $popts
191 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts = null ) {
196 * Returns a new WikitextContent object with the given section heading prepended, if supported.
197 * The default implementation just returns this Content object unmodified, ignoring the section header.
199 * @param $header String
202 public function addSectionHeader( $header ) {
207 * Returns a Content object with preload transformations applied (or this object if no transformations apply).
209 * @param Title $title
210 * @param null|ParserOptions $popts
213 public function preloadTransform( Title
$title, ParserOptions
$popts = null ) {
217 # TODO: minimize special cases for CSS/JS; how to handle extra message for JS/CSS previews??
218 # TODO: handle ImagePage and CategoryPage
219 # TODO: hook into dump generation to serialize and record model and format!
221 # TODO: make sure we cover lucene search / wikisearch.
222 # TODO: make sure ReplaceTemplates still works
223 # TODO: nice&sane integration of GeSHi syntax highlighting
224 # [11:59] <vvv> Hooks are ugly; make CodeHighlighter interface and a config to set the class which handles syntax highlighting
225 # [12:00] <vvv> And default it to a DummyHighlighter
227 # TODO: make sure we cover the external editor interface (does anyone actually use that?!)
229 # TODO: tie into API to provide contentModel for Revisions
230 # TODO: tie into API to provide serialized version and contentFormat for Revisions
231 # TODO: tie into API edit interface
232 # TODO: make EditForm plugin for EditPage
234 # XXX: isCacheable( ) # can/should we do this here?
238 * Content object implementation for representing flat text. The
240 abstract class TextContent
extends Content
{
241 public function __construct( $text, $modelName = null ) {
242 parent
::__construct($modelName);
244 $this->mText
= $text;
247 public function getTextForSummary( $maxlength = 250 ) {
250 $text = $this->getNativeData();
252 $truncatedtext = $wgContLang->truncate(
253 preg_replace( "/[\n\r]/", ' ', $text ),
254 max( 0, $maxlength ) );
256 return $truncatedtext;
260 * returns the content's nominal size in bogo-bytes.
262 public function getSize( ) { #FIXME: use! replace strlen in WikiPage.
263 $text = $this->getNativeData( );
264 return strlen( $text );
268 * Returns true if this content is not a redirect, and $wgArticleCountMethod is "any".
270 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
271 * to avoid redundant parsing to find out.
273 public function isCountable( $hasLinks = null ) {
274 global $wgArticleCountMethod;
276 if ( $this->isRedirect( ) ) {
280 if ( $wgArticleCountMethod === 'any' ) {
288 * Returns the text represented by this Content object, as a string.
290 * @return String the raw text
292 public function getNativeData( ) {
293 $text = $this->mText
;
298 * Returns the text represented by this Content object, as a string.
300 * @return String the raw text
302 public function getTextForSearchIndex( ) { #FIXME: use!
303 return $this->getNativeData();
307 * Returns the text represented by this Content object, as a string.
309 * @return String the raw text
311 public function getWikitextForTransclusion( ) { #FIXME: use!
312 return $this->getNativeData();
316 * Returns a generic ParserOutput object, wrapping the HTML returned by getHtml().
318 * @return ParserOutput representing the HTML form of the text
320 public function getParserOutput( Title
$title = null, $revId = null, ParserOptions
$options = null ) {
321 # generic implementation, relying on $this->getHtml()
323 $html = $this->getHtml( $options );
324 $po = new ParserOutput( $html );
329 protected abstract function getHtml( );
333 class WikitextContent
extends TextContent
{
334 public function __construct( $text ) {
335 parent
::__construct($text, CONTENT_MODEL_WIKITEXT
);
337 $this->mDefaultParserOptions
= null; #TODO: use per-class static member?!
340 protected function getHtml( ) {
341 throw new MWException( "getHtml() not implemented for wikitext. Use getParserOutput()->getText()." );
344 public function getDefaultParserOptions() {
345 global $wgUser, $wgContLang;
347 if ( !$this->mDefaultParserOptions
) { #TODO: use per-class static member?!
348 $this->mDefaultParserOptions
= ParserOptions
::newFromUserAndLang( $wgUser, $wgContLang );
351 return $this->mDefaultParserOptions
;
355 * Returns a ParserOutput object reesulting from parsing the content's text using $wgParser
357 * @return ParserOutput representing the HTML form of the text
359 public function getParserOutput( Title
$title = null, $revId = null, ParserOptions
$options = null ) {
363 $options = $this->getDefaultParserOptions();
366 $po = $wgParser->parse( $this->mText
, $title, $options, true, true, $revId );
372 * Returns the section with the given id.
374 * @param String $sectionId the section's id
375 * @return Content|false|null the section, or false if no such section exist, or null if sections are not supported
377 public function getSection( $section ) {
380 $text = $this->getNativeData();
381 $sect = $wgParser->getSection( $text, $section, false );
383 return new WikitextContent( $sect );
387 * Replaces a section in the wikitext
389 * @param $section empty/null/false or a section number (0, 1, 2, T1, T2...), or "new"
390 * @param $with Content: new content of the section
391 * @param $sectionTitle String: new section's subject, only if $section is 'new'
392 * @return string Complete article text, or null if error
394 public function replaceSection( $section, Content
$with, $sectionTitle = '' ) {
397 wfProfileIn( __METHOD__
);
399 $myModelName = $this->getModelName();
400 $sectionModelName = $with->getModelName();
402 if ( $sectionModelName != $myModelName ) {
403 throw new MWException( "Incompatible content model for section: document uses $myModelName, section uses $sectionModelName." );
406 $oldtext = $this->getNativeData();
407 $text = $with->getNativeData();
409 if ( $section == 'new' ) {
410 # Inserting a new section
411 $subject = $sectionTitle ?
wfMsgForContent( 'newsectionheaderdefaultlevel', $sectionTitle ) . "\n\n" : '';
412 if ( wfRunHooks( 'PlaceNewSection', array( $this, $oldtext, $subject, &$text ) ) ) {
413 $text = strlen( trim( $oldtext ) ) > 0
414 ?
"{$oldtext}\n\n{$subject}{$text}"
415 : "{$subject}{$text}";
418 # Replacing an existing section; roll out the big guns
421 $text = $wgParser->replaceSection( $oldtext, $section, $text );
424 $newContent = new WikitextContent( $text );
426 wfProfileOut( __METHOD__
);
431 * Returns a new WikitextContent object with the given section heading prepended.
433 * @param $header String
436 public function addSectionHeader( $header ) {
437 $text = wfMsgForContent( 'newsectionheaderdefaultlevel', $this->sectiontitle
) . "\n\n" . $this->getNativeData();
439 return new WikitextContent( $text );
443 * Returns a Content object with pre-save transformations applied (or this object if no transformations apply).
445 * @param Title $title
447 * @param null|ParserOptions $popts
450 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts = null ) {
453 if ( $popts == null ) $popts = $this->getDefaultParserOptions();
455 $text = $this->getNativeData();
456 $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
458 return new WikitextContent( $pst );
462 * Returns a Content object with preload transformations applied (or this object if no transformations apply).
464 * @param Title $title
465 * @param null|ParserOptions $popts
468 public function preloadTransform( Title
$title, ParserOptions
$popts = null ) {
471 if ( $popts == null ) $popts = $this->getDefaultParserOptions();
473 $text = $this->getNativeData();
474 $plt = $wgParser->getPreloadText( $text, $title, $popts );
476 return new WikitextContent( $plt );
479 public function getRedirectChain() {
480 $text = $this->getNativeData();
481 return Title
::newFromRedirectArray( $text );
484 public function getRedirectTarget() {
485 $text = $this->getNativeData();
486 return Title
::newFromRedirect( $text );
489 public function getUltimateRedirectTarget() {
490 $text = $this->getNativeData();
491 return Title
::newFromRedirectRecurse( $text );
495 * Returns true if this content is not a redirect, and this content's text is countable according to
496 * the criteria defiend by $wgArticleCountMethod.
498 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
499 * to avoid redundant parsing to find out.
501 public function isCountable( $hasLinks = null ) {
502 global $wgArticleCountMethod;
504 if ( $this->isRedirect( ) ) {
508 $text = $this->getNativeData();
510 switch ( $wgArticleCountMethod ) {
514 if ( $text === false ) {
515 $text = $this->getRawText();
517 return strpos( $text, ',' ) !== false;
519 if ( $hasLinks === null ) { # not know, find out
520 $po = $this->getParserOutput();
521 $links = $po->getLinks();
522 $hasLinks = !empty( $links );
529 public function getTextForSummary( $maxlength = 250 ) {
530 $truncatedtext = parent
::getTextForSummary( $maxlength );
532 #clean up unfinished links
533 #XXX: make this optional? wasn't there in autosummary, but required for deletion summary.
534 $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
536 return $truncatedtext;
541 class MessageContent
extends TextContent
{
542 public function __construct( $msg_key, $params = null, $options = null ) {
543 parent
::__construct(null, CONTENT_MODEL_WIKITEXT
); #XXX: messages may be wikitext, html or plain text! and maybe even something else entirely.
545 $this->mMessageKey
= $msg_key;
547 $this->mParameters
= $params;
549 if ( !$options ) $options = array();
550 $this->mOptions
= $options;
552 $this->mHtmlOptions
= null;
556 * Returns the message as rendered HTML, using the options supplied to the constructor plus "parse".
558 protected function getHtml( ) {
559 $opt = array_merge( $this->mOptions
, array('parse') );
561 return wfMsgExt( $this->mMessageKey
, $this->mParameters
, $opt );
566 * Returns the message as raw text, using the options supplied to the constructor minus "parse" and "parseinline".
568 public function getNativeData( ) {
569 $opt = array_diff( $this->mOptions
, array('parse', 'parseinline') );
571 return wfMsgExt( $this->mMessageKey
, $this->mParameters
, $opt );
577 class JavaScriptContent
extends TextContent
{
578 public function __construct( $text ) {
579 parent
::__construct($text, CONTENT_MODEL_JAVASCRIPT
);
582 protected function getHtml( ) {
584 $html .= "<pre class=\"mw-code mw-js\" dir=\"ltr\">\n";
585 $html .= htmlspecialchars( $this->getNativeData() );
586 $html .= "\n</pre>\n";
593 class CssContent
extends TextContent
{
594 public function __construct( $text ) {
595 parent
::__construct($text, CONTENT_MODEL_CSS
);
598 protected function getHtml( ) {
600 $html .= "<pre class=\"mw-code mw-css\" dir=\"ltr\">\n";
601 $html .= htmlspecialchars( $this->getNativeData() );
602 $html .= "\n</pre>\n";
608 #FUTURE: special type for redirects?!
609 #FUTURE: MultipartMultipart < WikipageContent (Main + Links + X)
610 #FUTURE: LinksContent < LanguageLinksContent, CategoriesContent
611 #EXAMPLE: CoordinatesContent
612 #EXAMPLE: WikidataContent