Debian Codegraph @

extensions/Cite/src/ResourceLoader/CitationToolDefinition.php

raw symbols SCIP
1 <?php
2
3 namespace Cite\ResourceLoader;
4
5 use MediaWiki\ResourceLoader as RL;
6
7 /**
8 * Callback to deliver cite-tool-definition.json and related messages.
9 *
10 * Temporary hack since 2015 for T93800.
11 *
12 * @copyright 2011-2018 VisualEditor Team's Cite sub-team and others; see AUTHORS.txt
13 * @license MIT
14 */
15 class CitationToolDefinition {
16
17 public static function makeScript( RL\Context $context ): string {
18 $citationDefinition = json_decode(
19 $context->msg( 'cite-tool-definition.json' )
20 ->inContentLanguage()
21 ->plain()
22 );
23
24 $citationDefinition ??= json_decode(
25 $context->msg( 'visualeditor-cite-tool-definition.json' )
26 ->inContentLanguage()
27 ->plain()
28 );
29
30 $citationTools = [];
31 if ( is_array( $citationDefinition ) ) {
32 foreach ( $citationDefinition as $tool ) {
33 // Skip incomplete entries that don't even have a name
34 if ( empty( $tool->name ) || !is_string( $tool->name ) ) {
35 continue;
36 }
37
38 // Users can hard-code titles in MediaWiki:Cite-tool-definition.json if they want
39 if ( empty( $tool->title ) || !is_string( $tool->title ) ) {
40 // The following messages are generated here:
41 // * visualeditor-cite-tool-name-book
42 // * visualeditor-cite-tool-name-journal
43 // * visualeditor-cite-tool-name-news
44 // * visualeditor-cite-tool-name-web
45 $msg = $context->msg( 'visualeditor-cite-tool-name-' . $tool->name );
46 // Fall back to the raw name if there is no message
47 $tool->title = $msg->isDisabled() ? $tool->name : $msg->text();
48 }
49
50 $citationTools[] = $tool;
51 }
52 }
53
54 // TODO: When this custom module is converted to adopt packageFiles, this data
55 // can be exported via a callback as a virtual "tools.json" file. Then the JS
56 // in MWReference.init.js can do `ve.ui.mwCitationTools = require( "./tools.json" )`
57
58 // Limit and expose
59 $limit = 5;
60 $citationTools = array_slice( $citationTools, 0, $limit );
61 return 've.ui.mwCitationTools = ' . $context->encodeJson( $citationTools ) . ';';
62 }
63 }