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