Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
ZObjectEditingPageTrait
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 1
30
0.00% covered (danger)
0.00%
0 / 1
 generateZObjectPayload
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2/**
3 * WikiLambda Object page util (trait) for edition or creation
4 *
5 * @file
6 * @ingroup Extensions
7 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
8 * @license MIT
9 */
10
11namespace MediaWiki\Extension\WikiLambda;
12
13use MediaWiki\Context\IContextSource;
14use MediaWiki\Extension\WikiLambda\Registry\ZLangRegistry;
15use MediaWiki\Html\Html;
16use MediaWiki\Output\OutputPage;
17use MediaWiki\Page\Article;
18use MediaWiki\Title\Title;
19
20trait ZObjectEditingPageTrait {
21    /**
22     * Generate the edition or creation header and render it in the output.
23     *
24     * @param IContextSource $context
25     * @param OutputPage $output
26     * @param array $jsEditingConfigVarOverride variables to pass to the mw.config in JavaScript.
27     */
28    public function generateZObjectPayload(
29        IContextSource $context,
30        OutputPage $output,
31        array $jsEditingConfigVarOverride
32    ) {
33        $userLang = $context->getLanguage();
34
35        // Only add no-JS notice for edit/create modes, not view mode (content handler handles it)
36        $isViewMode = ( $jsEditingConfigVarOverride['viewmode'] ?? false ) === true;
37        if ( !$isViewMode ) {
38            // Fallback no-JS notice.
39            $output->addHtml( Html::rawElement(
40                'noscript',
41                [],
42                $context->msg( 'wikilambda-nojs' )->inLanguage( $userLang )->parse()
43            ) );
44            // Vue app element with Codex progress indicator
45            $loadingMessage = $context->msg( 'wikilambda-loading' )->inLanguage( $userLang )->text();
46            $output->addHtml( Html::rawElement(
47                'div',
48                [ 'id' => 'ext-wikilambda-app' ],
49                UIUtils::createCodexProgressIndicator( $loadingMessage )
50            ) );
51        }
52
53        if ( array_key_exists( 'zId', $jsEditingConfigVarOverride ) ) {
54            $title = Title::newFromText( $jsEditingConfigVarOverride[ 'zId' ], NS_MAIN );
55
56            // Get oldid, if any, from the url parameter 'oldid'
57            $targetRevisionId = $context->getRequest()->getInt( 'oldid' ) ?: null;
58
59            // (T364318) Add the revision navigation bar if seeing an oldid
60            if ( $targetRevisionId !== null ) {
61                $output->setRevisionId( $targetRevisionId );
62                $article = Article::newFromTitle( $title, $context );
63                $article->setOldSubtitle( $targetRevisionId );
64            }
65        }
66
67        $userLangCode = $userLang->getCode();
68
69        $zLangRegistry = ZLangRegistry::singleton();
70        // If the userLang isn't recognised (e.g. it's qqx, or a language we don't support yet, or it's
71        // nonsense), then fall back to English.
72        $userLangZid = $zLangRegistry->getLanguageZidFromCode( $userLangCode, true );
73        // Normalise our used language code from what the Language object says
74        $userLangCode = $zLangRegistry->getLanguageCodeFromZid( $userLangZid );
75
76        $jsEditingConfigVarBase = [
77            'zlang' => $userLangCode,
78            'zlangZid' => $userLangZid,
79            'viewmode' => false
80        ];
81        $jsEditingConfigVar = array_merge( $jsEditingConfigVarBase, $jsEditingConfigVarOverride );
82
83        $output->addJsConfigVars( 'wgWikiLambda', $jsEditingConfigVar );
84    }
85}