5965753d0c0e0401e5aafcc44fce3dab3285a2a8
[lhc/web/wiklou.git] / includes / Content.php
1 <?php
2 /**
3 * A content object represents page content, e.g. the text to show on a page.
4 * Content objects have no knowledge about how they relate to Wiki pages.
5 *
6 * @since 1.WD
7 */
8 interface Content {
9
10 /**
11 * @since WD.1
12 *
13 * @return String a string representing the content in a way useful for building a full text search index.
14 * If no useful representation exists, this method returns an empty string.
15 *
16 * @todo: test that this actually works
17 * @todo: make sure this also works with LuceneSearch / WikiSearch
18 */
19 public function getTextForSearchIndex( );
20
21 /**
22 * @since WD.1
23 *
24 * @return String the wikitext to include when another page includes this content, or false if the content is not
25 * includable in a wikitext page.
26 *
27 * @TODO: allow native handling, bypassing wikitext representation, like for includable special pages.
28 * @TODO: allow transclusion into other content models than Wikitext!
29 * @TODO: used in WikiPage and MessageCache to get message text. Not so nice. What should we use instead?!
30 */
31 public function getWikitextForTransclusion( );
32
33 /**
34 * Returns a textual representation of the content suitable for use in edit summaries and log messages.
35 *
36 * @since WD.1
37 *
38 * @param int $maxlength maximum length of the summary text
39 * @return String the summary text
40 */
41 public function getTextForSummary( $maxlength = 250 );
42
43 /**
44 * Returns native representation of the data. Interpretation depends on the data model used,
45 * as given by getDataModel().
46 *
47 * @since WD.1
48 *
49 * @return mixed the native representation of the content. Could be a string, a nested array
50 * structure, an object, a binary blob... anything, really.
51 *
52 * @NOTE: review all calls carefully, caller must be aware of content model!
53 */
54 public function getNativeData( );
55
56 /**
57 * returns the content's nominal size in bogo-bytes.
58 *
59 * @return int
60 */
61 public function getSize( );
62
63 /**
64 * Returns the id of the content model used by this content objects.
65 * Corresponds to the CONTENT_MODEL_XXX constants.
66 *
67 * @since WD.1
68 *
69 * @return int the model id
70 */
71 public function getModel();
72
73 /**
74 * Convenience method that returns the ContentHandler singleton for handling the content
75 * model this Content object uses.
76 *
77 * Shorthand for ContentHandler::getForContent( $this )
78 *
79 * @since WD.1
80 *
81 * @return ContentHandler
82 */
83 public function getContentHandler();
84
85 /**
86 * Convenience method that returns the default serialization format for the content model
87 * model this Content object uses.
88 *
89 * Shorthand for $this->getContentHandler()->getDefaultFormat()
90 *
91 * @since WD.1
92 *
93 * @return ContentHandler
94 */
95 public function getDefaultFormat();
96
97 /**
98 * Convenience method that returns the list of serialization formats supported
99 * for the content model model this Content object uses.
100 *
101 * Shorthand for $this->getContentHandler()->getSupportedFormats()
102 *
103 * @since WD.1
104 *
105 * @return array of supported serialization formats
106 */
107 public function getSupportedFormats();
108
109 /**
110 * Returns true if $format is a supported serialization format for this Content object,
111 * false if it isn't.
112 *
113 * Note that this should always return true if $format is null, because null stands for the
114 * default serialization.
115 *
116 * Shorthand for $this->getContentHandler()->isSupportedFormat( $format )
117 *
118 * @since WD.1
119 *
120 * @param String $format the format to check
121 * @return bool whether the format is supported
122 */
123 public function isSupportedFormat( $format );
124
125 /**
126 * Convenience method for serializing this Content object.
127 *
128 * Shorthand for $this->getContentHandler()->serializeContent( $this, $format )
129 *
130 * @since WD.1
131 *
132 * @param null|String $format the desired serialization format (or null for the default format).
133 * @return String serialized form of this Content object
134 */
135 public function serialize( $format = null );
136
137 /**
138 * Returns true if this Content object represents empty content.
139 *
140 * @since WD.1
141 *
142 * @return bool whether this Content object is empty
143 */
144 public function isEmpty();
145
146 /**
147 * Returns whether the content is valid. This is intended for local validity checks, not considering global consistency.
148 * Content needs to be valid before it can be saved.
149 *
150 * This default implementation always returns true.
151 *
152 * @since WD.1
153 *
154 * @return boolean
155 */
156 public function isValid();
157
158 /**
159 * Returns true if this Content objects is conceptually equivalent to the given Content object.
160 * Contract:
161 *
162 * * Will return false if $that is null.
163 * * Will return true if $that === $this.
164 * * Will return false if $that->getModelName() != $this->getModel().
165 * * Will return false if $that->getNativeData() is not equal to $this->getNativeData(),
166 * where the meaning of "equal" depends on the actual data model.
167 *
168 * Implementations should be careful to make equals() transitive and reflexive:
169 *
170 * * $a->equals( $b ) <=> $b->equals( $a )
171 * * $a->equals( $b ) && $b->equals( $c ) ==> $a->equals( $c )
172 *
173 * @since WD.1
174 *
175 * @param Content $that the Content object to compare to
176 * @return bool true if this Content object is equal to $that, false otherwise.
177 */
178 public function equals( Content $that = null );
179
180 /**
181 * Return a copy of this Content object. The following must be true for the object returned
182 * if $copy = $original->copy()
183 *
184 * * get_class($original) === get_class($copy)
185 * * $original->getModel() === $copy->getModel()
186 * * $original->equals( $copy )
187 *
188 * If and only if the Content object is immutable, the copy() method can and should
189 * return $this. That is, $copy === $original may be true, but only for immutable content
190 * objects.
191 *
192 * @since WD.1
193 *
194 * @return Content. A copy of this object
195 */
196 public function copy( );
197
198 /**
199 * Returns true if this content is countable as a "real" wiki page, provided
200 * that it's also in a countable location (e.g. a current revision in the main namespace).
201 *
202 * @since WD.1
203 *
204 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
205 * to avoid redundant parsing to find out.
206 * @return boolean
207 */
208 public function isCountable( $hasLinks = null ) ;
209
210 /**
211 * Convenience method, shorthand for
212 * $this->getContentHandler()->getParserOutput( $this, $title, $revId, $options, $generateHtml )
213 *
214 * @note: subclasses should NOT override this to provide custom rendering.
215 * Override ContentHandler::getParserOutput() instead!
216 *
217 * @param Title $title
218 * @param null $revId
219 * @param null|ParserOptions $options
220 * @param Boolean $generateHtml whether to generate Html (default: true). If false,
221 * the result of calling getText() on the ParserOutput object returned by
222 * this method is undefined.
223 *
224 * @since WD.1
225 *
226 * @return ParserOutput
227 */
228 public function getParserOutput( Title $title, $revId = null, ParserOptions $options = null, $generateHtml = true );
229
230 /**
231 * Construct the redirect destination from this content and return an
232 * array of Titles, or null if this content doesn't represent a redirect.
233 * The last element in the array is the final destination after all redirects
234 * have been resolved (up to $wgMaxRedirects times).
235 *
236 * @since WD.1
237 *
238 * @return Array of Titles, with the destination last
239 */
240 public function getRedirectChain();
241
242 /**
243 * Construct the redirect destination from this content and return a Title,
244 * or null if this content doesn't represent a redirect.
245 * This will only return the immediate redirect target, useful for
246 * the redirect table and other checks that don't need full recursion.
247 *
248 * @since WD.1
249 *
250 * @return Title: The corresponding Title
251 */
252 public function getRedirectTarget();
253
254 /**
255 * Construct the redirect destination from this content and return the
256 * Title, or null if this content doesn't represent a redirect.
257 * This will recurse down $wgMaxRedirects times or until a non-redirect target is hit
258 * in order to provide (hopefully) the Title of the final destination instead of another redirect.
259 *
260 * @since WD.1
261 *
262 * @return Title
263 */
264 public function getUltimateRedirectTarget();
265
266 /**
267 * Returns whether this Content represents a redirect.
268 * Shorthand for getRedirectTarget() !== null.
269 *
270 * @since WD.1
271 *
272 * @return bool
273 */
274 public function isRedirect();
275
276 /**
277 * Returns the section with the given id.
278 *
279 * @since WD.1
280 *
281 * @param String $sectionId the section's id, given as a numeric string. The id "0" retrieves the section before
282 * the first heading, "1" the text between the first heading (included) and the second heading (excluded), etc.
283 * @return Content|Boolean|null the section, or false if no such section exist, or null if sections are not supported
284 */
285 public function getSection( $sectionId );
286
287 /**
288 * Replaces a section of the content and returns a Content object with the section replaced.
289 *
290 * @since WD.1
291 *
292 * @param $section empty/null/false or a section number (0, 1, 2, T1, T2...), or "new"
293 * @param $with Content: new content of the section
294 * @param $sectionTitle String: new section's subject, only if $section is 'new'
295 * @return string Complete article text, or null if error
296 */
297 public function replaceSection( $section, Content $with, $sectionTitle = '' );
298
299 /**
300 * Returns a Content object with pre-save transformations applied (or this object if no transformations apply).
301 *
302 * @since WD.1
303 *
304 * @param Title $title
305 * @param User $user
306 * @param null|ParserOptions $popts
307 * @return Content
308 */
309 public function preSaveTransform( Title $title, User $user, ParserOptions $popts );
310
311 /**
312 * Returns a new WikitextContent object with the given section heading prepended, if supported.
313 * The default implementation just returns this Content object unmodified, ignoring the section header.
314 *
315 * @since WD.1
316 *
317 * @param $header String
318 * @return Content
319 */
320 public function addSectionHeader( $header );
321
322 /**
323 * Returns a Content object with preload transformations applied (or this object if no transformations apply).
324 *
325 * @since WD.1
326 *
327 * @param Title $title
328 * @param null|ParserOptions $popts
329 * @return Content
330 */
331 public function preloadTransform( Title $title, ParserOptions $popts );
332
333 # TODO: handle ImagePage and CategoryPage
334 # TODO: make sure we cover lucene search / wikisearch.
335 # TODO: make sure ReplaceTemplates still works
336 # FUTURE: nice&sane integration of GeSHi syntax highlighting
337 # [11:59] <vvv> Hooks are ugly; make CodeHighlighter interface and a config to set the class which handles syntax highlighting
338 # [12:00] <vvv> And default it to a DummyHighlighter
339
340 # TODO: make sure we cover the external editor interface (does anyone actually use that?!)
341
342 # TODO: tie into API to provide contentModel for Revisions
343 # TODO: tie into API to provide serialized version and contentFormat for Revisions
344 # TODO: tie into API edit interface
345 # FUTURE: make EditForm plugin for EditPage
346
347 # FUTURE: special type for redirects?!
348 # FUTURE: MultipartMultipart < WikipageContent (Main + Links + X)
349 # FUTURE: LinksContent < LanguageLinksContent, CategoriesContent
350
351 // @TODO: add support for ar_content_format, ar_content_model, rev_content_format, rev_content_model to API
352 }
353
354
355 /**
356 * A content object represents page content, e.g. the text to show on a page.
357 * Content objects have no knowledge about how they relate to Wiki pages.
358 *
359 * @since 1.WD
360 */
361 abstract class AbstractContent implements Content {
362
363 /**
364 * Name of the content model this Content object represents.
365 * Use with CONTENT_MODEL_XXX constants
366 *
367 * @var String $model_id
368 */
369 protected $model_id;
370
371 /**
372 * @param int $model_id
373 */
374 public function __construct( $model_id = null ) {
375 $this->model_id = $model_id;
376 }
377
378 /**
379 * Returns the id of the content model used by this content objects.
380 * Corresponds to the CONTENT_MODEL_XXX constants.
381 *
382 * @since WD.1
383 *
384 * @return int the model id
385 */
386 public function getModel() {
387 return $this->model_id;
388 }
389
390 /**
391 * Throws an MWException if $model_id is not the id of the content model
392 * supported by this Content object.
393 *
394 * @param int $model_id the model to check
395 *
396 * @throws MWException
397 */
398 protected function checkModelID( $model_id ) {
399 if ( $model_id !== $this->model_id ) {
400 $model_name = ContentHandler::getContentModelName( $model_id );
401 $own_model_name = ContentHandler::getContentModelName( $this->model_id );
402
403 throw new MWException( "Bad content model: expected {$this->model_id} ($own_model_name) but got found $model_id ($model_name)." );
404 }
405 }
406
407 /**
408 * Convenience method that returns the ContentHandler singleton for handling the content
409 * model this Content object uses.
410 *
411 * Shorthand for ContentHandler::getForContent( $this )
412 *
413 * @since WD.1
414 *
415 * @return ContentHandler
416 */
417 public function getContentHandler() {
418 return ContentHandler::getForContent( $this );
419 }
420
421 /**
422 * Convenience method that returns the default serialization format for the content model
423 * model this Content object uses.
424 *
425 * Shorthand for $this->getContentHandler()->getDefaultFormat()
426 *
427 * @since WD.1
428 *
429 * @return ContentHandler
430 */
431 public function getDefaultFormat() {
432 return $this->getContentHandler()->getDefaultFormat();
433 }
434
435 /**
436 * Convenience method that returns the list of serialization formats supported
437 * for the content model model this Content object uses.
438 *
439 * Shorthand for $this->getContentHandler()->getSupportedFormats()
440 *
441 * @since WD.1
442 *
443 * @return array of supported serialization formats
444 */
445 public function getSupportedFormats() {
446 return $this->getContentHandler()->getSupportedFormats();
447 }
448
449 /**
450 * Returns true if $format is a supported serialization format for this Content object,
451 * false if it isn't.
452 *
453 * Note that this will always return true if $format is null, because null stands for the
454 * default serialization.
455 *
456 * Shorthand for $this->getContentHandler()->isSupportedFormat( $format )
457 *
458 * @since WD.1
459 *
460 * @param String $format the format to check
461 * @return bool whether the format is supported
462 */
463 public function isSupportedFormat( $format ) {
464 if ( !$format ) {
465 return true; // this means "use the default"
466 }
467
468 return $this->getContentHandler()->isSupportedFormat( $format );
469 }
470
471 /**
472 * Throws an MWException if $this->isSupportedFormat( $format ) doesn't return true.
473 *
474 * @param $format
475 * @throws MWException
476 */
477 protected function checkFormat( $format ) {
478 if ( !$this->isSupportedFormat( $format ) ) {
479 throw new MWException( "Format $format is not supported for content model " . $this->getModel() );
480 }
481 }
482
483 /**
484 * Convenience method for serializing this Content object.
485 *
486 * Shorthand for $this->getContentHandler()->serializeContent( $this, $format )
487 *
488 * @since WD.1
489 *
490 * @param null|String $format the desired serialization format (or null for the default format).
491 * @return String serialized form of this Content object
492 */
493 public function serialize( $format = null ) {
494 return $this->getContentHandler()->serializeContent( $this, $format );
495 }
496
497 /**
498 * Returns true if this Content object represents empty content.
499 *
500 * @since WD.1
501 *
502 * @return bool whether this Content object is empty
503 */
504 public function isEmpty() {
505 return $this->getSize() == 0;
506 }
507
508 /**
509 * Returns if the content is valid. This is intended for local validity checks, not considering global consistency.
510 * It needs to be valid before it can be saved.
511 *
512 * This default implementation always returns true.
513 *
514 * @since WD.1
515 *
516 * @return boolean
517 */
518 public function isValid() {
519 return true;
520 }
521
522 /**
523 * Returns true if this Content objects is conceptually equivalent to the given Content object.
524 *
525 * Will returns false if $that is null.
526 * Will return true if $that === $this.
527 * Will return false if $that->getModelName() != $this->getModel().
528 * Will return false if $that->getNativeData() is not equal to $this->getNativeData(),
529 * where the meaning of "equal" depends on the actual data model.
530 *
531 * Implementations should be careful to make equals() transitive and reflexive:
532 *
533 * * $a->equals( $b ) <=> $b->equals( $a )
534 * * $a->equals( $b ) && $b->equals( $c ) ==> $a->equals( $c )
535 *
536 * @since WD.1
537 *
538 * @param Content $that the Content object to compare to
539 * @return bool true if this Content object is euqual to $that, false otherwise.
540 */
541 public function equals( Content $that = null ) {
542 if ( is_null( $that ) ){
543 return false;
544 }
545
546 if ( $that === $this ) {
547 return true;
548 }
549
550 if ( $that->getModel() !== $this->getModel() ) {
551 return false;
552 }
553
554 return $this->getNativeData() === $that->getNativeData();
555 }
556
557 /**
558 * Convenience method, shorthand for
559 * $this->getContentHandler()->getParserOutput( $this, $title, $revId, $options, $generateHtml )
560 *
561 * @note: subclasses should NOT override this to provide custom rendering.
562 * Override ContentHandler::getParserOutput() instead!
563 *
564 * @param Title $title
565 * @param null $revId
566 * @param null|ParserOptions $options
567 * @param Boolean $generateHtml whether to generate Html (default: true). If false,
568 * the result of calling getText() on the ParserOutput object returned by
569 * this method is undefined.
570 *
571 * @since WD.1
572 *
573 * @return ParserOutput
574 */
575 public function getParserOutput( Title $title, $revId = null, ParserOptions $options = null, $generateHtml = true ) {
576 return $this->getContentHandler()->getParserOutput( $this, $title, $revId, $options, $generateHtml );
577 }
578
579 /**
580 * Construct the redirect destination from this content and return an
581 * array of Titles, or null if this content doesn't represent a redirect.
582 * The last element in the array is the final destination after all redirects
583 * have been resolved (up to $wgMaxRedirects times).
584 *
585 * There is usually no need to override the default behaviour, subclasses that
586 * want to implement redirects should override getRedirectTarget().
587 *
588 * @since WD.1
589 *
590 * @return Array of Titles, with the destination last
591 * @note: migrated here from Title::newFromRedirectArray
592 */
593 public function getRedirectChain() {
594 global $wgMaxRedirects;
595 $title = $this->getRedirectTarget();
596 if ( is_null( $title ) ) {
597 return null;
598 }
599 // recursive check to follow double redirects
600 $recurse = $wgMaxRedirects;
601 $titles = array( $title );
602 while ( --$recurse > 0 ) {
603 if ( $title->isRedirect() ) {
604 $page = WikiPage::factory( $title );
605 $newtitle = $page->getRedirectTarget();
606 } else {
607 break;
608 }
609 // Redirects to some special pages are not permitted
610 if ( $newtitle instanceOf Title && $newtitle->isValidRedirectTarget() ) {
611 // the new title passes the checks, so make that our current title so that further recursion can be checked
612 $title = $newtitle;
613 $titles[] = $newtitle;
614 } else {
615 break;
616 }
617 }
618 return $titles;
619 }
620
621 /**
622 * Construct the redirect destination from this content and return a Title,
623 * or null if this content doesn't represent a redirect.
624 *
625 * This shall only return the immediate redirect target, useful for
626 * the redirect table and other checks that don't need full recursion.
627 *
628 * This implementation always returns null, subclasses should implement it
629 * according to their data model.
630 *
631 * @since WD.1
632 *
633 * @return Title: The corresponding Title
634 */
635 public function getRedirectTarget() {
636 return null;
637 }
638
639 /**
640 * Construct the redirect destination from this content and return the
641 * Title, or null if this content doesn't represent a redirect.
642 * This will recurse down $wgMaxRedirects times or until a non-redirect target is hit
643 * in order to provide (hopefully) the Title of the final destination instead of another redirect.
644 *
645 * There is usually no need to override the default behaviour, subclasses that
646 * want to implement redirects should override getRedirectTarget().
647 *
648 * @since WD.1
649 *
650 * @return Title
651 * @note: migrated here from Title::newFromRedirectRecurse
652 */
653 public function getUltimateRedirectTarget() {
654 $titles = $this->getRedirectChain();
655 return $titles ? array_pop( $titles ) : null;
656 }
657
658 /**
659 * @since WD.1
660 *
661 * @return bool
662 */
663 public function isRedirect() {
664 return $this->getRedirectTarget() !== null;
665 }
666
667 /**
668 * Returns the section with the given id.
669 *
670 * The default implementation returns null.
671 *
672 * @since WD.1
673 *
674 * @param String $sectionId the section's id, given as a numeric string. The id "0" retrieves the section before
675 * the first heading, "1" the text between the first heading (included) and the second heading (excluded), etc.
676 * @return Content|Boolean|null the section, or false if no such section exist, or null if sections are not supported
677 */
678 public function getSection( $sectionId ) {
679 return null;
680 }
681
682 /**
683 * Replaces a section of the content and returns a Content object with the section replaced.
684 *
685 * @since WD.1
686 *
687 * @param $section empty/null/false or a section number (0, 1, 2, T1, T2...), or "new"
688 * @param $with Content: new content of the section
689 * @param $sectionTitle String: new section's subject, only if $section is 'new'
690 * @return string Complete article text, or null if error
691 */
692 public function replaceSection( $section, Content $with, $sectionTitle = '' ) {
693 return null;
694 }
695
696 /**
697 * Returns a Content object with pre-save transformations applied (or this object if no transformations apply).
698 *
699 * @since WD.1
700 *
701 * @param Title $title
702 * @param User $user
703 * @param null|ParserOptions $popts
704 * @return Content
705 */
706 public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
707 return $this;
708 }
709
710 /**
711 * Returns a new WikitextContent object with the given section heading prepended, if supported.
712 * The default implementation just returns this Content object unmodified, ignoring the section header.
713 *
714 * @since WD.1
715 *
716 * @param $header String
717 * @return Content
718 */
719 public function addSectionHeader( $header ) {
720 return $this;
721 }
722
723 /**
724 * Returns a Content object with preload transformations applied (or this object if no transformations apply).
725 *
726 * @since WD.1
727 *
728 * @param Title $title
729 * @param null|ParserOptions $popts
730 * @return Content
731 */
732 public function preloadTransform( Title $title, ParserOptions $popts ) {
733 return $this;
734 }
735 }
736
737 /**
738 * Content object implementation for representing flat text.
739 *
740 * TextContent instances are immutable
741 *
742 * @since WD.1
743 */
744 abstract class TextContent extends AbstractContent {
745
746 public function __construct( $text, $model_id = null ) {
747 parent::__construct( $model_id );
748
749 $this->mText = $text;
750 }
751
752 public function copy() {
753 return $this; #NOTE: this is ok since TextContent are immutable.
754 }
755
756 public function getTextForSummary( $maxlength = 250 ) {
757 global $wgContLang;
758
759 $text = $this->getNativeData();
760
761 $truncatedtext = $wgContLang->truncate(
762 preg_replace( "/[\n\r]/", ' ', $text ),
763 max( 0, $maxlength ) );
764
765 return $truncatedtext;
766 }
767
768 /**
769 * returns the text's size in bytes.
770 *
771 * @return int the size
772 */
773 public function getSize( ) {
774 $text = $this->getNativeData( );
775 return strlen( $text );
776 }
777
778 /**
779 * Returns true if this content is not a redirect, and $wgArticleCountMethod is "any".
780 *
781 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
782 * to avoid redundant parsing to find out.
783 *
784 * @return bool true if the content is countable
785 */
786 public function isCountable( $hasLinks = null ) {
787 global $wgArticleCountMethod;
788
789 if ( $this->isRedirect( ) ) {
790 return false;
791 }
792
793 if ( $wgArticleCountMethod === 'any' ) {
794 return true;
795 }
796
797 return false;
798 }
799
800 /**
801 * Returns the text represented by this Content object, as a string.
802 *
803 * @return String the raw text
804 */
805 public function getNativeData( ) {
806 $text = $this->mText;
807 return $text;
808 }
809
810 /**
811 * Returns the text represented by this Content object, as a string.
812 *
813 * @return String the raw text
814 */
815 public function getTextForSearchIndex( ) {
816 return $this->getNativeData();
817 }
818
819 /**
820 * Returns the text represented by this Content object, as a string.
821 *
822 * @return String the raw text
823 */
824 public function getWikitextForTransclusion( ) {
825 return $this->getNativeData();
826 }
827
828 /**
829 * Diff this content object with another content object..
830 *
831 * @since WD.diff
832 *
833 * @param Content $that the other content object to compare this content object to
834 * @param Language $lang the language object to use for text segmentation. If not given, $wgContentLang is used.
835 *
836 * @return DiffResult a diff representing the changes that would have to be made to this content object
837 * to make it equal to $that.
838 */
839 public function diff( Content $that, Language $lang = null ) {
840 global $wgContLang;
841
842 $this->checkModelID( $that->getModel() );
843
844 #@todo: could implement this in DifferenceEngine and just delegate here?
845
846 if ( !$lang ) $lang = $wgContLang;
847
848 $otext = $this->getNativeData();
849 $ntext = $this->getNativeData();
850
851 # Note: Use native PHP diff, external engines don't give us abstract output
852 $ota = explode( "\n", $wgContLang->segmentForDiff( $otext ) );
853 $nta = explode( "\n", $wgContLang->segmentForDiff( $ntext ) );
854
855 $diff = new Diff( $ota, $nta );
856 return $diff;
857 }
858
859
860 }
861
862 /**
863 * @since WD.1
864 */
865 class WikitextContent extends TextContent {
866
867 public function __construct( $text ) {
868 parent::__construct($text, CONTENT_MODEL_WIKITEXT);
869 }
870
871 /**
872 * Returns the section with the given id.
873 *
874 * @param String $section
875 *
876 * @internal param String $sectionId the section's id
877 * @return Content|false|null the section, or false if no such section exist, or null if sections are not supported
878 */
879 public function getSection( $section ) {
880 global $wgParser;
881
882 $text = $this->getNativeData();
883 $sect = $wgParser->getSection( $text, $section, false );
884
885 return new WikitextContent( $sect );
886 }
887
888 /**
889 * Replaces a section in the wikitext
890 *
891 * @param $section empty/null/false or a section number (0, 1, 2, T1, T2...), or "new"
892 * @param $with Content: new content of the section
893 * @param $sectionTitle String: new section's subject, only if $section is 'new'
894 *
895 * @throws MWException
896 * @return Content Complete article content, or null if error
897 */
898 public function replaceSection( $section, Content $with, $sectionTitle = '' ) {
899 wfProfileIn( __METHOD__ );
900
901 $myModelId = $this->getModel();
902 $sectionModelId = $with->getModel();
903
904 if ( $sectionModelId != $myModelId ) {
905 $myModelName = ContentHandler::getContentModelName( $myModelId );
906 $sectionModelName = ContentHandler::getContentModelName( $sectionModelId );
907
908 throw new MWException( "Incompatible content model for section: document uses $myModelId ($myModelName), "
909 . "section uses $sectionModelId ($sectionModelName)." );
910 }
911
912 $oldtext = $this->getNativeData();
913 $text = $with->getNativeData();
914
915 if ( $section === '' ) {
916 return $with; #XXX: copy first?
917 } if ( $section == 'new' ) {
918 # Inserting a new section
919 $subject = $sectionTitle ? wfMsgForContent( 'newsectionheaderdefaultlevel', $sectionTitle ) . "\n\n" : '';
920 if ( wfRunHooks( 'PlaceNewSection', array( $this, $oldtext, $subject, &$text ) ) ) {
921 $text = strlen( trim( $oldtext ) ) > 0
922 ? "{$oldtext}\n\n{$subject}{$text}"
923 : "{$subject}{$text}";
924 }
925 } else {
926 # Replacing an existing section; roll out the big guns
927 global $wgParser;
928
929 $text = $wgParser->replaceSection( $oldtext, $section, $text );
930 }
931
932 $newContent = new WikitextContent( $text );
933
934 wfProfileOut( __METHOD__ );
935 return $newContent;
936 }
937
938 /**
939 * Returns a new WikitextContent object with the given section heading prepended.
940 *
941 * @param $header String
942 * @return Content
943 */
944 public function addSectionHeader( $header ) {
945 $text = wfMsgForContent( 'newsectionheaderdefaultlevel', $header ) . "\n\n" . $this->getNativeData();
946
947 return new WikitextContent( $text );
948 }
949
950 /**
951 * Returns a Content object with pre-save transformations applied (or this object if no transformations apply).
952 *
953 * @param Title $title
954 * @param User $user
955 * @param ParserOptions $popts
956 * @return Content
957 */
958 public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) { #FIXME: also needed for JS/CSS!
959 global $wgParser, $wgConteLang;
960
961 $text = $this->getNativeData();
962 $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
963
964 return new WikitextContent( $pst );
965 }
966
967 /**
968 * Returns a Content object with preload transformations applied (or this object if no transformations apply).
969 *
970 * @param Title $title
971 * @param ParserOptions $popts
972 * @return Content
973 */
974 public function preloadTransform( Title $title, ParserOptions $popts ) {
975 global $wgParser, $wgConteLang;
976
977 $text = $this->getNativeData();
978 $plt = $wgParser->getPreloadText( $text, $title, $popts );
979
980 return new WikitextContent( $plt );
981 }
982
983 /**
984 * Implement redirect extraction for wikitext.
985 *
986 * @return null|Title
987 *
988 * @note: migrated here from Title::newFromRedirectInternal()
989 *
990 * @see Content::getRedirectTarget
991 * @see AbstractContent::getRedirectTarget
992 */
993 public function getRedirectTarget() {
994 global $wgMaxRedirects;
995 if ( $wgMaxRedirects < 1 ) {
996 //redirects are disabled, so quit early
997 return null;
998 }
999 $redir = MagicWord::get( 'redirect' );
1000 $text = trim( $this->getNativeData() );
1001 if ( $redir->matchStartAndRemove( $text ) ) {
1002 // Extract the first link and see if it's usable
1003 // Ensure that it really does come directly after #REDIRECT
1004 // Some older redirects included a colon, so don't freak about that!
1005 $m = array();
1006 if ( preg_match( '!^\s*:?\s*\[{2}(.*?)(?:\|.*?)?\]{2}!', $text, $m ) ) {
1007 // Strip preceding colon used to "escape" categories, etc.
1008 // and URL-decode links
1009 if ( strpos( $m[1], '%' ) !== false ) {
1010 // Match behavior of inline link parsing here;
1011 $m[1] = rawurldecode( ltrim( $m[1], ':' ) );
1012 }
1013 $title = Title::newFromText( $m[1] );
1014 // If the title is a redirect to bad special pages or is invalid, return null
1015 if ( !$title instanceof Title || !$title->isValidRedirectTarget() ) {
1016 return null;
1017 }
1018 return $title;
1019 }
1020 }
1021 return null;
1022 }
1023
1024 /**
1025 * Returns true if this content is not a redirect, and this content's text is countable according to
1026 * the criteria defined by $wgArticleCountMethod.
1027 *
1028 * @param Bool $hasLinks if it is known whether this content contains links, provide this information here,
1029 * to avoid redundant parsing to find out.
1030 * @param null|\Title $title
1031 *
1032 * @internal param \IContextSource $context context for parsing if necessary
1033 *
1034 * @return bool true if the content is countable
1035 */
1036 public function isCountable( $hasLinks = null, Title $title = null ) {
1037 global $wgArticleCountMethod;
1038
1039 if ( $this->isRedirect( ) ) {
1040 return false;
1041 }
1042
1043 $text = $this->getNativeData();
1044
1045 switch ( $wgArticleCountMethod ) {
1046 case 'any':
1047 return true;
1048 case 'comma':
1049 return strpos( $text, ',' ) !== false;
1050 case 'link':
1051 if ( $hasLinks === null ) { # not known, find out
1052 if ( !$title ) {
1053 $context = RequestContext::getMain();
1054 $title = $context->getTitle();
1055 }
1056
1057 $po = $this->getParserOutput( $title, null, null, false );
1058 $links = $po->getLinks();
1059 $hasLinks = !empty( $links );
1060 }
1061
1062 return $hasLinks;
1063 }
1064
1065 return false;
1066 }
1067
1068 public function getTextForSummary( $maxlength = 250 ) {
1069 $truncatedtext = parent::getTextForSummary( $maxlength );
1070
1071 #clean up unfinished links
1072 #XXX: make this optional? wasn't there in autosummary, but required for deletion summary.
1073 $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
1074
1075 return $truncatedtext;
1076 }
1077
1078 }
1079
1080 /**
1081 * @since WD.1
1082 */
1083 class MessageContent extends TextContent {
1084 public function __construct( $msg_key, $params = null, $options = null ) {
1085 parent::__construct(null, CONTENT_MODEL_WIKITEXT); #XXX: messages may be wikitext, html or plain text! and maybe even something else entirely.
1086
1087 $this->mMessageKey = $msg_key;
1088
1089 $this->mParameters = $params;
1090
1091 if ( is_null( $options ) ) {
1092 $options = array();
1093 }
1094 elseif ( is_string( $options ) ) {
1095 $options = array( $options );
1096 }
1097
1098 $this->mOptions = $options;
1099 }
1100
1101 /**
1102 * Returns the message as rendered HTML, using the options supplied to the constructor plus "parse".
1103 * @return String the message text, parsed
1104 */
1105 public function getHtml( ) {
1106 $opt = array_merge( $this->mOptions, array('parse') );
1107
1108 return wfMsgExt( $this->mMessageKey, $this->mParameters, $opt );
1109 }
1110
1111
1112 /**
1113 * Returns the message as raw text, using the options supplied to the constructor minus "parse" and "parseinline".
1114 *
1115 * @return String the message text, unparsed.
1116 */
1117 public function getNativeData( ) {
1118 $opt = array_diff( $this->mOptions, array('parse', 'parseinline') );
1119
1120 return wfMsgExt( $this->mMessageKey, $this->mParameters, $opt );
1121 }
1122
1123 }
1124
1125 /**
1126 * @since WD.1
1127 */
1128 class JavaScriptContent extends TextContent {
1129 public function __construct( $text ) {
1130 parent::__construct($text, CONTENT_MODEL_JAVASCRIPT);
1131 }
1132
1133 }
1134
1135 /**
1136 * @since WD.1
1137 */
1138 class CssContent extends TextContent {
1139 public function __construct( $text ) {
1140 parent::__construct($text, CONTENT_MODEL_CSS);
1141 }
1142 }