Commit graph

232 commits

Author SHA1 Message Date
Amir Sarabadani
15a278189f Reorg: Move MWTimestamp to MediaWiki\Utils
Bug: T321882
Change-Id: I48c10343295c4eb3d9ef8037343b0070e928f040
2023-08-19 05:53:40 +02:00
thiemowmde
ef84619cd3 Consistently use LogicException for test-only methods
"BadMethodCallException" sounds like it would fit, but it does
have a very different meaning, described as "exception thrown if
a callback refers to an undefined method or if some arguments are
missing". This is not what's going on here. These are methods that
should only be called from unit tests.

This appears to be a common mistake, often copy-pasted.

Change-Id: Ib39e28f596a883481d5f526460a5c871c75f5313
2023-07-22 16:21:42 +00:00
Subramanya Sastry
c6fcbbaf44 Fix comment in ParserOptions::optionUsed to reflect current reality
Change-Id: I4792e8d3b0be2507c72a6ca88ce2ed0ebddd452e
2023-06-29 18:20:46 -05:00
Umherirrender
e04d3a28f6 Replace internal Hooks::runner
The Hooks class contains deprecated functions and the whole class is
going to get removed, so remove the convenience function and inline the
code.

Bug: T335536
Change-Id: I8ef3468a64a0199996f26ef293543fcacdf2797f
2023-05-11 06:17:38 +00:00
C. Scott Ananian
cfd9c516e1 Allow setting a ParserOption to generate Parsoid HTML
This is an initial quick-and-dirty implementation.  The
ParsoidParser class will eventually inherit from \Parser,
but this is an initial placeholder to unblock other Parsoid
read views work.

Currently Parsoid does not fully implement all the ParserOutput
metadata set by the legacy parser, but we're working on it.

This patch also addresses T300325 by ensuring the the Page HTML
APIs use ParserOutput::getRawText(), which will return the entire
Parsoid HTML document without post-processing.  This is what
the Parsoid team refers to as "edit mode" HTML. The
ParserOutput::getText() method returns only the <body> contents
of the HTML, and applies several transformations, including
inserting Table of Contents and style deduplication; this is
the "read views" flavor of the Parsoid HTML.

We need to be careful of the interaction of the `useParsoid` flag with
the ParserCacheMetadata.  Effectively `useParsoid` should *always* be
marked as "used" or else the ParserCache will assume its value doesn't
matter and will serve legacy content for parsoid requests and
vice-versa.  T330677 is a follow up to address this more thoroughly by
splitting the parser cache in ParserOutputAccess; the stop gap in this
patch is fragile and, because it doesn't fork the ParserCacheMetadata
cache, may corrupt the ParserCacheMetadata in the case when Parsoid
and the legacy parser consult different sets of options to render a
page.

Bug: T300191
Bug: T330677
Bug: T300325
Change-Id: Ica09a4284c00d7917f8b6249e946232b2fb38011
2023-03-26 21:46:05 -04:00
James D. Forrester
ad06527fb4 Reorg: Namespace the Title class
This is moderately messy.

Process was principally:

* xargs rg --files-with-matches '^use Title;' | grep 'php$' | \
  xargs -P 1 -n 1 sed -i -z 's/use Title;/use MediaWiki\\Title\\Title;/1'
* rg --files-without-match 'MediaWiki\\Title\\Title;' . | grep 'php$' | \
  xargs rg --files-with-matches 'Title\b' | \
  xargs -P 1 -n 1 sed -i -z 's/\nuse /\nuse MediaWiki\\Title\\Title;\nuse /1'
* composer fix

Then manual fix-ups for a few files that don't have any use statements.

Bug: T166010
Follows-Up: Ia5d8cb759dc3bc9e9bbe217d0fb109e2f8c4101a
Change-Id: If8fc9d0d95fc1a114021e282a706fc3e7da3524b
2023-03-02 08:46:53 -05:00
C. Scott Ananian
24d69ef952 Deprecate Parser::getFunctionLang()
This is identical to Parser::getTargetLanguage() in modern MediaWiki,
since 7df3473cfe in MW 1.19 (2012).

Bug: T318860
Depends-On: If5fa696e27e84a3aa1343551d7482c933da0a9b6
Depends-On: I87a7ceedce173f6de4bb6722ffe594273c7b0359
Change-Id: Ieed03003095656e69b8e64ed307c6bd67c45c1e7
2022-11-16 16:47:16 -05:00
daniel
118d4980b2 Track the reason for rendering.
Allow the causeAction that triggers page rendering to be looped through
to ParserCache, so we can count what causes writes to the cache.

Change-Id: I6ad8e105a3ce457e3ab4f85cd154f47a32085e0d
2022-11-09 09:38:57 +00:00
Umherirrender
1b342a8893 Various doc fixes about false and null on method arguments/return types
Doc-only changes

Change-Id: Ice974b3ba41708859dfe646e94b31c5ebbf26410
2022-11-03 18:55:47 +01:00
Amir Sarabadani
0fff5089ba Reorg: Move StubObject classes in includes to its own directory
Bug: T166010
Change-Id: Idcf0e9dc6e0841e4f132207bce0f96774dad898c
2022-10-25 16:04:48 -04:00
Tim Starling
43a93d9782 Use the null coalescing assignment operator
Available since PHP 7.4.

Automated search, manual replacement.

Change-Id: Ibb163141526e799bff08cfeb4037b52144bb39fa
2022-10-21 13:26:49 +11:00
C. Scott Ananian
dd32fe1a34 Mark ParserOptions::{get,set}MaxTemplateDepth() @internal
This option is not used outside core, according to codesearch:

 https://codesearch.wmcloud.org/deployed/?q=etMaxTemplateDepth&i=nope&files=&excludeFiles=&repos=

ParserTests can set this in the site config; this doesn't need to be
a parser option.

Bug: T318826
Change-Id: I815d340a43870249db5e36084a5e5403bb71b6ca
2022-09-28 15:22:10 -04:00
C. Scott Ananian
b7b13c87f4 Mark ParserOptions::{get,set}ExternalLinkTarget() @internal
This option is not used outside core code, according to codesearch:

  https://codesearch.wmcloud.org/deployed/?q=etExternalLinkTarget&i=nope&files=&excludeFiles=&repos=

Users should set this in the site config, not as a parser option.

The installer will eventually invoke Parsoid directly (T317644) with
a custom site config.

Bug: T317647
Change-Id: I099c04045de256ef0fb4cb114ee3c2be350dfdc4
2022-09-28 15:17:33 -04:00
C. Scott Ananian
cc79898522 ParserOption/ParserOutput flag to suppress or hide the table of contents
There are two related issues here: first, when parsing non-wikitext
pages for side effects (categories, etc) we want to ensure that any
spurious `===` or `<h2>` on the page don't create nonsense "sections".
We introduce a ParserOption to suppress the ToC in this case; a
follow-up patch will set this parser option from the correct path in
CodeContentHandler and its subclasses. [T307691]

Second, modern skins can generate the ToC on-the-fly outside the
content area, and need to be able to regenerate the ToC from API
output when the page is edited.  A ParserOutput flag is added to
mirror the $enoughToc variable from the parser to indicate whether
or not the ToC should be generated and/or updated after edit.
(See I6cf76c870124c162dc1bcbc2f7e9ca0c5fdcd10e for parallel code
to echo this value in ApiParse.)

Bug: T294950
Bug: T307691
Change-Id: I35e199cca40c0e4359ac493e5806dcf4ae49321c
2022-07-21 15:29:11 -04:00
Kosta Harlan
a9ee442f7f ParserOptions: Add fallback to enableMagicLinks
Due to quirks of bootstrapping process in PHPUnit, enableMagicLinks can
end up as null instead of an array with keys expected by ParserOptions.
As a workaround, set a fallback for each expected key.

Change-Id: I1511503937f8ac4fcd2f2c8b98bfd7dba17385ec
2022-06-03 14:43:16 +00:00
Aryeh Gregor
7b791474a5 Use MainConfigNames instead of string literals, #4
Now largely automated:

VARS=$(grep -o "'[A-Za-z0-9_]*'" includes/MainConfigNames.php | \
  tr "\n" '|' | sed "s/|$/\n/;s/'//g")
sed -i -E "s/'($VARS)'/MainConfigNames::\1/g" \
  $(grep -ERIl "'($VARS)'" includes/)

Then git add -p with lots of error-prone manual checking. Then
semi-manually add all the necessary "use" lines:

vim $(grep -L 'use MediaWiki\\MainConfigNames;' \
  $(git diff --cached --name-only --diff-filter=M HEAD^))

I didn't bother fixing lines that were over 100 characters unless they
were over 120 and triggered phpcs.

Bug: T305805
Change-Id: I74e0ab511abecb276717ad4276a124760a268147
2022-04-26 19:03:37 +03:00
Derick Alangi
ccac0f2077
linker,parser: Remove deprecated Linker & Parser methods
Change-Id: I8207bf137c1c10534ed797901de2710dfc379ea7
2022-04-14 14:06:28 +01:00
Umherirrender
45a4868f55 parser: Adjust documentation about false for $wgExternalLinkTarget
The default for $wgExternalLinkTarget is false,
which can be set or returned on ParserOptions

Found by phan strict checks

Change-Id: Ibcf6c40eaf1ffb628fc1b92a0d0ecd79d9421100
2022-03-03 22:35:41 +01:00
daniel
026133bb05 remove access to config globals from includes/parser
Loops ServiceOptions through to CoreParserFunctions and CoreTagHooks to
avoid access to the main config from static methods.

Bug: T294739
Change-Id: Ia6c97f2d0952964c2ad6189f8053ad127589b37c
2022-02-01 07:48:57 -08:00
Reedy
2a2bb1e9bd Remove or replace usages of "sane"
Bug: T254646
Change-Id: I096b2cf738a1395a14f1d47bcbed0c2c686c2581
2021-11-22 13:35:17 +00:00
jenkins-bot
0d188e7cdd Merge "parser: Make ParserOptions::registerWatcher() public and ::optionUsed() private" 2021-11-16 18:36:26 +00:00
Ppchelko
643fc535c3 Reapply "Move limit report rendering to ParserOutput"
This reverts commit 2bcb3fe567.

Reason for revert: this is a good change,
just needed more work to not break CI

Change-Id: I23768bee242e3cf81b1493a740cf070e7ad1e224
2021-11-09 11:08:08 -08:00
Ppchelko
2bcb3fe567 Revert "Move limit report rendering to ParserOutput"
This reverts commit 89028e0b8e.

Reason for revert: Temporary until we deal with T295357

Change-Id: I556de18dbf900a9bc58d5ae22d1bf194682d0840
2021-11-09 15:57:18 +00:00
Petr Pchelko
89028e0b8e Move limit report rendering to ParserOutput
This does not move the actual limit report data into
ParserOptions yet, that should be done separately
given that it will require serialization changes.
Let's get this change settled first before messing
with serialization.

This unifies canonical and non-canonical ParserOptions,
so ParserCache can now be used with both. It is hard
to say how this will affect the ParserCache capacity,
so we should monitor it after releasing this.

Change-Id: I154c0a77a5b0287b5572614d56339fb57ac56c33
2021-11-08 12:45:41 -08:00
Timo Tijhof
066690c8f4 parser: Make ParserOptions::registerWatcher() public and ::optionUsed() private
Follows-up fe14fdefa7 (r79018), which introduced the registerWatcher()
method with the comment that it was for internal use by the Parser.

Howevever it lacked an `@internal` annotation, and it is de-facto used
in various places now, so perhaps we should just consider it public.

This can be changed of course, but that should probably be done through
its own explicit deprecation given it's been around so long and used
in various places.

The ::optionUsed() option is only used internally, introduced by the
same commit, and has no known caller or theoreretical need to be called
outside this class. Make this private going forward.

Change-Id: Iab006be742b7bdf55a0d79509e8b827e109e9ceb
2021-11-08 20:33:36 +00:00
Subramanya Sastry
f70b15558f Get rid of ParserOptions::setTidy()
Bug: T198214
Change-Id: I90ea6ce6831caa92e5d88d843132161a5fc66184
2021-10-07 15:13:59 -05:00
Amir Sarabadani
649bbdd6c5 Remove "auto-number headings" preference
Bug: T284921
Change-Id: Ic9ed88f419419cf4cc5cc32010539eea8b76314b
2021-10-03 00:47:08 +02:00
Petr Pchelko
a407ce4124 Remove hard-deprecated methods returning User from Parser
Change-Id: I4ff100136a777974f7206f599422589d51f02b88
2021-09-26 14:50:14 -07:00
Petr Pchelko
5afc62f258 Remove stub threshold feature
Bug: T284917
Change-Id: I5c9ea04a9deca136bb7210f07200d18f97fd6686
2021-09-13 09:26:38 -07:00
Arlo Breault
e6970ac8d3 Fix throws from parserTestsRunner.php
This lets test/parser/parserTests.php run.

Follow up to I9e820045443be1df2c89e1ed4b56f0d55662c486

Change-Id: I92f00a58ed1e0717a39ab5a6d258800326eebeed
2021-08-19 02:23:03 +00:00
vladshapik
1091f7753f Hard-deprecate Parser::mUser public access, Parser::getUser and ParserOptions::getUser
Bug: T285713
Depends-On: Ie75c9cd66d296ce7cf15432e2093817e18004443
Change-Id: I4297aea3489bb66c98c664da2332584c27793bfa
2021-08-17 15:42:05 +00:00
Petr Pchelko
79475042a5 ParserOptions: support setting a default for lazy options
If the default is not set for a lazy loaded option,
canonical parser output key ends up being 'lazy_option=default',
because when computing the keys we exclude defaults.

If on the other hand we register a default for a lazy option,
it does not get loaded, since in lazyLoadOption we believe
the default is already the loaded value.

Change-Id: I92b3e18fabef4eecac2ec2a4844f1be2716e5d89
Needed-By: I3bce04684070ad306685dabbc51267def25773cd
2021-08-10 10:22:05 -07:00
Petr Pchelko
39c6c5e34c Tests: reset ParserOptions static cache with MW services
ParserOptions store the result of ParserOptionsRegister
hook execution in a static cache. If we register a lazy
option, which depends on user options and use OptionsLookup,
tests break because a lookup from a destroyed service container
ends up being reused between tests.

The reset was added to resetLegacyGlobals because 1) it's
called at all the right times 2) this static cache is legacy,
when we have a factory service for ParserOptions, this will
not be needed anymore.

Change-Id: I9e820045443be1df2c89e1ed4b56f0d55662c486
2021-08-10 09:11:19 -07:00
DannyS712
29ec3ec7e3 Remove $wgUser fallback in ParserOptions
ParserOptions::__construct() and ::newCanonical()
no longer accept null and fallback to the global
$wgUser - instead, ::__construct() has a typehint
for a UserIdentity, and ::newCanonical() will throw
an exception.

Bug: T284977
Change-Id: I35865e160190582ab10abaa696c6fc6686cc8989
2021-06-24 02:55:20 +00:00
Petr Pchelko
61599cd74a Clean up hard-deprecated Parser methods returning Revision
Bug: T278376
Change-Id: Ia4b5ab71c1df20e07dbfa3465be022225e8b44c1
2021-04-26 13:59:53 -07:00
Petr Pchelko
7bf51ccef3 Convert ParserOptions to UserIdentity
We still need a lot of refactoring in ParserOptions
constructions, but for now converting the public interface
should be enough.

Change-Id: I04663c39ca037129b827b33555c3f59def5f9b59
2021-03-24 09:40:42 -06:00
Peter Ovchyn
45140daa29 Avoid using User ::getDefaultOption, ::getDefaultOptions
This patch hard-deprecates the methods above

Bug: T276035
Change-Id: Ic36b0702f7547acce0d162d6e0b54bbd4ecf4d81
2021-03-16 17:24:17 +02:00
Tim Starling
f28578f9da Make ParserOptionsTest work on PHP<7.4.9 with opcache enabled
Instead of refusing to run MediaWiki at all, just fix the test which
fails on 7.4.3.

* Add ParserOptions::clearStaticCache(), which resets static cached
  values back to their pre-hook values.
* Add accessors for these private static variables to protect against
  access before initialisation.
* Rename "inCache" to "cacheVaryingOptionsHash" for clarity.
* In ParserOptions::match(), filter static properties by detecting them
  with ReflectionProperty, instead of making a big list of them.
* Update ParserOptionsTest for the fact that stubthreshold is no longer
  a cache-varying option. This was hidden by
  ParserOptionsTest::clearCache() injecting the wrong array into
  ParserOptions.

Bug: T270228
Change-Id: I6b5ba022e1b889a10f9fbe93da63da9831504df8
2021-03-02 13:03:58 +11:00
Umherirrender
8de3b7d324 Use static closures where safe to use
This is micro-optimization of closure code to avoid binding the closure
to $this where it is not needed.

Created by I25a17fb22b6b669e817317a0f45051ae9c608208

Change-Id: I0ffc6200f6c6693d78a3151cb8cea7dce7c21653
2021-02-11 00:13:52 +00:00
Ammarpad
af303cafe4 ParserOptions: Fix param/return type doc
onAccessCallback can be null, and setTidy() does not return boolean

Also use variable for MW services call.

Change-Id: If4e0c986ce47221d75888029bc32d788a9528681
2021-02-02 20:52:52 +00:00
Petr Pchelko
b964bd2e8d Make ParserOptions::isSafeToCache more robust
Bug: T273120
Change-Id: I516133d07d9ff4d7930de68b88ed4b40ca1c6150
2021-01-27 18:34:26 -06:00
Petr Pchelko
46b66f093a Respect used options for ParserOptions::isSafeToCache
Bug: T269293
Change-Id: Ic3cf908265ad470815f0ac81442d33bde04a5665
2021-01-04 10:32:34 -06:00
jenkins-bot
4257367a01 Merge "Record all used options in metadata." 2020-12-16 17:59:54 +00:00
DannyS712
25db391d53 ParserOptions: Deprecate fallback to $wgUser
Emit deprecation notices in the constructor
if falling back to $wgUser, and fix core
calls

Bug: T246861
Depends-On: I51117931d527a3bdda468b48de577a7faafbcd69
Change-Id: Ibd0a8ffd0494c17a378cc43e6b6164166130adf4
2020-12-16 09:03:29 +00:00
Petr Pchelko
5413678f06 Record all used options in metadata.
Bug: T269293
Change-Id: I69ac24fcf8bf81de1d25c5a331b2155d9fe21977
2020-12-15 12:51:14 -06:00
Petr Pchelko
235c56d649 Remove 'stubthreshold' from ParserCache key.
Stubthreshold option used to be a cache-varying option,
but in all places where we interact with the ParserCache
we are checking that it's 0 before using the cache.

Instead, we can just remove all the special cases for
stubthreshold option, remove it from cache key and rely
on ParserOptions::isSafeToCache to avoid caching non-default
stubthreshold outputs.

Bug: T264351
Change-Id: Ifaf69a3e651eef21c88da3aa3044b490059958ca
2020-12-07 14:47:05 -06:00
Ammar Abdulhamid
71571191d4 Chain MutableRevisionRecord method calls 2
Change-Id: I86578cfbc892f171a4e433283b86d1b78fe4167d
2020-11-27 05:26:54 +01:00
C. Scott Ananian
c64e71615e Replace $wgDisable{Lang,Title}Conversion with LanguageConverterFactory methods
Replace direct access to $wgDisableLangConversion with
LanguageConverterFactory::isConversionDisabled(), and replace direct
access to $wgDisableTitleConversion with
LanguageConverterFactory::isTitleConversionDisabled().  However, most
places that check ::isTitleConversionDisabled() actually want
::isLinkConversionDisabled(), so add that too (and deprecate
isTitleConversionDisabled()).

Code search:
https://codesearch.wmcloud.org/search/?q=Disable%28Lang|Title%29Conversion&i=nope&files=&repos=

This change removes a number of spurious dependencies on the global
configuration and reduces code duplication (for example, if the logic
for disabling language conversion were ever to change).

Depends-On: I6fa8230ae97b0e34c381003548e61f9b7387d363
Change-Id: Icc4687638ff1815003dd903854efdbd904854f1e
2020-11-25 12:47:26 -05:00
daniel
67d0986211 Introduce ParserOutputAccess
Encapsulate logic for getting rendered page content, for any revision,
with caching and pooling hidden away.

Introducing such a service object will also give us a leverage point for
supporting output transformations. Output transformations are currently
implemented partially in ParserOutput, partially in Parser, and partially
duplicated in Parsoid.

Bug: T267234
Change-Id: I566d7a7936633823ba68b5aecbc8c2d88949b4f8
2020-11-10 15:12:12 +01:00
Thiemo Kreuz
1fc8d79ac6 Remove documentation that literally repeats the code
For example, documenting the method getUser() with "get the User
object" does not add any information that's not already there.
But I have to read the text first to understand that it doesn't
document anything that's not already obvious from the code.

Some of this is from a time when we had a PHPCS sniff that was
complaining when a line like `@param User $user` doesn't end
with some descriptive text. Some users started adding text like
`@param User $user The User` back then. Let's please remove
this.

Change-Id: I0ea8d051bc732466c73940de9259f87ffb86ce7a
2020-10-27 19:20:26 +00:00