Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 17
CRAP
0.00% covered (danger)
0.00%
0 / 1
PageConfig
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 17
870
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
 getLinkTarget
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\Parser\ParserOptions;
23use MediaWiki\Revision\RevisionRecord;
24use MediaWiki\Revision\SlotRecord;
25use MediaWiki\Revision\SlotRoleHandler;
26use MediaWiki\Title\Title;
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    public function getContentModel(): string {
72        // @todo Check just the main slot, or all slots, or what?
73        $rev = $this->getRevision();
74        if ( $rev ) {
75            $content = $rev->getContent( SlotRecord::MAIN );
76            if ( $content ) {
77                return $content->getModel();
78            } else {
79                // The page does have a content model but we can't see it. Returning the
80                // default model is not really correct. But we can't see the content either
81                // so it won't matter much what we do here.
82                return $this->slotRoleHandler->getDefaultModel( $this->title );
83            }
84        } else {
85            return $this->slotRoleHandler->getDefaultModel( $this->title );
86        }
87    }
88
89    /** @inheritDoc */
90    public function getLinkTarget(): Title {
91        return $this->title;
92    }
93
94    /** @inheritDoc */
95    public function getPageId(): int {
96        return $this->title->getArticleID();
97    }
98
99    /** @inheritDoc */
100    public function getPageLanguageBcp47(): Bcp47Code {
101        return $this->pageLanguage;
102    }
103
104    /** @inheritDoc */
105    public function getPageLanguageDir(): string {
106        return $this->pageLanguageDir;
107    }
108
109    /**
110     * @internal Used by DataAccess; not part of Parsoid's interface.
111     * @return ParserOptions
112     */
113    public function getParserOptions(): ParserOptions {
114        return $this->parserOptions;
115    }
116
117    /**
118     * Use ParserOptions::getTemplateCallback() to fetch the correct
119     * (usually latest) RevisionRecord for the given title.
120     *
121     * @param Title $title
122     * @return ?RevisionRecord
123     */
124    public function fetchRevisionRecordOfTemplate( Title $title ): ?RevisionRecord {
125        // See Parser::fetchTemplateAndTitle(), but stateless
126        // (Parsoid will track dependencies, etc, itself.)
127        // The callback defaults to Parser::statelessFetchTemplate()
128        $templateCb = $this->parserOptions->getTemplateCallback();
129        $stuff = $templateCb( $title, $this );
130        return $stuff['revision-record'] ?? null;
131    }
132
133    private function getRevision(): ?RevisionRecord {
134        return $this->revision;
135    }
136
137    /** @inheritDoc */
138    public function getRevisionId(): ?int {
139        $rev = $this->getRevision();
140        return $rev ? $rev->getId() : null;
141    }
142
143    /** @inheritDoc */
144    public function getParentRevisionId(): ?int {
145        $rev = $this->getRevision();
146        return $rev ? $rev->getParentId() : null;
147    }
148
149    /** @inheritDoc */
150    public function getRevisionTimestamp(): ?string {
151        $rev = $this->getRevision();
152        return $rev ? $rev->getTimestamp() : null;
153    }
154
155    /** @inheritDoc */
156    public function getRevisionUser(): ?string {
157        $rev = $this->getRevision();
158        $user = $rev ? $rev->getUser() : null;
159        return $user ? $user->getName() : null;
160    }
161
162    /** @inheritDoc */
163    public function getRevisionUserId(): ?int {
164        $rev = $this->getRevision();
165        $user = $rev ? $rev->getUser() : null;
166        return $user ? $user->getId() : null;
167    }
168
169    /** @inheritDoc */
170    public function getRevisionSha1(): ?string {
171        $rev = $this->getRevision();
172        if ( $rev ) {
173            // This matches what the Parsoid/JS gets from the API
174            // FIXME: Maybe we don't need to do this in the future?
175            return \Wikimedia\base_convert( $rev->getSha1(), 36, 16, 40 );
176        } else {
177            return null;
178        }
179    }
180
181    /** @inheritDoc */
182    public function getRevisionSize(): ?int {
183        $rev = $this->getRevision();
184        return $rev ? $rev->getSize() : null;
185    }
186
187    /** @inheritDoc */
188    public function getRevisionContent(): ?IPageContent {
189        $rev = $this->getRevision();
190        return $rev ? new PageContent( $rev ) : null;
191    }
192
193}