Last modified: 2011-02-13 23:09:54 UTC

Wikimedia Bugzilla is closed!

Wikimedia migrated from Bugzilla to Phabricator. Bug reports are handled in Wikimedia Phabricator.
This static website is read-only and for historical purposes. It is not possible to log in and except for displaying bug reports and their history, links might be broken. See T28250, the corresponding Phabricator task for complete and up-to-date bug report information.
Bug 26250 - ForeignAPIRepo broken if native json module not available
ForeignAPIRepo broken if native json module not available
Status: RESOLVED FIXED
Product: MediaWiki
Classification: Unclassified
File management (Other open bugs)
1.16.x
All All
: Normal blocker (vote)
: ---
Assigned To: Brion Vibber
:
Depends on: 23817
Blocks:
  Show dependency treegraph
 
Reported: 2010-12-05 15:33 UTC by Manuel Schneider
Modified: 2011-02-13 23:09 UTC (History)
5 users (show)

See Also:
Web browser: ---
Mobile Platform: ---
Assignee Huggle Beta Tester: ---


Attachments

Description Manuel Schneider 2010-12-05 15:33:48 UTC
Tried both, 1.16.0 tarball from mediawiki.org as well as SVN REL1_16.

PHP version: 5.2.12

from LocalSettings.php:
--- snip ---
$wgForeignFileRepos[]   = array(
  'class'               => 'ForeignAPIRepo',
  'name'                => 'shared',
  'apibase'             => 'http://commons.wikimedia.org/w/api.php',
  'fetchDescription'    => true
);
--- snap ---

Whenever I try to embedd an image - be it local or external - I get the following error message:
Fatal error: Cannot use object of type stdClass as array in /var/www/[...]/includes/filerepo/ForeignAPIFile.php on line 101
Comment 1 Bryan Tong Minh 2010-12-05 17:26:10 UTC
Can't reproduce on PHP 5.3.2 on IIS/Windows Vista using the latest REL1_16 branch.
Comment 2 Derk-Jan Hartman 2010-12-05 22:49:11 UTC
The line in question would be:

if( !isset( $this->mInfo['mime'] ) ) {
Comment 3 Chad H. 2011-01-05 15:53:53 UTC
$this->mInfo was always written (and expects to be) an associative array. For some reason it sounds like it's getting turned into an object :(
Comment 4 Bryan Tong Minh 2011-01-11 19:43:21 UTC
I have no clue how PHP would turn an array in an object. stdClass is the type of a a database result row right? My best guess is that somehow mInfo is turned into a result row. Or otherwise perhaps a fucked up version of the json decoder?
Comment 5 Mark A. Hershberger 2011-01-19 21:23:39 UTC
http://php.net/manual/en/reserved.classes.php stdClass is "Created by typecasting to object" where typcasting: 

> If a value of any other type is converted to an object, a new instance of the
> stdClass built-in class is created. If the value was NULL, the new instance
> will be empty. Arrays convert to an object with properties named by keys, and
> corresponding values. For any other value, a member variable named scalar will
> contain the value.
Comment 6 Chad H. 2011-01-19 21:28:51 UTC
I think we all know what stdClass is and how you cast an array to it.

The confusion is where this casting is happening...we've been using an array in this code since inception.
Comment 7 Brion Vibber 2011-01-19 21:55:27 UTC
json_decode() produces stdClass objects for JSON objects/maps/dicts by default; to get an associative array, we pass in 'true' as the second parameter.

For our FormatJson::decode() wrapper, if json_decode is not natively available passes the object retrieved from another library through wfObjectToArray().

It looks like wfObjectToArray()'s recursion isn't actually sufficient: json_decode($img, true) and wfObjectToArray(json_decode($img)) give different results, and the latter fails to translate the imageinfo entry:

Testing on API return data from http://commons.wikimedia.org/w/api.php?action=query&titles=Image:Wikipedia.png&iiprop=timestamp|user|comment|url|size|sha1|metadata|mime&prop=imageinfo&format=json

> return wfObjectToArray(json_decode($img, false));
array(2) {
  ["query"]=>
  array(2) {
    ["normalized"]=>
    array(1) {
      [0]=>
      object(stdClass)#22 (2) {
        ["from"]=>
        string(19) "Image:Wikipedia.png"
        ["to"]=>
        string(18) "File:Wikipedia.png"
      }
    }
    ["pages"]=>
    array(1) {
      [17105]=>
      array(5) {
        ["pageid"]=>
        int(17105)
        ["ns"]=>
        int(6)
        ["title"]=>
        string(18) "File:Wikipedia.png"
        ["imagerepository"]=>
        string(5) "local"
        ["imageinfo"]=>
        array(1) {
          [0]=>
          object(stdClass)#25 (11) {
            ["timestamp"]=>
            string(20) "2005-08-13T20:55:13Z"
            ["user"]=>
            string(9) "Thuresson"
            ["size"]=>
            int(11742)
            ["width"]=>
            int(135)
            ["height"]=>
            int(155)
            ["url"]=>
            string(64) "http://upload.wikimedia.org/wikipedia/commons/1/12/Wikipedia.png"
            ["descriptionurl"]=>
            string(52) "http://commons.wikimedia.org/wiki/File:Wikipedia.png"
            ["comment"]=>
            string(29) "Återgått till yngre version"
            ["sha1"]=>
            string(40) "f755e536d96cb430f23b53806ffcd88eada3d572"
            ["metadata"]=>
            NULL
            ["mime"]=>
            string(9) "image/png"
          }
        }
      }
    }
  }
  ["query-continue"]=>
  array(1) {
    ["imageinfo"]=>
    array(1) {
      ["iistart"]=>
      string(20) "2005-08-08T20:30:06Z"
    }
  }
}

So if we're on a system without PHP's native JSON module, we'll run the object form through wfObjectToArray() and get that bogus output. Most of the data structure is associative arrays -- so we extract the imageinfo entry successfully -- but when things try to read out of it, they've got the wrong data type.

Looks like the recursion isn't diving into linear arrays:

	foreach ( get_object_vars( $object ) as $key => $value ) {
		if ( is_object( $value ) && $recursive ) {
			$value = wfObjectToArray( $value );
		}

		$array[$key] = $value;
	}
Comment 8 Brion Vibber 2011-02-12 06:31:06 UTC
Adding bug 23817 as dependency -- there's a patch on that bug to fix wfObjectToArray.
Comment 9 Brion Vibber 2011-02-13 23:09:54 UTC
Patches from bug 23817 are applied in r82090 and r82091, fixing this.

Note You need to log in before you can comment on or make changes to this bug.


Navigation
Links