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 ) { #FIXME: really need revId? annoying! #FIXME: really $title? or just when parsing, every time?
12 $this->mModelName
= $modelName;
15 public function getModelName() {
16 return $this->mModelName
;
19 public abstract function getTextForSearchIndex( );
21 public abstract function getWikitextForTransclusion( );
23 public abstract function getTextForSummary( $maxlength = 250 );
26 * Returns native represenation of the data. Interpretation depends on the data model used,
27 * as given by getDataModel().
29 * @return mixed the native representation of the content. Could be a string, a nested array
30 * structure, an object, a binary blob... anything, really.
32 public abstract function getNativeData( ); #FIXME: review all calls carefully, caller must be aware of content model!
35 * returns the content's nominal size in bogo-bytes.
37 public abstract function getSize( ); #XXX: do we really need/want this here? we could just use the byte syse of the serialized form...
39 public function isEmpty() {
40 return $this->getSize() == 0;
43 public function equals( Content
$that ) {
44 if ( empty( $that ) ) return false;
45 if ( $that === $this ) return true;
46 if ( $that->getModelName() !== $this->getModelName() ) return false;
48 return $this->getNativeData() == $that->getNativeData();
52 * Returns true if this content is countable as a "real" wiki page, provided
53 * that it's also in a countable location (e.g. a current revision in the main namespace).
55 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
56 * to avoid redundant parsing to find out.
58 public abstract function isCountable( $hasLinks = null ) ;
60 public abstract function getParserOutput( Title
$title = null, $revId = null, ParserOptions
$options = NULL );
62 public function getRedirectChain() { #TODO: document!
66 public function getRedirectTarget() {
70 public function isRedirect() {
71 return $this->getRedirectTarget() != null;
75 * Returns the section with the given id.
77 * The default implementation returns null.
79 * @param String $sectionId the section's id
80 * @return Content|Boolean|null the section, or false if no such section exist, or null if sections are not supported
82 public function getSection( $sectionId ) {
87 * Replaces a section of the content and returns a Content object with the section replaced.
89 * @param $section empty/null/false or a section number (0, 1, 2, T1, T2...), or "new"
90 * @param $with Content: new content of the section
91 * @param $sectionTitle String: new section's subject, only if $section is 'new'
92 * @return string Complete article text, or null if error
94 public function replaceSection( $section, Content
$with, $sectionTitle = '' ) {
98 #TODO: implement specialized ParserOutput for Wikidata model
99 #TODO: provide "combined" ParserOutput for Multipart... somehow.
101 # XXX: isCacheable( ) # can/should we do this here?
103 # TODO: WikiPage::getUndoText( Revision $undo, Revision $undoafter = null )
105 # TODO: EditPage::getPreloadedText( $preload ) // $wgParser->getPreloadText
106 # TODO: tie into EditPage, make it use Content-objects throughout, make edit form aware of content model and format
107 # TODO: tie into WikiPage, make it use Content-objects throughout, especially in doEdit(), doDelete(), updateRevisionOn(), etc
108 # TODO: make model-aware diff view!
109 # TODO: handle ImagePage and CategoryPage
111 # TODO: Title::newFromRedirectRecurse( $this->getRawText() );
113 # TODO: tie into API to provide contentModel for Revisions
114 # TODO: tie into API to provide serialized version and contentFormat for Revisions
115 # TODO: tie into API edit interface
120 * Content object implementation for representing flat text. The
122 abstract class TextContent
extends Content
{
123 public function __construct( $text, $modelName = null ) {
124 parent
::__construct($modelName);
126 $this->mText
= $text;
129 public function getTextForSummary( $maxlength = 250 ) {
132 $text = $this->getNativeData();
134 $truncatedtext = $wgContLang->truncate(
135 preg_replace( "/[\n\r]/", ' ', $text ),
136 max( 0, $maxlength ) );
138 return $truncatedtext;
142 * returns the content's nominal size in bogo-bytes.
144 public function getSize( ) { #FIXME: use! replace strlen in WikiPage.
145 $text = $this->getNativeData( );
146 return strlen( $text );
150 * Returns true if this content is not a redirect, and $wgArticleCountMethod is "any".
152 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
153 * to avoid redundant parsing to find out.
155 public function isCountable( $hasLinks = null ) {
156 global $wgArticleCountMethod;
158 if ( $this->isRedirect( ) ) {
162 if ( $wgArticleCountMethod === 'any' ) {
170 * Returns the text represented by this Content object, as a string.
172 * @return String the raw text
174 public function getNativeData( ) {
175 $text = $this->mText
;
180 * Returns the text represented by this Content object, as a string.
182 * @return String the raw text
184 public function getTextForSearchIndex( ) { #FIXME: use!
185 return $this->getNativeData();
189 * Returns the text represented by this Content object, as a string.
191 * @return String the raw text
193 public function getWikitextForTransclusion( ) { #FIXME: use!
194 return $this->getNativeData();
198 * Returns a generic ParserOutput object, wrapping the HTML returned by getHtml().
200 * @return ParserOutput representing the HTML form of the text
202 public function getParserOutput( Title
$title = null, $revId = null, ParserOptions
$options = null ) {
203 # generic implementation, relying on $this->getHtml()
205 $html = $this->getHtml( $options );
206 $po = new ParserOutput( $html );
208 if ( $this->mTitle
) $po->setTitleText( $this->mTitle
->getText() );
210 #TODO: cache settings, etc?
215 protected abstract function getHtml( );
219 class WikitextContent
extends TextContent
{
220 public function __construct( $text ) {
221 parent
::__construct($text, CONTENT_MODEL_WIKITEXT
);
223 $this->mDefaultParserOptions
= null; #TODO: use per-class static member?!
226 protected function getHtml( ) {
227 throw new MWException( "getHtml() not implemented for wikitext. Use getParserOutput()->getText()." );
230 public function getDefaultParserOptions() {
231 global $wgUser, $wgContLang;
233 if ( !$this->mDefaultParserOptions
) { #TODO: use per-class static member?!
234 $this->mDefaultParserOptions
= ParserOptions
::newFromUserAndLang( $wgUser, $wgContLang );
237 return $this->mDefaultParserOptions
;
241 * Returns a ParserOutput object reesulting from parsing the content's text using $wgParser
243 * @return ParserOutput representing the HTML form of the text
245 public function getParserOutput( Title
$title = null, $revId = null, ParserOptions
$options = null ) {
249 $options = $this->getDefaultParserOptions();
252 $po = $wgParser->parse( $this->mText
, $this->getTitle(), $options, true, true, $this->mRevId
);
258 * Returns the section with the given id.
260 * @param String $sectionId the section's id
261 * @return Content|false|null the section, or false if no such section exist, or null if sections are not supported
263 public function getSection( $section ) {
266 $text = $this->getNativeData();
267 $sect = $wgParser->getSection( $text, $section, false );
269 return new WikitextContent( $sect );
273 * Replaces a section in the wikitext
275 * @param $section empty/null/false or a section number (0, 1, 2, T1, T2...), or "new"
276 * @param $with Content: new content of the section
277 * @param $sectionTitle String: new section's subject, only if $section is 'new'
278 * @return string Complete article text, or null if error
280 public function replaceSection( $section, Content
$with, $sectionTitle = '' ) {
283 wfProfileIn( __METHOD__
);
285 $myModelName = $this->getModelName();
286 $sectionModelName = $with->getModelName();
288 if ( $sectionModelName != $myModelName ) {
289 throw new MWException( "Incompatible content model for section: document uses $myModelName, section uses $sectionModelName." );
292 $oldtext = $this->getNativeData();
293 $text = $with->getNativeData();
295 if ( $section == 'new' ) {
296 # Inserting a new section
297 $subject = $sectionTitle ?
wfMsgForContent( 'newsectionheaderdefaultlevel', $sectionTitle ) . "\n\n" : '';
298 if ( wfRunHooks( 'PlaceNewSection', array( $this, $oldtext, $subject, &$text ) ) ) {
299 $text = strlen( trim( $oldtext ) ) > 0
300 ?
"{$oldtext}\n\n{$subject}{$text}"
301 : "{$subject}{$text}";
304 # Replacing an existing section; roll out the big guns
307 $text = $wgParser->replaceSection( $oldtext, $section, $text );
310 $newContent = new WikitextContent( $text );
312 wfProfileOut( __METHOD__
);
316 public function getRedirectChain() {
317 $text = $this->getNativeData();
318 return Title
::newFromRedirectArray( $text );
321 public function getRedirectTarget() {
322 $text = $this->getNativeData();
323 return Title
::newFromRedirect( $text );
327 * Returns true if this content is not a redirect, and this content's text is countable according to
328 * the criteria defiend by $wgArticleCountMethod.
330 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
331 * to avoid redundant parsing to find out.
333 public function isCountable( $hasLinks = null ) {
334 global $wgArticleCountMethod;
336 if ( $this->isRedirect( ) ) {
340 $text = $this->getNativeData();
342 switch ( $wgArticleCountMethod ) {
346 if ( $text === false ) {
347 $text = $this->getRawText();
349 return strpos( $text, ',' ) !== false;
351 if ( $hasLinks === null ) { # not know, find out
352 $po = $this->getParserOutput();
353 $links = $po->getLinks();
354 $hasLinks = !empty( $links );
361 public function getTextForSummary( $maxlength = 250 ) {
362 $truncatedtext = parent
::getTextForSummary( $maxlength );
364 #clean up unfinished links
365 #XXX: make this optional? wasn't there in autosummary, but required for deletion summary.
366 $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
368 return $truncatedtext;
373 class MessageContent
extends TextContent
{
374 public function __construct( $msg_key, $params = null, $options = null ) {
375 parent
::__construct(null, CONTENT_MODEL_WIKITEXT
);
377 $this->mMessageKey
= $msg_key;
379 $this->mParameters
= $params;
381 if ( !$options ) $options = array();
382 $this->mOptions
= $options;
384 $this->mHtmlOptions
= null;
388 * Returns the message as rendered HTML, using the options supplied to the constructor plus "parse".
390 protected function getHtml( ) {
391 $opt = array_merge( $this->mOptions
, array('parse') );
393 return wfMsgExt( $this->mMessageKey
, $this->mParameters
, $opt );
398 * Returns the message as raw text, using the options supplied to the constructor minus "parse" and "parseinline".
400 public function getNativeData( ) {
401 $opt = array_diff( $this->mOptions
, array('parse', 'parseinline') );
403 return wfMsgExt( $this->mMessageKey
, $this->mParameters
, $opt );
409 class JavaScriptContent
extends TextContent
{
410 public function __construct( $text ) {
411 parent
::__construct($text, CONTENT_MODEL_JAVASCRIPT
);
414 protected function getHtml( ) {
416 $html .= "<pre class=\"mw-code mw-js\" dir=\"ltr\">\n";
417 $html .= htmlspecialchars( $this->getNativeData() );
418 $html .= "\n</pre>\n";
425 class CssContent
extends TextContent
{
426 public function __construct( $text ) {
427 parent
::__construct($text, CONTENT_MODEL_CSS
);
430 protected function getHtml( ) {
432 $html .= "<pre class=\"mw-code mw-css\" dir=\"ltr\">\n";
433 $html .= htmlspecialchars( $this->getNativeData() );
434 $html .= "\n</pre>\n";
440 #FUTURE: special type for redirects?!
441 #FUTURE: MultipartMultipart < WikipageContent (Main + Links + X)
442 #FUTURE: LinksContent < LanguageLinksContent, CategoriesContent
443 #EXAMPLE: CoordinatesContent
444 #EXAMPLE: WikidataContent