Commit graph

97 commits

Author SHA1 Message Date
Jackmcbarn ece8cc5e82 Only set indexOffset when the function name is indexed
If a function name contains an equals sign, it doesn't increase the index,
so indexOffset=1 makes Lua see arguments as starting with 0. This change
fixes that, so that indexOffset is only set to 1 if the function name has
an index.

Change-Id: Ifa2dfad4d2f7228c3fd5721ccfc4c235576b663d
2014-09-06 15:42:13 -04:00
addshore e29c8eda63 Use public instead of var in classes
Change-Id: I0c059515cbf0fa8ef3846526a989f1a322fcd8ae
2014-08-17 22:11:26 +01:00
Jackmcbarn fd9ecb9cbe Expose cascading protection directly to Lua
Add a way to fetch cascading protection information from Lua without
needing to call the CASCADINGSOURCES parser function.

Change-Id: I1b3ac18af11d3066f78d27b31da8d6709a6a2631
2014-08-13 12:34:47 -04:00
Brad Jorsch 0367e9bddd Fix deceptively-simple pattern in pure-Lua ustring
The pure-Lua ustring pattern matching functions short-circuit to the
much faster string library when the pattern would match the same against
the raw bytes.

A pattern like "[^a-z]" can match a partial UTF-8 character when applied
bytewise, and so must be detected as unsafe.

Let's also directly test the pure-Lua module, instead of me having to
comment out lines in Scribunto_LuaUstringLibrary::register() whenever I
want to test them.

Change-Id: I91ed3374aadfea379b9db2e13b4248ab20df509e
2014-08-10 01:18:18 +00:00
Jackmcbarn 40b8bd2caa Add comments and remove trailing whitespace
Clean up trailing whitespace from all of our code, and add comments
indicating that apparently unused variables are ScopedCallbacks.

Change-Id: I8e5997797cc7b1c64c5351ec112a18f30edc8fef
2014-07-07 14:46:59 -04:00
Brad Jorsch 85aca87e94 Improve mw.getCurrentFrame handling
Two similar bugs are handled here:
* mw.getCurrentFrame() doesn't work when the module is loaded (only when
  a function is called), which breaks os.date and os.time at module
  scope since I59ad364d.
* mw.getCurrentFrame() gives access to frame args from inside
  mw.loadData, which allows for data leakage between #invokes.

Bug: 67498
Bug: 65687
Change-Id: I82dde43e2601b59c03c6ed4b9365829c40a953a5
2014-07-07 13:11:20 -04:00
Brad Jorsch 631feb8a3d Allow for skipping Lua tests
From the PHP subclass of Scribunto_LuaEngineTestBase, Lua tests run by
Scribunto_LuaEngineTestBase::testLua() may be skipped by adding an entry
for the test name to $this->skipTests.

From the Lua code run by Scribunto_LuaEngineTestBase::testLua(), tests
may be skipped by calling the "markTestSkipped" function exported by the
TestFramework module.

Also, use this new mechanism to skip certain mw.language tests if
Extension:CLDR or a similar extension is not available.

Bug: 67343
Change-Id: I4f0b15073a84bf2f9d8a5d905c3c960941dd2b71
2014-07-01 10:48:36 -04:00
Jackmcbarn 8d1d5ac84c Fix strange mw.html errors with numeric arguments
Some functions in mw.html accept numbers as arguments, but later fail when
constructing the string. This disallows numbers in attribute names, since
they aren't valid anyway, and fixes the remainder of the cases to properly
build the string.

Bug: 67201
Change-Id: Ie7bcbb9d8df580dd8793681f78a8b0719d8a287a
2014-06-27 14:41:42 -04:00
Brad Jorsch bf39827980 mw.ustring functions should accept numbers where string functions do
Lua's string functions tend to auto-convert numbers to strings. We
should do the same in mw.ustring.

Bug: 67201
Change-Id: Icd3c5e93bac19dafd78d737ec9b315daba9f1729
2014-06-27 12:31:04 -04:00
Brad Jorsch ccab415701 Don't cache volatile wikitext
Certain wikitext, such as that containing Cite.php <ref> or <references>
or the #tag versions of the same, should not be cached. This uses the
isVolatile method added to PPFrame in I95b3cf87 to avoid caching the
preprocessed output of such wikitext from frame:preprocess and similar
methods.

Bug: 46815
Change-Id: I1084f87fd863eb22f2f3f3d3ff308b24e20a08ef
2014-06-26 22:03:35 -04:00
Jackmcbarn 780d8e1ec7 Set TTLs on outputs containing times
When os.date, os.time, or mw.language:formatDate are called, set the
appropriate TTL on the output. This needs I412febf3 in core to function at
all, and I3f5a80aa in core to function with formatDate.

Change-Id: I59ad364d502fc247500d94c5606516ad9f98a24d
2014-06-23 15:52:17 +00:00
Jackmcbarn d1030989bc Allow passing nils to mw.html
Rather than calling error() when nils get passed to mw.html methods,
either remove whatever it was that the nil would go to (if that makes
sense), or just do nothing. The seemingly inconsistent use of "not x" and
"x ~= nil" is to allow any falsey value where it wouldn't be ambiguous
(such as class names), but not where it could be (such as attribute values).

Bug: 62982
Change-Id: I76773abbb4394aa9bb8c8a08445e019cade3b2bf
2014-06-19 11:40:39 -04:00
Jackmcbarn 1201618b7d Keep modules' export tables inside Lua
When tables are passed from Lua to PHP, their metatables are lost. Because
of this, they need to be kept inside of Lua to allow the __index
metamethod to return a method to be called by #invoke.

Bug: 64141
Change-Id: I0840bc12b25dee72828ec97d2b205812e4929f2b
2014-05-26 20:01:18 -04:00
Brad Jorsch 35ee461a0b Improve frame:callParserFunction argument validation
From wikitext, $parser->callParserFunction() will always get an array of
strings with at least an element [0]. Let's match this from Scribunto:
stringify numbers, and require that [0] (although in Lua it'll be [1]).

Also fix an old broken unit test.

Bug: 63597
Change-Id: Ie7ac34ae4bce70cec455d90c3f02a658644f6866
2014-05-13 05:14:07 +00:00
aude bb0f10b751 Make LuaEngineTestBase compatible with phpunit 4.1
PHPUnit_Framework_TestSuite::isPublicTestMethod has
been removed from phpunit 4.1.

Change-Id: I191b5f460fbef122d8e916c8f661f09083b33970
2014-05-08 07:11:14 +02:00
Jackmcbarn d74c81c233 Add name to nosuchfunction and nosuchmodule errors
When displaying a nosuchfunction or nosuchmodule error, include the name
of the nonexistent function or module.

Change-Id: I17fc2c68dc8267302a82eee3cb2c5df9b5a3c46c
2014-04-13 21:29:05 -04:00
Brad Jorsch 708d87c181 Prevent passing information between #invokes using math.random
It's possible to pass information between multiple #invokes on a page by
having the first call math.randomseed with one of a set of known seeds
and then having the second examine the output from math.random to
determine which of those known seeds was used.

Prevent that by calling math.randomseed( 1 ) when invoking (see the bug
for details on why that seed). But avoid doing so if e.g. a
frame:expandTemplate() call results in a recursive invoke.

Bug: 62291
Change-Id: Id01cb63eca52ced29bf4efebc38beb9f159b7b0e
2014-04-01 13:46:09 -04:00
Brad Jorsch 29452c9bef mw.language: Fix exceptions and add tests
Various methods are throwing exceptions when passed invalid language
codes. Those need to be caught.

And we should really add unit tests for the mw.language library, too.
Doing so exposed another bug (in lang:gender), which is also fixed here.

Bug: 62242
Change-Id: Ib7d257cbb1ce179c510273526910d6ac5f3cac5d
2014-03-09 02:56:06 +00:00
Brad Jorsch 84d96e843c LuaStandalone: Release functions when no longer referenced
The LuaStandalone interpreter needs to keep a mapping from integers
returned to PHP to the corresponding function. But if it never releases
these functions when PHP no longer has any reference to them, it can
result in Lua running out of memory if a module with a large number of
functions is invoked many times in one page.

The fix here is to track which function ids are referenced from PHP, and
periodically send the list to Lua so it can remove any that are no
longer used from its cache.

This also takes care of another issue where having multiple interpreter
instances and passing function objects from one into another could call
the wrong function in Lua.

Bug: 51886
Change-Id: I4f15841051f7748d1d6df24080949e5cbd88f217
2014-02-25 22:59:05 +00:00
Brad Jorsch ed54cd7032 Fix PHP implementation of mw.ustring %W
Incorrect regex is making it match basically everything.

Bug: 60908
Change-Id: Ic1c5a740c6b825bb7dfacf684d0c7fa905b135ee
2014-02-10 00:45:46 +00:00
Jackmcbarn 2b06e83816 Remove message formats other than plain
Message formats other than plain should have never been exposed in this
way, as they allow link tables, etc. to be bypassed and serve no useful
purpose. This removes them, and also removes title, as it serves no
purpose without them.

Bug: 60758
Change-Id: I96284ffbe986a9cd92d2bde1ffdb746029bad989
2014-02-03 15:29:39 -05:00
Marius Hoch aa943d5795 Validate the type of the tagName given to mw.html.create
Change-Id: I35f70701ba6156325dfd268903452e6279aca528
2014-01-31 02:08:11 +01:00
Brad Jorsch 5cf28c5a15 Use a child frame in frame:expandTemplate
If we don't do this, then the section edit links point to the wrong page
if we expand a template that contains section headings.

Bug: 55525
Change-Id: I00bda935be3e8b9c0f86fd0f131814207fbb34a7
2014-01-30 22:47:31 +00:00
Marius Hoch 2bcd815d62 Allow changing the title used by Scribunto_LuaEngineTestBase
Change-Id: I8916fc855b96a566d775c947b117a31ad4a49dc8
2014-01-16 17:00:30 +01:00
jenkins-bot b1d7bac7b0 Merge "Add protectionLevels variable to mw.title output" 2014-01-15 20:21:48 +00:00
Jackmcbarn 9902ec1284 Add protectionLevels variable to mw.title output
Include a protectionLevels variable in the output of the mw.title.*
functions, containing the contents of the title's mRestrictions
array (i.e., its protection levels)

Change-Id: I79c9fed64bacfc90aee1d411a3e1b47e44c99755
2014-01-15 14:53:18 -05:00
Brad Jorsch f52136eada Handle invalid keys in Lua-to-PHP calls for LuaStandalone
PHP can't handle having arrays/objects or functions as keys in its
arrays, so make sure we don't try to pass them from Lua. Booleans aren't
really well-handled either, so let's disallow them too.

Also, add tests for proper stringification of floats and infinities when
those are used as keys.

Note this behavior change is needed to match the change in LuaSandbox
for fixing bug 54527, but isn't itself a security issue.

Change-Id: I1e2951bbe8cb78358650ad377bf7119fcac4485d
2014-01-14 10:03:42 -05:00
Marius Hoch ac62e34952 Add mw.html to Scribunto
A module for building complex HTML from Lua using a
fluent interface. The module is originally from enwiki,
but the authors allowed us to reuse it under GPLv2+
(as stated in the file).
The module will be loaded per default and comes with
unit tests.

As discussed on wikitech-l:
http://lists.wikimedia.org/pipermail/wikitech-l/2013-December/073320.html

Change-Id: I7c8d4378091c13d5ace0dd1fcbb4e27163e8c896
2014-01-03 00:04:34 +01:00
Brad Jorsch 0763e22292 Expose PPFrame::getTitle to Lua
This field already exists in PHP with exactly the content requested in
bug 47089, so we may as well expose it on the frame object.

Bug: 47089
Change-Id: I672820589f6ebc7c4daad29b5eb156733a5bc5cc
2013-12-10 04:26:03 +00:00
jenkins-bot a18a762fd7 Merge "Add mw.isSubsting()" 2013-11-06 18:26:56 +00:00
Brad Jorsch 8872dd3eda Update phpunit @group annotations
Some tests weren't being run as they should have been when phpunit's
--group option was used.

Change-Id: I29ff9a04322b91cc085247e5663dfc7bc67d3439
2013-11-01 11:56:24 -04:00
Brad Jorsch d4331f0c96 Add mw.isSubsting()
It's already possible to detect whether the current template is being
substituted via ParserFunctions (see [[en:Template:Ifsubst]]), and a
similar trick works with frame:preprocess. So we may as well provide the
flag directly.

Bug: 47828
Change-Id: Id06d27c6283ee589a8830b78c04e56978e0ac6da
2013-10-31 22:05:00 -04:00
jenkins-bot 47fc1462c4 Merge "Properly handle spaces/underscores in mw.site.namespaces" 2013-10-31 19:36:03 +00:00
Brad Jorsch ec2f67c7ea Regularize whitespace handling in mw.title
Specifically:
* String conversion in non-URL contexts (e.g. .prefixedText) uses spaces
  instead of underscores.
* Setting .fragment now applies the same transformations that are done
  (in PHP) by mw.title.new.

Bug: 56217
Change-Id: I12e354636bcde3327864088175fb4de61aecc81a
2013-10-31 11:54:26 -04:00
Brad Jorsch f31aa37adc Properly handle spaces/underscores in mw.site.namespaces
The PHP call that makes mw.site.namespaces work case-insensitively
doesn't handle non-standard spaces/underscores. So standardize them
before the call.

Bug: 56216
Change-Id: I4758478b126858fb581614f64eb15472f42fef51
2013-10-31 11:09:16 -04:00
jenkins-bot 99b96d8b14 Merge "Add frontier pattern (%f[set]) to ustring" 2013-08-30 06:22:40 +00:00
Brad Jorsch d8314539da Fix mw.ustring edge cases
A few edge cases were being incorrectly handled:
* mw.ustring.sub( 'abc', 1, 0 ) returned 'a', not ''.
* mw.ustring.codepoint( 'abc', 1, 0 ) returned 97, not no results.
* mw.ustring.codepoint( 'abc', 4, 4 ) returned 99, not no results.
* mw.ustring.gcodepoint had the same issues as mw.ustring.codepoint.

Change-Id: Ib8c0ef5a8073106eb7d90d0aa0513be4525dca08
2013-07-03 11:49:52 -04:00
Brad Jorsch ad3e5a7c48 Fix LuaSandbox mw.ustring.byteoffset with negative 'i'
Negative values for 'i' in mw.ustring.byteoffset are supposed to count
from the end of the string. But in LuaSandbox, it was actually counting
from two bytes before the end of the string due to a typo.

Fix that, and add some tests for it.

Bug: 50176
Change-Id: Iceee1022a55abd7a08df1ea7843e1277eb02798b
2013-06-25 10:06:01 -04:00
Brad Jorsch 82820aafc8 Add frontier pattern (%f[set]) to ustring
The "%f[set]" frontier pattern has been in Lua 5.1 since the beginning,
but was undocumented until Lua 5.2. And the code is even unchanged from
5.1.0 to 5.2.1. So there's no reason not to implement it in ustring too.

Note the changes to UstringLibrary.php are somewhat large, because it
splits the "convert a Lua bracketed charset to PCRE" code into a
separate function and it changes the handling of mw.ustring.find's and
mw.ustring.match's 'init' parameter from "substring, match from 0, then
add back on $init" to "use preg_match's $offset and use \G instead of ^
where this matters". Both of these are necessary to properly support
%f.

This also fixes a bug in the pure-Lua code (not used in Scribunto)
exposed by the unit tests for %f where %z was matching '\1' rather than
'\0' and %Z everything except '\1' instead of everything except '\0'.

Bug: 48331
Change-Id: Ie0b95ef5b734db53d6adc9de5dae4874f8944c08
2013-05-12 10:27:36 -04:00
Brad Jorsch d6f3633428 (bug 47365) Fix edge cases in mw.ustring.find, mw.ustring.match
The following errors are fixed:
* PHP warning and wrong return value with empty pattern and plain
* Incorrect offsets returned when init is larger than the string length
* Incorrect captured offsets returned when init is excessively negative

Bug: 47365
Change-Id: I9741418287dc727747326d6a19678370ce155a2b
2013-05-10 06:00:02 +00:00
Brad Jorsch adb7b5f939 Make pairs() work with various library objects
Users are reporting disappointment that using pairs on title objects
doesn't let them see the fields available. It's easy enough to add a
__pairs metamethod to allow pairs to work on title objects, so let's do
that.

The same can be done for mw.uri objects.

For mw.message objects, we can easily enough change the implementation
to be like mw.language objects, which doesn't have this problem.

For mw.site.stats, we may as well just remove the load-on-demand feature
since it will be demanded as soon as the environment is cloned for the
first module.

Change-Id: Ie5a3b5684b2bb6c090f9994baa03977687688929
2013-04-02 17:23:42 +00:00
jenkins-bot a85275592c Merge "Add text module" 2013-03-27 16:55:07 +00:00
Brad Jorsch 3d4a81653c (bug 46405) Fix errors in mw.title.new( pageid )
mw.title.new( pageid ) should not throw an error for an nonexisting
pageid, just return nil. Similarly, it should always return nil for 0,
rather than returning the last non-existent title created.

Change-Id: I3cdbb24fc785aef0f8e75fba1feccd26ac5b7370
2013-03-21 09:39:25 -04:00
Brad Jorsch 0db3d7c6d2 Add text module
This exists for some common text-processing functions that aren't
included in string (and therefore also aren't in mw.ustring), as well as
a logical place for the "unstrip" function requested in bug 45085.

Bug: 45085
Change-Id: I47356215fcc8ddeed5f901cd933a30021394bd78
2013-03-20 10:10:15 -04:00
Brad Jorsch 541e61a1c4 (bug 41769) Add frame:callParserFunction() and frame::extensionTag()
Requires change I339b882010dedd714e7965e25ad650ed8b8cd48f to
mediawiki/core.

Bug: 41769
Change-Id: I0138836654b0e34c5c23daaedcdf5d4f9d1c7ab2
2013-03-18 13:46:26 -04:00
Brad Jorsch b6ea64f132 Make mw.getCurrentFrame() work in console, add frame:newChild()
It would be helpful for debugging if a frame object could be gotten in
the console. To that end, add an empty frame when running in the console
and allow it to be returned by mw.getCurrentFrame().

It would also be helpful to be able to create frames with arbitrary
arguments, again for testing. Fortunately support for creating child
frames with arbitrary arguments already exists in core, so we can just
use it.

And for good measure, be sure to restore the $engine->currentFrames
array even if the Lua code throws an exception.

Change-Id: I1dc8602d63af75424f267c42a3743fabbc1827f7
2013-03-12 14:42:36 +11:00
Brad Jorsch 5faa00150c (bug 45684) Don't count argument parsing time towards Lua limit
Currently, the time taken to parse the arguments passed to a Lua
function from #invoke will be counted against Lua's 10-second limit.
This is counterintuitive, and can remove incentive for users to convert
templates to Lua since they may have to convert a whole stack at once.

Note this requires change I11881232 to mediawiki/php/luasandbox to
actually have any effect.

Bug: 45684
Change-Id: I773950e4c399b8a1cfa6d1cde781a069d286b3bd
2013-03-12 03:18:05 +00:00
jenkins-bot ec86b161cd Merge "Fix unit test engine↔parser assignment" 2013-03-07 19:44:54 +00:00
Brad Jorsch b926846a7a mw.title:getContent() should record in templatelinks
Since the content of a page is being loaded, this should be recorded in
templatelinks so things can be updated properly when that page is
edited.

Change-Id: I3e720fee2705f6c08ac0456c3cab0ed4ede84536
2013-03-07 13:02:41 -05:00
Brad Jorsch 981ea4c39f Fix unit test engine↔parser assignment
In the unit tests, the engine is given a parser, but the parser is not
being associated back with the engine. So in some cases, that could lead
to *another*, default engine being created for certain operations.

Fix that.

Change-Id: I79995c2635d9e470931b84dc1854dae26772bbe3
2013-03-07 01:42:19 +00:00