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.
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.
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
23 class GadgetDefinitionContent extends JsonContent {
25 public function __construct( $text ) {
26 parent::__construct( $text, 'GadgetDefinition' );
29 public function isValid() {
30 // parent::isValid() is called in validate()
31 return $this->validate()->isOK();
37 * If called before validation, it may return JSON "null".
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 );
51 * @param ParserOptions $options
52 * @param bool $generateHtml
53 * @param ParserOutput &$output
55 protected function fillParserOutput( Title $title, $revId,
56 ParserOptions $options, $generateHtml, ParserOutput &$output
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 );
64 $output->addLink( $title );
73 public function validate() {
74 if ( !parent::isValid() ) {
75 return $this->getData();
78 $validator = new GadgetDefinitionValidator();
79 return $validator->validate( $this->getAssocArray() );
83 * Get the JSON content as an associative array with
84 * all fields filled out, populating defaults as necessary.
88 public function getAssocArray() {
89 $info = wfObjectToArray( $this->getData()->getValue() );
90 /** @var GadgetDefinitionContentHandler $handler */
91 $handler = $this->getContentHandler();
92 $info = wfArrayPlus2d( $info, $handler->getDefaultMetadata() );
98 * @param WikiPage $page
99 * @param ParserOutput $parserOutput
100 * @return DataUpdate[]
102 public function getDeletionUpdates( WikiPage $page, ParserOutput $parserOutput = null ) {
104 parent::getDeletionUpdates( $page, $parserOutput ),
105 [ new GadgetDefinitionDeletionUpdate( $page->getTitle()->getText() ) ]
110 * @param Title $title
111 * @param Content $old
112 * @param bool $recursive
113 * @param ParserOutput $parserOutput
114 * @return DataUpdate[]
116 public function getSecondaryDataUpdates( Title $title, Content $old = null,
117 $recursive = true, ParserOutput $parserOutput = null
120 parent::getSecondaryDataUpdates( $title, $old, $recursive, $parserOutput ),
121 [ new GadgetDefinitionSecondaryDataUpdate( $title->getText() ) ]