8000 RC sweep: user reviews pagination, dead imports, stale todos by irfan-dahir · Pull Request #659 · jikan-me/jikan · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ which changes a consumer can see.
- **`AnimeCard::getMembers()` returns `?int`**: `null` when a card shows no member count. In beta.2 it raised `ParserException`, which failed the whole page. **You must** handle `null`.

### Fixed
- `getUserReviews()` pagination: `hasNextPage()` was always `true` and `getLastVisiblePage()` always `1`, so paging through a user's reviews never ended. MyAnimeList shows 10 reviews per page and links only the next page ("More Reviews"), so `hasNextPage()` now reports that link and `getLastVisiblePage()` is the next page when there is one.
- `getAnimeScores()` and `getMangaScores()` on anime reviews, manga reviews and reviewers return the reviewer's overall rating. They always failed: the anime one called a parser that had been deleted, and the manga one read a score table that no longer exists. MyAnimeList's 2022 review redesign dropped the per-category ratings, so `getOverall()` is the "Reviewer's Rating" and the category getters (`getStory()`, `getAnimation()`, `getArt()` and so on) return `null`. `AnimeReviewScores`/`MangaReviewScores` are built with `fromOverall()`; their `fromParser()` and `Jikan\Parser\Manga\MangaReviewScoresParser`, which could not work, are removed.

---
Expand Down
1 change: 0 additions & 1 deletion src/Model/Recommendations/RecentRecommendations.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Jikan\Model\Common\Collection\Pagination;
use Jikan\Model\Common\Collection\Results;
use Jikan\Model\User\UserReviews;
use Jikan\Parser;

/**
Expand Down
2 changes: 0 additions & 2 deletions src/Model/User/AnimeListItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
use Jikan\Helper\Constants;
use Jikan\Helper\JString;
use Jikan\Helper\Parser;
use Jikan\Model\Common\LicensorMeta;
use Jikan\Model\Common\MalUrl;
use Jikan\Model\Common\StudioMeta;
use Jikan\Model\Resource\CommonImageResource\CommonImageResource;

/**
Expand Down
1 change: 0 additions & 1 deletion src/Parser/Anime/AnimeRecentlyUpdatedByUsersParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ function ($c) {
}
}

// @todo anime user updates pagination
public function getHasNextPage(): bool
{
return $this->nextPage() !== null;
Expand Down
1 change: 0 additions & 1 deletion src/Parser/Common/PicturesPageParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Jikan\Parser\Common;

use Jikan\Model\Common\Picture;
use Jikan\Model\Resource\AnimeImageResource\AnimePicturesImageResource;
use Jikan\Model\Resource\CommonImageResource\CommonImageResource;
use Jikan\Parser\ParserInterface;
use Symfony\Component\DomCrawler\Crawler;
Expand Down
1 change: 0 additions & 1 deletion src/Parser/Manga/MangaRecentlyUpdatedByUsersParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ function ($c) {
}


// @todo anime user updates pagination
public function getHasNextPage(): bool
{
return $this->nextPage() !== null;
Expand Down
2 changes: 0 additions & 2 deletions src/Parser/Recommendations/RecentRecommendationsParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
use Jikan\Model\Recommendations\RecentRecommendations;
use Jikan\Model\Recommendations\RecommendationListItem;
use Jikan\Model\Reviews\Reviews;
use Jikan\Parser\Anime\AnimeReviewParser;
use Jikan\Parser\Manga\MangaReviewParser;
use Symfony\Component\DomCrawler\Crawler;

/**
Expand Down
2 changes: 0 additions & 2 deletions src/Parser/Recommendations/UserRecommendationsParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use Jikan\Model\Recommendations\RecommendationListItem;
use Jikan\Model\Recommendations\UserRecommendations;
use Jikan\Model\Reviews\Reviews;
use Jikan\Parser\Anime\AnimeReviewParser;
use Jikan\Parser\Manga\MangaReviewParser;
use Symfony\Component\DomCrawler\Crawler;

/**
Expand Down
30 changes: 26 additions & 4 deletions src/Parser/User/Reviews/UserReviewsParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,35 @@
}
public function hasNextPage(): bool
{
// TODO: Add implementation
return true;
return $this->nextPage() !== null;
}

/**
* The page only links the next one ("More Reviews", ?p=N, 10 reviews per page), not the last, so this is the
* furthest page it lets you reach: the next page when there is one.
*/
public function getLastVisiblePage(): ?int
{
// TODO: Add implementation
return 1;
return $this->nextPage() ?? $this->currentPage();
}

private function currentPage(): int
{
return preg_match('~[?&]p=(\d+)~', $this->crawler->getUri() ?? '', $m) ? (int) $m[1] : 1;

Check warning on line 73 in src/Parser/User/Reviews/UserReviewsParser.php

View workflow job for this annotation

GitHub Actions / infection (changed lines)

Escaped Mutant for Mutator "CastInt": @@ @@ private function currentPage(): int { - return preg_match('~[?&]p=(\d+)~', $this->crawler->getUri() ?? '', $m) ? (int) $m[1] : 1; + return preg_match('~[?&]p=(\d+)~', $this->crawler->getUri() ?? '', $m) ? $m[1] : 1; } /** The lowest linked page after the current one ("More Reviews"), or null on the last page. */
}

/** The lowest linked page after the current one ("More Reviews"), or null on the last page. */
private function nextPage(): ?int
{
$current = $this->currentPage();
$pages = [];
foreach ($this->crawler->filterXPath('//a[contains(@href, "/reviews?p=")]') as $link) {
if ($link instanceof \DOMElement && preg_match('~[?&]p=(\d+)~', $link->getAttribute('href'), $m)) {
$pages[] = (int) $m[1];

Check warning on line 83 in src/Parser/User/Reviews/UserReviewsParser.php

View workflow job for this annotation

GitHub Actions / infection (changed lines)

Escaped Mutant for Mutator "CastInt": @@ @@ $pages = []; foreach ($this->crawler->filterXPath('//a[contains(@href, "/reviews?p=")]') as $link) { if ($link instanceof \DOMElement && preg_match('~[?&]p=(\d+)~', $link->getAttribute('href'), $m)) { - $pages[] = (int) $m[1]; + $pages[] = $m[1]; } } $later = array_filter($pages, static fn(int $page): bool => $page > $current);
}
}
$later = array_filter($pages, static fn(int $page): bool => $page > $current);

return $later === [] ? null : min($later);
}
}
87 changes: 87 additions & 0 deletions test/unit/Corpus/UserReviewsPaginationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace JikanTest\Corpus;

use Jikan\Parser\User\Reviews\UserReviewsParser;
use JikanTest\TestCase;
use PHPUnit\Framework\Attributes\Test;
use Symfony\Component\DomCrawler\Crawler;

/**
* A user's reviews list 10 per page and link only the next page ("More Reviews", ?p=N), never the last.
* hasNextPage() used to be hard-coded to true and getLastVisiblePage() to 1, so paging never ended.
* The recorded profile has 5 reviews (one page); the other cases add the link as MyAnimeList renders it.
*/
class UserReviewsPaginationTest extends TestCase
{
#[Test]
public function a_single_page_of_reviews_has_no_next_page(): void
{
$parser = new UserReviewsParser(self::page('https://myanimelist.net/profile/DatRandomDude/reviews?p=1'));

self::assertFalse($parser->hasNextPage());
self::assertSame(1, $parser->getLastVisiblePage());
}

#[Test]
public function a_more_reviews_link_means_there_is_a_next_page(): void
{
$page = self::page('https://myanimelist.net/profile/DatRandomDude/reviews?p=1', ['2']);
$parser = new UserReviewsParser($page);

self::assertTrue($parser->hasNextPage());
self::assertSame(2, $parser->getLastVisiblePage());
}

#[Test]
public function a_link_back_to_an_earlier_page_is_not_a_next_page(): void
{
$parser = new UserReviewsParser(self::page('https://myanimelist.net/profile/DatRandomDude/reviews?p=3', ['2']));

self::assertFalse($parser->hasNextPage());
self::assertSame(3, $parser->getLastVisiblePage());
}

#[Test]
public function without_a_page_number_the_first_page_is_assumed(): void
{
$parser = new UserReviewsParser(self::page('https://myanimelist.net/profile/DatRandomDude/reviews', ['1']));

self::assertFalse($parser->hasNextPage());
self::assertSame(1, $parser->getLastVisiblePage());
}

#[Test]
public function the_nearest_later_page_is_the_next_one(): void
{
// A link to the current page and several later ones: the next page is the lowest one after the current.
$parser = new UserReviewsParser(self::page('https://myanimelist.net/profile/DatRandomDude/reviews?p=2', ['2', '5', '3']));

self::assertTrue($parser->hasNextPage());
self::assertSame(3, $parser->getLastVisiblePage());
}

/**
* The recorded reviews page, served as $uri, with a "More Reviews" link for each of $links.
*
* @param list<string> $links page numbers
*/
private static function page(string $uri, array $links = []): Crawler
{
[, $html] = Variants::page('user-reviews-datrandomdude');
$crawler = new Crawler(null, $uri);
$crawler->addHtmlContent($html);
$content = $crawler->filterXPath('//*[@id="content"]')->getNode(0);
foreach ($links as $p) {
$div = $content->ownerDocument->createElement('div');
$div->setAttribute('class', 'mt4 mb8');
$a = $content->ownerDocument->createElement('a', 'More Reviews');
$a->setAttribute('href', "https://myanimelist.net/profile/DatRandomDude/reviews?p=$p");
$a->setAttribute('class', 'ga-click');
$div->appendChild($a);
$content->appendChild($div);
}

return $crawler;
}
}

This file was deleted.

This file was deleted.

0