WolfgangFahl / pymediawikidocker

Python controlled mediawiki docker image installation
Apache License 2.0
7 stars 1 forks source link

Allow Extension from Json File to overwrite existing definition #75

Closed Kusko25 closed 2 months ago

Kusko25 commented 2 months ago

When you pass extension information, as a json file, to the program it checks for duplicates in MwConfig.getExtensionMap and discards them.
In my case the problem was that 'Page Forms' is on the list of known extensions, but as an older version, so without editing extensions.yaml manually there seems to be no way to specify a different version for a known extension.

WolfgangFahl commented 2 months ago

@Kusko25 Could you please supply your extension.json file example so that i can craft a testcase

Kusko25 commented 2 months ago

Sure, here you go:

{
    "extensions": [
        {
            "name": "Flex Diagrams",
            "extension": "FlexDiagrams",
            "giturl": "https://gerrit.wikimedia.org/r/mediawiki/extensions/FlexDiagrams.git",
            "url": "https://www.mediawiki.org/wiki/Extension:Flex_Diagrams"
        },
        {
            "name": "Page Forms",
            "extension": "PageForms",
            "url": "https://www.mediawiki.org/wiki/Extension:Page_Forms",
            "composer": "\"mediawiki/page-forms\": \"^5.8\""
        }
    ]
}

Flex Diagrams installs as desired, but Page Forms gets essentially ignored

WolfgangFahl commented 2 months ago
def getExtensionMap(
        self, extensionNameList: list = None,
         extensionJsonFile: str = None
    ):
        """
        get map of extensions to handle

        Args:
            extensionNameList (list): a list of extension names
            extensionJsonFile (str): the name of an extra extensionJsonFile (if any)
        """
        self.extensionMap = {}
        extensionList = ExtensionList.restore()
        if extensionJsonFile is not None:
            extraExtensionList = ExtensionList.load_from_json_file(extensionJsonFile)
            for ext in extraExtensionList.extensions:
                extensionList.extensions.append(ext)
        self.extByName, duplicates = LOD.getLookup(extensionList.extensions, "name")
        if len(duplicates) > 0:
            print(f"{len(duplicates)} duplicate extensions: ")
            for duplicate in duplicates:
                print(duplicate.name)
        if extensionNameList is not None:
            self.addExtensions(extensionNameList)
        return self.extensionMap

essential should give you a "duplicate extensions" output. I assume you just want a different logic here that replaces existing list entries if an extension with the same name is loaded.

WolfgangFahl commented 2 months ago
def test_duplicate_extensions(self):
        """
        Test the handling of duplicate extensions when
        loading from both default and custom JSON files
        """
        extension_dict={
            "extensions": [
                {
                    "name": "Flex Diagrams",
                    "extension": "FlexDiagrams",
                    "giturl": "https://gerrit.wikimedia.org/r/mediawiki/extensions/FlexDiagrams.git",
                    "url": "https://www.mediawiki.org/wiki/Extension:Flex_Diagrams"
                },
                {
                    "name": "Page Forms",
                    "extension": "PageForms",
                    "url": "https://www.mediawiki.org/wiki/Extension:Page_Forms",
                    "composer": "\"mediawiki/page-forms\": \"^5.8\""
                }
            ]
        }
        temp_json = tempfile.NamedTemporaryFile(mode='w+', delete=False, suffix='.json')
        json.dump(extension_dict,temp_json)
        temp_json.close()

        config = MwClusterConfig()
        stdout = io.StringIO()
        with redirect_stdout(stdout):
            ext_map=config.getExtensionMap(["Page Forms","Flex Diagrams"], temp_json.name)
        log_msg=stdout.getvalue()
        debug=True
        if debug:
            print(ext_map)
        os.unlink(temp_json.name)
        self.assertEqual(2,len(ext_map))
        self.assertTrue("5.8" in ext_map["Page Forms"].composer)
        self.assertTrue("overriding Page Forms" in log_msg)
Kusko25 commented 2 months ago

Essentially yes, with the thought that if you explicitly wrote a json file, you either know what you are doing or it is your own fault when it causes issues.
Besides versions, this would also give users the ability to specify different download locations for extensions if you need to do that for whatever reason.