| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
namespace MediaWiki\Parser\Parsoid\Config; |
| 21 |
|
| 22 |
use MediaWiki\Languages\LanguageFactory; |
| 23 |
use MediaWiki\Logger\LoggerFactory; |
| 24 |
use MediaWiki\Page\PageIdentity; |
| 25 |
use MediaWiki\Parser\ParserOptions; |
| 26 |
use MediaWiki\Revision\RevisionAccessException; |
| 27 |
use MediaWiki\Revision\RevisionRecord; |
| 28 |
use MediaWiki\Revision\RevisionStore; |
| 29 |
use MediaWiki\Revision\SlotRecord; |
| 30 |
use MediaWiki\Revision\SlotRoleRegistry; |
| 31 |
use MediaWiki\Revision\SuppressedDataException; |
| 32 |
use MediaWiki\Title\Title; |
| 33 |
use MediaWiki\User\UserIdentity; |
| 34 |
use Wikimedia\Bcp47Code\Bcp47Code; |
| 35 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
class PageConfigFactory extends \Wikimedia\Parsoid\Config\PageConfigFactory { |
| 44 |
private RevisionStore $revisionStore; |
| 45 |
private SlotRoleRegistry $slotRoleRegistry; |
| 46 |
private LanguageFactory $languageFactory; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public function __construct( |
| 54 |
RevisionStore $revisionStore, |
| 55 |
SlotRoleRegistry $slotRoleRegistry, |
| 56 |
LanguageFactory $languageFactory |
| 57 |
) { |
| 58 |
$this->revisionStore = $revisionStore; |
| 59 |
$this->slotRoleRegistry = $slotRoleRegistry; |
| 60 |
$this->languageFactory = $languageFactory; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public function create( |
| 84 |
PageIdentity $pageId, |
| 85 |
?UserIdentity $user = null, |
| 86 |
$revision = null, |
| 87 |
?string $unused = null, |
| 88 |
?Bcp47Code $pageLanguageOverride = null, |
| 89 |
bool $ensureAccessibleContent = false |
| 90 |
): \Wikimedia\Parsoid\Config\PageConfig { |
| 91 |
$title = Title::newFromPageIdentity( $pageId ); |
| 92 |
|
| 93 |
if ( $unused !== null ) { |
| 94 |
wfDeprecated( __METHOD__ . ' with non-null 4th arg', '1.40' ); |
| 95 |
} |
| 96 |
|
| 97 |
if ( $revision === null ) { |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
$revisionRecord = $this->revisionStore->getKnownCurrentRevision( |
| 106 |
$title |
| 107 |
) ?: null; |
| 108 |
|
| 109 |
|
| 110 |
} elseif ( !is_int( $revision ) ) { |
| 111 |
$revisionRecord = $revision; |
| 112 |
} else { |
| 113 |
if ( $revision === 0 ) { |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
throw new \UnexpectedValueException( |
| 120 |
"Got revision ID 0 indicating unsaved content. " . |
| 121 |
"Unsaved content must be provided as a RevisionRecord object." |
| 122 |
); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
$revisionRecord = $this->revisionStore->getRevisionById( $revision ); |
| 129 |
if ( $revisionRecord === null ) { |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
$revisionRecord = $this->revisionStore->getRevisionById( |
| 135 |
$revision, IDBAccessObject::READ_LATEST |
| 136 |
); |
| 137 |
$success = ( $revisionRecord !== null ) ? 'success' : 'failure'; |
| 138 |
LoggerFactory::getInstance( 'Parsoid' )->error( |
| 139 |
"Retried revision fetch after failure: {$success}", [ |
| 140 |
'id' => $revision, |
| 141 |
'title' => $title->getPrefixedText(), |
| 142 |
] |
| 143 |
); |
| 144 |
} |
| 145 |
if ( $revisionRecord === null ) { |
| 146 |
throw new RevisionAccessException( "Can't find revision {$revision}" ); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
if ( |
| 153 |
$revisionRecord !== null && |
| 154 |
!$revisionRecord->audienceCan( |
| 155 |
RevisionRecord::DELETED_TEXT, RevisionRecord::FOR_PUBLIC |
| 156 |
) |
| 157 |
) { |
| 158 |
throw new SuppressedDataException( 'Not an available content version.' ); |
| 159 |
} |
| 160 |
|
| 161 |
$parserOptions = |
| 162 |
$user |
| 163 |
? ParserOptions::newFromUser( $user ) |
| 164 |
: ParserOptions::newFromAnon(); |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
$parserOptions->setOption( 'enableLimitReport', false ); |
| 170 |
|
| 171 |
$slotRoleHandler = $this->slotRoleRegistry->getRoleHandler( SlotRecord::MAIN ); |
| 172 |
if ( $pageLanguageOverride ) { |
| 173 |
$pageLanguage = $this->languageFactory->getLanguage( $pageLanguageOverride ); |
| 174 |
$parserOptions->setTargetLanguage( $pageLanguage ); |
| 175 |
} else { |
| 176 |
$pageLanguage = $title->getPageLanguage(); |
| 177 |
} |
| 178 |
|
| 179 |
$pageConfig = new PageConfig( |
| 180 |
$parserOptions, |
| 181 |
$slotRoleHandler, |
| 182 |
$title, |
| 183 |
$revisionRecord, |
| 184 |
$pageLanguage, |
| 185 |
$pageLanguage->getDir() |
| 186 |
); |
| 187 |
|
| 188 |
if ( $ensureAccessibleContent ) { |
| 189 |
if ( $revisionRecord === null ) { |
| 190 |
|
| 191 |
throw new RevisionAccessException( 'The specified revision does not exist.' ); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
$pageConfig->getRevisionContent()->getContent( SlotRecord::MAIN ); |
| 197 |
} |
| 198 |
|
| 199 |
return $pageConfig; |
| 200 |
} |
| 201 |
|
| 202 |
} |