Page MenuHomePhabricator

Replace Parser's creation from "Parser::__constructor" to "ParserFactory::create" across all the code
Closed, ResolvedPublic

Description

Motivation

Migration to context-based DI aims to separate creation from logic. Using Parser::__constructor (new Parser) demands to support backward compatibility.
At the moment Parser already uses DI, but a lot of legacy code in extension creates it directly via constructor.
https://codesearch.wmflabs.org/search/?q=new%20%5C%5C%3FParser%5C(&i=nope&files=&repos=

Expected Result:

All occurrences of new Parser() should be replaced in favor of creating it via factory (both core and extensions):
MediaWikiServices::getInstance()->getParserFactory()->create();

Details

SubjectRepoBranchLines +/-
mediawiki/extensions/intersectionmaster+1 -1
mediawiki/extensions/Lingomaster+32 -30
mediawiki/extensions/BiblioPlusmaster+6 -3
mediawiki/extensions/RSSmaster+1 -1
mediawiki/extensions/MediaWikiChatmaster+1 -1
mediawiki/extensions/Videomaster+1 -7
mediawiki/extensions/BlogPagemaster+4 -1
mediawiki/extensions/FanBoxesmaster+4 -4
mediawiki/extensions/NumerAlphamaster+11 -5
mediawiki/extensions/DynamicPageListmaster+1 -3
mediawiki/extensions/SocialProfilemaster+8 -3
mediawiki/extensions/Translatemaster+6 -2
mediawiki/extensions/CommentStreamsmaster+9 -1
mediawiki/extensions/TemplateStylesmaster+0 -57
mediawiki/extensions/DataTable2master+4 -2
mediawiki/extensions/MassMessageEmailmaster+6 -1
mediawiki/extensions/Kartographermaster+6 -4
mediawiki/extensions/Flowmaster+2 -3
mediawiki/extensions/FileAnnotationsmaster+1 -1
mediawiki/extensions/FeaturedFeedsmaster+2 -2
mediawiki/extensions/Wikibasemaster+35 -25
mediawiki/extensions/NewsTickermaster+1 -1
mediawiki/extensions/ProofreadPagemaster+4 -2
mediawiki/extensions/ApprovedRevsmaster+14 -2
mediawiki/extensions/MassMessagemaster+2 -2
mediawiki/extensions/Mathmaster+7 -3
mediawiki/extensions/BlueSpiceFoundationmaster+2 -0
mediawiki/extensions/BlueSpiceFoundationmaster+3 -3
Show related patches Customize query in gerrit

Event Timeline

Change 572646 had a related patch set uploaded (by Peter.ovchyn; owner: Peter.ovchyn):
[mediawiki/extensions/Wikibase@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572646

Change 572683 had a related patch set uploaded (by Peter.ovchyn; owner: Peter.ovchyn):
[mediawiki/extensions/BiblioPlus@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572683

Change 572687 had a related patch set uploaded (by Peter.ovchyn; owner: Peter.ovchyn):
[mediawiki/extensions/Video@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572687

Change 572689 had a related patch set uploaded (by Peter.ovchyn; owner: Peter.ovchyn):
[mediawiki/extensions/Math@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572689

Change 572694 had a related patch set uploaded (by Peter.ovchyn; owner: Peter.ovchyn):
[mediawiki/extensions/Lingo@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572694

Change 572699 had a related patch set uploaded (by Peter.ovchyn; owner: Peter.ovchyn):
[mediawiki/extensions/DataTable2@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572699

Change 572700 had a related patch set uploaded (by Peter.ovchyn; owner: Peter.ovchyn):
[mediawiki/extensions/FeaturedFeeds@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572700

Change 572703 had a related patch set uploaded (by Peter.ovchyn; owner: Peter.ovchyn):
[mediawiki/extensions/CommentStreams@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572703

Change 572704 had a related patch set uploaded (by Peter.ovchyn; owner: Peter.ovchyn):
[mediawiki/extensions/MassMessage@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572704

Change 572706 had a related patch set uploaded (by Peter.ovchyn; owner: Peter.ovchyn):
[mediawiki/extensions/FanBoxes@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572706

Change 572709 had a related patch set uploaded (by Peter.ovchyn; owner: Peter.ovchyn):
[mediawiki/extensions/BlueSpiceFoundation@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572709

Change 572710 had a related patch set uploaded (by Peter.ovchyn; owner: Peter.ovchyn):
[mediawiki/extensions/ApprovedRevs@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572710

Change 572720 had a related patch set uploaded (by Peter.ovchyn; owner: Peter.ovchyn):
[mediawiki/extensions/intersection@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572720

Change 572721 had a related patch set uploaded (by Peter.ovchyn; owner: Peter.ovchyn):
[mediawiki/extensions/RSS@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572721

Is there a follow up change to the coding convention check. Otherwise people like me might reintroduce the shorter alternative in the future without being aware of the longer form.

Is there a follow up change to the coding convention check. Otherwise people like me might reintroduce the shorter alternative in the future without being aware of the longer form.

Didn’t get you. What do you mean by shorter/longer form?

Change 572744 had a related patch set uploaded (by Peter.ovchyn; owner: Peter.ovchyn):
[mediawiki/extensions/NumerAlpha@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572744

Didn’t get you. What do you mean by shorter/longer form?

short = new Parser()

long = MediaWikiServices::getInstance()->getParserFactory()->create()

Change 572709 merged by jenkins-bot:
[mediawiki/extensions/BlueSpiceFoundation@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572709

Change 572816 had a related patch set uploaded (by Robert Vogel; owner: Robert Vogel):
[mediawiki/extensions/BlueSpiceFoundation@master] Add missing use statement

https://gerrit.wikimedia.org/r/572816

Change 572816 merged by jenkins-bot:
[mediawiki/extensions/BlueSpiceFoundation@master] Add missing use statement

https://gerrit.wikimedia.org/r/572816

Didn’t get you. What do you mean by shorter/longer form?

short = new Parser()

long = MediaWikiServices::getInstance()->getParserFactory()->create()

I don't know how to implement this rule yet but you're 100% right, it would be really great to have it working across all the code. However, a few difficulties: not all new Parser statements refer to MediaWiki\Parser. PhpParser, Lua parser, and a few other 3d-party libs also have class Parser. Also, the follow up it to make new Parser() hard deprecated, and make it only possible to use providing all arguments.

I hope it'll sort your question out.
Anyway, having code style rules is a brilliant idea here.

While reading this, I was thinking about a private constructor. Not sure if that makes sense, but it would be an alternative to a code style rule.

Change 572689 merged by jenkins-bot:
[mediawiki/extensions/Math@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572689

While reading this, I was thinking about a private constructor. Not sure if that makes sense, but it would be an alternative to a code style rule.

Other languages have "internal" or "module" visibility modifications. It's exactly what you're talking about. Making constructor "private" automatically makes factory (ParserFactory) senseless, since it can't call it as well.

Change 572704 merged by jenkins-bot:
[mediawiki/extensions/MassMessage@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572704

Change 572951 had a related patch set uploaded (by Peter.ovchyn; owner: Peter.ovchyn):
[mediawiki/extensions/ProofreadPage@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572951

Change 572961 had a related patch set uploaded (by Peter.ovchyn; owner: Peter.ovchyn):
[mediawiki/extensions/SocialProfile@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572961

Change 572962 had a related patch set uploaded (by Peter.ovchyn; owner: Peter.ovchyn):
[mediawiki/extensions/DynamicPageList@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572962

Change 572963 had a related patch set uploaded (by Peter.ovchyn; owner: Peter.ovchyn):
[mediawiki/extensions/BlogPage@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572963

Change 572964 had a related patch set uploaded (by Peter.ovchyn; owner: Peter.ovchyn):
[mediawiki/extensions/NewsTicker@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572964

Change 572965 had a related patch set uploaded (by Peter.ovchyn; owner: Peter.ovchyn):
[mediawiki/extensions/FileAnnotations@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572965

Change 572968 had a related patch set uploaded (by Peter.ovchyn; owner: Peter.ovchyn):
[mediawiki/extensions/MassMessageEmail@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572968

Change 572969 had a related patch set uploaded (by Peter.ovchyn; owner: Peter.ovchyn):
[mediawiki/extensions/Translate@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572969

Change 572971 had a related patch set uploaded (by Peter.ovchyn; owner: Peter.ovchyn):
[mediawiki/extensions/Flow@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572971

Change 572972 had a related patch set uploaded (by Peter.ovchyn; owner: Peter.ovchyn):
[mediawiki/extensions/TemplateStyles@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572972

Change 572973 had a related patch set uploaded (by Peter.ovchyn; owner: Peter.ovchyn):
[mediawiki/extensions/MediaWikiChat@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572973

Change 572974 had a related patch set uploaded (by Peter.ovchyn; owner: Peter.ovchyn):
[mediawiki/extensions/Kartographer@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572974

Change 572710 merged by jenkins-bot:
[mediawiki/extensions/ApprovedRevs@master] Use MediaWikiServices::getParserFactory() for MW 1.32+

https://gerrit.wikimedia.org/r/572710

Change 572951 merged by jenkins-bot:
[mediawiki/extensions/ProofreadPage@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572951

Change 572964 merged by jenkins-bot:
[mediawiki/extensions/NewsTicker@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572964

Change 572646 merged by jenkins-bot:
[mediawiki/extensions/Wikibase@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572646

Change 572972 merged by jenkins-bot:
[mediawiki/extensions/TemplateStyles@master] Remove onParserAfterTidy hood as tidy is dead

https://gerrit.wikimedia.org/r/572972

Change 572700 merged by jenkins-bot:
[mediawiki/extensions/FeaturedFeeds@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572700

Change 572971 merged by jenkins-bot:
[mediawiki/extensions/Flow@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572971

Change 572974 merged by jenkins-bot:
[mediawiki/extensions/Kartographer@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572974

Change 572965 merged by jenkins-bot:
[mediawiki/extensions/FileAnnotations@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572965

Change 572968 merged by jenkins-bot:
[mediawiki/extensions/MassMessageEmail@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572968

Change 572699 merged by jenkins-bot:
[mediawiki/extensions/DataTable2@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572699

Change 572703 merged by jenkins-bot:
[mediawiki/extensions/CommentStreams@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572703

Change 572969 merged by Abijeet Patro:
[mediawiki/extensions/Translate@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572969

Change 572961 merged by jenkins-bot:
[mediawiki/extensions/SocialProfile@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572961

Change 572962 merged by jenkins-bot:
[mediawiki/extensions/DynamicPageList@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572962

Change 572744 merged by jenkins-bot:
[mediawiki/extensions/NumerAlpha@master] Remove not needed new Parser(), fix broken test setup

https://gerrit.wikimedia.org/r/572744

Change 572706 merged by jenkins-bot:
[mediawiki/extensions/FanBoxes@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572706

Change 572963 merged by jenkins-bot:
[mediawiki/extensions/BlogPage@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572963

Change 572687 merged by jenkins-bot:
[mediawiki/extensions/Video@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572687

Change 572973 merged by jenkins-bot:
[mediawiki/extensions/MediaWikiChat@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572973

Change 572721 merged by jenkins-bot:
[mediawiki/extensions/RSS@master] parser: Replace 'new Parser' in favour of using Parser::getFreshParser

https://gerrit.wikimedia.org/r/572721

Change 572683 merged by jenkins-bot:
[mediawiki/extensions/BiblioPlus@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572683

Change 572694 merged by jenkins-bot:
[mediawiki/extensions/Lingo@master] parser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

https://gerrit.wikimedia.org/r/572694

Change 572720 abandoned by C. Scott Ananian:
parser: Replace 'new Parser' in favour of using Parser::getFreshParser

Reason:
I777475d0ab0144e53240173f501d6c8da35d33fb did this already.

https://gerrit.wikimedia.org/r/572720