Skip to content

Commit

Permalink
FilePage: raise ValueError when title doesn't have a valid file exten…
Browse files Browse the repository at this point in the history
…sion

Bug: T345786
Change-Id: Iba951506619f1f14f75e7bfb33c7afc99a240a5a
  • Loading branch information
JJMC89 committed Sep 7, 2023
1 parent 9eb13f6 commit 8c86bcb
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
7 changes: 7 additions & 0 deletions pywikibot/page/_filepage.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ def __init__(self, source, title: str = '') -> None:
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:
Expand Down
5 changes: 5 additions & 0 deletions pywikibot/site/_apisite.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,11 @@ def logout(self) -> None:
# Clear also cookies for site's second level domain (T224712)
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.
Expand Down
16 changes: 8 additions & 8 deletions tests/file_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,24 +164,24 @@ class TestFilePage(TestCase):
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

Expand Down Expand Up @@ -322,13 +322,13 @@ def test_changed_title(self):
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)


Expand Down Expand Up @@ -373,7 +373,7 @@ def test_data_item(self):
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()
Expand Down
8 changes: 8 additions & 0 deletions tests/page_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,14 @@ def testFileTitle(self):
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."""
Expand Down

0 comments on commit 8c86bcb

Please sign in to comment.