Page MenuHomePhabricator

Replace Parser's creation from "Parser::__constructor" to "ParserFactory::create" across all the code
Open, Needs TriagePublic

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

Related Gerrit Patches:
mediawiki/extensions/intersection : masterparser: Replace 'new Parser' in favour of using Parser::getFreshParser
mediawiki/extensions/BiblioPlus : masterparser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices
mediawiki/extensions/RSS : masterparser: Replace 'new Parser' in favour of using Parser::getFreshParser
mediawiki/extensions/MediaWikiChat : masterparser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices
mediawiki/extensions/Video : masterparser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices
mediawiki/extensions/Lingo : masterparser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices
mediawiki/extensions/BlogPage : masterparser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices
mediawiki/extensions/FanBoxes : masterparser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices
mediawiki/extensions/NumerAlpha : masterRemove not needed `new Parser()`, fix broken test setup
mediawiki/extensions/DynamicPageList : masterparser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices
mediawiki/extensions/SocialProfile : masterparser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices
mediawiki/extensions/Translate : masterparser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices
mediawiki/extensions/CommentStreams : masterparser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices
mediawiki/extensions/TemplateStyles : masterRemove onParserAfterTidy hood as tidy is dead
mediawiki/extensions/DataTable2 : masterparser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices
mediawiki/extensions/MassMessageEmail : masterparser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices
mediawiki/extensions/Kartographer : masterparser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices
mediawiki/extensions/Flow : masterparser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices
mediawiki/extensions/FileAnnotations : masterparser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices
mediawiki/extensions/FeaturedFeeds : masterparser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices
mediawiki/extensions/Wikibase : masterparser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices
mediawiki/extensions/NewsTicker : masterparser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices
mediawiki/extensions/ProofreadPage : masterparser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices
mediawiki/extensions/ApprovedRevs : masterUse MediaWikiServices::getParserFactory() for MW 1.32+
mediawiki/extensions/MassMessage : masterparser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices
mediawiki/extensions/Math : masterparser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices
mediawiki/extensions/BlueSpiceFoundation : masterAdd missing `use` statement
mediawiki/extensions/BlueSpiceFoundation : masterparser: Replace 'new Parser' in favour of using ParserFactory/MediaWikiServices

Event Timeline

Restricted Application added a subscriber: Aklapper. · View Herald TranscriptFeb 1 2020, 12:11 PM
Peter.ovchyn updated the task description. (Show Details)Feb 11 2020, 9:40 AM

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

Daimona updated the task description. (Show Details)Feb 22 2020, 5:01 PM

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