Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
DataAccess
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 2
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getPageInfo
n/a
0 / 0
n/a
0 / 0
0
 getFileInfo
n/a
0 / 0
n/a
0 / 0
0
 getExternalUrlInfo
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
20
 parseWikitextWithTitle
n/a
0 / 0
n/a
0 / 0
0
 preprocessWikitext
n/a
0 / 0
n/a
0 / 0
0
 fetchTemplateSource
n/a
0 / 0
n/a
0 / 0
0
 fetchTemplateData
n/a
0 / 0
n/a
0 / 0
0
 logLinterData
n/a
0 / 0
n/a
0 / 0
0
 addTrackingCategory
n/a
0 / 0
n/a
0 / 0
0
1<?php
2declare( strict_types = 1 );
3
4namespace Wikimedia\Parsoid\Config;
5
6use Wikimedia\Parsoid\Core\ContentMetadataCollector;
7use Wikimedia\Parsoid\Core\LinkTarget;
8use Wikimedia\Parsoid\Ext\ParsoidExtensionAPI;
9use Wikimedia\Parsoid\Fragments\PFragment;
10use Wikimedia\Parsoid\Utils\UrlUtils;
11
12/**
13 * MediaWiki data access abstract class for Parsoid
14 */
15abstract class DataAccess {
16    /**
17     * Base constructor.
18     *
19     * This constructor is public because it is used to create mock objects
20     * in our test suite.
21     */
22    public function __construct() {
23    }
24
25    /**
26     * Return target data for formatting links.
27     *
28     * Replaces Batcher.getPageProps()
29     *
30     * @param PageConfig|LinkTarget $pageConfigOrTitle
31     *  Either a PageConfig or else just the context title from the PageConfig
32     *  (as a LinkTarget)
33     * @param string[] $titles
34     * @param bool $defaultLinkCaption Whether the links have default captions. This flag
35     *  may affect the link classes returned. For definition of "default caption", see
36     *  LinkRenderer::isDefaultLinkCaption() in MediaWiki core.
37     * @return array<string,array> [ string Title => array ], where the array contains
38     *  - pageId: (int|null) Page ID
39     *  - revId: (int|null) Current revision of the page
40     *  - missing: (bool) Whether the page is missing
41     *  - known: (bool) Whether the special page is known
42     *  - redirect: (bool) Whether the page is a redirect
43     *  - linkclasses: (string[]) Extensible "link color" information; see
44     *      ApiQueryInfo::getLinkClasses() in MediaWiki core
45     */
46    abstract public function getPageInfo( $pageConfigOrTitle, array $titles, bool $defaultLinkCaption = false ): array;
47
48    /**
49     * Return information about files (images)
50     *
51     * This replaces ImageInfoRequest and Batcher.imageinfo()
52     *
53     * @param PageConfig $pageConfig
54     * @param array $files [ [string Name, array Dims] ]. The array may contain
55     *  - width: (int) Requested thumbnail width
56     *  - height: (int) Requested thumbnail height
57     *  - page: (int) Requested thumbnail page number
58     *  - seek: (int) Requested thumbnail time offset
59     * @return array [ array|null ], where the array contains
60     *  - width: (int|false) File width, false if unknown
61     *  - height: (int|false) File height, false if unknown
62     *  - size: (int|false) File size in bytes, false if unknown
63     *  - mediatype: (string) File media type
64     *  - mime: (string) File MIME type
65     *  - url: (string) File URL
66     *  - mustRender: (bool) False if the file can be directly rendered by browsers
67     *  - badFile: (bool) Whether the file is on the "bad image list"
68     *  - duration: (float, optional) Duration of the media in seconds
69     *  - thumberror: (string, optional) Error text if thumbnailing failed. Ugh.
70     *  - responsiveUrls: (string[], optional) Map of display densities to URLs.
71     *  - thumbdata: (mixed, optional) MediaWiki File->getAPIData()
72     *  - thumburl: (string, optional) Thumbnail URL
73     *  - thumbwidth: (int, optional) Thumbnail width
74     *  - thumbheight: (int, optional) Thumbnail height
75     *  - timestamp: (string, optional) Timestamp
76     *  - sha1: (string, optional) SHA-1
77     */
78    abstract public function getFileInfo( PageConfig $pageConfig, array $files ): array;
79
80    /**
81     * Return information about blocked/rewritten external URLs.
82     *
83     * @param SiteConfig $siteConfig
84     * @param list<string> $hrefList The external URLs to query.
85     * @param LinkTarget $contextTitle The context title, for
86     *   wgNoFollowNsExceptions lookups.
87     * @return list<array{originalHref:string,href:?string,class:?string,rel:?string,title:?string}>
88     *   Information about the external URL: the href can be rewritten
89     *   or blocked (null), and optional class, title, and rel attributes can
90     *   be added (if non-null).  The results are in the same order as
91     *   the input $hrefList.
92     */
93    public function getExternalUrlInfo(
94        SiteConfig $siteConfig,
95        array $hrefList,
96        LinkTarget $contextTitle
97    ): array {
98        $ns = $contextTitle->getNamespace();
99        $noFollowConfig = $siteConfig->getNoFollowConfig();
100        $checkDomain = ( $noFollowConfig['nofollow'] ?? true ) &&
101            !in_array( $ns, ( $noFollowConfig['nsexceptions'] ?? [] ), true );
102        // Cast to an array because parserTests sets it as a string
103        $domainExceptions = (array)( $noFollowConfig['domainexceptions'] ?? [] );
104        // default implementation: return 'nofollow' where appropriate
105        return array_map( static fn ( $href ) => [
106            'originalHref' => $href,
107            'href' => $href,
108            'class' => null,
109            'rel' => (
110                $checkDomain &&
111                !UrlUtils::matchesDomainList( $href, $domainExceptions )
112            ) ? 'nofollow' : null,
113            'title' => null,
114        ], $hrefList );
115    }
116
117    /**
118     * Perform a parse on wikitext with an optional title override
119     *
120     * This replaces PHPParseRequest with onlypst = false, and Batcher.parse()
121     *
122     * @todo This is used mostly for expanding legacy extensions, but Parsoid
123     * should be able to/could? do this itself.
124     * @param PageConfig $pageConfig
125     * @param ContentMetadataCollector $metadata Will collect metadata about
126     *   the parsed content.
127     * @param string|PFragment $wikitext
128     * @param ?LinkTarget $titleOverride
129     * @return string Output HTML
130     */
131    abstract public function parseWikitextWithTitle(
132        PageConfig $pageConfig,
133        ContentMetadataCollector $metadata,
134        string|PFragment $wikitext,
135        ?LinkTarget $titleOverride = null,
136        ?ParsoidExtensionAPI $extApi = null,
137    ): string;
138
139    /**
140     * Preprocess wikitext
141     *
142     * This replaces PreprocessorRequest and Batcher.preprocess()
143     *
144     * @todo Parsoid should be able to do this itself.
145     * @param PageConfig $pageConfig
146     * @param ContentMetadataCollector $metadata Will collect metadata about
147     *   the preprocessed content.
148     * @param string|PFragment $wikitext
149     * @return string|PFragment Expanded wikitext
150     */
151    abstract public function preprocessWikitext(
152        PageConfig $pageConfig,
153        ContentMetadataCollector $metadata,
154        $wikitext
155    );
156
157    /**
158     * Fetch latest revision of article/template content for transclusion.
159     *
160     * Technically, the ParserOptions might select a different
161     * revision other than the latest via
162     * ParserOptions::getTemplateCallback() (used for FlaggedRevisions,
163     * etc), but the point is that template lookups are by title, not
164     * revision id.
165     *
166     * This replaces TemplateRequest
167     *
168     * @todo TemplateRequest also returns a bunch of other data, but seems to never use it except for
169     *   TemplateRequest.setPageSrcInfo() which is replaced by PageConfig.
170     * @param PageConfig $pageConfig
171     * @param LinkTarget $title Title of the page to fetch
172     * @return PageContent|null
173     */
174    abstract public function fetchTemplateSource(
175        PageConfig $pageConfig, LinkTarget $title
176    ): ?PageContent;
177
178    /**
179     * Fetch templatedata for a title
180     *
181     * This replaces TemplateDataRequest
182     *
183     * @param PageConfig $pageConfig
184     * @param LinkTarget $title
185     * @return array|null
186     */
187    abstract public function fetchTemplateData( PageConfig $pageConfig, LinkTarget $title ): ?array;
188
189    /**
190     * Log linter data.
191     *
192     * @param PageConfig $pageConfig
193     * @param array $lints
194     */
195    abstract public function logLinterData( PageConfig $pageConfig, array $lints ): void;
196
197    /**
198     * Add a tracking category with the given key to the metadata for the page.
199     *
200     * @param PageConfig $pageConfig the page on which the tracking category
201     *   is to be added
202     * @param ContentMetadataCollector $metadata The metadata for the page
203     * @param string $key Message key (not localized)
204     */
205    abstract public function addTrackingCategory(
206        PageConfig $pageConfig,
207        ContentMetadataCollector $metadata,
208        string $key
209    ): void;
210
211}