Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 58
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
FlowCreateTemplates
0.00% covered (danger)
0.00%
0 / 52
0.00% covered (danger)
0.00%
0 / 5
56
0.00% covered (danger)
0.00%
0 / 1
 getTemplates
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 1
2
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getUpdateKey
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 doDBUpdates
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 create
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace Flow\Maintenance;
4
5use Flow\OccupationController;
6use LoggedUpdateMaintenance;
7use MediaWiki\MediaWikiServices;
8use MediaWiki\Status\Status;
9use MediaWiki\Title\Title;
10use WikitextContent;
11
12$IP = getenv( 'MW_INSTALL_PATH' );
13if ( $IP === false ) {
14    $IP = __DIR__ . '/../../..';
15}
16
17require_once "$IP/maintenance/Maintenance.php";
18
19/**
20 * The templates will be created with a default content, but can be customized.
21 * If the templates already exists, they will be left untouched.
22 *
23 * @ingroup Maintenance
24 */
25class FlowCreateTemplates extends LoggedUpdateMaintenance {
26    /**
27     * Returns an array of templates to be created (= pages in NS_TEMPLATE)
28     *
29     * The key in the array is an i18n message so the template titles can be
30     * internationalized and/or edited per wiki.
31     * The value is a callback function that will only receive $title and is
32     * expected to return the page content in wikitext.
33     *
34     * @return array [title i18n key => content callback]
35     */
36    protected function getTemplates() {
37        return [
38            // Template:FlowMention, used to render mentions in Flow's Visual Editor
39            'flow-ve-mention-template-title' => function ( Title $title ) {
40                // get "User:" namespace prefix in wiki language
41                $namespaces = $this->getServiceContainer()->getContentLanguage()
42                    ->getFormattedNamespaces();
43
44                return '@[[' . $namespaces[NS_USER] . ':{{{1|Example}}}|{{{2|{{{1|Example}}}}}}]]';
45            },
46            // LiquidThread import templates
47            'flow-importer-lqt-moved-thread-template' => static function ( Title $title ) {
48                return wfMessage( 'flow-importer-lqt-moved-thread-template-content' )->inContentLanguage()->plain();
49            },
50            'flow-importer-lqt-converted-template' => static function ( Title $title ) {
51                return wfMessage( 'flow-importer-lqt-converted-template-content' )->inContentLanguage()->plain();
52            },
53            'flow-importer-lqt-converted-archive-template' => static function ( Title $title ) {
54                return wfMessage( 'flow-importer-lqt-converted-archive-template-content' )->inContentLanguage()->plain();
55            },
56            'flow-importer-lqt-suppressed-user-template' => static function ( Title $title ) {
57                return wfMessage( 'flow-importer-lqt-suppressed-user-template-content' )->inContentLanguage()->plain();
58            },
59            'flow-importer-lqt-different-author-signature-template' => static function ( Title $title ) {
60                return wfMessage( 'flow-importer-lqt-different-author-signature-template-content' )->inContentLanguage()->plain();
61            },
62            // Wikitext import templates
63            'flow-importer-wt-converted-template' => static function ( Title $title ) {
64                return wfMessage( 'flow-importer-wt-converted-template-content' )->inContentLanguage()->plain();
65            },
66            'flow-importer-wt-converted-archive-template' => static function ( Title $title ) {
67                return wfMessage( 'flow-importer-wt-converted-archive-template-content' )->inContentLanguage()->plain();
68            },
69        ];
70    }
71
72    public function __construct() {
73        parent::__construct();
74
75        $this->addDescription( "Creates templates required by Flow" );
76
77        $this->requireExtension( 'Flow' );
78    }
79
80    protected function getUpdateKey() {
81        $templates = $this->getTemplates();
82        $keys = array_keys( $templates );
83        sort( $keys );
84
85        // make the updatekey unique for the i18n keys of the pages to be created
86        // so we can easily skip this update if there are no changes
87        return 'FlowCreateTemplates:' . md5( implode( ',', $keys ) );
88    }
89
90    protected function doDBUpdates() {
91        $status = Status::newGood();
92
93        $templates = $this->getTemplates();
94        foreach ( $templates as $key => $callback ) {
95            $title = Title::newFromText( wfMessage( $key )->inContentLanguage()->plain(), NS_TEMPLATE );
96            $content = new WikitextContent( $callback( $title ) );
97
98            $status->merge( $this->create( $title, $content ) );
99        }
100
101        return $status->isOK();
102    }
103
104    /**
105     * Creates a page with the given content (unless it already exists)
106     *
107     * @param Title $title
108     * @param WikitextContent $content
109     * @return Status
110     */
111    protected function create( Title $title, WikitextContent $content ) {
112        $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
113
114        if ( $page->getRevisionRecord() !== null ) {
115            // template already exists, don't overwrite it
116            return Status::newGood();
117        }
118
119        /** @var OccupationController $occupationController */
120        $occupationController = MediaWikiServices::getInstance()->getService( 'FlowTalkpageManager' );
121        return $page->doUserEditContent(
122            $content,
123            $occupationController->getTalkpageManager(),
124            '/* Automatically created by Flow */',
125            EDIT_FORCE_BOT | EDIT_SUPPRESS_RC
126        );
127    }
128}
129
130$maintClass = FlowCreateTemplates::class;
131require_once RUN_MAINTENANCE_IF_MAIN;