Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- ini_set("display_errors", 1);
- error_reporting(E_ALL);
- header('Content-Type:text/plain');
- class Helper {
- /** Grabs template Wikicode of first instance encountered of that template. Case insensitive. Throws an error if no template found. */
- static function slice_first_template_found($wikicode, $template_name) {
- $starting_position = strpos(strtolower($wikicode), "{{" . strtolower($template_name));
- $counter = 0;
- $length = strlen($wikicode);
- for ( $i = $starting_position + 2; $i < $length; $i++ ) {
- $next_two = substr($wikicode, $i, 2);
- if ( $next_two == "{{" ) {
- $counter++;
- continue;
- } elseif ( $next_two == "}}" ) {
- if ( $counter == 0 ) {
- return substr($wikicode, $starting_position, $i - $starting_position + 2);
- } else {
- $counter--;
- continue;
- }
- }
- }
- throw new UnexpectedValueException("Template not found");
- }
- /** Example: <noinclude>This is an example.</noinclude>. Throws an error if no tags found. */
- static function slice_first_tag_found($wikicode, $tag_with_no_lt_gt) {
- preg_match("/(<" . $tag_with_no_lt_gt . ">.*?<\/" . $tag_with_no_lt_gt . ">)/s", $wikicode, $result);
- if ( $result ) {
- return $result[0];
- } else {
- throw new InvalidArgumentException("Tag not found");
- }
- }
- }
- class Promote {
- /** https://en.wikipedia.org/wiki/User:Aza24/FTC/Promote_Instructions, Step 2 */
- static function make_topic_page($objwiki, $page_title, $good_or_featured) {
- $wikitext = $objwiki->getpage($page_title);
- $wikitext_featured_topic_box = Helper::slice_first_template_found($wikitext, 'Featured topic box');
- $wikitext_topic_description = Helper::slice_first_tag_found($wikitext, 'noinclude');
- preg_match("/Wikipedia:Featured and good topic candidates\/(.*)\/archive\d+/", $page_title, $result);
- if ( $result ) {
- $topic_name = $result[0];
- } else {
- throw new InvalidArgumentException('$page_title does not appear to be a "Featured and good topic candidates" page.');
- }
- // convert Wikipedia:Featured and good topic candidates/Fabula Nova Crystallis Final Fantasy/archive1 to Wikipedia:Good topics/Fabula Nova Crystallis Final Fantasy
- if ( $good_or_featured == 'good' ) {
- $wp_page_title = "Wikipedia:Good topics/" . $topic_name;
- } elseif ( $good_or_featured == 'featured' ) {
- $wp_page_title = "Wikipedia:Featured topics/" . $topic_name;
- } else {
- throw new InvalidArgumentException('$good_or_featured must equal "good" or "featured"');
- }
- // TODO: $wp_page_title... namespace? underscores?
- $wp_page_text = $wikitext_featured_topic_box . "\r\n\r\n" . $wikitext_topic_description;
- $objwiki->edit($wp_page_title, $wp_page_text);
- }
- static function make_topic_talk_page($objwiki) {
- // [[User:Aza24/FTC/Promote Instructions]], Step 3
- // Step 3a
- // $wp_talk_page_title =
- // $topic_title =
- // $datetime_step_1_completed = // this format: 21:38, 5 January 2021
- // $result = // '''Promoted''' with articles '''[[LEAD ARTICLE/LIST]]''', [[OTHER ARTICLE]], [[OTHER ARTICLE]] etc.
- /*
- $wp_talk_page_text = "{{Featuredtopictalk
- |title = " . $topic_title . "
- |action1 = " . $good_or_featured . "
- |action1date = " . $datetime_step_1_completed . "
- |action1link = Wikipedia:Featured and good topic candidates/" . $topic_title . "/archive1
- |action1result = " . $result . "
- |currentstatus = current
- }}";
- */
- // $topic_lead_article_title =
- // complex regex to grab lead article's wikiproject banners
- // step 3b
- // $wp_talk_page_text .= "\r\n\r\n" .
- /*
- {{WikiProject banner shell|1=
- {{WikiProject INSERTNAME}}
- {{WikiProject INSERTNAME}}
- }}
- */
- }
- static function update_talk_pages_of_child_articles($objwiki) {
- // [[User:Aza24/FTC/Promote Instructions]], Step 4
- // $list_of_article_titles[] =
- // foreach ( $list_of_article_titles as $key => $article_title ) {
- // Step 4a
- // $article_wikitext =
- // $article_wikitext = // remove FTC/GTC nomination template
- // make edit
- // Step 4b
- // add the following to article history (only add "ftmain = yes to the main article of the topic):
- /*
- |action4 = GTC OR FTC
- |action4date = DATE AND TIME THAT STEP 1 WAS COMPLETED (the same as 3a's time; format example: 21:38, 5 January 2021)
- |action4link = Wikipedia:Featured and good topic candidates/NAME OF THE TOPIC/archive1
- |action4result = promoted
- |ftname = NAME OF THE TOPIC
- |ftmain = NO (YES FOR THE MAIN ARTICLE)
- this should remain below:
- |currentstatus=Keep this how it is
- }}
- */
- // }
- }
- static function update_count($objwiki) {
- // $counter_page_title = ( $good_or_featured == "featured" ) ? Wikipedia:Featured topics/count" : "Wikipedia:Good topics/count";
- // TODO:
- }
- static function update_topic_list($objwiki) {
- }
- static function create_and_fill_child_categories($objwiki) {
- }
- static function create_and_fill_parent_category($objwiki) {
- }
- static function add_to_log($objwiki) {
- }
- static function announce_featured_topic($objwiki) {
- }
- }
- /** Wrapper for I/O. Makes it easier to test. botclasses.php read and write functions can be replaced with other code when testing. */
- class WikiAPIWrapper {
- function __construct($wiki_username, $wiki_password) {
- echo "\nLogging in...\n";
- $this->objwiki = new wikipedia();
- $this->objwiki->http->useragent = '[[en:User:NovemBot]], owner [[en:User:Novem Linguae]], framework [[en:User:RMCD_bot/botclasses.php]]';
- $this->objwiki->login($wiki_username, $wiki_password);
- echo "Done!\n";
- }
- function getpage($namespace_and_title) {
- echo "\nReading page $namespace_and_title...\n";
- $output = $this->objwiki->getpage($namespace_and_title);
- echo "...done.\n";
- return $output;
- }
- // TODO: does page title need underscores?
- function edit($namespace_and_title, $wikicode, $edit_summary = 'NovemBot Task 2: promote successful featured topic/good topic candidate') {
- echo "\nWriting data to $namespace_and_title...\n";
- /*
- $this->objwiki->edit(
- $namespace_and_title,
- $wikicode,
- $edit_summary
- );
- */
- echo $wikicode . "\n";
- echo "...done.\n";
- }
- }
- // set_time_limit(1440); # 24 minutes
- include("../botclasses.php");
- include("../logininfo.php");
- // Keep randos from running the bot in browser and in bash
- if ( ($_GET['password'] ?? '') != $http_get_password && ($argv[1] ?? '') != $http_get_password ) {
- die('Invalid password.');
- }
- echo "PHP version: " . PHP_VERSION . "\n\n";
- $objwiki = new WikiAPIWrapper($wiki_username, $wiki_password);
- // TODO: Security. Only an extended confirmed user should be able to trigger the bot.
- // TODO: Security. Each foreach loop should have a maximum.
- // TODO: logging.
- // TODO: This is just to test. Later, pull a list of template transclusions. We'll make a template called {{Promote topic}} or something and use it to mark the ones to be promoted.
- $page_titles_to_process = ['Wikipedia:Featured and good topic candidates/Fabula Nova Crystallis Final Fantasy/archive1'];
- foreach ( $page_titles_to_process as $key => $page_title ) {
- // try {
- $good_or_featured = 'good'; //TODO: logic instead of static value
- Promote::make_topic_page($objwiki, $page_title, $good_or_featured);
- Promote::make_topic_talk_page($objwiki);
- Promote::update_talk_pages_of_child_articles($objwiki);
- Promote::update_count($objwiki);
- Promote::update_topic_list($objwiki);
- Promote::create_and_fill_child_categories($objwiki);
- Promote::create_and_fill_parent_category($objwiki);
- Promote::add_to_log($objwiki);
- Promote::announce_featured_topic($objwiki);
- // create new page
- // edit original page, replace {{Promote topic}} with *: Promotion complete. ~~~~
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement