| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\EditPage; |
| 4 |
|
| 5 |
use MediaWiki\Content\Content; |
| 6 |
use MediaWiki\Content\IContentHandlerFactory; |
| 7 |
use MediaWiki\Content\Transform\ContentTransformer; |
| 8 |
use MediaWiki\HookContainer\HookContainer; |
| 9 |
use MediaWiki\HookContainer\HookRunner; |
| 10 |
use MediaWiki\Page\PageReference; |
| 11 |
use MediaWiki\Page\ProperPageIdentity; |
| 12 |
use MediaWiki\Page\RedirectLookup; |
| 13 |
use MediaWiki\Page\WikiPageFactory; |
| 14 |
use MediaWiki\Parser\ParserOptions; |
| 15 |
use MediaWiki\Permissions\Authority; |
| 16 |
use MediaWiki\Revision\RevisionRecord; |
| 17 |
use MediaWiki\SpecialPage\SpecialPageFactory; |
| 18 |
use MediaWiki\Title\Title; |
| 19 |
use MessageCache; |
| 20 |
use Wikimedia\Assert\Assert; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
class PreloadedContentBuilder { |
| 31 |
|
| 32 |
use ParametersHelper; |
| 33 |
|
| 34 |
private IContentHandlerFactory $contentHandlerFactory; |
| 35 |
private WikiPageFactory $wikiPageFactory; |
| 36 |
private RedirectLookup $redirectLookup; |
| 37 |
private SpecialPageFactory $specialPageFactory; |
| 38 |
private ContentTransformer $contentTransformer; |
| 39 |
private HookRunner $hookRunner; |
| 40 |
|
| 41 |
public function __construct( |
| 42 |
IContentHandlerFactory $contentHandlerFactory, |
| 43 |
WikiPageFactory $wikiPageFactory, |
| 44 |
RedirectLookup $redirectLookup, |
| 45 |
SpecialPageFactory $specialPageFactory, |
| 46 |
ContentTransformer $contentTransformer, |
| 47 |
HookContainer $hookContainer |
| 48 |
) { |
| 49 |
|
| 50 |
$this->contentHandlerFactory = $contentHandlerFactory; |
| 51 |
$this->wikiPageFactory = $wikiPageFactory; |
| 52 |
$this->redirectLookup = $redirectLookup; |
| 53 |
$this->specialPageFactory = $specialPageFactory; |
| 54 |
$this->contentTransformer = $contentTransformer; |
| 55 |
$this->hookRunner = new HookRunner( $hookContainer ); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public function getPreloadedContent( |
| 70 |
ProperPageIdentity $page, |
| 71 |
Authority $performer, |
| 72 |
?string $preload, |
| 73 |
array $preloadParams, |
| 74 |
?string $section |
| 75 |
): Content { |
| 76 |
Assert::parameterElementType( 'string', $preloadParams, '$preloadParams' ); |
| 77 |
|
| 78 |
$content = null; |
| 79 |
if ( $section !== 'new' ) { |
| 80 |
$content = $this->getDefaultContent( $page ); |
| 81 |
} |
| 82 |
if ( $content === null ) { |
| 83 |
if ( ( $preload === null || $preload === '' ) && $section === 'new' ) { |
| 84 |
|
| 85 |
$preload = 'MediaWiki:addsection-preload'; |
| 86 |
} |
| 87 |
$content = $this->getPreloadedContentFromParams( $page, $performer, $preload, $preloadParams ); |
| 88 |
} |
| 89 |
$title = Title::newFromPageIdentity( $page ); |
| 90 |
if ( !$title->getArticleID() ) { |
| 91 |
$contentModel = $title->getContentModel(); |
| 92 |
$contentHandler = $this->contentHandlerFactory->getContentHandler( $contentModel ); |
| 93 |
$contentFormat = $contentHandler->getDefaultFormat(); |
| 94 |
$text = $contentHandler->serializeContent( $content, $contentFormat ); |
| 95 |
$this->hookRunner->onEditFormPreloadText( $text, $title ); |
| 96 |
$content = $contentHandler->unserializeContent( $text, $contentFormat ); |
| 97 |
} |
| 98 |
return $content; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public function getDefaultContent( ProperPageIdentity $page ): ?Content { |
| 112 |
$title = Title::newFromPageIdentity( $page ); |
| 113 |
$contentModel = $title->getContentModel(); |
| 114 |
$contentHandler = $this->contentHandlerFactory->getContentHandler( $contentModel ); |
| 115 |
$contentFormat = $contentHandler->getDefaultFormat(); |
| 116 |
if ( $title->getNamespace() === NS_MEDIAWIKI ) { |
| 117 |
|
| 118 |
$text = $title->getDefaultMessageText(); |
| 119 |
if ( $text !== false ) { |
| 120 |
return $contentHandler->unserializeContent( $text, $contentFormat ); |
| 121 |
} |
| 122 |
} |
| 123 |
return null; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
private function getPreloadedContentFromParams( |
| 136 |
ProperPageIdentity $contextPage, |
| 137 |
Authority $performer, |
| 138 |
?string $preload, |
| 139 |
array $preloadParams |
| 140 |
): Content { |
| 141 |
$contextTitle = Title::newFromPageIdentity( $contextPage ); |
| 142 |
$contentModel = $contextTitle->getContentModel(); |
| 143 |
$handler = $this->contentHandlerFactory->getContentHandler( $contentModel ); |
| 144 |
|
| 145 |
|
| 146 |
if ( !$handler->supportsPreloadContent() || $preload === null || $preload === '' ) { |
| 147 |
return $handler->makeEmptyContent(); |
| 148 |
} |
| 149 |
|
| 150 |
$title = Title::newFromText( $preload ); |
| 151 |
|
| 152 |
if ( $title && $title->getNamespace() == NS_MEDIAWIKI ) { |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
$msg = wfMessage( MessageCache::normalizeKey( $title->getText() ) ); |
| 157 |
|
| 158 |
if ( $msg->isDisabled() ) { |
| 159 |
|
| 160 |
return $handler->makeEmptyContent(); |
| 161 |
} |
| 162 |
|
| 163 |
return $this->transform( |
| 164 |
$handler->unserializeContent( $msg |
| 165 |
->page( $title ) |
| 166 |
->params( $preloadParams ) |
| 167 |
->inContentLanguage() |
| 168 |
->plain() |
| 169 |
), |
| 170 |
$title |
| 171 |
); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
$title = $this->getTargetTitleIfSpecialMyLanguage( $title ); |
| 177 |
|
| 178 |
|
| 179 |
if ( !$this->isPageExistingAndViewable( $title, $performer ) ) { |
| 180 |
|
| 181 |
return $handler->makeEmptyContent(); |
| 182 |
} |
| 183 |
|
| 184 |
$page = $this->wikiPageFactory->newFromTitle( $title ); |
| 185 |
if ( $page->isRedirect() ) { |
| 186 |
$redirTarget = $this->redirectLookup->getRedirectTarget( $title ); |
| 187 |
$redirTarget = Title::castFromLinkTarget( $redirTarget ); |
| 188 |
|
| 189 |
if ( !$this->isPageExistingAndViewable( $redirTarget, $performer ) ) { |
| 190 |
|
| 191 |
return $handler->makeEmptyContent(); |
| 192 |
} |
| 193 |
$page = $this->wikiPageFactory->newFromTitle( $redirTarget ); |
| 194 |
} |
| 195 |
|
| 196 |
$content = $page->getContent( RevisionRecord::RAW ); |
| 197 |
|
| 198 |
if ( !$content ) { |
| 199 |
|
| 200 |
return $handler->makeEmptyContent(); |
| 201 |
} |
| 202 |
|
| 203 |
if ( $content->getModel() !== $handler->getModelID() ) { |
| 204 |
$converted = $content->convert( $handler->getModelID() ); |
| 205 |
|
| 206 |
if ( !$converted ) { |
| 207 |
|
| 208 |
wfDebug( "Attempt to preload incompatible content: " . |
| 209 |
"can't convert " . $content->getModel() . |
| 210 |
" to " . $handler->getModelID() ); |
| 211 |
|
| 212 |
return $handler->makeEmptyContent(); |
| 213 |
} |
| 214 |
|
| 215 |
$content = $converted; |
| 216 |
} |
| 217 |
return $this->transform( $content, $title, $preloadParams ); |
| 218 |
} |
| 219 |
|
| 220 |
private function transform( |
| 221 |
Content $content, |
| 222 |
PageReference $title, |
| 223 |
array $preloadParams = [] |
| 224 |
) { |
| 225 |
return $this->contentTransformer->preloadTransform( |
| 226 |
$content, |
| 227 |
$title, |
| 228 |
|
| 229 |
ParserOptions::newFromAnon(), |
| 230 |
$preloadParams |
| 231 |
); |
| 232 |
} |
| 233 |
} |