[ Index ] |
PHP Cross Reference of MediaWiki-1.24.0 |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Content object implementation for representing flat text. 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation; either version 2 of the License, or 8 * (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License along 16 * with this program; if not, write to the Free Software Foundation, Inc., 17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 18 * http://www.gnu.org/copyleft/gpl.html 19 * 20 * @since 1.21 21 * 22 * @file 23 * @ingroup Content 24 * 25 * @author Daniel Kinzler 26 */ 27 28 /** 29 * Content object implementation for representing flat text. 30 * 31 * TextContent instances are immutable 32 * 33 * @ingroup Content 34 */ 35 class TextContent extends AbstractContent { 36 37 /** 38 * @param string $text 39 * @param string $model_id 40 */ 41 public function __construct( $text, $model_id = CONTENT_MODEL_TEXT ) { 42 parent::__construct( $model_id ); 43 44 if ( $text === null || $text === false ) { 45 wfWarn( "TextContent constructed with \$text = " . var_export( $text, true ) . "! " 46 . "This may indicate an error in the caller's scope.", 2 ); 47 48 $text = ''; 49 } 50 51 if ( !is_string( $text ) ) { 52 throw new MWException( "TextContent expects a string in the constructor." ); 53 } 54 55 $this->mText = $text; 56 } 57 58 /** 59 * @note Mutable subclasses MUST override this to return a copy! 60 * 61 * @return Content $this 62 */ 63 public function copy() { 64 return $this; # NOTE: this is ok since TextContent are immutable. 65 } 66 67 public function getTextForSummary( $maxlength = 250 ) { 68 global $wgContLang; 69 70 $text = $this->getNativeData(); 71 72 $truncatedtext = $wgContLang->truncate( 73 preg_replace( "/[\n\r]/", ' ', $text ), 74 max( 0, $maxlength ) ); 75 76 return $truncatedtext; 77 } 78 79 /** 80 * Returns the text's size in bytes. 81 * 82 * @return int 83 */ 84 public function getSize() { 85 $text = $this->getNativeData(); 86 87 return strlen( $text ); 88 } 89 90 /** 91 * Returns true if this content is not a redirect, and $wgArticleCountMethod 92 * is "any". 93 * 94 * @param bool $hasLinks If it is known whether this content contains links, 95 * provide this information here, to avoid redundant parsing to find out. 96 * 97 * @return bool 98 */ 99 public function isCountable( $hasLinks = null ) { 100 global $wgArticleCountMethod; 101 102 if ( $this->isRedirect() ) { 103 return false; 104 } 105 106 if ( $wgArticleCountMethod === 'any' ) { 107 return true; 108 } 109 110 return false; 111 } 112 113 /** 114 * Returns the text represented by this Content object, as a string. 115 * 116 * @return string The raw text. 117 */ 118 public function getNativeData() { 119 return $this->mText; 120 } 121 122 /** 123 * Returns the text represented by this Content object, as a string. 124 * 125 * @return string The raw text. 126 */ 127 public function getTextForSearchIndex() { 128 return $this->getNativeData(); 129 } 130 131 /** 132 * Returns attempts to convert this content object to wikitext, 133 * and then returns the text string. The conversion may be lossy. 134 * 135 * @note this allows any text-based content to be transcluded as if it was wikitext. 136 * 137 * @return string|bool The raw text, or false if the conversion failed. 138 */ 139 public function getWikitextForTransclusion() { 140 $wikitext = $this->convert( CONTENT_MODEL_WIKITEXT, 'lossy' ); 141 142 if ( $wikitext ) { 143 return $wikitext->getNativeData(); 144 } else { 145 return false; 146 } 147 } 148 149 /** 150 * Returns a Content object with pre-save transformations applied. 151 * This implementation just trims trailing whitespace. 152 * 153 * @param Title $title 154 * @param User $user 155 * @param ParserOptions $popts 156 * 157 * @return Content 158 */ 159 public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) { 160 $text = $this->getNativeData(); 161 $pst = rtrim( $text ); 162 163 return ( $text === $pst ) ? $this : new static( $pst, $this->getModel() ); 164 } 165 166 /** 167 * Diff this content object with another content object. 168 * 169 * @since 1.21 170 * 171 * @param Content $that The other content object to compare this content object to. 172 * @param Language $lang The language object to use for text segmentation. 173 * If not given, $wgContentLang is used. 174 * 175 * @return Diff A diff representing the changes that would have to be 176 * made to this content object to make it equal to $that. 177 */ 178 public function diff( Content $that, Language $lang = null ) { 179 global $wgContLang; 180 181 $this->checkModelID( $that->getModel() ); 182 183 // @todo could implement this in DifferenceEngine and just delegate here? 184 185 if ( !$lang ) { 186 $lang = $wgContLang; 187 } 188 189 $otext = $this->getNativeData(); 190 $ntext = $that->getNativeData(); 191 192 # Note: Use native PHP diff, external engines don't give us abstract output 193 $ota = explode( "\n", $lang->segmentForDiff( $otext ) ); 194 $nta = explode( "\n", $lang->segmentForDiff( $ntext ) ); 195 196 $diff = new Diff( $ota, $nta ); 197 198 return $diff; 199 } 200 201 /** 202 * Fills the provided ParserOutput object with information derived from the content. 203 * Unless $generateHtml was false, this includes an HTML representation of the content 204 * provided by getHtml(). 205 * 206 * For content models listed in $wgTextModelsToParse, this method will call the MediaWiki 207 * wikitext parser on the text to extract any (wikitext) links, magic words, etc. 208 * 209 * Subclasses may override this to provide custom content processing. 210 * For custom HTML generation alone, it is sufficient to override getHtml(). 211 * 212 * @param Title $title Context title for parsing 213 * @param int $revId Revision ID (for {{REVISIONID}}) 214 * @param ParserOptions $options Parser options 215 * @param bool $generateHtml Whether or not to generate HTML 216 * @param ParserOutput $output The output object to fill (reference). 217 */ 218 protected function fillParserOutput( Title $title, $revId, 219 ParserOptions $options, $generateHtml, ParserOutput &$output 220 ) { 221 global $wgParser, $wgTextModelsToParse; 222 223 if ( in_array( $this->getModel(), $wgTextModelsToParse ) ) { 224 // parse just to get links etc into the database, HTML is replaced below. 225 $output = $wgParser->parse( $this->getNativeData(), $title, $options, true, true, $revId ); 226 } 227 228 if ( $generateHtml ) { 229 $html = $this->getHtml(); 230 } else { 231 $html = ''; 232 } 233 234 $output->setText( $html ); 235 } 236 237 /** 238 * Generates an HTML version of the content, for display. Used by 239 * fillParserOutput() to provide HTML for the ParserOutput object. 240 * 241 * Subclasses may override this to provide a custom HTML rendering. 242 * If further information is to be derived from the content (such as 243 * categories), the fillParserOutput() method can be overridden instead. 244 * 245 * For backwards-compatibility, this default implementation just calls 246 * getHighlightHtml(). 247 * 248 * @return string An HTML representation of the content 249 */ 250 protected function getHtml() { 251 return $this->getHighlightHtml(); 252 } 253 254 /** 255 * Generates an HTML version of the content, for display. 256 * 257 * This default implementation returns an HTML-escaped version 258 * of the raw text content. 259 * 260 * @note The functionality of this method should really be implemented 261 * in getHtml(), and subclasses should override getHtml() if needed. 262 * getHighlightHtml() is kept around for backward compatibility with 263 * extensions that already override it. 264 * 265 * @deprecated since 1.24. Use getHtml() instead. In particular, subclasses overriding 266 * getHighlightHtml() should override getHtml() instead. 267 * 268 * @return string An HTML representation of the content 269 */ 270 protected function getHighlightHtml() { 271 return htmlspecialchars( $this->getNativeData() ); 272 } 273 274 /** 275 * This implementation provides lossless conversion between content models based 276 * on TextContent. 277 * 278 * @param string $toModel The desired content model, use the CONTENT_MODEL_XXX flags. 279 * @param string $lossy Flag, set to "lossy" to allow lossy conversion. If lossy conversion is not 280 * allowed, full round-trip conversion is expected to work without losing information. 281 * 282 * @return Content|bool A content object with the content model $toModel, or false if that 283 * conversion is not supported. 284 * 285 * @see Content::convert() 286 */ 287 public function convert( $toModel, $lossy = '' ) { 288 $converted = parent::convert( $toModel, $lossy ); 289 290 if ( $converted !== false ) { 291 return $converted; 292 } 293 294 $toHandler = ContentHandler::getForModelID( $toModel ); 295 296 if ( $toHandler instanceof TextContentHandler ) { 297 // NOTE: ignore content serialization format - it's just text anyway. 298 $text = $this->getNativeData(); 299 $converted = $toHandler->unserializeContent( $text ); 300 } 301 302 return $converted; 303 } 304 305 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 14:03:12 2014 | Cross-referenced by PHPXref 0.7.1 |