Commit graph

214 commits

Author SHA1 Message Date
C. Scott Ananian 248e886dc0 Use class instead of id for scribunto errors
This avoids having multiple HTML elements with the same `id` attribute
when certain Scribunto errors reoccur more than once on the same page.

Bug: T375539
Change-Id: I123a0f0046a616f63506f096651ae917bbc65cc5
2024-09-24 15:47:25 -04:00
Umherirrender 7c1ea4284e build: Use inline ignore for MediaWiki.Usage.ForbiddenFunctions
This avoids addition of new code with the function,
or at least it gives extra attention on review when new code also uses
the inline ignore

Change-Id: I17981b52d9f96ee5f19ba46cf370c7477c81a72a
2024-09-23 17:40:51 +00:00
Siddharth VP 286add50b5 Don't record transclusion of current page from title methods
When the content or categories fields are accessed for the title object
of the current page, skip adding an entry in templatelinks.

* Cuts 5.9 million rows from enwiki templatelinks, where citation
templates use :getContent() to know what date format to use while
formatting citations.
* Prevents confusion about why a page is shown to transclude itself,
frequently brought up on technical support forums.

Change-Id: Id6a5e00572031f4a7258c99c6dc0f802385ee5fe
2024-09-23 16:28:03 +00:00
thiemowmde a630e3d129 Make use of ??=, ?: and similar operators where possible
The main benefit of these operators is that they avoid repeating
parts of the code.

Change-Id: I86ea0eb02715ad5b7c62a71849309ed7095c5972
2024-09-21 20:00:50 +00:00
Isabelle Hurbain-Palatin b1cc0225a9 Set language on ScribuntoContentHandler new ParserOutput
I have reproduced at least one instance of the message "ParserOutput
does not specify a language and no page language set in helper." by
trying to query a rest.php html endpoint on a module with the
scribunto-doc-page-does-not-exist message deactivated (which is the case
on wikidata).
I do not know if that path is the one that triggers the current large
amount of these messages coming from Wikidata, but it looks reasonable
to fix that one at least.
I'm also not sure this fix is the right one - I'll let people with more
Scribunto and ContentHandler experience chime on that :)

Bug: T362312
Change-Id: I26c1683c803f5cafd08de83e8b428031414d1c4e
2024-09-12 11:49:40 +02:00
Siddharth VP c08452684c Rename SyntaxHighlight RL module
Bug: T368166
Depends-On: I999937c1f6303ecc64adb6285e73a9ce10f67bd8
Change-Id: I68d13d6d3fed7c70d795b0864bf5fb9ef1409c0a
2024-09-09 19:35:26 +00:00
Ebrahim Byagowi 27136f2afa Avoid use of deprecated wfGetUrlUtils
Change-Id: Ia36e0cf4a390f32fad7da55a503ac15afa85317a
2024-09-08 09:47:06 +03:30
James D. Forrester bb71303a81 Replace use of deprecated global URL functions with wfGetUrlUtils()
This is a quick fix rather than the proper DI fix.

Bug: T319340
Change-Id: I9e3bd0049b28efc634e1f394a487f86e27ef9e0d
2024-08-29 13:35:12 -04:00
thiemowmde 5ced07c366 Improve a few PHPDoc type hints
Mostly unspecific arrays that can be made more specific. This patch
intentionally doesn't touch any code, only comments.

Change-Id: I5b4690e6160c6c543f9fcb2f62c41f6329d48bdb
2024-08-27 16:54:41 +00:00
thiemowmde ffafba0695 Remove meaningless return true from hook handler functions
Returning true is the same as returning nothing. It's only meaningful
when a hook handler can also return false. Some actually do this.
I'm not touching these.

See Icccf60b for the reasoning why the added `@return void` are
beneficial.

Change-Id: I6de7addee853ff183058e6c84e87a5b275c785e8
2024-08-27 08:47:46 +00:00
C. Scott Ananian 13b7e5ab31 Add title.content alias for Title::getContent()
No other access in the Title class contains a `get` prefix, so this
makes the title methods consistent.

Bug: T373047
Change-Id: I4a3a7498462b7b3b6143dc61f529e526ccb112e2
2024-08-22 11:30:26 -04:00
Siddharth VP a414aba445 Expose page categories to Lua
Added `categories` property in the lua title class which fetches page
categories. Analogous to getContent() which fetches page content. This
increments the expensive function count.

This enables category-specific editnotices (T85372) and other use-cases.

Bug: T50175
Change-Id: Ie8e0762c537374e6086abe9f9916b8200582776a
2024-08-07 16:50:07 +00:00
Mark A. Hershberger c684be1e52 Capture stderr from ‘lua -v' to get the version number.
Bug: T368832
Change-Id: Iff6e908686ee0e81088a81316927d1b250e6af1a
2024-07-08 21:11:23 +00:00
Bartosz Dziewoński ce1fa91c54 Don't depend on Message implementation details
You're not really supposed to know how Message represents the
parameters internally.

Change-Id: I5767dcc96af77b21655609cb9ce43bcaf9e07e19
2024-06-17 16:25:22 +00:00
Umherirrender db8f22c1d4 Use namespaced classes
Changes to the use statements done automatically via script

Change-Id: Iedad37d75aee4d2a6960ab20148f813c6cf7061a
2024-06-10 20:41:43 +02:00
theknightwho acd7ec66ea Avoid modules that should return false wrongly returning true
When modules are first loaded by `require` in package.lua, it checks
for a return value with `if res then`. If that check fails, the module
will be cached with the default value `true` at line 109, and that
value is then returned.

That check should be changed to `if res ~= nil then`, because
otherwise a module with a return value of `false` will be cached with
the value `true`.

This also necessitates a corresponding change to the check at line 93,
which is when `package.loaded` is initially checked to see if a package
has been loaded before, as that value could be `false`.

Bug: T362045
Change-Id: Id42e26a922c13971c25ed8654e822dc6fdcb6ca7
2024-05-24 02:10:41 +00:00
theknightwho fc3104e9c6 Optimisations for mw.clone
`mw.clone` is called on the global table for each new `{{#invoke:}}`,
which is a major contributor to loading times on large pages. This
change introduces various speed optimisations that make it around
15% faster:

* `recursiveClone` is declared outside of the main function, to avoid
  generating a new closure each time `mw.clone` is called.
* This means `tableRefs` has to be passed as an argument to
  `recursiveClone` instead of being an upvalue, which also decreases
  access time.
* `recursiveClone` is only called after a `type` then `tableRefs` check
  confirms a value is an unseen table, meaning it's only called when
  needed, not for every key/value pair (which add noticeable overhead
  when cloning large tables).
* `getmetable` is only called once, not twice, by using a local
  variable.
* `tableRefs[elt]` is only accessed once, by using a ternary operator.
* Any global accesses have been replaced by local ones.

Bug: T357199
Change-Id: I51593a971f0be728255723ca300d1ce692ead256
2024-05-24 02:08:41 +00:00
theknightwho a091b46d65 Use correct variable for error message in mw.loadJsonData
`mw.loadJsonData` in mw.lua throws an error if the input argument is
not a string. The error message should refer to the type of the input
argument `module`, not that of the variable `arg` (which is usually
undeclared, giving the type `nil`).

This also necessitates updating test `mw.loadJsonData, bad title (1)`
in CommonTests.lua, which checks for the error message if the input
is the number 0; the correct message should say "got number", not
"got nil".

Bug: T364987
Change-Id: I8362e91489a8ddc4c890e2799892f692c497c9b9
2024-05-15 14:05:32 +01:00
Wandji69 a7111aac69 Use ObjectCacheFactory instead of deprecated ObjectCache method
Bug: T363770
Change-Id: I416c511ef22afde3d519f66818c9b64e0ef21178
2024-05-14 20:07:42 +00:00
wfan fed1972733 Migrate MediaWiki.scribunto.traces.* to statslib
Bug: T359275
Change-Id: Idd234bbb34747b4557dcded73eace69131a2566a
2024-05-04 22:40:52 -07:00
thiemowmde 4c4392b4f3 Use PHP's more recent ?? operator where it makes sense
… as well as avoid an overly complicated printf call where a string
cast does the same.

Change-Id: I66b51572d0f2f9e72b9ef7c32f9a62ebb698d739
2024-04-28 17:28:51 +00:00
thiemowmde fbbb56f1ef Stop using the Xml class in this codebase
Bug: T341775
Change-Id: I2ccecaccf1558d9c54d88e2d1f43ca59d7157d15
2024-04-26 13:20:06 +02:00
Dringsim 8307c6b328 Change use of AtEase to at operator
Change-Id: I0c92ba1ca00d43b3e7d636872372dd451f372a29
2024-04-23 01:57:14 +00:00
C. Scott Ananian dfdf5cb54a LanguageLibrary: Ensure language code is a string
Defense in depth: catch the error early when calling mw.language.new()
with a non-string `code` argument; but also check late when popping
the code off the argument array in case something altered the code
property after construction.

Bug: T361664
Change-Id: I04476d3e1c32c87e1df95cc9a0dddcaff475b756
2024-04-11 16:20:24 +00:00
C. Scott Ananian 5bde75bf38 TitleLibrary: Don't register external titles as dependencies
In TitleLibrary::getContentInternal(), an external (interwiki) title will
fail when we try to `Parser::fetchCurrentRevisionRecordOfTitle`, but by
that time we've already tried to add it to the dependency list for the
page via `ParserOutput::addTemplate()`, which causes issues further on.

Bug: T362222
Change-Id: I171e97f17b6de176f92ced47757d10c341c979fd
2024-04-10 11:32:44 -04:00
Timo Tijhof 796c3dddfd Replace 'hash' with CACHE_HASH to improve discovery
Bug: T139216
Change-Id: Icd75953ccdb71543f3100f659fa32e1a1e7d1e26
2024-03-29 15:38:08 -07:00
Umherirrender 52af61330f Add explicit parentheses around mixed boolean operator
Mixing different binary boolean operators within an expression
without using parentheses to clarify precedence is not allowed (T358966)

Change-Id: I6d3edc5f8dddcfc6bd6a7d2a8f2ad9467372908d
2024-03-23 00:59:35 +01:00
Fomafix 2a45dbb577 Inject services into ApiScribuntoConsole, CodeEditorHooks and Hooks
Also use private instead of public for internal function reportTiming.

Change-Id: I52c301d11244436ddc142ec84a7c6740a4ea4d32
2024-03-18 04:40:47 +00:00
Fomafix 70605bf630 Replace global configuration variables
Use overrideConfigValues instead of setMwGlobals in tests.

Change-Id: Icaf7d8d9bb3a4c1eb981ef83a12719c2619039fa
2024-03-18 04:40:44 +00:00
Umherirrender 09f2905ae6 build: Upgrade mediawiki/mediawiki-codesniffer to v43.0.0
Change-Id: Idf24dd1742439d80aaecc308eda386a1afbb0bdc
2024-03-10 23:46:30 +01:00
C. Scott Ananian d00b6aa0ea ParserOutput::setText() has been renamed to ::setRawText()
Depends-On: I3ef34814ab9473cc70d0a6806e8c5a4a02b73491
Change-Id: Iaf220266c8544ab2f1969af4c2b4ec16aca1e46d
2024-02-21 17:13:48 +00:00
vlakoff 6c340bff8d Synchronize mw.text.nowiki() with wfEscapeWikiText in core
Added escapes for "!" and ";" as well as additional escapes
at beginning and end of string.

Bug: T168763
Co-Authored-By: vlakoff <vlakoff@gmail.com>
Co-Authored-By: C. Scott Ananian <cananian@wikimedia.org>
Depends-On: I34f2fa8c329e6f6771453b2f94dc4afbec31dac8
Change-Id: I6c9dcfdbbb2c6eff9414e24d3f2693ebe576505a
2024-02-15 05:29:36 +00:00
Reedy d07d4039e6 mw.title: add pageLang property
Bug: T321239
Bug: T161976
Change-Id: I0f7c80a4bd2339c3cce34030b862e29d984aa714
2024-01-17 18:20:06 +00:00
C. Scott Ananian f5f4355633 Set some title properties to nil for interwiki links
In order to avoid misleading the caller, set some title properties to
nil for interwiki links. That value should still be falsey, but can
prevent making unwarranted assumptions about the destination of
interwiki links.

Split from I847ac4b7587b98be06b25fe14765e9efdc7b774d because this
could possibly have effects on existing modules.

Change-Id: I06efea9b264ba0f09bfb36e6bf1bb04f1cdd03e4
2024-01-16 20:49:42 +00:00
vlakoff e1bd4911bd Add a property "talkNsText" to mw.title objects
Bug: T180911
Change-Id: I847ac4b7587b98be06b25fe14765e9efdc7b774d
2024-01-16 20:36:22 +00:00
Umherirrender 68aebf853c Use namespaced classes
This requires 1.42 for some new names

Done automatically via script

Change-Id: I6f6899981eab571cabb57534d7d98ce546a30a75
2024-01-05 19:24:06 +01:00
Reedy 836baa7142 LuaBit: Minor cleanup
Bug: T353678
Change-Id: I19527eb162c20b5a6017dc3c4dd2b98bf5cad5d9
2023-12-18 23:46:16 +00:00
Reedy 54e9dd05be LuaBit: Save files with unix EOL
Change-Id: Ia757198e32e2beb060ccee9577012bf3c91d49e5
2023-12-18 23:46:09 +00:00
C. Scott Ananian 9694e8f543 mw.language: add :toBcp47Code() method
This allows conversion of MediaWiki-internal codes to standardized
codes suitable for inclusion in HTML.

Change-Id: I5d2102ca57cc6861b8ec144a90f9c17b630f38ce
2023-12-15 00:18:54 +00:00
DannyS712 e30641e439 Add redirects for modules
ScribuntoContent now supports content being redirects, if the underlying
ScribuntoEngine does so. For Lua, a redirect looks like:

    return require [[Module:Foo]]

which also happens to be perfectly valid Lua. There is intentionally no
`#REDIRECT`-style token (like in wikitext/JavaScript/CSS) because no one
will create a page with this content except for the purposes of a
redirect.

Bug: T120794
Co-Authored-By: DannyS712 <DannyS712.enwiki@gmail.com>
Co-Authored-By: C. Scott Ananian <cscott@cscott.net>
Co-Authored-By: Jackmcbarn <jackmcbarn@gmail.com>
Change-Id: I405e7953d00af8a34d5e8addc61a245732c71e8e
2023-12-13 17:34:30 -05:00
Timo Tijhof 8e0684da7a ScribuntoContentHandler: Add lang="en" to fallback <pre>
It currently inherents from the mw-parser-output div, which sets
it based on the documentation page (content language).

Change-Id: I79847cd8cfe75598c843e96a09d9aa61b00304a9
2023-12-12 12:31:10 -05:00
Bartosz Dziewoński ae8c68b051 ScribuntoContentHandler: Fix Phan suppressions
Title::castFromPageReference() and similar methods can return null
instead of Title when the parameter is null. Although that could not
happen in this code, there is no way to tell that to Phan, so
suppressions were required to avoid its warnings about possible nulls.

Instead, since MW 1.41, we can just use Title::newFromPageReference(),
which can't return null (and doesn't accept null as the parameter).
See also Ida4da75953cf3bca372a40dc88022443109ca0cb in MediaWiki core.

Change-Id: Ia3c415cdd68fe4b19869aa8eb8e816e707bb5ad6
2023-12-01 22:37:57 +00:00
Timo Tijhof 09f92105ed ScribuntoContentHandler: Remove redundant Title::getPageViewLanguage use
When OutputPage/Skin call ParserOutput->getText(), this already takes
care of the appropriate attributes. This redundancy is non-obvious
however, because in this code $parserOutput is re-purposed in an odd
way that makes it seem like it has to mark its own language, so as to
separate it from the appended portions for validation error and
syntax highlighted code.

However, at least the way the code has been in recent years, this
has always been a redundant wrapper since OutputPage/Skin already
set the same attributes on the resulting container. With T341244
closed, this is more obvious now that it is located in
ParserOutput::getText, but even before I worked on T341244, the Skin
will have already been doing the same thing as this code was doing,
setting the same redundant attributes.

Bug: T350806
Change-Id: Idb8471eec5d5ac39b7a347c70f3a618eba18a57b
2023-12-01 22:35:41 +00:00
Timo Tijhof 61874ca30e ScribuntoContentHandler: Refactor fillParserOutput()
The ParserOutput object used here starts life as the ParserOutput for
parsing the docpage (wrapped via an interface message). In order to
remove use of the Title::getPageViewLanguage method there, we need to
re-arrange some logic such that we parse the doc page first, and see
what language it was rendered in, instead of currently where
Title::getPageViewLanguage tries to "guess" what Parser and
LanguageConverter will do.

As prep for that stop reading/writing the HTML text of this
ParserOutput object in favour of being more like the Parser itself,
which accumulates metadata in ParserOutput and calls setText only
once at the end.

* Refactor highlight() to return standalone HTML instead.
* Refactor validation error to append to $html instead.

Other improvements while at it:

* Document how stuff works today.
* Clarify variable names.
* Separate concerns better by moving responsibility of `<pre>`
  fallback to highlight(), and limiting knowledge of
  ScribuntoEngineBase to the caller.

Bug: T350806
Change-Id: I9fe6d93727f29c284ea21db6edd6a2b1663e8e06
2023-12-01 22:25:51 +00:00
C. Scott Ananian d97ce9851c Ensure that Scribunto exceptions have a page context set
Some exception messages add tracking categories, which fail when there
is no title context.

Bug: T351045
Change-Id: I47d0160010c9da5a9a9974718a432fd5e79f8286
2023-11-27 14:08:12 +00:00
Umherirrender 8f6ead1f82 Replace empty() with falsy check
empty() should only be used to suppress errors
When the type of the variable is array,
a falsy check is the same (checks for null, false and empty array)
Found by a new phan plugin (T234237)

Change-Id: Id855be9dd25a27d9e46e3065dacbd268177b073d
2023-10-21 23:30:25 +02:00
Umherirrender 945a7871cd Replace empty() with isset()
The property is set to true or is unset,
isset is easier to understand in that case

Change-Id: Ib98e387ae1a2352556f57aee93da92bdf5453633
2023-10-21 20:38:47 +02:00
C. Scott Ananian 1eea8281c9 Assign UUIDs to Scribunto errors independent of page parse order
This replaces the use of ParserOutput::addJsConfigVars(), deprecated
since 1.38, and ensures that the IDs used for error messages are
independent of page parse order. (See T300979.)

This is an improved replacement for Ibd3fbcbc774491179b0d4fe29ba3b6a128220703
which was reverted (T346094).

Bug: T300307
Bug: T305161
Bug: T346094
Change-Id: I2c660972b289bbad730ceee1325d70d5ba75d27e
2023-10-13 15:23:22 +00:00
Umherirrender 29a9a8aec6 build: Change phan suppression to @phan-var for proc_get_status return
phan under php8.1 also detects PhanImpossibleTypeComparisonInLoop here,
but that cannot suppressed under php7.4,
so use @phan-var to set to the correct type, but keep the comment.
The suppression was added with 8328acb9 for a update of phan

According to the doc proc_get_status can only return array
(at least under php8.1)

Change-Id: Ieda5abc30126eed2e3a9f5fc283d36e64180f496
2023-10-06 19:37:10 +00:00
Umherirrender e126de6a78 Rethrow TimeoutException when using mw.language.formatDate function
timeout while converting timestamps should be a timeout for the whole
parse of that wikitext and not hidden by a invalid time.
Just let the exception bubble up to the caller
See ca71e69f for more information

Change-Id: I1f44e45dcc9b052717814990a3f5ce3a1bdf9d26
2023-10-02 21:03:02 +02:00