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