Last active
September 21, 2023 06:20
-
-
Save lokal-profil/2e59b4b07d449a2c9a8ed98429787d80 to your computer and use it in GitHub Desktop.
Source code for https://slumpartikel.toolforge.org/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by User:Prolineserver | |
* Released under GPL per the following statement | |
* https://sv.wikipedia.org/w/index.php?title=Anv%C3%A4ndardiskussion:Prolineserver&oldid=53364425#Licens_f%C3%B6r_Slumpartikel | |
* @license GPL-3.0-or-later | |
*/ | |
if(isset($_REQUEST['source'])){ | |
if(intval($_REQUEST['source']) == 1) { | |
show_source(__FILE__); | |
} | |
} | |
$servername = "svwiki.labsdb"; | |
$database = "svwiki_p"; | |
// sets $username and $password | |
include_once '../.password.php'; | |
$conn = new mysqli($servername, $username, $password, $database); | |
if ($conn->connect_error) { | |
die("Connection failed: " . $conn->connect_error); | |
} | |
$i = 0; | |
$article_found = false; | |
while($i < 100) { | |
$i++; | |
$random = mt_rand() / mt_getrandmax(); | |
$query = "SELECT page_id, page_title | |
FROM page | |
WHERE page_namespace = 0 | |
AND page_is_redirect = 0 | |
AND page_random >= {$random} | |
ORDER BY page_random | |
LIMIT 1"; | |
if(!$res = $conn->query($query)){ | |
die('There was an error running the query [' . $conn->error . ']'); | |
} | |
$res->data_seek(0); | |
$page = $res->fetch_assoc(); | |
$query = "SELECT page.page_id,page_title | |
FROM categorylinks | |
JOIN page ON categorylinks.cl_to = page.page_title | |
AND page.page_namespace = 14 | |
WHERE categorylinks.cl_from = {$page['page_id']}"; | |
if(!$res = $conn->query($query)){ | |
die('There was an error running the query [' . $conn->error . ']'); | |
} | |
$categories = array(); | |
$category_ids = array(); | |
while($row = $res->fetch_assoc()){ | |
if($row['page_title'] == 'Robotskapade_artiklar') { | |
continue 2; | |
} | |
$categories[] = $row; | |
$category_ids[] = $row['page_id']; | |
} | |
if(count($category_ids) == 0) { | |
$article_found = true; | |
break; | |
} | |
$category_ids = implode(', ', $category_ids); | |
$query = "SELECT cl_to | |
FROM categorylinks | |
WHERE categorylinks.cl_from IN ({$category_ids})"; | |
if(!$res = $conn->query($query)){ | |
die('There was an error running the query [' . $conn->error . ']'); | |
} | |
while($row = $res->fetch_assoc()){ | |
if($row['cl_to'] == 'Robotskapade_artiklar') { | |
continue 2; | |
} | |
} | |
$article_found = true; | |
break; | |
} | |
$conn->close(); | |
$mobile = ''; | |
if(isset($_REQUEST['mobile'])){ | |
if(intval($_REQUEST['mobile']) == 1) { | |
$mobile = 'm.'; | |
} | |
} | |
if($article_found) { | |
header("Location: https://sv.{$mobile}wikipedia.org/wiki/".$page['page_title']); | |
} else { | |
header("Location: https://sv.{$mobile}wikipedia.org/"); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment