8280fc91786b193d61ba8267e6337bf3bdb5c0dd
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 getSearchText( );
21 public abstract function getWikitextForTransclusion( );
24 * Returns native represenation of the data. Interpretation depends on the data model used,
25 * as given by getDataModel().
28 public abstract function getNativeData( );
30 public abstract function getSize( );
32 public abstract function getParserOutput( Title
$title = null, $revId = null, ParserOptions
$options = NULL );
34 public function getRedirectChain() {
39 * Returns the section with the given id.
41 * The default implementation returns null.
43 * @param String $sectionId the section's id
44 * @return Content|Boolean|null the section, or false if no such section exist, or null if sections are not supported
46 public function getSection( $sectionId ) {
51 * Replaces a section of the content.
53 * @param $section empty/null/false or a section number (0, 1, 2, T1, T2...), or "new"
54 * @param $with Content: new content of the section
55 * @param $sectionTitle String: new section's subject, only if $section is 'new'
56 * @return string Complete article text, or null if error
58 public function replaceSection( $section, Content
$with, $sectionTitle = '' ) {
62 #TODO: implement specialized ParserOutput for Wikidata model
63 #TODO: provide "combined" ParserOutput for Multipart... somehow.
65 # TODO: Wikipage::isCountable(Content $a)
67 # TODO: isCacheable( )
70 # TODO: WikiPage::getUndoText( Revision $undo, Revision $undoafter = null )
71 # TODO: WikiPage::getAutosummary( $oldtext, $text, $flags )
73 # TODO: EditPage::getPreloadedText( $preload ) // $wgParser->getPreloadText
74 # TODO: tie into EditPage, make it use Content-objects throughout, make edit form aware of content model and format
75 # TODO: make model-aware diff view!
76 # TODO: handle ImagePage and CategoryPage
78 # TODO: Title::newFromRedirectRecurse( $this->getRawText() );
80 # TODO: tie into API to provide contentModel for Revisions
81 # TODO: tie into API to provide serialized version and contentFormat for Revisions
82 # TODO: tie into API edit interface
87 * Content object implementation for representing flat text. The
89 abstract class TextContent
extends Content
{
90 public function __construct( $text, $modelName = null ) {
91 parent
::__construct($modelName);
97 * Returns the text represented by this Content object, as a string.
99 * @return String the raw text
101 public function getNativeData( ) {
102 $text = $this->mText
;
107 * Returns the text represented by this Content object, as a string.
109 * @return String the raw text
111 public function getSearchText( ) { #FIXME: use!
112 return $this->getNativeData();
116 * Returns the text represented by this Content object, as a string.
118 * @return String the raw text
120 public function getWikitextForTransclusion( ) { #FIXME: use!
121 return $this->getNativeData();
125 * Returns a generic ParserOutput object, wrapping the HTML returned by getHtml().
127 * @return ParserOutput representing the HTML form of the text
129 public function getParserOutput( Title
$title = null, $revId = null, ParserOptions
$options = null ) {
130 # generic implementation, relying on $this->getHtml()
132 $html = $this->getHtml( $options );
133 $po = new ParserOutput( $html );
135 if ( $this->mTitle
) $po->setTitleText( $this->mTitle
->getText() );
137 #TODO: cache settings, etc?
142 protected abstract function getHtml( );
146 class WikitextContent
extends TextContent
{
147 public function __construct( $text ) {
148 parent
::__construct($text, CONTENT_MODEL_WIKITEXT
);
150 $this->mDefaultParserOptions
= null; #TODO: use per-class static member?!
153 protected function getHtml( ) {
154 throw new MWException( "getHtml() not implemented for wikitext. Use getParserOutput()->getText()." );
157 public function getDefaultParserOptions() {
158 global $wgUser, $wgContLang;
160 if ( !$this->mDefaultParserOptions
) { #TODO: use per-class static member?!
161 $this->mDefaultParserOptions
= ParserOptions
::newFromUserAndLang( $wgUser, $wgContLang );
164 return $this->mDefaultParserOptions
;
168 * Returns a ParserOutput object reesulting from parsing the content's text using $wgParser
170 * @return ParserOutput representing the HTML form of the text
172 public function getParserOutput( Title
$title = null, $revId = null, ParserOptions
$options = null ) {
176 $options = $this->getDefaultParserOptions();
179 $po = $wgParser->parse( $this->mText
, $this->getTitle(), $options, true, true, $this->mRevId
);
185 * Returns the section with the given id.
187 * @param String $sectionId the section's id
188 * @return Content|false|null the section, or false if no such section exist, or null if sections are not supported
190 public function getSection( $section ) {
193 $text = $this->getNativeData();
194 $sect = $wgParser->getSection( $text, $section, false );
196 return new WikitextContent( $sect );
200 * Replaces a section in the wikitext
202 * @param $section empty/null/false or a section number (0, 1, 2, T1, T2...), or "new"
203 * @param $with Content: new content of the section
204 * @param $sectionTitle String: new section's subject, only if $section is 'new'
205 * @return string Complete article text, or null if error
207 public function replaceSection( $section, Content
$with, $sectionTitle = '' ) {
210 wfProfileIn( __METHOD__
);
212 $myModelName = $this->getModelName();
213 $sectionModelName = $with->getModelName();
215 if ( $sectionModelName != $myModelName ) {
216 throw new MWException( "Incompatible content model for section: document uses $myModelName, section uses $sectionModelName." );
219 $oldtext = $this->getNativeData();
220 $text = $with->getNativeData();
222 if ( $section == 'new' ) {
223 # Inserting a new section
224 $subject = $sectionTitle ?
wfMsgForContent( 'newsectionheaderdefaultlevel', $sectionTitle ) . "\n\n" : '';
225 if ( wfRunHooks( 'PlaceNewSection', array( $this, $oldtext, $subject, &$text ) ) ) {
226 $text = strlen( trim( $oldtext ) ) > 0
227 ?
"{$oldtext}\n\n{$subject}{$text}"
228 : "{$subject}{$text}";
231 # Replacing an existing section; roll out the big guns
234 $text = $wgParser->replaceSection( $oldtext, $section, $text );
237 $newContent = new WikitextContent( $text );
239 wfProfileOut( __METHOD__
);
243 public function getRedirectChain() {
244 $text = $this->getNativeData();
245 return Title
::newFromRedirectArray( $text );
250 class MessageContent
extends TextContent
{
251 public function __construct( $msg_key, $params = null, $options = null ) {
252 parent
::__construct(null, CONTENT_MODEL_WIKITEXT
);
254 $this->mMessageKey
= $msg_key;
256 $this->mParameters
= $params;
258 if ( !$options ) $options = array();
259 $this->mOptions
= $options;
261 $this->mHtmlOptions
= null;
265 * Returns the message as rendered HTML, using the options supplied to the constructor plus "parse".
267 protected function getHtml( ) {
268 $opt = array_merge( $this->mOptions
, array('parse') );
270 return wfMsgExt( $this->mMessageKey
, $this->mParameters
, $opt );
275 * Returns the message as raw text, using the options supplied to the constructor minus "parse" and "parseinline".
277 public function getNativeData( ) {
278 $opt = array_diff( $this->mOptions
, array('parse', 'parseinline') );
280 return wfMsgExt( $this->mMessageKey
, $this->mParameters
, $opt );
286 class JavaScriptContent
extends TextContent
{
287 public function __construct( $text ) {
288 parent
::__construct($text, CONTENT_MODEL_JAVASCRIPT
);
291 protected function getHtml( ) {
293 $html .= "<pre class=\"mw-code mw-js\" dir=\"ltr\">\n";
294 $html .= htmlspecialchars( $this->getNativeData() );
295 $html .= "\n</pre>\n";
302 class CssContent
extends TextContent
{
303 public function __construct( $text ) {
304 parent
::__construct($text, CONTENT_MODEL_CSS
);
307 protected function getHtml( ) {
309 $html .= "<pre class=\"mw-code mw-css\" dir=\"ltr\">\n";
310 $html .= htmlspecialchars( $this->getNativeData() );
311 $html .= "\n</pre>\n";
317 #FUTURE: special type for redirects?!
318 #FUTURE: MultipartMultipart < WikipageContent (Main + Links + X)
319 #FUTURE: LinksContent < LanguageLinksContent, CategoriesContent
320 #EXAMPLE: CoordinatesContent
321 #EXAMPLE: WikidataContent