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