* (bug 20336) changed json_decode json_encode to static class in global functions
authorMichael Dale <dale@users.mediawiki.org>
Wed, 9 Sep 2009 22:26:16 +0000 (22:26 +0000)
committerMichael Dale <dale@users.mediawiki.org>
Wed, 9 Sep 2009 22:26:16 +0000 (22:26 +0000)
** we should update extensions as well
* added config for fileCheckModify date check to scriptLoader unique script id generation

includes/DefaultSettings.php
includes/GlobalFunctions.php
includes/OutputPage.php
includes/api/ApiFormatJson.php
includes/filerepo/ForeignAPIRepo.php
includes/upload/UploadFromChunks.php

index 2765e3f..8b216f0 100644 (file)
@@ -2742,6 +2742,18 @@ $wgJSAutoloadClasses = array();
  */
 $wgEnableScriptLoader = false;
 
+/*
+ * $wgScriptModifiedCheck should run a file modified check on javascript files when
+ * generating unique request ids for javascript include using the script-loader
+ *
+ * note this will only check core scripts that are directly included on the page.
+ * (not scripts loaded after the initial page display since after initial page
+ * display scripts inherit the unique request id) 
+ *
+ * and or you can update $wgStyleVersion
+ */
+$wgScriptModifiedCheck = true;
+
 /*
  * enable js2 Script System
  * if enabled we include jquery, mv_embed and js2 versions of editPage.js
index 4cbed9e..b6de6f0 100644 (file)
@@ -3344,3 +3344,27 @@ function wfBCP47( $code ) {
        $langCode = implode ( '-' , $codeBCP );
        return $langCode;
 }
+class FormatJson{
+       public static function encode($value, $isHtml=false){
+               // Some versions of PHP have a broken json_encode, see PHP bug
+               // 46944. Test encoding an affected character (U+20000) to
+               // avoid this.
+               if (!function_exists('json_encode') || $isHtml || strtolower(json_encode("\xf0\xa0\x80\x80")) != '\ud840\udc00') {
+                       $json = new Services_JSON();
+                       return $json->encode($value, $isHtml) ;
+               } else {
+                       return json_encode($value);
+               }
+       }
+       public static function decode($value, $assoc=false){
+               if (!function_exists('json_decode') ) {
+                       $json = new Services_JSON();
+                       $jsonDec = $json->decode($value);
+                       if($assoc)
+                               $jsonDec = (array) $jsonDec;
+                       return $jsonDec;
+               } else {
+                       return json_decode($value, $assoc);
+               }
+       }
+}
\ No newline at end of file
index cbbd3a8..ebdbcc6 100644 (file)
@@ -227,34 +227,49 @@ class OutputPage {
         * gets the scriptLoader javascript include
         * @param $forcClassAry Boolean: false by default
         */
-       function getScriptLoaderJs( $forceClassAry = false ){
+       function getScriptLoaderJs( $classAry = array() ){
                global $wgRequest, $wgDebugJavaScript;
-
-               if( !$forceClassAry ){
-                       $class_list = implode( ',', $this->mScriptLoaderClassList );
-               } else {
-                       $class_list = implode( ',', $forceClassAry );
+               //if no class array provided use the mScriptLoaderClassList var
+               if( !count($classAry) ){
+                       $classAry = $this->mScriptLoaderClassList;
                }
+               $class_list = implode(',', $classAry);
 
                $debug_param = ( $wgDebugJavaScript ||
                                                 $wgRequest->getVal( 'debug' ) == 'true' ||
                                                 $wgRequest->getVal( 'debug' ) == '1' )
                                         ? '&debug=true' : '';
 
-               //@@todo intelligent unique id generation based on svn version of file (rather than just grabbing the $wgStyleVersion var)
-               //@@todo we should check the packaged message text in this javascript file for updates and update the $mScriptLoaderURID id (in getJsClassFromPath)
-
-               //generate the unique request param (combine with the most recent revision id of any wiki page with the $wgStyleVersion var)
-
-               return Html::linkedScript( wfScript( 'mwScriptLoader' ) . "?class={$class_list}{$debug_param}&" . $this->getURIDparam() );
+               return Html::linkedScript( wfScript( 'mwScriptLoader' ) . "?class={$class_list}{$debug_param}&" . $this->getURIDparam( $classAry) );
        }
 
-       function getURIDparam(){
-               global $wgDebugJavaScript, $wgStyleVersion;
+       function getURIDparam( $classAry=array() ){
+               global $wgDebugJavaScript, $wgStyleVersion, $IP, $wgScriptModifiedCheck;
                if( $wgDebugJavaScript ){
                        return 'urid=' . time();
                } else {
-                       return "urid={$wgStyleVersion}_{$this->mLatestScriptRevID}";
+                       $ftime=0;
+                       if($wgScriptModifiedCheck){
+                               foreach( $classAry as $class ){
+                                       $js_path =  jsScriptLoader::getJsPathFromClass( $class );
+                                       if( $js_path ){
+                                               $cur_ftime = filemtime ( $IP ."/". $js_path );
+                                               if( $cur_ftime > $ftime )
+                                                       $ftime = $cur_ftime;
+                                       }
+                               }
+                       }
+                       //set up the urid:
+                       $urid = "urid={$wgStyleVersion}";
+
+                       //if we have a $this->mLatestScriptRevID (wiki page revision ids)
+                       if($this->mLatestScriptRevID != 0 )
+                               $urid .= "_{$this->mLatestScriptRevID}";
+
+                       if( $ftime != 0 )
+                               $urid .= "_".$ftime;
+
+                       return $urid;
                }
        }
 
index c789fbb..d831810 100644 (file)
@@ -67,24 +67,7 @@ class ApiFormatJson extends ApiFormatBase {
                        $prefix = preg_replace("/[^][.\\'\\\"_A-Za-z0-9]/", "", $callback ) . "(";
                        $suffix = ")";
                }
-
-               // Some versions of PHP have a broken json_encode, see PHP bug 
-               // 46944. Test encoding an affected character (U+20000) to 
-               // avoid this.
-               $this->printText( $prefix . $this->getJsonEncode($this->getResultData(),   $this->getIsHtml() )  . $suffix);            
-       }       
-       /*
-        * static to support static calls to json output (instead of json_encode function) 
-        * @param array $results  the results array to output as a json string
-        * @parm isHTML if the output is html
-        */     
-       public static function getJsonEncode($value, $isHtml=false){
-               if (!function_exists('json_encode') || $isHtml || strtolower(json_encode("\xf0\xa0\x80\x80")) != '\ud840\udc00') {
-                       $json = new Services_JSON();
-                       return $json->encode($value, $isHtml) ;
-               } else {                        
-                       return json_encode($value);
-               }
+               $this->printText( $prefix . FormatJson::encode( $this->getResultData(),   $this->getIsHtml() )  . $suffix);
        }
 
        public function getAllowedParams() {
index 2f1c391..0fb5097 100644 (file)
@@ -130,7 +130,7 @@ class ForeignAPIRepo extends FileRepo {
                        }
                        $this->mQueryCache[$url] = $data;
                }
-               return json_decode( $this->mQueryCache[$url], true );
+               return FormatJson::decode( $this->mQueryCache[$url], true );
        }
        
        function getImageInfo( $title, $time = false ) {
index 83b2734..478b8f5 100644 (file)
@@ -167,7 +167,7 @@ class UploadFromChunks extends UploadBase {
                        // c) (we need the token to validate chunks are coming from a non-xss request)
                        $token = urlencode( $wgUser->editToken() );
                        ob_clean();
-                       echo ApiFormatJson::getJsonEncode( array(
+                       echo FormatJson::encode( array(
                                        'uploadUrl' => wfExpandUrl( wfScript( 'api' ) ) . "?action=upload&".
                                                                        "token={$token}&format=json&enablechunks=true&chunksessionkey=".
                                                                        $this->setupChunkSession( $summary, $comment, $watch ) ) );
@@ -179,7 +179,7 @@ class UploadFromChunks extends UploadBase {
                                // firefogg expects a specific result per:
                                // http://www.firefogg.org/dev/chunk_post.html
                                ob_clean();
-                               echo ApiFormatJson::getJsonEncode( array(
+                               echo FormatJson::encode( array(
                                                'result' => 1,
                                                'filesize' => filesize( $this->getRealPath( $this->mTempAppendPath ) )
                                        )
@@ -209,7 +209,7 @@ class UploadFromChunks extends UploadBase {
                        // firefogg expects a specific result per:
                        // http://www.firefogg.org/dev/chunk_post.html
                        ob_clean();
-                       echo ApiFormatJson::getJsonEncode( array(
+                       echo FormatJson::encode( array(
                                        'result' => 1,
                                        'done' => 1,
                                        'resultUrl' => $file->getDescriptionUrl()