Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.37% covered (warning)
85.37%
35 / 41
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
AbstractContentEditPageTrait
85.37% covered (warning)
85.37%
35 / 41
0.00% covered (danger)
0.00%
0 / 2
8.20
0.00% covered (danger)
0.00%
0 / 1
 generateAbstractContentPayload
90.00% covered (success)
90.00%
27 / 30
0.00% covered (danger)
0.00%
0 / 1
4.02
 getAbstractContentForTitle
72.73% covered (warning)
72.73%
8 / 11
0.00% covered (danger)
0.00%
0 / 1
4.32
1<?php
2/**
3 * WikiLambda Abstract content page trait for edit and create pages.
4 *
5 * @file
6 * @ingroup Extensions
7 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
8 * @license MIT
9 */
10
11namespace MediaWiki\Extension\WikiLambda\AbstractContent;
12
13use MediaWiki\Context\IContextSource;
14use MediaWiki\Extension\WikiLambda\UIUtils;
15use MediaWiki\Html\Html;
16use MediaWiki\MediaWikiServices;
17use MediaWiki\Output\OutputPage;
18use MediaWiki\Page\Article;
19use MediaWiki\Title\Title;
20
21trait AbstractContentEditPageTrait {
22
23    /**
24     * Generate the Abstract Wiki content payload for edit and creation pages.
25     * Pass content payload through js config vars.
26     *
27     * @param IContextSource $context
28     * @param OutputPage $output
29     * @param Title $title
30     */
31    public function generateAbstractContentPayload(
32        IContextSource $context,
33        OutputPage $output,
34        Title $title
35    ): void {
36        // Get user language
37        // TODO: find a way to get the language zid for the user
38        // lang code and pass it to the app via JS config vars.
39        $userLang = $context->getLanguage();
40        $userLangCode = $userLang->getCode();
41
42        $createNewPage = true;
43        $jsonContent = false;
44
45        // Get oldid, if any, from the url parameter 'oldid'
46        $targetRevisionId = $context->getRequest()->getInt( 'oldid' ) ?: null;
47
48        if ( $title->exists() ) {
49            // Content exists: retrieve given or latest revision
50            $createNewPage = false;
51            $jsonContent = $this->getAbstractContentForTitle( $title, $targetRevisionId );
52            // FIXME if jsonContent is false, then title and oldid don't match
53        } else {
54            // Content does not exist: generate empty content object
55            $contentHandler = MediaWikiServices::getInstance()
56                ->getContentHandlerFactory()
57                ->getContentHandler( CONTENT_MODEL_ABSTRACT );
58            '@phan-var \MediaWiki\Extension\WikiLambda\AbstractContent\AbstractWikiContentHandler $contentHandler';
59            $jsonContent = $contentHandler->makeEmptyContent()->getText();
60        }
61
62        // (T364318) Add the revision navigation bar if seeing an oldid
63        if ( $targetRevisionId !== null ) {
64            $output->setRevisionId( $targetRevisionId );
65            $article = Article::newFromTitle( $title, $context );
66            $article->setOldSubtitle( $targetRevisionId );
67        }
68
69        // Fallback no-JS notice.
70        $noJsMsg = $context->msg( 'wikilambda-nojs' )->inLanguage( $userLang )->parse();
71        $output->addHtml( Html::rawElement( 'noscript', [], $noJsMsg ) );
72
73        // Vue app element with Codex progress indicator
74        $loadingMessage = $context->msg( 'wikilambda-loading' )->inLanguage( $userLang )->text();
75        $progressIndicator = UIUtils::createCodexProgressIndicator( $loadingMessage );
76        $output->addHtml( Html::rawElement( 'div', [ 'id' => 'ext-wikilambda-app' ], $progressIndicator ) );
77
78        // Set up config variables
79        $configVars = [
80            'abstractContent' => true,
81            'content' => $jsonContent,
82            'createNewPage' => $createNewPage,
83            'title' => $title->getText(),
84            'zlang' => $userLangCode,
85            'viewmode' => false
86        ];
87        $output->addJsConfigVars( 'wgWikiLambda', $configVars );
88    }
89
90    /**
91     * Return the AbstractWikiContent JSON encoded as a string
92     *
93     * @param Title $title
94     * @param int|null $revisionId
95     * @return string|bool
96     */
97    private function getAbstractContentForTitle( Title $title, ?int $revisionId ) {
98        // TODO inject revision store
99        $revisionStore = MediaWikiServices::getInstance()->getRevisionStore();
100        if ( $revisionId ) {
101            $revision = $revisionStore->getRevisionByTitle( $title, $revisionId, 0 );
102        } else {
103            $revision = $revisionStore->getKnownCurrentRevision( $title );
104        }
105
106        if ( !$revision ) {
107            return false;
108        }
109
110        // Check content model
111        $contentModel = $revision->getMainContentModel();
112        if ( $contentModel !== CONTENT_MODEL_ABSTRACT ) {
113            return false;
114        }
115
116        $content = $revision->getMainContentRaw();
117        '@phan-var AbstractWikiContent $content';
118        return $content->getText();
119    }
120}