jenkins-bot has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/955411 )

Change subject: FilePage: raise ValueError when title doesn't have a valid file 
extension
......................................................................

FilePage: raise ValueError when title doesn't have a valid file extension

Bug: T345786
Change-Id: Iba951506619f1f14f75e7bfb33c7afc99a240a5a
---
M tests/page_tests.py
M pywikibot/page/_filepage.py
M pywikibot/site/_apisite.py
M tests/file_tests.py
4 files changed, 38 insertions(+), 8 deletions(-)

Approvals:
  Xqt: Looks good to me, approved
  jenkins-bot: Verified




diff --git a/pywikibot/page/_filepage.py b/pywikibot/page/_filepage.py
index 2317fac..0161835 100644
--- a/pywikibot/page/_filepage.py
+++ b/pywikibot/page/_filepage.py
@@ -44,6 +44,13 @@
         super().__init__(source, title, 6)
         if self.namespace() != 6:
             raise ValueError(f"'{self.title()}' is not in the file namespace!")
+        title = self.title(with_section=False)
+        extension = title.rpartition('.')[2].lower()
+        if extension not in self.site.file_extensions:
+            raise ValueError(
+                f'{title!r} does not have a valid extension '
+                f'({", ".join(self.site.file_extensions)}).'
+            )

     def _load_file_revisions(self, imageinfo) -> None:
         for file_rev in imageinfo:
diff --git a/pywikibot/site/_apisite.py b/pywikibot/site/_apisite.py
index 636fcf5..1a1f0ee 100644
--- a/pywikibot/site/_apisite.py
+++ b/pywikibot/site/_apisite.py
@@ -472,6 +472,11 @@
         api._invalidate_superior_cookies(self.family)

     @property
+    def file_extensions(self) -> List[str]:
+        """File extensions enabled on the wiki."""
+        return sorted(e['ext'] for e in self.siteinfo.get('fileextensions'))
+
+    @property
     def maxlimit(self) -> int:
         """Get the maximum limit of pages to be retrieved.

diff --git a/tests/file_tests.py b/tests/file_tests.py
index be0fafd..fb3bdf0 100755
--- a/tests/file_tests.py
+++ b/tests/file_tests.py
@@ -164,24 +164,24 @@
     def test_file_info_with_no_page(self):
         """FilePage:latest_file_info raises NoPageError for missing pages."""
         site = self.get_site()
-        image = pywikibot.FilePage(site, 'File:NoPage')
+        image = pywikibot.FilePage(site, 'File:NoPage.jpg')
         self.assertFalse(image.exists())

         with self.assertRaisesRegex(
                 NoPageError,
-                (r'Page \[\[(wikipedia\:|)test:File:NoPage\]\] '
+                (r'Page \[\[(wikipedia\:|)test:File:NoPage\.jpg\]\] '
                  r"doesn't exist\.")):
             image = image.latest_file_info

     def test_file_info_with_no_file(self):
         """FilePage:latest_file_info raises PagerelatedError if no file."""
         site = self.get_site()
-        image = pywikibot.FilePage(site, 'File:Test with no image')
+        image = pywikibot.FilePage(site, 'File:Test with no image.png')
         self.assertTrue(image.exists())
         with self.assertRaisesRegex(
                 PageRelatedError,
                 (r'loadimageinfo: Query on '
-                 r'\[\[(wikipedia\:|)test:File:Test with no image\]\]'
+                 r'\[\[(wikipedia\:|)test:File:Test with no image\.png\]\]'
                  r' returned no imageinfo')):
             image = image.latest_file_info

@@ -322,13 +322,13 @@
     def test_not_existing_download(self):
         """Test not existing download."""
         page = pywikibot.FilePage(self.site,
-                                  'File:Albert Einstein.jpg_notexisting')
+                                  'File:notexisting_Albert Einstein.jpg')
         filename = join_images_path('Albert Einstein.jpg')

         with self.assertRaisesRegex(
                 NoPageError,
-                re.escape('Page [[commons:File:Albert Einstein.jpg '
-                          "notexisting]] doesn't exist.")):
+                re.escape('Page [[commons:File:Notexisting Albert '
+                          "Einstein.jpg]] doesn't exist.")):
             page.download(filename)


@@ -373,7 +373,7 @@
     def test_data_item_not_existing(self):
         """Test data item associated to file that does not exist."""
         page = pywikibot.FilePage(self.site,
-                                  'File:Albert Einstein.jpg_notexisting')
+                                  'File:Notexisting_Albert Einstein.jpg')
         item = page.data_item()
         with self.assertRaises(NoWikibaseEntityError):
             item.get()
diff --git a/tests/page_tests.py b/tests/page_tests.py
index 6e92e99..123ea9d 100755
--- a/tests/page_tests.py
+++ b/tests/page_tests.py
@@ -263,6 +263,14 @@
             p2.title(as_link=True, textlink=True,
                      with_ns=False, insite=site),
             '[[:File:Jean-Léon Gérôme 003.jpg|Jean-Léon Gérôme 003.jpg]]')
+        for title in (
+            'Help:Example',  # wrong namespace
+            'File:Example',  # no file extension
+            'File:Example #3.jpg',  # file extension in section
+        ):
+            with self.subTest(title=title):
+                with self.assertRaises(ValueError):
+                    pywikibot.FilePage(site, title)

     def testImageAndDataRepository(self):
         """Test image_repository and data_repository page attributes."""

--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/955411
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.wikimedia.org/r/settings

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Iba951506619f1f14f75e7bfb33c7afc99a240a5a
Gerrit-Change-Number: 955411
Gerrit-PatchSet: 3
Gerrit-Owner: JJMC89 <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
Gerrit-CC: Matěj Suchánek <[email protected]>
Gerrit-MessageType: merged
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to