initial commit

This commit is contained in:
Tan, Kian-ting 2022-12-27 00:43:53 +08:00
commit c518e3ae4c
4 changed files with 156 additions and 0 deletions

31
extension.json Normal file
View file

@ -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"
}
}
}

5
i18n/en.json Normal file
View file

@ -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."
}

6
i18n/qqq.json Normal file
View file

@ -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."}

View file

@ -0,0 +1,114 @@
<?php
use MediaWiki\MediaWikiServices;
class MyRelatedArticle extends SpecialPage {
function __construct() {
parent::__construct( 'MyRelatedArticle' );
$this->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);
}
}