From c518e3ae4c1b91d2d876a3c965d9d75b1321fe38 Mon Sep 17 00:00:00 2001 From: "Chen, Chien-ting" Date: Tue, 27 Dec 2022 00:43:53 +0800 Subject: [PATCH] initial commit --- extension.json | 31 +++++++++ i18n/en.json | 5 ++ i18n/qqq.json | 6 ++ includes/MyRelatedArticle.php | 114 ++++++++++++++++++++++++++++++++++ 4 files changed, 156 insertions(+) create mode 100644 extension.json create mode 100644 i18n/en.json create mode 100644 i18n/qqq.json create mode 100644 includes/MyRelatedArticle.php diff --git a/extension.json b/extension.json new file mode 100644 index 0000000..4bb8923 --- /dev/null +++ b/extension.json @@ -0,0 +1,31 @@ +{ + "name": "MyRelatedArticle", + "version": "0.0.0", + "author": [ + "Tan, Kian-ting" + ], + "url": "", + "descriptionmsg": "my-related-article-desc", + "license-name": "MIT", + "type": "other", + "AutoloadClasses": { + "MyRelatedArticle": "includes/MyRelatedArticle.php" + }, + + "SpecialPages": { + "MyRelatedArticle": "MyRelatedArticle" + }, + "MessagesDirs": { + "MyRelatedArticle": [ + "i18n" + ] + }, + "manifest_version": 2, + "requires": { + "MediaWiki": ">= 1.36.0", + "platform": { + "php": ">= 5.6" + } + } +} + diff --git a/i18n/en.json b/i18n/en.json new file mode 100644 index 0000000..2323e23 --- /dev/null +++ b/i18n/en.json @@ -0,0 +1,5 @@ +{ + "my-related-article": "My Related Article", + "my-related-article-desc": "show related articles", + "my-related-article-summary": "show related articles in the category or the parent cateygory of it." +} diff --git a/i18n/qqq.json b/i18n/qqq.json new file mode 100644 index 0000000..8333c7b --- /dev/null +++ b/i18n/qqq.json @@ -0,0 +1,6 @@ +{ + "my-related-article": "My Related Article, + "my-related-article-desc": "show related articles", + "my-related-article-summary": "show related articles in the +category or the parent cateygory of it."} + diff --git a/includes/MyRelatedArticle.php b/includes/MyRelatedArticle.php new file mode 100644 index 0000000..a445b2f --- /dev/null +++ b/includes/MyRelatedArticle.php @@ -0,0 +1,114 @@ +mIncludable = true; + } + + + + + function execute( $par ) { + global $wgOut; + + +$homepage = file_get_contents('https://kianting.info/wiki/api.php?action=query&format=json&prop=categories&titles='.$par); +$jsonResult = json_decode($homepage, true); + +$categoryArray = array(); + +foreach ($jsonResult["query"]["pages"] as $k1 => $v1){ + foreach($v1["categories"] as $k2 => $v2){ + array_push($categoryArray, $v2["title"]); + } +} + +$articleArray = array(); + +foreach ($categoryArray as $i => $category){ + $query = file_get_contents('https://kianting.info/wiki/api.php?action=query&cmtype=page&list=categorymembers&cmtitle='.$category.'&format=json'); + $jsonResult = json_decode($query, true); + foreach ($jsonResult["query"]["categorymembers"] as $i => $article){ + array_push($articleArray, $article["title"]); + } +} + + + +if(count($articleArray)<5){ +$parentCategoryArray = array(); + + +foreach ($categoryArray as $i => $category){ + $query = file_get_contents('https://kianting.info/wiki/api.php?action=query&format=json&prop=categories&titles='.$category); + $jsonResult = json_decode($query, true); + + foreach ($jsonResult["query"]["pages"] as $k1 => $v1){ + foreach($v1["categories"] as $k2 => $v2){ + array_push($parentCategoryArray, $v2["title"]); + } +} +} + +foreach ($parentCategoryArray as $i => $category){ + $query = file_get_contents('https://kianting.info/wiki/api.php?action=query&cmtype=page&list=categorymembers&cmtitle='.$category.'&format=json'); + $jsonResult = json_decode($query, true); + foreach ($jsonResult["query"]["categorymembers"] as $i => $article){ + array_push($articleArray, $article["title"]); + } +} +} + + + +$articleArray = array_diff($articleArray, array($par)); + + +$selectedArray = array(); + + + + +if (count($articleArray) > 5){ + +foreach( array_rand($articleArray, 5) as $key ) { + array_push($selectedArray, $articleArray[$key]); +} +}else{ + $selectedArray = $articleArray; +} + + # addWikiMsg + +$outputContent = "== 關聯條目 ==\n"; + +foreach ($selectedArray as $i => $article){ + $outputContent = $outputContent. "* [[" . $article . "]]\n"; +} + + +$wgOut->addWikiTextAsInterface($outputContent); +# echo( $json_result["query"]["pages"][644]); +#$wgOut->addHTML($outputContent); + } + + + + + + +} + +