Skip to content

Commit

Permalink
fix #67 determine topic name by looking at {{Featured topic box|title…
Browse files Browse the repository at this point in the history
…=}} parameter, if present
  • Loading branch information
NovemLinguae committed Oct 28, 2021
1 parent 430e4af commit 755c26e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 25 deletions.
37 changes: 25 additions & 12 deletions src/Promote.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ function getMainArticleTitle($topicBoxWikicode, $title) {
return $mainArticleTitle;
}

function getTopicTitle($topicBoxWikicode, $mainArticleTitle) {
// search for |title= parameter
preg_match("/\|\s*title\s*=\s*([^\|\}]+)\s*/is", $topicBoxWikicode, $matches);

// if not found, return $mainArticleTitle as topicTitle
if ( ! $matches ) {
return $mainArticleTitle;
}

// if found, return that as topicTitle
return trim($matches[1]);
}

/** It's OK if this one isn't able to find anything. Not a critical error. It can return blank. */
function getTopicDescriptionWikicode($callerPageWikicode) {
preg_match('/===(\n.*?)\{\{(?:Featured topic box|Good topic box)/si', $callerPageWikicode, $matches);
Expand All @@ -60,8 +73,8 @@ function getTopicDescriptionWikicode($callerPageWikicode) {
return $output;
}

function getTopicWikipediaPageTitle($mainArticleTitle) {
return "Wikipedia:Featured topics/$mainArticleTitle";
function getTopicWikipediaPageTitle($topicTitle) {
return "Wikipedia:Featured topics/$topicTitle";
}

function getTopicWikipediaPageWikicode($topicDescriptionWikicode, $topicBoxWikicode) {
Expand Down Expand Up @@ -106,7 +119,7 @@ function getAllArticleTitles($topicBoxWikicode, $title) {
return $listOfTitles;
}

function makeTopicTalkPageWikicode($mainArticleTitle, $nonMainArticleTitles, $goodOrFeatured, $datetime, $wikiProjectBanners, $nominationPageTitle) {
function makeTopicTalkPageWikicode($mainArticleTitle, $topicTitle, $nonMainArticleTitles, $goodOrFeatured, $datetime, $wikiProjectBanners, $nominationPageTitle) {
assert($goodOrFeatured == 'good' || $goodOrFeatured == 'featured');
$nonMainArticleTitlestring = '';
$count = 1;
Expand All @@ -122,7 +135,7 @@ function makeTopicTalkPageWikicode($mainArticleTitle, $nonMainArticleTitles, $go
$actionCode = ($goodOrFeatured == 'good') ? 'GTC' : 'FTC';
$talkWikicode =
"{{Featuredtopictalk
|title = $mainArticleTitle
|title = $topicTitle
|action1 = $actionCode
|action1date = $datetime
|action1link = $nominationPageTitle
Expand All @@ -133,8 +146,8 @@ function makeTopicTalkPageWikicode($mainArticleTitle, $nonMainArticleTitles, $go
return $talkWikicode;
}

function getTopicTalkPageTitle($mainArticleTitle) {
return 'Wikipedia talk:Featured topics/' . $mainArticleTitle;
function getTopicTalkPageTitle($topicTitle) {
return 'Wikipedia talk:Featured topics/' . $topicTitle;
}

function getWikiProjectBanners($mainArticleTalkPageWikicode, $title) {
Expand Down Expand Up @@ -181,7 +194,7 @@ function determineNextActionNumber($talkPageWikicode, $ARTICLE_HISTORY_MAX_ACTIO
throw new GiveUpOnThisTopic("On page $talkPageTitle, in {{Article history}} template, unable to determine next |action= number.");
}

function updateArticleHistory($talkPageWikicode, $nextActionNumber, $goodOrFeatured, $datetime, $mainArticleTitle, $articleTitle, $talkPageTitle, $nominationPageTitle) {
function updateArticleHistory($talkPageWikicode, $nextActionNumber, $goodOrFeatured, $datetime, $mainArticleTitle, $topicTitle, $articleTitle, $talkPageTitle, $nominationPageTitle) {
assert($goodOrFeatured == 'good' || $goodOrFeatured == 'featured');
$main = ( $mainArticleTitle == $articleTitle ) ? 'yes' : 'no';
$ftcOrGTC = ( $goodOrFeatured == 'featured' ) ? 'FTC' : 'GTC';
Expand All @@ -190,7 +203,7 @@ function updateArticleHistory($talkPageWikicode, $nextActionNumber, $goodOrFeatu
|action{$nextActionNumber}date = $datetime
|action{$nextActionNumber}link = $nominationPageTitle
|action{$nextActionNumber}result = promoted
|ftname = $mainArticleTitle
|ftname = $topicTitle
|ftmain = $main";
$newWikicode = $this->h->insertCodeAtEndOfFirstTemplate($talkPageWikicode, 'Article ?history', $addToArticleHistory);
if ( $newWikicode == $talkPageWikicode ) {
Expand Down Expand Up @@ -378,17 +391,17 @@ function getLogPageTitle($datetime, $goodOrFeatured) {
return "Wikipedia:Featured and good topic candidates/$goodOrFeatured log/$monthAndYear";
}

function addTopicToGoingsOn($goingsOnTitle, $goingsOnWikicode, $topicWikipediaPageTitle, $mainArticleTitle, $timestamp) {
function addTopicToGoingsOn($goingsOnTitle, $goingsOnWikicode, $topicWikipediaPageTitle, $topicTitle, $timestamp) {
$date = date('j M', $timestamp); // gmdate = UTC
$newWikicode = preg_replace("/('''\[\[Wikipedia:Featured topics\|Topics]] that gained featured status'''.*?)(\|})/s", "$1* [[$topicWikipediaPageTitle|$mainArticleTitle]] ($date)\n$2", $goingsOnWikicode);
$newWikicode = preg_replace("/('''\[\[Wikipedia:Featured topics\|Topics]] that gained featured status'''.*?)(\|})/s", "$1* [[$topicWikipediaPageTitle|$topicTitle]] ($date)\n$2", $goingsOnWikicode);
if ( $newWikicode == $goingsOnWikicode ) {
throw new GiveUpOnThisTopic("On page $goingsOnTitle, unable to figure out where to insert code.");
}
return $newWikicode;
}

function addTopicToNewFeaturedContent($newFeaturedContentTitle, $newFeaturedContentWikicode, $topicWikipediaPageTitle, $mainArticleTitle) {
$newWikicode = preg_replace("/(<!-- Topics \(15, most recent first\) -->)/", "$1\n* [[$topicWikipediaPageTitle|$mainArticleTitle]]", $newFeaturedContentWikicode);
function addTopicToNewFeaturedContent($newFeaturedContentTitle, $newFeaturedContentWikicode, $topicWikipediaPageTitle, $topicTitle) {
$newWikicode = preg_replace("/(<!-- Topics \(15, most recent first\) -->)/", "$1\n* [[$topicWikipediaPageTitle|$topicTitle]]", $newFeaturedContentWikicode);
if ( $newWikicode == $newFeaturedContentWikicode ) {
throw new GiveUpOnThisTopic("On page $newFeaturedContentTitle, unable to figure out where to insert code.");
}
Expand Down
27 changes: 14 additions & 13 deletions src/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
$topicBoxWikicode = $p->getTopicBoxWikicode($nominationPageWikicode, $nominationPageTitle);
$topicBoxWikicode = $p->setTopicBoxViewParameterToYes($topicBoxWikicode);
$mainArticleTitle = $p->getMainArticleTitle($topicBoxWikicode, $nominationPageTitle);
$topicTitle = $p->getTopicTitle($topicBoxWikicode, $mainArticleTitle);
$topicBoxWikicode = $p->setTopicBoxTitleParameter($topicBoxWikicode, $mainArticleTitle);
$topicBoxWikicode = $p->cleanTopicBoxTitleParameter($topicBoxWikicode);
$allArticleTitles = $p->getAllArticleTitles($topicBoxWikicode, $nominationPageTitle);
Expand All @@ -101,17 +102,17 @@
// STEP 2 - MAKE TOPIC PAGE ===============================================================
$topicDescriptionWikicode = $p->getTopicDescriptionWikicode($nominationPageWikicode);
$topicDescriptionWikicode = $p->removeSignaturesFromTopicDescription($topicDescriptionWikicode);
$topicWikipediaPageTitle = $p->getTopicWikipediaPageTitle($mainArticleTitle);
$topicWikipediaPageTitle = $p->getTopicWikipediaPageTitle($topicTitle);
$topicWikipediaPageWikicode = $p->getTopicWikipediaPageWikicode($topicDescriptionWikicode, $topicBoxWikicode);
$wapi->edit($topicWikipediaPageTitle, $topicWikipediaPageWikicode, $topicWikipediaPageTitle, $goodOrFeatured);

// STEP 3 - MAKE TOPIC TALK PAGE ==========================================================
$topicTalkPageTitle = $p->getTopicTalkPageTitle($mainArticleTitle);
$topicTalkPageTitle = $p->getTopicTalkPageTitle($topicTitle);
$datetime = $p->getDatetime();
$nonMainArticleTitles = $p->getNonMainArticleTitles($allArticleTitles, $mainArticleTitle);
$mainArticleTalkPageWikicode = $wapi->getpage('Talk:'.$mainArticleTitle);
$wikiProjectBanners = $p->getWikiProjectBanners($mainArticleTalkPageWikicode, $mainArticleTitle);
$topicTalkPageWikicode = $p->makeTopicTalkPageWikicode($mainArticleTitle, $nonMainArticleTitles, $goodOrFeatured, $datetime, $wikiProjectBanners, $nominationPageTitle);
$wikiProjectBanners = $p->getWikiProjectBanners($mainArticleTalkPageWikicode, $topicTitle);
$topicTalkPageWikicode = $p->makeTopicTalkPageWikicode($mainArticleTitle, $topicTitle, $nonMainArticleTitles, $goodOrFeatured, $datetime, $wikiProjectBanners, $nominationPageTitle);
$wapi->edit($topicTalkPageTitle, $topicTalkPageWikicode, $topicWikipediaPageTitle, $goodOrFeatured);

// STEP 4 - UPDATE TALK PAGES OF ARTICLES =================================================
Expand All @@ -123,7 +124,7 @@
$talkPageWikicode = $p->removeGTCFTCTemplate($talkPageWikicode);
$talkPageWikicode = $p->addArticleHistoryIfNotPresent($talkPageWikicode, $talkPageTitle);
$nextActionNumber = $p->determineNextActionNumber($talkPageWikicode, $ARTICLE_HISTORY_MAX_ACTIONS, $talkPageTitle);
$talkPageWikicode = $p->updateArticleHistory($talkPageWikicode, $nextActionNumber, $goodOrFeatured, $datetime, $mainArticleTitle, $articleTitle, $talkPageTitle, $nominationPageTitle);
$talkPageWikicode = $p->updateArticleHistory($talkPageWikicode, $nextActionNumber, $goodOrFeatured, $datetime, $mainArticleTitle, $topicTitle, $articleTitle, $talkPageTitle, $nominationPageTitle);
$wapi->edit($talkPageTitle, $talkPageWikicode, $topicWikipediaPageTitle, $goodOrFeatured);
}

Expand All @@ -140,19 +141,19 @@

// STEP 7 - CREATE CHILD CATEGORIES =====================================================
if ( $goodArticleCount > 0 ) {
$goodArticleCategoryTitle = "Category:Wikipedia featured topics $mainArticleTitle good content";
$goodArticleCategoryWikitext = "[[Category:Wikipedia featured topics $mainArticleTitle]]";
$goodArticleCategoryTitle = "Category:Wikipedia featured topics $topicTitle good content";
$goodArticleCategoryWikitext = "[[Category:Wikipedia featured topics $topicTitle]]";
$wapi->edit($goodArticleCategoryTitle, $goodArticleCategoryWikitext, $topicWikipediaPageTitle, $goodOrFeatured);
}
if ( $featuredArticleCount > 0 ) {
$featuredArticleCategoryTitle = "Category:Wikipedia featured topics $mainArticleTitle featured content";
$featuredArticleCategoryWikitext = "[[Category:Wikipedia featured topics $mainArticleTitle]]";
$featuredArticleCategoryTitle = "Category:Wikipedia featured topics $topicTitle featured content";
$featuredArticleCategoryWikitext = "[[Category:Wikipedia featured topics $topicTitle]]";
$wapi->edit($featuredArticleCategoryTitle, $featuredArticleCategoryWikitext, $topicWikipediaPageTitle, $goodOrFeatured);
}

// STEP 8 - CREATE PARENT CATEGORY ========================================================
$parentCategoryTitle = "Category:Wikipedia featured topics $mainArticleTitle";
$parentCategoryWikitext = "[[Category:Wikipedia featured topics categories|$mainArticleTitle]]";
$parentCategoryTitle = "Category:Wikipedia featured topics $topicTitle";
$parentCategoryWikitext = "[[Category:Wikipedia featured topics categories|$topicTitle]]";
$wapi->edit($parentCategoryTitle, $parentCategoryWikitext, $topicWikipediaPageTitle, $goodOrFeatured);

// STEP 9 - ADD TO LOG ====================================================================
Expand All @@ -166,15 +167,15 @@
// [[Template:Announcements/New featured content]]: add this article to top, remove 1 from the bottom
$newFeaturedContentTitle = 'Template:Announcements/New featured content';
$newFeaturedContentWikicode = $wapi->getpage($newFeaturedContentTitle);
$newFeaturedContentWikicode = $p->addTopicToNewFeaturedContent($newFeaturedContentTitle, $newFeaturedContentWikicode, $topicWikipediaPageTitle, $mainArticleTitle);
$newFeaturedContentWikicode = $p->addTopicToNewFeaturedContent($newFeaturedContentTitle, $newFeaturedContentWikicode, $topicWikipediaPageTitle, $topicTitle);
$newFeaturedContentWikicode = $p->removeBottomTopicFromNewFeaturedContent($newFeaturedContentTitle, $newFeaturedContentWikicode);
$wapi->edit($newFeaturedContentTitle, $newFeaturedContentWikicode, $topicWikipediaPageTitle, $goodOrFeatured);

// [[Wikipedia:Goings-on]]: add
$goingsOnTitle = 'Wikipedia:Goings-on';
$goingsOnWikicode = $wapi->getpage($goingsOnTitle);
$timestamp = time();
$goingsOnWikicode = $p->addTopicToGoingsOn($goingsOnTitle, $goingsOnWikicode, $topicWikipediaPageTitle, $mainArticleTitle, $timestamp);
$goingsOnWikicode = $p->addTopicToGoingsOn($goingsOnTitle, $goingsOnWikicode, $topicWikipediaPageTitle, $topicTitle, $timestamp);
$wapi->edit($goingsOnTitle, $goingsOnWikicode, $topicWikipediaPageTitle, $goodOrFeatured);
}

Expand Down
22 changes: 22 additions & 0 deletions tests/PromoteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -835,4 +835,26 @@ function test_getTopicDescriptionWikicode_commentedNoInclude() {
'<noinclude>The [[EFL League Two play-offs]] are a series of play-off matches contested by the association football teams finishing from fourth to seventh in [[EFL League Two]], the fourth tier of English football, and are part of the [[English Football League play-offs]]. As of 2021, the play-offs comprise two semi-finals, where the team finishing third plays the team finishing sixth, and the team finishing fourth plays the team finishing fifth, each conducted as a two-legged tie. The winners of the semi-finals progress to the final which is contested at [[Wembley Stadium]].</noinclude>'
, $result);
}

function test_getTopicTitle_withTopic() {
$topicBoxWikicode =
'{{Featured topic box |title=UEFA European Championship finals |count=17 |image=Coupe Henri Delaunay 2017.jpg |imagesize=
|lead={{icon|FL}} [[List of UEFA European Championship finals|UEFA European Championship finals]]
|column1=
:{{icon|GA}} [[UEFA Euro 2020 Final]] }}';
$mainArticleTitle = 'List of UEFA European Championship finals';
$result = $this->p->getTopicTitle($topicBoxWikicode, $mainArticleTitle);
$this->assertSame('UEFA European Championship finals', $result);
}

function test_getTopicTitle_noTopic() {
$topicBoxWikicode =
'{{Featured topic box |count=17 |image=Coupe Henri Delaunay 2017.jpg |imagesize=
|lead={{icon|FL}} [[List of UEFA European Championship finals|UEFA European Championship finals]]
|column1=
:{{icon|GA}} [[UEFA Euro 2020 Final]] }}';
$mainArticleTitle = 'List of UEFA European Championship finals';
$result = $this->p->getTopicTitle($topicBoxWikicode, $mainArticleTitle);
$this->assertSame('List of UEFA European Championship finals', $result);
}
}

0 comments on commit 755c26e

Please sign in to comment.