]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - extensions/Gadgets/includes/content/GadgetDefinitionContent.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / extensions / Gadgets / includes / content / GadgetDefinitionContent.php
1 <?php
2 /**
3  * Copyright 2014
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  * http://www.gnu.org/copyleft/gpl.html
19  *
20  * @file
21  */
22
23 class GadgetDefinitionContent extends JsonContent {
24
25         public function __construct( $text ) {
26                 parent::__construct( $text, 'GadgetDefinition' );
27         }
28
29         public function isValid() {
30                 // parent::isValid() is called in validate()
31                 return $this->validate()->isOK();
32         }
33
34         /**
35          * Pretty-print JSON.
36          *
37          * If called before validation, it may return JSON "null".
38          *
39          * @return string
40          */
41         public function beautifyJSON() {
42                 // @todo we should normalize entries in module.scripts and module.styles
43                 return FormatJson::encode( $this->getAssocArray(), "\t", FormatJson::UTF8_OK );
44         }
45
46         /**
47          * Register some links
48          *
49          * @param Title $title
50          * @param int $revId
51          * @param ParserOptions $options
52          * @param bool $generateHtml
53          * @param ParserOutput &$output
54          */
55         protected function fillParserOutput( Title $title, $revId,
56                 ParserOptions $options, $generateHtml, ParserOutput &$output
57         ) {
58                 parent::fillParserOutput( $title, $revId, $options, $generateHtml, $output );
59                 $assoc = $this->getAssocArray();
60                 foreach ( [ 'scripts', 'styles' ] as $type ) {
61                         foreach ( $assoc['module'][$type] as $page ) {
62                                 $title = Title::makeTitleSafe( NS_GADGET, $page );
63                                 if ( $title ) {
64                                         $output->addLink( $title );
65                                 }
66                         }
67                 }
68         }
69
70         /**
71          * @return Status
72          */
73         public function validate() {
74                 if ( !parent::isValid() ) {
75                         return $this->getData();
76                 }
77
78                 $validator = new GadgetDefinitionValidator();
79                 return $validator->validate( $this->getAssocArray() );
80         }
81
82         /**
83          * Get the JSON content as an associative array with
84          * all fields filled out, populating defaults as necessary.
85          *
86          * @return array
87          */
88         public function getAssocArray() {
89                 $info = wfObjectToArray( $this->getData()->getValue() );
90                 /** @var GadgetDefinitionContentHandler $handler */
91                 $handler = $this->getContentHandler();
92                 $info = wfArrayPlus2d( $info, $handler->getDefaultMetadata() );
93
94                 return $info;
95         }
96
97         /**
98          * @param WikiPage $page
99          * @param ParserOutput $parserOutput
100          * @return DataUpdate[]
101          */
102         public function getDeletionUpdates( WikiPage $page, ParserOutput $parserOutput = null ) {
103                 return array_merge(
104                         parent::getDeletionUpdates( $page, $parserOutput ),
105                         [ new GadgetDefinitionDeletionUpdate( $page->getTitle()->getText() ) ]
106                 );
107         }
108
109         /**
110          * @param Title $title
111          * @param Content $old
112          * @param bool $recursive
113          * @param ParserOutput $parserOutput
114          * @return DataUpdate[]
115          */
116         public function getSecondaryDataUpdates( Title $title, Content $old = null,
117                 $recursive = true, ParserOutput $parserOutput = null
118         ) {
119                 return array_merge(
120                         parent::getSecondaryDataUpdates( $title, $old, $recursive, $parserOutput ),
121                         [ new GadgetDefinitionSecondaryDataUpdate( $title->getText() ) ]
122                 );
123         }
124 }