Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
61.80% |
55 / 89 |
|
14.29% |
1 / 7 |
CRAP | |
0.00% |
0 / 1 |
| PageRestHelperFactory | |
61.80% |
55 / 89 |
|
14.29% |
1 / 7 |
22.42 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
1 | |||
| newRevisionContentHelper | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
| newPageContentHelper | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
| newHtmlOutputRendererHelper | |
80.77% |
21 / 26 |
|
0.00% |
0 / 1 |
4.11 | |||
| newHtmlMessageOutputHelper | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| newHtmlInputTransformHelper | |
93.75% |
15 / 16 |
|
0.00% |
0 / 1 |
3.00 | |||
| newPageRedirectHelper | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\Rest\Handler\Helper; |
| 4 | |
| 5 | use MediaWiki\ChangeTags\ChangeTagsStore; |
| 6 | use MediaWiki\Config\ServiceOptions; |
| 7 | use MediaWiki\Content\IContentHandlerFactory; |
| 8 | use MediaWiki\Edit\ParsoidOutputStash; |
| 9 | use MediaWiki\Languages\LanguageConverterFactory; |
| 10 | use MediaWiki\Languages\LanguageFactory; |
| 11 | use MediaWiki\Page\PageIdentity; |
| 12 | use MediaWiki\Page\PageLookup; |
| 13 | use MediaWiki\Page\ParserOutputAccess; |
| 14 | use MediaWiki\Page\RedirectStore; |
| 15 | use MediaWiki\Parser\ParserOptions; |
| 16 | use MediaWiki\Parser\Parsoid\Config\SiteConfig as ParsoidSiteConfig; |
| 17 | use MediaWiki\Parser\Parsoid\HtmlTransformFactory; |
| 18 | use MediaWiki\Permissions\Authority; |
| 19 | use MediaWiki\Rest\RequestInterface; |
| 20 | use MediaWiki\Rest\ResponseFactory; |
| 21 | use MediaWiki\Rest\Router; |
| 22 | use MediaWiki\Revision\RevisionLookup; |
| 23 | use MediaWiki\Revision\RevisionRecord; |
| 24 | use MediaWiki\Revision\RevisionRenderer; |
| 25 | use MediaWiki\Title\TitleFactory; |
| 26 | use MediaWiki\Title\TitleFormatter; |
| 27 | use Wikimedia\Bcp47Code\Bcp47Code; |
| 28 | use Wikimedia\Rdbms\IConnectionProvider; |
| 29 | use Wikimedia\Stats\StatsFactory; |
| 30 | |
| 31 | /** |
| 32 | * @since 1.40 Factory for helper objects designed for sharing logic between REST handlers that deal with page content. |
| 33 | * @unstable during Parsoid migration |
| 34 | */ |
| 35 | class PageRestHelperFactory { |
| 36 | |
| 37 | /** |
| 38 | * @internal |
| 39 | */ |
| 40 | public const CONSTRUCTOR_OPTIONS = PageContentHelper::CONSTRUCTOR_OPTIONS; |
| 41 | |
| 42 | private ServiceOptions $options; |
| 43 | private RevisionLookup $revisionLookup; |
| 44 | private RevisionRenderer $revisionRenderer; |
| 45 | private TitleFormatter $titleFormatter; |
| 46 | private PageLookup $pageLookup; |
| 47 | private ParsoidOutputStash $parsoidOutputStash; |
| 48 | private ParserOutputAccess $parserOutputAccess; |
| 49 | private ParsoidSiteConfig $parsoidSiteConfig; |
| 50 | private HtmlTransformFactory $htmlTransformFactory; |
| 51 | private IContentHandlerFactory $contentHandlerFactory; |
| 52 | private LanguageFactory $languageFactory; |
| 53 | private RedirectStore $redirectStore; |
| 54 | private LanguageConverterFactory $languageConverterFactory; |
| 55 | private TitleFactory $titleFactory; |
| 56 | private IConnectionProvider $dbProvider; |
| 57 | private ChangeTagsStore $changeTagsStore; |
| 58 | private StatsFactory $statsFactory; |
| 59 | |
| 60 | public function __construct( |
| 61 | ServiceOptions $options, |
| 62 | RevisionLookup $revisionLookup, |
| 63 | RevisionRenderer $revisionRenderer, |
| 64 | TitleFormatter $titleFormatter, |
| 65 | PageLookup $pageLookup, |
| 66 | ParsoidOutputStash $parsoidOutputStash, |
| 67 | ParserOutputAccess $parserOutputAccess, |
| 68 | ParsoidSiteConfig $parsoidSiteConfig, |
| 69 | HtmlTransformFactory $htmlTransformFactory, |
| 70 | IContentHandlerFactory $contentHandlerFactory, |
| 71 | LanguageFactory $languageFactory, |
| 72 | RedirectStore $redirectStore, |
| 73 | LanguageConverterFactory $languageConverterFactory, |
| 74 | TitleFactory $titleFactory, |
| 75 | IConnectionProvider $dbProvider, |
| 76 | ChangeTagsStore $changeTagsStore, |
| 77 | StatsFactory $statsFactory |
| 78 | ) { |
| 79 | $this->options = $options; |
| 80 | $this->revisionLookup = $revisionLookup; |
| 81 | $this->revisionRenderer = $revisionRenderer; |
| 82 | $this->titleFormatter = $titleFormatter; |
| 83 | $this->pageLookup = $pageLookup; |
| 84 | $this->parsoidOutputStash = $parsoidOutputStash; |
| 85 | $this->parserOutputAccess = $parserOutputAccess; |
| 86 | $this->parsoidSiteConfig = $parsoidSiteConfig; |
| 87 | $this->htmlTransformFactory = $htmlTransformFactory; |
| 88 | $this->contentHandlerFactory = $contentHandlerFactory; |
| 89 | $this->languageFactory = $languageFactory; |
| 90 | $this->redirectStore = $redirectStore; |
| 91 | $this->languageConverterFactory = $languageConverterFactory; |
| 92 | $this->statsFactory = $statsFactory; |
| 93 | $this->titleFactory = $titleFactory; |
| 94 | $this->dbProvider = $dbProvider; |
| 95 | $this->changeTagsStore = $changeTagsStore; |
| 96 | } |
| 97 | |
| 98 | public function newRevisionContentHelper(): RevisionContentHelper { |
| 99 | return new RevisionContentHelper( |
| 100 | $this->options, |
| 101 | $this->revisionLookup, |
| 102 | $this->titleFormatter, |
| 103 | $this->pageLookup, |
| 104 | $this->titleFactory, |
| 105 | $this->dbProvider, |
| 106 | $this->changeTagsStore |
| 107 | ); |
| 108 | } |
| 109 | |
| 110 | public function newPageContentHelper(): PageContentHelper { |
| 111 | return new PageContentHelper( |
| 112 | $this->options, |
| 113 | $this->revisionLookup, |
| 114 | $this->titleFormatter, |
| 115 | $this->pageLookup, |
| 116 | $this->titleFactory, |
| 117 | $this->dbProvider, |
| 118 | $this->changeTagsStore |
| 119 | ); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Should we ignore page id mismatches between page and revision objects |
| 124 | * in HTML/pagebundle requests? Mismatches arise because of page moves. |
| 125 | * This is recommended only for handling calls to internal APIs. |
| 126 | * @note Since 1.43, passing 'null' for $page has been deprecated. |
| 127 | * @note Since 1.43, passing 'null' for $authority has been deprecated. |
| 128 | * @note Since 1.43, passing $lenientRevHandling as the first parameter |
| 129 | * has been deprecated. |
| 130 | * @param bool|PageIdentity|null $page |
| 131 | * If `false`, this argument is used as the value for $lenientRevHandling, |
| 132 | * for backward-compatibility. |
| 133 | * @param array $parameters |
| 134 | * @param ?Authority $authority |
| 135 | * @param int|RevisionRecord|null $revision |
| 136 | * @param bool $lenientRevHandling |
| 137 | * @param ParserOptions|null $parserOptions |
| 138 | * @return HtmlOutputRendererHelper |
| 139 | */ |
| 140 | public function newHtmlOutputRendererHelper( |
| 141 | $page = null, |
| 142 | array $parameters = [], |
| 143 | ?Authority $authority = null, |
| 144 | $revision = null, |
| 145 | bool $lenientRevHandling = false, |
| 146 | ?ParserOptions $parserOptions = null |
| 147 | ): HtmlOutputRendererHelper { |
| 148 | if ( is_bool( $page ) ) { |
| 149 | // Backward compatibility w/ pre-1.43 (deprecated) |
| 150 | $lenientRevHandling = $page; |
| 151 | $page = null; |
| 152 | wfDeprecated( __METHOD__ . ' with boolean first parameter', '1.43' ); |
| 153 | } |
| 154 | if ( $page === null ) { |
| 155 | wfDeprecated( __METHOD__ . ' with null $page', '1.43' ); |
| 156 | } |
| 157 | if ( $authority === null ) { |
| 158 | wfDeprecated( __METHOD__ . ' with null $authority', '1.43' ); |
| 159 | } |
| 160 | return new HtmlOutputRendererHelper( |
| 161 | $this->parsoidOutputStash, |
| 162 | $this->statsFactory, |
| 163 | $this->parserOutputAccess, |
| 164 | $this->pageLookup, |
| 165 | $this->revisionLookup, |
| 166 | $this->revisionRenderer, |
| 167 | $this->parsoidSiteConfig, |
| 168 | $this->htmlTransformFactory, |
| 169 | $this->contentHandlerFactory, |
| 170 | $this->languageFactory, |
| 171 | $page, |
| 172 | $parameters, |
| 173 | $authority, |
| 174 | $revision, |
| 175 | $lenientRevHandling, |
| 176 | $parserOptions |
| 177 | ); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * @note Since 1.43, passing a null $page is deprecated. |
| 182 | */ |
| 183 | public function newHtmlMessageOutputHelper( ?PageIdentity $page = null ): HtmlMessageOutputHelper { |
| 184 | if ( $page === null ) { |
| 185 | wfDeprecated( __METHOD__ . ' with null $page', '1.43' ); |
| 186 | } |
| 187 | return new HtmlMessageOutputHelper( $page ); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * @param array $envOptions |
| 192 | * @param ?PageIdentity $page |
| 193 | * @param array|string|null $body |
| 194 | * @param array $parameters |
| 195 | * @param RevisionRecord|null $originalRevision |
| 196 | * @param Bcp47Code|null $pageLanguage |
| 197 | */ |
| 198 | public function newHtmlInputTransformHelper( |
| 199 | $envOptions = [], |
| 200 | ?PageIdentity $page = null, |
| 201 | $body = null, |
| 202 | array $parameters = [], |
| 203 | ?RevisionRecord $originalRevision = null, |
| 204 | ?Bcp47Code $pageLanguage = null |
| 205 | ): HtmlInputTransformHelper { |
| 206 | if ( $page === null || $body === null ) { |
| 207 | wfDeprecated( __METHOD__ . ' without $page or $body' ); |
| 208 | } |
| 209 | return new HtmlInputTransformHelper( |
| 210 | $this->statsFactory, |
| 211 | $this->htmlTransformFactory, |
| 212 | $this->parsoidOutputStash, |
| 213 | $this->parserOutputAccess, |
| 214 | $this->pageLookup, |
| 215 | $this->revisionLookup, |
| 216 | $envOptions, |
| 217 | $page, |
| 218 | $body ?? '', |
| 219 | $parameters, |
| 220 | $originalRevision, |
| 221 | $pageLanguage |
| 222 | ); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * @since 1.41 |
| 227 | */ |
| 228 | public function newPageRedirectHelper( |
| 229 | ResponseFactory $responseFactory, |
| 230 | Router $router, |
| 231 | string $route, |
| 232 | RequestInterface $request |
| 233 | ): PageRedirectHelper { |
| 234 | return new PageRedirectHelper( |
| 235 | $this->redirectStore, |
| 236 | $this->titleFormatter, |
| 237 | $responseFactory, |
| 238 | $router, |
| 239 | $route, |
| 240 | $request, |
| 241 | $this->languageConverterFactory |
| 242 | ); |
| 243 | } |
| 244 | |
| 245 | } |