fix var name
[lhc/web/wiklou.git] / includes / Content.php
1 <?php
2
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.
6 *
7 * @since 1.WD
8 */
9 abstract class Content {
10
11 /**
12 * Name of the content model this Content object represents.
13 * Use with CONTENT_MODEL_XXX constants
14 *
15 * @var String $model_name
16 */
17 protected $modelName;
18
19 /**
20 * @since WD.1
21 *
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.
24 */
25 public abstract function getTextForSearchIndex( );
26
27 /**
28 * @since WD.1
29 *
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.
32 *
33 * @TODO: allow native handling, bypassing wikitext representation, like for includable special pages.
34 * @TODO: use in parser, etc!
35 */
36 public abstract function getWikitextForTransclusion( );
37
38 /**
39 * Returns a textual representation of the content suitable for use in edit summaries and log messages.
40 *
41 * @since WD.1
42 *
43 * @param int $maxlength maximum length of the summary text
44 * @return String the summary text
45 */
46 public abstract function getTextForSummary( $maxlength = 250 );
47
48 /**
49 * Returns native represenation of the data. Interpretation depends on the data model used,
50 * as given by getDataModel().
51 *
52 * @since WD.1
53 *
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.
56 *
57 * @NOTE: review all calls carefully, caller must be aware of content model!
58 */
59 public abstract function getNativeData( );
60
61 /**
62 * returns the content's nominal size in bogo-bytes.
63 *
64 * @return int
65 */
66 public abstract function getSize( );
67
68 /**
69 * @param $model_name
70 */
71 public function __construct( $modelName = null ) {
72 $this->modelName = $modelName;
73 }
74
75 /**
76 * Returns the name of the content model used by this content objects.
77 * Corresponds to the CONTENT_MODEL_XXX constants.
78 *
79 * @since WD.1
80 *
81 * @return String the model name
82 */
83 public function getModelName() {
84 return $this->modelName;
85 }
86
87 /**
88 * Throws an MWException if $model_name is not the name of the content model
89 * supported by this Content object.
90 *
91 * @param String $modelName the model to check
92 */
93 protected function checkModelName( $modelName ) {
94 if ( $modelName !== $this->modelName ) {
95 throw new MWException( "Bad content model: expected " . $this->modelName . " but got found " . $modelName );
96 }
97 }
98
99 /**
100 * Conveniance method that returns the ContentHandler singleton for handling the content
101 * model this Content object uses.
102 *
103 * Shorthand for ContentHandler::getForContent( $this )
104 *
105 * @since WD.1
106 *
107 * @return ContentHandler
108 */
109 public function getContentHandler() {
110 return ContentHandler::getForContent( $this );
111 }
112
113 /**
114 * Conveniance method that returns the default serialization format for the content model
115 * model this Content object uses.
116 *
117 * Shorthand for $this->getContentHandler()->getDefaultFormat()
118 *
119 * @since WD.1
120 *
121 * @return ContentHandler
122 */
123 public function getDefaultFormat() {
124 return $this->getContentHandler()->getDefaultFormat();
125 }
126
127 /**
128 * Conveniance method that returns the list of serialization formats supported
129 * for the content model model this Content object uses.
130 *
131 * Shorthand for $this->getContentHandler()->getSupportedFormats()
132 *
133 * @since WD.1
134 *
135 * @return array of supported serialization formats
136 */
137 public function getSupportedFormats() {
138 return $this->getContentHandler()->getSupportedFormats();
139 }
140
141 /**
142 * Returns true if $format is a supported serialization format for this Content object,
143 * false if it isn't.
144 *
145 * Note that this will always return true if $format is null, because null stands for the
146 * default serialization.
147 *
148 * Shorthand for $this->getContentHandler()->isSupportedFormat( $format )
149 *
150 * @since WD.1
151 *
152 * @param String $format the format to check
153 * @return bool whether the format is supported
154 */
155 public function isSupportedFormat( $format ) {
156 if ( !$format ) {
157 return true; // this means "use the default"
158 }
159
160 return $this->getContentHandler()->isSupportedFormat( $format );
161 }
162
163 /**
164 * Throws an MWException if $this->isSupportedFormat( $format ) doesn't return true.
165 *
166 * @param $format
167 * @throws MWException
168 */
169 protected function checkFormat( $format ) {
170 if ( !$this->isSupportedFormat( $format ) ) {
171 throw new MWException( "Format $format is not supported for content model " . $this->getModelName() );
172 }
173 }
174
175 /**
176 * Conveniance method for serializing this Content object.
177 *
178 * Shorthand for $this->getContentHandler()->serializeContent( $this, $format )
179 *
180 * @since WD.1
181 *
182 * @param null|String $format the desired serialization format (or null for the default format).
183 * @return String serialized form of this Content object
184 */
185 public function serialize( $format = null ) {
186 return $this->getContentHandler()->serializeContent( $this, $format );
187 }
188
189 /**
190 * Returns true if this Content object represents empty content.
191 *
192 * @since WD.1
193 *
194 * @return bool whether this Content object is empty
195 */
196 public function isEmpty() {
197 return $this->getSize() == 0;
198 }
199
200 /**
201 * Returns true if this Content objects is conceptually equivalent to the given Content object.
202 *
203 * Will returns false if $that is null.
204 * Will return true if $that === $this.
205 * Will return false if $that->getModleName() != $this->getModelName().
206 * Will return false if $that->getNativeData() is not equal to $this->getNativeData(),
207 * where the meaning of "equal" depends on the actual data model.
208 *
209 * Implementations should be careful to make equals() transitive and reflexive:
210 *
211 * * $a->equals( $b ) <=> $b->equals( $b )
212 * * $a->equals( $b ) && $b->equals( $c ) ==> $a->equals( $c )
213 *
214 * @since WD.1
215 *
216 * @param Content $that the Content object to compare to
217 * @return bool true if this Content object is euqual to $that, false otherwise.
218 */
219 public function equals( Content $that = null ) {
220 if ( is_null( $that ) ){
221 return false;
222 }
223
224 if ( $that === $this ) {
225 return true;
226 }
227
228 if ( $that->getModelName() !== $this->getModelName() ) {
229 return false;
230 }
231
232 return $this->getNativeData() === $that->getNativeData();
233 }
234
235 /**
236 * Return a copy of this Content object. The following must be true for the object returned
237 * if $copy = $original->copy()
238 *
239 * * get_class($original) === get_class($copy)
240 * * $original->getModelName() === $copy->getModelName()
241 * * $original->equals( $copy )
242 *
243 * If and only if the Content object is imutable, the copy() method can and should
244 * return $this. That is, $copy === $original may be true, but only for imutable content
245 * objects.
246 *
247 * @since WD.1
248 *
249 * @return Content. A copy of this object
250 */
251 public abstract function copy( );
252
253 /**
254 * Returns true if this content is countable as a "real" wiki page, provided
255 * that it's also in a countable location (e.g. a current revision in the main namespace).
256 *
257 * @since WD.1
258 *
259 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
260 * to avoid redundant parsing to find out.
261 * @return boolean
262 */
263 public abstract function isCountable( $hasLinks = null ) ;
264
265 /**
266 * @param IContextSource $context
267 * @param null $revId
268 * @param null|ParserOptions $options
269 * @param Boolean $generateHtml whether to generate Html (default: true). If false,
270 * the result of calling getText() on the ParserOutput object returned by
271 * this method is undefined.
272 *
273 * @since WD.1
274 *
275 * @return ParserOutput
276 */
277 public abstract function getParserOutput( IContextSource $context, $revId = null, ParserOptions $options = NULL, $generateHtml = true );
278
279 /**
280 * Construct the redirect destination from this content and return an
281 * array of Titles, or null if this content doesn't represent a redirect.
282 * The last element in the array is the final destination after all redirects
283 * have been resolved (up to $wgMaxRedirects times).
284 *
285 * @since WD.1
286 *
287 * @return Array of Titles, with the destination last
288 */
289 public function getRedirectChain() {
290 return null;
291 }
292
293 /**
294 * Construct the redirect destination from this content and return an
295 * array of Titles, or null if this content doesn't represent a redirect.
296 * This will only return the immediate redirect target, useful for
297 * the redirect table and other checks that don't need full recursion.
298 *
299 * @since WD.1
300 *
301 * @return Title: The corresponding Title
302 */
303 public function getRedirectTarget() {
304 return null;
305 }
306
307 /**
308 * Construct the redirect destination from this content and return the
309 * Title, or null if this content doesn't represent a redirect.
310 * This will recurse down $wgMaxRedirects times or until a non-redirect target is hit
311 * in order to provide (hopefully) the Title of the final destination instead of another redirect.
312 *
313 * @since WD.1
314 *
315 * @return Title
316 */
317 public function getUltimateRedirectTarget() {
318 return null;
319 }
320
321 /**
322 * @since WD.1
323 *
324 * @return bool
325 */
326 public function isRedirect() {
327 return $this->getRedirectTarget() !== null;
328 }
329
330 /**
331 * Returns the section with the given id.
332 *
333 * The default implementation returns null.
334 *
335 * @since WD.1
336 *
337 * @param String $sectionId the section's id, given as a numeric string. The id "0" retrieves the section before
338 * the first heading, "1" the text between the first heading (inluded) and the second heading (excluded), etc.
339 * @return Content|Boolean|null the section, or false if no such section exist, or null if sections are not supported
340 */
341 public function getSection( $sectionId ) {
342 return null;
343 }
344
345 /**
346 * Replaces a section of the content and returns a Content object with the section replaced.
347 *
348 * @since WD.1
349 *
350 * @param $section empty/null/false or a section number (0, 1, 2, T1, T2...), or "new"
351 * @param $with Content: new content of the section
352 * @param $sectionTitle String: new section's subject, only if $section is 'new'
353 * @return string Complete article text, or null if error
354 */
355 public function replaceSection( $section, Content $with, $sectionTitle = '' ) {
356 return null;
357 }
358
359 /**
360 * Returns a Content object with pre-save transformations applied (or this object if no transformations apply).
361 *
362 * @since WD.1
363 *
364 * @param Title $title
365 * @param User $user
366 * @param null|ParserOptions $popts
367 * @return Content
368 */
369 public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
370 return $this;
371 }
372
373 /**
374 * Returns a new WikitextContent object with the given section heading prepended, if supported.
375 * The default implementation just returns this Content object unmodified, ignoring the section header.
376 *
377 * @since WD.1
378 *
379 * @param $header String
380 * @return Content
381 */
382 public function addSectionHeader( $header ) {
383 return $this;
384 }
385
386 /**
387 * Returns a Content object with preload transformations applied (or this object if no transformations apply).
388 *
389 * @since WD.1
390 *
391 * @param Title $title
392 * @param null|ParserOptions $popts
393 * @return Content
394 */
395 public function preloadTransform( Title $title, ParserOptions $popts ) {
396 return $this;
397 }
398
399 # TODO: handle ImagePage and CategoryPage
400 # TODO: make sure we cover lucene search / wikisearch.
401 # TODO: make sure ReplaceTemplates still works
402 # FUTURE: nice&sane integration of GeSHi syntax highlighting
403 # [11:59] <vvv> Hooks are ugly; make CodeHighlighter interface and a config to set the class which handles syntax highlighting
404 # [12:00] <vvv> And default it to a DummyHighlighter
405
406 # TODO: make sure we cover the external editor interface (does anyone actually use that?!)
407
408 # TODO: tie into API to provide contentModel for Revisions
409 # TODO: tie into API to provide serialized version and contentFormat for Revisions
410 # TODO: tie into API edit interface
411 # FUTURE: make EditForm plugin for EditPage
412 }
413 # FUTURE: special type for redirects?!
414 # FUTURE: MultipartMultipart < WikipageContent (Main + Links + X)
415 # FUTURE: LinksContent < LanguageLinksContent, CategoriesContent
416
417 /**
418 * Content object implementation for representing flat text.
419 *
420 * TextContent instances are imutable
421 *
422 * @since WD.1
423 */
424 abstract class TextContent extends Content {
425
426 public function __construct( $text, $model_name = null ) {
427 parent::__construct( $model_name );
428
429 $this->mText = $text;
430 }
431
432 public function copy() {
433 return $this; #NOTE: this is ok since TextContent are imutable.
434 }
435
436 public function getTextForSummary( $maxlength = 250 ) {
437 global $wgContLang;
438
439 $text = $this->getNativeData();
440
441 $truncatedtext = $wgContLang->truncate(
442 preg_replace( "/[\n\r]/", ' ', $text ),
443 max( 0, $maxlength ) );
444
445 return $truncatedtext;
446 }
447
448 /**
449 * returns the text's size in bytes.
450 *
451 * @return int the size
452 */
453 public function getSize( ) {
454 $text = $this->getNativeData( );
455 return strlen( $text );
456 }
457
458 /**
459 * Returns true if this content is not a redirect, and $wgArticleCountMethod is "any".
460 *
461 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
462 * to avoid redundant parsing to find out.
463 *
464 * @return bool true if the content is countable
465 */
466 public function isCountable( $hasLinks = null ) {
467 global $wgArticleCountMethod;
468
469 if ( $this->isRedirect( ) ) {
470 return false;
471 }
472
473 if ( $wgArticleCountMethod === 'any' ) {
474 return true;
475 }
476
477 return false;
478 }
479
480 /**
481 * Returns the text represented by this Content object, as a string.
482 *
483 * @return String the raw text
484 */
485 public function getNativeData( ) {
486 $text = $this->mText;
487 return $text;
488 }
489
490 /**
491 * Returns the text represented by this Content object, as a string.
492 *
493 * @return String the raw text
494 */
495 public function getTextForSearchIndex( ) {
496 return $this->getNativeData();
497 }
498
499 /**
500 * Returns the text represented by this Content object, as a string.
501 *
502 * @return String the raw text
503 */
504 public function getWikitextForTransclusion( ) {
505 return $this->getNativeData();
506 }
507
508 /**
509 * Returns a generic ParserOutput object, wrapping the HTML returned by getHtml().
510 *
511 * @return ParserOutput representing the HTML form of the text
512 */
513 public function getParserOutput( IContextSource $context, $revId = null, ParserOptions $options = null, $generateHtml = true ) {
514 # generic implementation, relying on $this->getHtml()
515
516 if ( $generateHtml ) $html = $this->getHtml( $options );
517 else $html = '';
518
519 $po = new ParserOutput( $html );
520
521 return $po;
522 }
523
524 protected abstract function getHtml( );
525
526 }
527
528 /**
529 * @since WD.1
530 */
531 class WikitextContent extends TextContent {
532
533 public function __construct( $text ) {
534 parent::__construct($text, CONTENT_MODEL_WIKITEXT);
535 }
536
537 protected function getHtml( ) {
538 throw new MWException( "getHtml() not implemented for wikitext. Use getParserOutput()->getText()." );
539 }
540
541 /**
542 * Returns a ParserOutput object resulting from parsing the content's text using $wgParser.
543 *
544 * @since WikiData1
545 *
546 * @param IContextSource|null $context
547 * @param null $revId
548 * @param null|ParserOptions $options
549 * @param bool $generateHtml
550 *
551 * @return ParserOutput representing the HTML form of the text
552 */
553 public function getParserOutput( IContextSource $context, $revId = null, ParserOptions $options = null, $generateHtml = true ) {
554 global $wgParser;
555
556 if ( !$options ) {
557 $options = ParserOptions::newFromUserAndLang( $context->getUser(), $context->getLanguage() );
558 }
559
560 $po = $wgParser->parse( $this->mText, $context->getTitle(), $options, true, true, $revId );
561
562 return $po;
563 }
564
565 /**
566 * Returns the section with the given id.
567 *
568 * @param String $sectionId the section's id
569 * @return Content|false|null the section, or false if no such section exist, or null if sections are not supported
570 */
571 public function getSection( $section ) {
572 global $wgParser;
573
574 $text = $this->getNativeData();
575 $sect = $wgParser->getSection( $text, $section, false );
576
577 return new WikitextContent( $sect );
578 }
579
580 /**
581 * Replaces a section in the wikitext
582 *
583 * @param $section empty/null/false or a section number (0, 1, 2, T1, T2...), or "new"
584 * @param $with Content: new content of the section
585 * @param $sectionTitle String: new section's subject, only if $section is 'new'
586 * @return Content Complete article content, or null if error
587 */
588 public function replaceSection( $section, Content $with, $sectionTitle = '' ) {
589 wfProfileIn( __METHOD__ );
590
591 $myModelName = $this->getModelName();
592 $sectionModelName = $with->getModelName();
593
594 if ( $sectionModelName != $myModelName ) {
595 throw new MWException( "Incompatible content model for section: document uses $myModelName, section uses $sectionModelName." );
596 }
597
598 $oldtext = $this->getNativeData();
599 $text = $with->getNativeData();
600
601 if ( $section === '' ) {
602 return $with; #XXX: copy first?
603 } if ( $section == 'new' ) {
604 # Inserting a new section
605 $subject = $sectionTitle ? wfMsgForContent( 'newsectionheaderdefaultlevel', $sectionTitle ) . "\n\n" : '';
606 if ( wfRunHooks( 'PlaceNewSection', array( $this, $oldtext, $subject, &$text ) ) ) {
607 $text = strlen( trim( $oldtext ) ) > 0
608 ? "{$oldtext}\n\n{$subject}{$text}"
609 : "{$subject}{$text}";
610 }
611 } else {
612 # Replacing an existing section; roll out the big guns
613 global $wgParser;
614
615 $text = $wgParser->replaceSection( $oldtext, $section, $text );
616 }
617
618 $newContent = new WikitextContent( $text );
619
620 wfProfileOut( __METHOD__ );
621 return $newContent;
622 }
623
624 /**
625 * Returns a new WikitextContent object with the given section heading prepended.
626 *
627 * @param $header String
628 * @return Content
629 */
630 public function addSectionHeader( $header ) {
631 $text = wfMsgForContent( 'newsectionheaderdefaultlevel', $header ) . "\n\n" . $this->getNativeData();
632
633 return new WikitextContent( $text );
634 }
635
636 /**
637 * Returns a Content object with pre-save transformations applied (or this object if no transformations apply).
638 *
639 * @param Title $title
640 * @param User $user
641 * @param ParserOptions $popts
642 * @return Content
643 */
644 public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
645 global $wgParser, $wgConteLang;
646
647 $text = $this->getNativeData();
648 $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
649
650 return new WikitextContent( $pst );
651 }
652
653 /**
654 * Returns a Content object with preload transformations applied (or this object if no transformations apply).
655 *
656 * @param Title $title
657 * @param ParserOptions $popts
658 * @return Content
659 */
660 public function preloadTransform( Title $title, ParserOptions $popts ) {
661 global $wgParser, $wgConteLang;
662
663 $text = $this->getNativeData();
664 $plt = $wgParser->getPreloadText( $text, $title, $popts );
665
666 return new WikitextContent( $plt );
667 }
668
669 public function getRedirectChain() {
670 $text = $this->getNativeData();
671 return Title::newFromRedirectArray( $text );
672 }
673
674 public function getRedirectTarget() {
675 $text = $this->getNativeData();
676 return Title::newFromRedirect( $text );
677 }
678
679 public function getUltimateRedirectTarget() {
680 $text = $this->getNativeData();
681 return Title::newFromRedirectRecurse( $text );
682 }
683
684 /**
685 * Returns true if this content is not a redirect, and this content's text is countable according to
686 * the criteria defiend by $wgArticleCountMethod.
687 *
688 * @param Bool $hasLinks if it is known whether this content contains links, provide this information here,
689 * to avoid redundant parsing to find out.
690 * @param IContextSource $context context for parsing if necessary
691 *
692 * @return bool true if the content is countable
693 */
694 public function isCountable( $hasLinks = null, IContextSource $context = null ) {
695 global $wgArticleCountMethod, $wgRequest;
696
697 if ( $this->isRedirect( ) ) {
698 return false;
699 }
700
701 $text = $this->getNativeData();
702
703 switch ( $wgArticleCountMethod ) {
704 case 'any':
705 return true;
706 case 'comma':
707 return strpos( $text, ',' ) !== false;
708 case 'link':
709 if ( $hasLinks === null ) { # not known, find out
710 if ( !$context ) { # make dummy context
711 //XXX: caller of this method often knows the title, but not a context...
712 $context = new RequestContext( $wgRequest );
713 }
714
715 $po = $this->getParserOutput( $context, null, null, false );
716 $links = $po->getLinks();
717 $hasLinks = !empty( $links );
718 }
719
720 return $hasLinks;
721 }
722 }
723
724 public function getTextForSummary( $maxlength = 250 ) {
725 $truncatedtext = parent::getTextForSummary( $maxlength );
726
727 #clean up unfinished links
728 #XXX: make this optional? wasn't there in autosummary, but required for deletion summary.
729 $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
730
731 return $truncatedtext;
732 }
733
734 }
735
736 /**
737 * @since WD.1
738 */
739 class MessageContent extends TextContent {
740 public function __construct( $msg_key, $params = null, $options = null ) {
741 parent::__construct(null, CONTENT_MODEL_WIKITEXT); #XXX: messages may be wikitext, html or plain text! and maybe even something else entirely.
742
743 $this->mMessageKey = $msg_key;
744
745 $this->mParameters = $params;
746
747 if ( is_null( $options ) ) {
748 $options = array();
749 }
750 elseif ( is_string( $options ) ) {
751 $options = array( $options );
752 }
753
754 $this->mOptions = $options;
755
756 $this->mHtmlOptions = null;
757 }
758
759 /**
760 * Returns the message as rendered HTML, using the options supplied to the constructor plus "parse".
761 */
762 protected function getHtml( ) {
763 $opt = array_merge( $this->mOptions, array('parse') );
764
765 return wfMsgExt( $this->mMessageKey, $this->mParameters, $opt );
766 }
767
768
769 /**
770 * Returns the message as raw text, using the options supplied to the constructor minus "parse" and "parseinline".
771 */
772 public function getNativeData( ) {
773 $opt = array_diff( $this->mOptions, array('parse', 'parseinline') );
774
775 return wfMsgExt( $this->mMessageKey, $this->mParameters, $opt );
776 }
777
778 }
779
780 /**
781 * @since WD.1
782 */
783 class JavaScriptContent extends TextContent {
784 public function __construct( $text ) {
785 parent::__construct($text, CONTENT_MODEL_JAVASCRIPT);
786 }
787
788 protected function getHtml( ) {
789 $html = "";
790 $html .= "<pre class=\"mw-code mw-js\" dir=\"ltr\">\n";
791 $html .= htmlspecialchars( $this->getNativeData() );
792 $html .= "\n</pre>\n";
793
794 return $html;
795 }
796
797 }
798
799 /**
800 * @since WD.1
801 */
802 class CssContent extends TextContent {
803 public function __construct( $text ) {
804 parent::__construct($text, CONTENT_MODEL_CSS);
805 }
806
807 protected function getHtml( ) {
808 $html = "";
809 $html .= "<pre class=\"mw-code mw-css\" dir=\"ltr\">\n";
810 $html .= htmlspecialchars( $this->getNativeData() );
811 $html .= "\n</pre>\n";
812
813 return $html;
814 }
815 }