| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
namespace MediaWiki\Content; |
| 29 |
|
| 30 |
use InvalidArgumentException; |
| 31 |
use MediaWiki\Context\RequestContext; |
| 32 |
use MediaWiki\HookContainer\HookRunner; |
| 33 |
use MediaWiki\MainConfigNames; |
| 34 |
use MediaWiki\MediaWikiServices; |
| 35 |
use MediaWiki\Parser\MagicWord; |
| 36 |
use MediaWiki\Title\Title; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
class WikitextContent extends TextContent { |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
private $preSaveTransformFlags = []; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public function __construct( $text ) { |
| 57 |
parent::__construct( $text, CONTENT_MODEL_WIKITEXT ); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public function getSection( $sectionId ) { |
| 68 |
$text = $this->getText(); |
| 69 |
$sect = MediaWikiServices::getInstance()->getParserFactory()->getInstance() |
| 70 |
->getSection( $text, $sectionId, false ); |
| 71 |
|
| 72 |
if ( $sect === false ) { |
| 73 |
return false; |
| 74 |
} else { |
| 75 |
return new static( $sect ); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public function replaceSection( $sectionId, Content $with, $sectionTitle = '' ) { |
| 89 |
|
| 90 |
$myModelId = $this->getModel(); |
| 91 |
$sectionModelId = $with->getModel(); |
| 92 |
|
| 93 |
if ( $sectionModelId != $myModelId ) { |
| 94 |
throw new InvalidArgumentException( "Incompatible content model for section: " . |
| 95 |
"document uses $myModelId but " . |
| 96 |
"section uses $sectionModelId." ); |
| 97 |
} |
| 98 |
|
| 99 |
'@phan-var self $with'; |
| 100 |
|
| 101 |
$oldtext = $this->getText(); |
| 102 |
$text = $with->getText(); |
| 103 |
|
| 104 |
if ( strval( $sectionId ) === '' ) { |
| 105 |
return $with; |
| 106 |
} |
| 107 |
|
| 108 |
if ( $sectionId === 'new' ) { |
| 109 |
|
| 110 |
$subject = strval( $sectionTitle ) !== '' ? wfMessage( 'newsectionheaderdefaultlevel' ) |
| 111 |
->plaintextParams( $sectionTitle )->inContentLanguage()->text() . "\n\n" : ''; |
| 112 |
$hookRunner = ( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) ); |
| 113 |
if ( $hookRunner->onPlaceNewSection( $this, $oldtext, $subject, $text ) ) { |
| 114 |
$text = strlen( trim( $oldtext ) ) > 0 |
| 115 |
? "{$oldtext}\n\n{$subject}{$text}" |
| 116 |
: "{$subject}{$text}"; |
| 117 |
} |
| 118 |
} else { |
| 119 |
|
| 120 |
$text = MediaWikiServices::getInstance()->getParserFactory()->getInstance() |
| 121 |
->replaceSection( $oldtext, $sectionId, $text ); |
| 122 |
} |
| 123 |
|
| 124 |
$newContent = new static( $text ); |
| 125 |
|
| 126 |
return $newContent; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public function addSectionHeader( $header ) { |
| 138 |
$text = strval( $header ) !== '' ? wfMessage( 'newsectionheaderdefaultlevel' ) |
| 139 |
->plaintextParams( $header )->inContentLanguage()->text() . "\n\n" : ''; |
| 140 |
$text .= $this->getText(); |
| 141 |
|
| 142 |
return new static( $text ); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public function getRedirectTargetAndText() { |
| 154 |
wfDeprecated( __METHOD__, '1.41' ); |
| 155 |
|
| 156 |
$handler = $this->getContentHandler(); |
| 157 |
[ $target, $content ] = $handler->extractRedirectTargetAndText( $this ); |
| 158 |
|
| 159 |
return [ Title::castFromLinkTarget( $target ), $content->getText() ]; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
public function getRedirectTarget() { |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
$handler = $this->getContentHandler(); |
| 174 |
[ $target, ] = $handler->extractRedirectTargetAndText( $this ); |
| 175 |
|
| 176 |
return Title::castFromLinkTarget( $target ); |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
public function updateRedirect( Title $target ) { |
| 192 |
if ( !$this->isRedirect() ) { |
| 193 |
return $this; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
$newText = preg_replace( '/ \[ \[ [^\]]* \] \] /x', |
| 200 |
'[[' . $target->getFullText() . ']]', |
| 201 |
$this->getText(), 1 ); |
| 202 |
|
| 203 |
return new static( $newText ); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
public function isCountable( $hasLinks = null, ?Title $title = null ) { |
| 218 |
$articleCountMethod = MediaWikiServices::getInstance()->getMainConfig() |
| 219 |
->get( MainConfigNames::ArticleCountMethod ); |
| 220 |
|
| 221 |
if ( $this->isRedirect() ) { |
| 222 |
return false; |
| 223 |
} |
| 224 |
|
| 225 |
if ( $articleCountMethod === 'link' ) { |
| 226 |
if ( $hasLinks === null ) { |
| 227 |
|
| 228 |
if ( !$title ) { |
| 229 |
$context = RequestContext::getMain(); |
| 230 |
$title = $context->getTitle(); |
| 231 |
} |
| 232 |
$contentRenderer = MediaWikiServices::getInstance()->getContentRenderer(); |
| 233 |
|
| 234 |
$po = $contentRenderer->getParserOutput( $this, $title, null, null, false ); |
| 235 |
$links = $po->getLinks(); |
| 236 |
$hasLinks = $links !== []; |
| 237 |
} |
| 238 |
|
| 239 |
return $hasLinks; |
| 240 |
} |
| 241 |
|
| 242 |
return true; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
public function getTextForSummary( $maxlength = 250 ) { |
| 251 |
$truncatedtext = parent::getTextForSummary( $maxlength ); |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
$truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext ); |
| 257 |
|
| 258 |
return $truncatedtext; |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
public function matchMagicWord( MagicWord $word ) { |
| 271 |
return $word->match( $this->getText() ); |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
public function setPreSaveTransformFlags( array $flags ) { |
| 282 |
$this->preSaveTransformFlags = $flags; |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
public function getPreSaveTransformFlags() { |
| 292 |
return $this->preSaveTransformFlags; |
| 293 |
} |
| 294 |
|
| 295 |
public function getContentHandler(): WikitextContentHandler { |
| 296 |
$handler = parent::getContentHandler(); |
| 297 |
'@phan-var WikitextContentHandler $handler'; |
| 298 |
|
| 299 |
return $handler; |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
class_alias( WikitextContent::class, 'WikitextContent' ); |