poked at adding diff stuff but gave up and just left some todos
[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 if the content is valid.
202 * It needs to be valid before it can be saved.
203 *
204 * @since WD.1
205 *
206 * @return boolean
207 */
208 public function isValid() {
209 // TODO
210 return true;
211 }
212
213 /**
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.
216 *
217 * @since WD.diff
218 *
219 * @return ContentDiff
220 */
221 public function diffToDatabase() {
222 // TODO
223 }
224
225 /**
226 * Returns true if this Content objects is conceptually equivalent to the given Content object.
227 *
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.
233 *
234 * Implementations should be careful to make equals() transitive and reflexive:
235 *
236 * * $a->equals( $b ) <=> $b->equals( $b )
237 * * $a->equals( $b ) && $b->equals( $c ) ==> $a->equals( $c )
238 *
239 * @since WD.1
240 *
241 * @param Content $that the Content object to compare to
242 * @return bool true if this Content object is euqual to $that, false otherwise.
243 */
244 public function equals( Content $that = null ) {
245 if ( is_null( $that ) ){
246 return false;
247 }
248
249 if ( $that === $this ) {
250 return true;
251 }
252
253 if ( $that->getModelName() !== $this->getModelName() ) {
254 return false;
255 }
256
257 return $this->getNativeData() === $that->getNativeData();
258 }
259
260 /**
261 * Return a copy of this Content object. The following must be true for the object returned
262 * if $copy = $original->copy()
263 *
264 * * get_class($original) === get_class($copy)
265 * * $original->getModelName() === $copy->getModelName()
266 * * $original->equals( $copy )
267 *
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.
271 *
272 * @since WD.1
273 *
274 * @return Content. A copy of this object
275 */
276 public abstract function copy( );
277
278 /**
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).
281 *
282 * @since WD.1
283 *
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
287 */
288 public abstract function isCountable( $hasLinks = null ) ;
289
290 /**
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.
297 *
298 * @since WD.1
299 *
300 * @return ParserOutput
301 */
302 public abstract function getParserOutput( IContextSource $context, $revId = null, ParserOptions $options = NULL, $generateHtml = true );
303
304 /**
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).
309 *
310 * @since WD.1
311 *
312 * @return Array of Titles, with the destination last
313 */
314 public function getRedirectChain() {
315 return null;
316 }
317
318 /**
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.
323 *
324 * @since WD.1
325 *
326 * @return Title: The corresponding Title
327 */
328 public function getRedirectTarget() {
329 return null;
330 }
331
332 /**
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.
337 *
338 * @since WD.1
339 *
340 * @return Title
341 */
342 public function getUltimateRedirectTarget() {
343 return null;
344 }
345
346 /**
347 * @since WD.1
348 *
349 * @return bool
350 */
351 public function isRedirect() {
352 return $this->getRedirectTarget() !== null;
353 }
354
355 /**
356 * Returns the section with the given id.
357 *
358 * The default implementation returns null.
359 *
360 * @since WD.1
361 *
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
365 */
366 public function getSection( $sectionId ) {
367 return null;
368 }
369
370 /**
371 * Replaces a section of the content and returns a Content object with the section replaced.
372 *
373 * @since WD.1
374 *
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
379 */
380 public function replaceSection( $section, Content $with, $sectionTitle = '' ) {
381 return null;
382 }
383
384 /**
385 * Returns a Content object with pre-save transformations applied (or this object if no transformations apply).
386 *
387 * @since WD.1
388 *
389 * @param Title $title
390 * @param User $user
391 * @param null|ParserOptions $popts
392 * @return Content
393 */
394 public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
395 return $this;
396 }
397
398 /**
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.
401 *
402 * @since WD.1
403 *
404 * @param $header String
405 * @return Content
406 */
407 public function addSectionHeader( $header ) {
408 return $this;
409 }
410
411 /**
412 * Returns a Content object with preload transformations applied (or this object if no transformations apply).
413 *
414 * @since WD.1
415 *
416 * @param Title $title
417 * @param null|ParserOptions $popts
418 * @return Content
419 */
420 public function preloadTransform( Title $title, ParserOptions $popts ) {
421 return $this;
422 }
423
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
430
431 # TODO: make sure we cover the external editor interface (does anyone actually use that?!)
432
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
437 }
438 # FUTURE: special type for redirects?!
439 # FUTURE: MultipartMultipart < WikipageContent (Main + Links + X)
440 # FUTURE: LinksContent < LanguageLinksContent, CategoriesContent
441
442 /**
443 * Content object implementation for representing flat text.
444 *
445 * TextContent instances are imutable
446 *
447 * @since WD.1
448 */
449 abstract class TextContent extends Content {
450
451 public function __construct( $text, $model_name = null ) {
452 parent::__construct( $model_name );
453
454 $this->mText = $text;
455 }
456
457 public function copy() {
458 return $this; #NOTE: this is ok since TextContent are imutable.
459 }
460
461 public function getTextForSummary( $maxlength = 250 ) {
462 global $wgContLang;
463
464 $text = $this->getNativeData();
465
466 $truncatedtext = $wgContLang->truncate(
467 preg_replace( "/[\n\r]/", ' ', $text ),
468 max( 0, $maxlength ) );
469
470 return $truncatedtext;
471 }
472
473 /**
474 * returns the text's size in bytes.
475 *
476 * @return int the size
477 */
478 public function getSize( ) {
479 $text = $this->getNativeData( );
480 return strlen( $text );
481 }
482
483 /**
484 * Returns true if this content is not a redirect, and $wgArticleCountMethod is "any".
485 *
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.
488 *
489 * @return bool true if the content is countable
490 */
491 public function isCountable( $hasLinks = null ) {
492 global $wgArticleCountMethod;
493
494 if ( $this->isRedirect( ) ) {
495 return false;
496 }
497
498 if ( $wgArticleCountMethod === 'any' ) {
499 return true;
500 }
501
502 return false;
503 }
504
505 /**
506 * Returns the text represented by this Content object, as a string.
507 *
508 * @return String the raw text
509 */
510 public function getNativeData( ) {
511 $text = $this->mText;
512 return $text;
513 }
514
515 /**
516 * Returns the text represented by this Content object, as a string.
517 *
518 * @return String the raw text
519 */
520 public function getTextForSearchIndex( ) {
521 return $this->getNativeData();
522 }
523
524 /**
525 * Returns the text represented by this Content object, as a string.
526 *
527 * @return String the raw text
528 */
529 public function getWikitextForTransclusion( ) {
530 return $this->getNativeData();
531 }
532
533 /**
534 * Returns a generic ParserOutput object, wrapping the HTML returned by getHtml().
535 *
536 * @return ParserOutput representing the HTML form of the text
537 */
538 public function getParserOutput( IContextSource $context, $revId = null, ParserOptions $options = null, $generateHtml = true ) {
539 # generic implementation, relying on $this->getHtml()
540
541 if ( $generateHtml ) $html = $this->getHtml( $options );
542 else $html = '';
543
544 $po = new ParserOutput( $html );
545
546 return $po;
547 }
548
549 protected abstract function getHtml( );
550
551 }
552
553 /**
554 * @since WD.1
555 */
556 class WikitextContent extends TextContent {
557
558 public function __construct( $text ) {
559 parent::__construct($text, CONTENT_MODEL_WIKITEXT);
560 }
561
562 protected function getHtml( ) {
563 throw new MWException( "getHtml() not implemented for wikitext. Use getParserOutput()->getText()." );
564 }
565
566 /**
567 * Returns a ParserOutput object resulting from parsing the content's text using $wgParser.
568 *
569 * @since WikiData1
570 *
571 * @param IContextSource|null $context
572 * @param null $revId
573 * @param null|ParserOptions $options
574 * @param bool $generateHtml
575 *
576 * @return ParserOutput representing the HTML form of the text
577 */
578 public function getParserOutput( IContextSource $context, $revId = null, ParserOptions $options = null, $generateHtml = true ) {
579 global $wgParser;
580
581 if ( !$options ) {
582 $options = ParserOptions::newFromUserAndLang( $context->getUser(), $context->getLanguage() );
583 }
584
585 $po = $wgParser->parse( $this->mText, $context->getTitle(), $options, true, true, $revId );
586
587 return $po;
588 }
589
590 /**
591 * Returns the section with the given id.
592 *
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
595 */
596 public function getSection( $section ) {
597 global $wgParser;
598
599 $text = $this->getNativeData();
600 $sect = $wgParser->getSection( $text, $section, false );
601
602 return new WikitextContent( $sect );
603 }
604
605 /**
606 * Replaces a section in the wikitext
607 *
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
612 */
613 public function replaceSection( $section, Content $with, $sectionTitle = '' ) {
614 wfProfileIn( __METHOD__ );
615
616 $myModelName = $this->getModelName();
617 $sectionModelName = $with->getModelName();
618
619 if ( $sectionModelName != $myModelName ) {
620 throw new MWException( "Incompatible content model for section: document uses $myModelName, section uses $sectionModelName." );
621 }
622
623 $oldtext = $this->getNativeData();
624 $text = $with->getNativeData();
625
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}";
635 }
636 } else {
637 # Replacing an existing section; roll out the big guns
638 global $wgParser;
639
640 $text = $wgParser->replaceSection( $oldtext, $section, $text );
641 }
642
643 $newContent = new WikitextContent( $text );
644
645 wfProfileOut( __METHOD__ );
646 return $newContent;
647 }
648
649 /**
650 * Returns a new WikitextContent object with the given section heading prepended.
651 *
652 * @param $header String
653 * @return Content
654 */
655 public function addSectionHeader( $header ) {
656 $text = wfMsgForContent( 'newsectionheaderdefaultlevel', $header ) . "\n\n" . $this->getNativeData();
657
658 return new WikitextContent( $text );
659 }
660
661 /**
662 * Returns a Content object with pre-save transformations applied (or this object if no transformations apply).
663 *
664 * @param Title $title
665 * @param User $user
666 * @param ParserOptions $popts
667 * @return Content
668 */
669 public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
670 global $wgParser, $wgConteLang;
671
672 $text = $this->getNativeData();
673 $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
674
675 return new WikitextContent( $pst );
676 }
677
678 /**
679 * Returns a Content object with preload transformations applied (or this object if no transformations apply).
680 *
681 * @param Title $title
682 * @param ParserOptions $popts
683 * @return Content
684 */
685 public function preloadTransform( Title $title, ParserOptions $popts ) {
686 global $wgParser, $wgConteLang;
687
688 $text = $this->getNativeData();
689 $plt = $wgParser->getPreloadText( $text, $title, $popts );
690
691 return new WikitextContent( $plt );
692 }
693
694 public function getRedirectChain() {
695 $text = $this->getNativeData();
696 return Title::newFromRedirectArray( $text );
697 }
698
699 public function getRedirectTarget() {
700 $text = $this->getNativeData();
701 return Title::newFromRedirect( $text );
702 }
703
704 public function getUltimateRedirectTarget() {
705 $text = $this->getNativeData();
706 return Title::newFromRedirectRecurse( $text );
707 }
708
709 /**
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.
712 *
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
716 *
717 * @return bool true if the content is countable
718 */
719 public function isCountable( $hasLinks = null, IContextSource $context = null ) {
720 global $wgArticleCountMethod, $wgRequest;
721
722 if ( $this->isRedirect( ) ) {
723 return false;
724 }
725
726 $text = $this->getNativeData();
727
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 );
738 }
739
740 $po = $this->getParserOutput( $context, null, null, false );
741 $links = $po->getLinks();
742 $hasLinks = !empty( $links );
743 }
744
745 return $hasLinks;
746 }
747 }
748
749 public function getTextForSummary( $maxlength = 250 ) {
750 $truncatedtext = parent::getTextForSummary( $maxlength );
751
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 );
755
756 return $truncatedtext;
757 }
758
759 }
760
761 /**
762 * @since WD.1
763 */
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.
767
768 $this->mMessageKey = $msg_key;
769
770 $this->mParameters = $params;
771
772 if ( is_null( $options ) ) {
773 $options = array();
774 }
775 elseif ( is_string( $options ) ) {
776 $options = array( $options );
777 }
778
779 $this->mOptions = $options;
780
781 $this->mHtmlOptions = null;
782 }
783
784 /**
785 * Returns the message as rendered HTML, using the options supplied to the constructor plus "parse".
786 */
787 protected function getHtml( ) {
788 $opt = array_merge( $this->mOptions, array('parse') );
789
790 return wfMsgExt( $this->mMessageKey, $this->mParameters, $opt );
791 }
792
793
794 /**
795 * Returns the message as raw text, using the options supplied to the constructor minus "parse" and "parseinline".
796 */
797 public function getNativeData( ) {
798 $opt = array_diff( $this->mOptions, array('parse', 'parseinline') );
799
800 return wfMsgExt( $this->mMessageKey, $this->mParameters, $opt );
801 }
802
803 }
804
805 /**
806 * @since WD.1
807 */
808 class JavaScriptContent extends TextContent {
809 public function __construct( $text ) {
810 parent::__construct($text, CONTENT_MODEL_JAVASCRIPT);
811 }
812
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";
818
819 return $html;
820 }
821
822 }
823
824 /**
825 * @since WD.1
826 */
827 class CssContent extends TextContent {
828 public function __construct( $text ) {
829 parent::__construct($text, CONTENT_MODEL_CSS);
830 }
831
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";
837
838 return $html;
839 }
840 }