Commit graph

33 commits

Author SHA1 Message Date
Kevin Israel
e9443cf677 FormatJson: microoptimizations for UTF8_OK mode
* Replace strtr with str_replace where faster.
* Use addcslashes to escape json_encode's output. Because no control
  characters are included, the only characters that have to be
  escaped are \ and ". (irrelevant for PHP 5.4+ installations)

Re-encoding a ~1.5 MB API response from the Chinese Wikipedia:
* PHP 5.3: 32% faster (from 347 ms to 239 ms)
* PHP 5.4: 70% faster (from 51 ms to 15 ms)
* HHVM: 42% faster (from 326 ms to 191 ms)

Change-Id: I7c9342682986d40a2f2436ac978390b6018a3521
2013-04-08 05:07:07 +00:00
Kevin Israel
217cb2e3a6 Fix pretty JSON when strings end with backslashes
If a string encoded as part of the output ends in a backslash
(e.g. an edit token), FormatJson::prettyPrint() may incorrectly
treat the unescaped double quote marking the end of the string as
a character that is part of the string.

This is a serious problem in that the "pretty" output may not
necessarily be valid JSON; a later string literal might contain
one or more of these tokens: :[{,]}

To fix the bug, I exploit strtr's behavior when it is given an
associative array having keys of the same length to skip over
escaped backslashes while replacing escaped double quotes with "\x01".

I also updated the corresponding unit test.

Change-Id: I159105b6493c14b82cd0a41a95e04bfed744931e
2013-03-30 16:23:24 -04:00
Kevin Israel
79f80cc495 Combine JavaScript and JSON encoding logic
This will help with improving human readability of JS and JSON
objects encoded by both ResourceLoader and the API. This patch
also adds new "utf8" parameter to the JSON formatter of the API.

Changes to FormatJson class:

* Added escaping of '<', '>', and '&' by default to protect against XSS.
* Removed unnecessary escaping of '/' and added an additional option to
  unescape non-ASCII characters (those above U+007F) as well.
* Added PHP 5.3 pretty printing code (to replace Services_JSON) that
  uses a four-space indent as PHP 5.4 does.

Changes to Xml class:

* Defined Xml::encodeJsVar() in terms of FormatJson::encode()
  and added a pretty printing option. Also added a pretty printing
  option to Xml::encodeJsCall() as well.
* Deprecated Xml::escapeJsString() and QuickTemplate::jstext();
  callers have to add quotes themselves, hence the escaping of
  both double quotes and apostrophes.

Bug: 26818
Change-Id: I1987190f1ba5bf41738e7bd611209706c1f6bb5c
2013-03-27 20:22:45 -04:00
Yuri Astrakhan
9506e3d812 Spellchecked /includes directory
* Ran spell-checker over code comments in /includes/
* A few spellchecking fixes for wfDebug() calls

Found one very strange (NOOP?) line in Linker.php - see "TODO: BUG?"

Change-Id: Ibb86b51073b980eda9ecce2cf0b8dd33f058adbf
2013-03-13 03:42:41 -04:00
Tyler Anthony Romeo
4dcc7961df Fixed @param tags to conform with Doxygen format.
Doxygen expects parameter types to come before the
parameter name in @param tags. Used a quick regex
to switch everything around where possible. This
only fixes cases where a primitve variable (or a
primitive followed by other types) is the variable
type. Other cases will need to be fixed manually.

Change-Id: Ic59fd20856eb0489d70f3469a56ebce0efb3db13
2013-03-11 13:15:01 -04:00
Ori Livneh
10254ee6e8 Fix misleading param name in FormatJson::encode signature
As the FIXME notes, $isHtml has nothing to do with HTML; it simply controls
whether the resultant JSON should be formatted for readability by inserting
whitespace as appropriate.

Change-Id: I90d46d6624d683f18a39c98500bd71bbd0ca3800
2012-11-23 13:51:15 -08:00
jeroendedauw
38c7f444e1 Use __DIR__ instead of dirname( __FILE__ )
We can now do this since we finally switched to PHP 5.3 for MW 1.20 and get rid of the silly dirname(__FILE__) stuff :)

Change-Id: Id9b2c9cd2e678197aa81c78adced5d1d31ff57b1
2012-08-27 21:45:00 +02:00
Reedy
28e7830d78 PHP 5.4 has JSON_PRETTY_PRINT
Use this conditionally when $isHtml is true, and is
also running on PHP > = 5.4. Else return default 0

Change-Id: Ief775720a99d1a305c3f9f4ba7cc04eb96817fb3
2012-08-14 18:57:40 +02:00
Reedy
de185ca16f Remove workaround hack for php bug 46944
https://bugs.php.net/bug.php?id=46944

Fixed for 5.3.0, and as we require >= 5.3.2, workaround is redundant

http://php.net/ChangeLog-5.php

Change-Id: I567466c0c747dba2f903e9258d0f06f725cefb8f
2012-08-12 21:11:46 +00:00
Antoine Musso
aab43dd495 escape tags and entity in doxygen comments
When inserting XML elements inline <such as this one>, doxygen chokes
about it not being known. Simply enclosing the tag in double quotes
prevents doxygen from emitting a warning.

Also enclosed a few invalid functions calls such as \. and double quoted
the HTML entities such as &foobar;

Change-Id: I4019637145e683c2bec3d17b2fd98b0c50a932f1
2012-07-10 17:08:32 +02:00
Alexandre Emsenhuber
aad9d5fd6b Removed checks for the "MEDIAWIKI" constant on files that only define classes.
This checks are not needed in that case.

Change-Id: Ia83447427de8b7ea32aced8ff43c7a252b8d504c
2012-05-23 21:20:42 +02:00
Alexandre Emsenhuber
63176b99b7 Added missing GPLv2 headers in some places.
Also made file/class documentation more consistent.

Change-Id: I1deb70318d01a257b51948ba806d80cd1a239f4f
2012-05-04 08:47:07 +02:00
Chad Horohoe
9ee17e43f6 Simplify $assoc check 2011-12-20 20:15:42 +00:00
Sam Reed
4622da783c More documentation! 2011-10-26 04:15:09 +00:00
Siebrand Mazeland
75c6696aa8 Use consistent notation for "@todo FIXME". Should update http://svn.wikimedia.org/doc/todo.html nicely. 2011-05-17 22:03:20 +00:00
Roan Kattouw
e77aa77cd8 Remove debugging code accidentally committed in r86253 2011-04-17 10:49:27 +00:00
Roan Kattouw
3a7731a6f0 Fix broken check for bad JSON encoders, had been broken since inception and caused the native JSON encoder to always be bypassed in favor of Services_JSON. 2011-04-17 10:48:17 +00:00
Bryan Tong Minh
cd311a1678 Revert r83647, was based on the inability to read the function signature 2011-03-10 12:04:38 +00:00
Bryan Tong Minh
e018563918 Add wfObjectToArray to json_decode to ensure that the return value is an array 2011-03-10 12:02:35 +00:00
Platonides
e98e6abdbd Follow up r82091, which uses the local file constant SERVICES_JSON_LOOSE_TYPE. 2011-02-19 16:59:34 +00:00
Brion Vibber
8982ac25b7 * (bug 23817, bug 26250) User Service_JSON's native associative array mode in FormatJson::decode(), bypassing wfObjectToArray (which is also fixed)
Patches from Tim Yates on https://bugzilla.wikimedia.org/show_bug.cgi?id=23817
2011-02-13 23:08:28 +00:00
Brion Vibber
d10a408d5f Add a @fixme comment on FormatJson::encode(); the '$isHtml' parameter's purpose and function is not documented, but it seems to map to a pretty-printing parameter on Services_JSON(). If that's its purpose, it should be renamed and documented. Since it's a bare boolean parameter with no clear purpose in calls when "true" or "false" is passed bare, it probably also wants a nice legible constant value or two. 2011-02-05 21:06:44 +00:00
Alexandre Emsenhuber
dc50e1a4bf * Added file description headers
* Added @file where needed
2010-08-22 14:31:05 +00:00
Jeroen De Dauw
539ab6f5e5 Documentation o_O 2010-08-12 07:27:48 +00:00
Chad Horohoe
ddbca1b039 Followup r70875, fix default value for decode() 2010-08-11 13:03:45 +00:00
Chad Horohoe
50fe92ffb8 Revert r70874: <RoanKattouw> Dude that just looks kinda ugly 2010-08-11 12:45:59 +00:00
Chad Horohoe
fe72845c82 Boolean parameters suck, but we can make them suck less by adding constants you can use. Also doc, spacing, etc 2010-08-11 12:42:40 +00:00
Jack Phoenix
4bc6efd5be coding style tweaks 2010-05-15 10:39:41 +00:00
Platonides
21a58342fb json_decode -> FormatJson::decode 2010-05-04 15:29:17 +00:00
Michael Dale
3d958564ec * Enhanced OutputPage to support js2 style javascript without scriptloader enabled
** loadGM output
** unique request id based on wikiTitle revision / file time
* Moved jsAutoLoadClasses to setup.php ( made conditional )
2009-09-17 01:19:02 +00:00
Michael Dale
f6a57deb50 * 3983#c3983 array casting does not work so well on json_decode arrays... object2array should work 2009-09-10 19:24:56 +00:00
Michael Dale
df1c7e2055 * should use <?php instead of <?
* and end with \n
2009-09-10 16:56:58 +00:00
Michael Dale
1e4165061b more (bug 20336)
* added json folder 
* moved Services_JSON.php there
* moved Format JSON into its own class
* updated AutoLoader
2009-09-10 14:33:25 +00:00