Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 44
0.00% covered (danger)
0.00%
0 / 19
CRAP
0.00% covered (danger)
0.00%
0 / 1
PageConfig
0.00% covered (danger)
0.00%
0 / 44
0.00% covered (danger)
0.00%
0 / 19
992
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 getContentModel
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 getTitle
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getLinkTarget
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getNs
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getPageId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getPageLanguageBcp47
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getPageLanguageDir
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getParserOptions
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 fetchRevisionRecordOfTemplate
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getRevision
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getRevisionId
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 getParentRevisionId
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 getRevisionTimestamp
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 getRevisionUser
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
 getRevisionUserId
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
 getRevisionSha1
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getRevisionSize
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 getRevisionContent
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * Copyright (C) 2011-2022 Wikimedia Foundation and others.
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 */
19
20namespace MediaWiki\Parser\Parsoid\Config;
21
22use MediaWiki\Revision\RevisionRecord;
23use MediaWiki\Revision\SlotRecord;
24use MediaWiki\Revision\SlotRoleHandler;
25use MediaWiki\Title\Title;
26use ParserOptions;
27use Wikimedia\Bcp47Code\Bcp47Code;
28use Wikimedia\Parsoid\Config\PageConfig as IPageConfig;
29use Wikimedia\Parsoid\Config\PageContent as IPageContent;
30
31/**
32 * Page-level configuration interface for Parsoid
33 *
34 * This is effectively "Parsoid's view of ParserOptions".
35 *
36 * @since 1.39
37 * @internal
38 */
39class PageConfig extends IPageConfig {
40    private ParserOptions $parserOptions;
41    private SlotRoleHandler $slotRoleHandler;
42    private Title $title;
43    private ?RevisionRecord $revision = null;
44    private Bcp47Code $pageLanguage;
45    private string $pageLanguageDir;
46
47    /**
48     * @param ParserOptions $parserOptions
49     * @param SlotRoleHandler $slotRoleHandler
50     * @param Title $title Title being parsed
51     * @param ?RevisionRecord $revision
52     * @param Bcp47Code $pageLanguage
53     * @param string $pageLanguageDir
54     */
55    public function __construct(
56        ParserOptions $parserOptions,
57        SlotRoleHandler $slotRoleHandler,
58        Title $title,
59        ?RevisionRecord $revision,
60        Bcp47Code $pageLanguage,
61        string $pageLanguageDir
62    ) {
63        $this->parserOptions = $parserOptions;
64        $this->slotRoleHandler = $slotRoleHandler;
65        $this->title = $title;
66        $this->revision = $revision;
67        $this->pageLanguage = $pageLanguage;
68        $this->pageLanguageDir = $pageLanguageDir;
69    }
70
71    /**
72     * Get content model
73     * @return string
74     */
75    public function getContentModel(): string {
76        // @todo Check just the main slot, or all slots, or what?
77        $rev = $this->getRevision();
78        if ( $rev ) {
79            $content = $rev->getContent( SlotRecord::MAIN );
80            if ( $content ) {
81                return $content->getModel();
82            } else {
83                // The page does have a content model but we can't see it. Returning the
84                // default model is not really correct. But we can't see the content either
85                // so it won't matter much what we do here.
86                return $this->slotRoleHandler->getDefaultModel( $this->title );
87            }
88        } else {
89            return $this->slotRoleHandler->getDefaultModel( $this->title );
90        }
91    }
92
93    /** @inheritDoc */
94    public function getTitle(): string {
95        return $this->title->getPrefixedText();
96    }
97
98    /** @inheritDoc */
99    public function getLinkTarget(): Title {
100        return $this->title;
101    }
102
103    /** @inheritDoc */
104    public function getNs(): int {
105        return $this->title->getNamespace();
106    }
107
108    /** @inheritDoc */
109    public function getPageId(): int {
110        return $this->title->getArticleID();
111    }
112
113    /** @inheritDoc */
114    public function getPageLanguageBcp47(): Bcp47Code {
115        return $this->pageLanguage;
116    }
117
118    /** @inheritDoc */
119    public function getPageLanguageDir(): string {
120        return $this->pageLanguageDir;
121    }
122
123    /**
124     * @internal Used by DataAccess; not part of Parsoid's interface.
125     * @return ParserOptions
126     */
127    public function getParserOptions(): ParserOptions {
128        return $this->parserOptions;
129    }
130
131    /**
132     * Use ParserOptions::getTemplateCallback() to fetch the correct
133     * (usually latest) RevisionRecord for the given title.
134     *
135     * @param Title $title
136     * @return ?RevisionRecord
137     */
138    public function fetchRevisionRecordOfTemplate( Title $title ): ?RevisionRecord {
139        // See Parser::fetchTemplateAndTitle(), but stateless
140        // (Parsoid will track dependencies, etc, itself.)
141        // The callback defaults to Parser::statelessFetchTemplate()
142        $templateCb = $this->parserOptions->getTemplateCallback();
143        $stuff = $templateCb( $title, $this );
144        return $stuff['revision-record'] ?? null;
145    }
146
147    /**
148     * @return ?RevisionRecord
149     */
150    private function getRevision(): ?RevisionRecord {
151        return $this->revision;
152    }
153
154    /** @inheritDoc */
155    public function getRevisionId(): ?int {
156        $rev = $this->getRevision();
157        return $rev ? $rev->getId() : null;
158    }
159
160    /** @inheritDoc */
161    public function getParentRevisionId(): ?int {
162        $rev = $this->getRevision();
163        return $rev ? $rev->getParentId() : null;
164    }
165
166    /** @inheritDoc */
167    public function getRevisionTimestamp(): ?string {
168        $rev = $this->getRevision();
169        return $rev ? $rev->getTimestamp() : null;
170    }
171
172    /** @inheritDoc */
173    public function getRevisionUser(): ?string {
174        $rev = $this->getRevision();
175        $user = $rev ? $rev->getUser() : null;
176        return $user ? $user->getName() : null;
177    }
178
179    /** @inheritDoc */
180    public function getRevisionUserId(): ?int {
181        $rev = $this->getRevision();
182        $user = $rev ? $rev->getUser() : null;
183        return $user ? $user->getId() : null;
184    }
185
186    /** @inheritDoc */
187    public function getRevisionSha1(): ?string {
188        $rev = $this->getRevision();
189        if ( $rev ) {
190            // This matches what the Parsoid/JS gets from the API
191            // FIXME: Maybe we don't need to do this in the future?
192            return \Wikimedia\base_convert( $rev->getSha1(), 36, 16, 40 );
193        } else {
194            return null;
195        }
196    }
197
198    /** @inheritDoc */
199    public function getRevisionSize(): ?int {
200        $rev = $this->getRevision();
201        return $rev ? $rev->getSize() : null;
202    }
203
204    /** @inheritDoc */
205    public function getRevisionContent(): ?IPageContent {
206        $rev = $this->getRevision();
207        return $rev ? new PageContent( $rev ) : null;
208    }
209
210}