MediaWiki  REL1_24
JsonContent.php
Go to the documentation of this file.
00001 <?php
00015 class JsonContent extends TextContent {
00016 
00017     public function __construct( $text, $modelId = CONTENT_MODEL_JSON ) {
00018         parent::__construct( $text, $modelId );
00019     }
00020 
00025     public function getJsonData() {
00026         return FormatJson::decode( $this->getNativeData(), true );
00027     }
00028 
00032     public function isValid() {
00033         return $this->getJsonData() !== null;
00034     }
00035 
00041     public function beautifyJSON() {
00042         $decoded = FormatJson::decode( $this->getNativeData(), true );
00043         if ( !is_array( $decoded ) ) {
00044             return null;
00045         }
00046         return FormatJson::encode( $decoded, true );
00047 
00048     }
00049 
00057     public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
00058         return new static( $this->beautifyJSON() );
00059     }
00060 
00071     protected function fillParserOutput( Title $title, $revId,
00072         ParserOptions $options, $generateHtml, ParserOutput &$output
00073     ) {
00074         if ( $generateHtml ) {
00075             $output->setText( $this->objectTable( $this->getJsonData() ) );
00076             $output->addModuleStyles( 'mediawiki.content.json' );
00077         } else {
00078             $output->setText( '' );
00079         }
00080     }
00086     protected function objectTable( $mapping ) {
00087         $rows = array();
00088 
00089         foreach ( $mapping as $key => $val ) {
00090             $rows[] = $this->objectRow( $key, $val );
00091         }
00092         return Xml::tags( 'table', array( 'class' => 'mw-json' ),
00093             Xml::tags( 'tbody', array(), join( "\n", $rows ) )
00094         );
00095     }
00096 
00103     protected function objectRow( $key, $val ) {
00104         $th = Xml::elementClean( 'th', array(), $key );
00105         if ( is_array( $val ) ) {
00106             $td = Xml::tags( 'td', array(), self::objectTable( $val ) );
00107         } else {
00108             if ( is_string( $val ) ) {
00109                 $val = '"' . $val . '"';
00110             } else {
00111                 $val = FormatJson::encode( $val );
00112             }
00113 
00114             $td = Xml::elementClean( 'td', array( 'class' => 'value' ), $val );
00115         }
00116 
00117         return Xml::tags( 'tr', array(), $th . $td );
00118     }
00119 
00120 }