Debian Codegraph @

includes/parser/Parsoid/Config/PageConfig.php

raw symbols SCIP
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
20 namespace MediaWiki\Parser\Parsoid\Config;
21
22 use MediaWiki\Parser\ParserOptions;
23 use MediaWiki\Revision\RevisionRecord;
24 use MediaWiki\Revision\SlotRecord;
25 use MediaWiki\Revision\SlotRoleHandler;
26 use MediaWiki\Title\Title;
27 use Wikimedia\Bcp47Code\Bcp47Code;
28 use Wikimedia\Parsoid\Config\PageConfig as IPageConfig;
29 use 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 */
39 class 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 getLinkTarget(): Title {
95 return $this->title;
96 }
97
98 /** @inheritDoc */
99 public function getPageId(): int {
100 return $this->title->getArticleID();
101 }
102
103 /** @inheritDoc */
104 public function getPageLanguageBcp47(): Bcp47Code {
105 return $this->pageLanguage;
106 }
107
108 /** @inheritDoc */
109 public function getPageLanguageDir(): string {
110 return $this->pageLanguageDir;
111 }
112
113 /**
114 * @internal Used by DataAccess; not part of Parsoid's interface.
115 * @return ParserOptions
116 */
117 public function getParserOptions(): ParserOptions {
118 return $this->parserOptions;
119 }
120
121 /**
122 * Use ParserOptions::getTemplateCallback() to fetch the correct
123 * (usually latest) RevisionRecord for the given title.
124 *
125 * @param Title $title
126 * @return ?RevisionRecord
127 */
128 public function fetchRevisionRecordOfTemplate( Title $title ): ?RevisionRecord {
129 // See Parser::fetchTemplateAndTitle(), but stateless
130 // (Parsoid will track dependencies, etc, itself.)
131 // The callback defaults to Parser::statelessFetchTemplate()
132 $templateCb = $this->parserOptions->getTemplateCallback();
133 $stuff = $templateCb( $title, $this );
134 return $stuff['revision-record'] ?? null;
135 }
136
137 /**
138 * @return ?RevisionRecord
139 */
140 private function getRevision(): ?RevisionRecord {
141 return $this->revision;
142 }
143
144 /** @inheritDoc */
145 public function getRevisionId(): ?int {
146 $rev = $this->getRevision();
147 return $rev ? $rev->getId() : null;
148 }
149
150 /** @inheritDoc */
151 public function getParentRevisionId(): ?int {
152 $rev = $this->getRevision();
153 return $rev ? $rev->getParentId() : null;
154 }
155
156 /** @inheritDoc */
157 public function getRevisionTimestamp(): ?string {
158 $rev = $this->getRevision();
159 return $rev ? $rev->getTimestamp() : null;
160 }
161
162 /** @inheritDoc */
163 public function getRevisionUser(): ?string {
164 $rev = $this->getRevision();
165 $user = $rev ? $rev->getUser() : null;
166 return $user ? $user->getName() : null;
167 }
168
169 /** @inheritDoc */
170 public function getRevisionUserId(): ?int {
171 $rev = $this->getRevision();
172 $user = $rev ? $rev->getUser() : null;
173 return $user ? $user->getId() : null;
174 }
175
176 /** @inheritDoc */
177 public function getRevisionSha1(): ?string {
178 $rev = $this->getRevision();
179 if ( $rev ) {
180 // This matches what the Parsoid/JS gets from the API
181 // FIXME: Maybe we don't need to do this in the future?
182 return \Wikimedia\base_convert( $rev->getSha1(), 36, 16, 40 );
183 } else {
184 return null;
185 }
186 }
187
188 /** @inheritDoc */
189 public function getRevisionSize(): ?int {
190 $rev = $this->getRevision();
191 return $rev ? $rev->getSize() : null;
192 }
193
194 /** @inheritDoc */
195 public function getRevisionContent(): ?IPageContent {
196 $rev = $this->getRevision();
197 return $rev ? new PageContent( $rev ) : null;
198 }
199
200 }