Advertisement
CarefulCoder

Featured topic bot

May 18th, 2021
581
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.75 KB | None | 0 0
  1. <?php
  2.  
  3. ini_set("display_errors", 1);
  4. error_reporting(E_ALL);
  5. header('Content-Type:text/plain');
  6.  
  7. class Helper {
  8.     /** Grabs template Wikicode of first instance encountered of that template. Case insensitive. Throws an error if no template found. */
  9.     static function slice_first_template_found($wikicode, $template_name) {
  10.         $starting_position = strpos(strtolower($wikicode), "{{" . strtolower($template_name));
  11.         $counter = 0;
  12.         $length = strlen($wikicode);
  13.         for ( $i = $starting_position + 2; $i < $length; $i++ ) {
  14.             $next_two = substr($wikicode, $i, 2);
  15.             if ( $next_two == "{{" ) {
  16.                 $counter++;
  17.                 continue;
  18.             } elseif ( $next_two == "}}" ) {
  19.                 if ( $counter == 0 ) {
  20.                     return substr($wikicode, $starting_position, $i - $starting_position + 2);
  21.                 } else {
  22.                     $counter--;
  23.                     continue;
  24.                 }
  25.             }
  26.         }
  27.         throw new UnexpectedValueException("Template not found");
  28.     }
  29.    
  30.     /** Example: <noinclude>This is an example.</noinclude>. Throws an error if no tags found. */
  31.     static function slice_first_tag_found($wikicode, $tag_with_no_lt_gt) {
  32.         preg_match("/(<" . $tag_with_no_lt_gt . ">.*?<\/" . $tag_with_no_lt_gt . ">)/s", $wikicode, $result);
  33.         if ( $result ) {
  34.             return $result[0];
  35.         } else {
  36.             throw new InvalidArgumentException("Tag not found");
  37.         }
  38.     }
  39. }
  40.  
  41. class Promote {
  42.     /** https://en.wikipedia.org/wiki/User:Aza24/FTC/Promote_Instructions, Step 2 */
  43.     static function make_topic_page($objwiki, $page_title, $good_or_featured) {
  44.         $wikitext = $objwiki->getpage($page_title);
  45.         $wikitext_featured_topic_box = Helper::slice_first_template_found($wikitext, 'Featured topic box');
  46.         $wikitext_topic_description = Helper::slice_first_tag_found($wikitext, 'noinclude');
  47.        
  48.         preg_match("/Wikipedia:Featured and good topic candidates\/(.*)\/archive\d+/", $page_title, $result);
  49.         if ( $result ) {
  50.             $topic_name = $result[0];
  51.         } else {
  52.             throw new InvalidArgumentException('$page_title does not appear to be a "Featured and good topic candidates" page.');
  53.         }
  54.        
  55.         // convert Wikipedia:Featured and good topic candidates/Fabula Nova Crystallis Final Fantasy/archive1 to Wikipedia:Good topics/Fabula Nova Crystallis Final Fantasy
  56.         if ( $good_or_featured == 'good' ) {
  57.             $wp_page_title = "Wikipedia:Good topics/" . $topic_name;
  58.         } elseif ( $good_or_featured == 'featured' ) {
  59.             $wp_page_title = "Wikipedia:Featured topics/" . $topic_name;
  60.         } else {
  61.             throw new InvalidArgumentException('$good_or_featured must equal "good" or "featured"');
  62.         }
  63.        
  64.         // TODO: $wp_page_title... namespace? underscores?
  65.        
  66.         $wp_page_text = $wikitext_featured_topic_box . "\r\n\r\n" . $wikitext_topic_description;
  67.         $objwiki->edit($wp_page_title, $wp_page_text);
  68.     }
  69.    
  70.     static function make_topic_talk_page($objwiki) {
  71.         // [[User:Aza24/FTC/Promote Instructions]], Step 3
  72.             // Step 3a
  73.                 // $wp_talk_page_title =
  74.                 // $topic_title =
  75.                 // $datetime_step_1_completed = // this format: 21:38, 5 January 2021
  76.                 // $result = // '''Promoted''' with articles '''[[LEAD ARTICLE/LIST]]''', [[OTHER ARTICLE]], [[OTHER ARTICLE]] etc.
  77.                 /*
  78.                 $wp_talk_page_text = "{{Featuredtopictalk
  79. |title = " . $topic_title . "
  80. |action1 = " . $good_or_featured . "
  81. |action1date = " . $datetime_step_1_completed . "
  82. |action1link = Wikipedia:Featured and good topic candidates/" . $topic_title . "/archive1
  83. |action1result = " . $result . "
  84. |currentstatus = current
  85. }}";
  86.                 */
  87.                 // $topic_lead_article_title =
  88.                 // complex regex to grab lead article's wikiproject banners
  89.            
  90.             // step 3b
  91.                 // $wp_talk_page_text .= "\r\n\r\n" .
  92.                     /*
  93.                     {{WikiProject banner shell|1=
  94.                     {{WikiProject INSERTNAME}}
  95.                     {{WikiProject INSERTNAME}}
  96.                     }}
  97.                     */
  98.     }
  99.    
  100.     static function update_talk_pages_of_child_articles($objwiki) {
  101.         // [[User:Aza24/FTC/Promote Instructions]], Step 4
  102.         // $list_of_article_titles[] =
  103.         // foreach ( $list_of_article_titles as $key => $article_title ) {
  104.             // Step 4a
  105.                 // $article_wikitext =
  106.                 // $article_wikitext = // remove FTC/GTC nomination template
  107.                 // make edit
  108.            
  109.             // Step 4b
  110.                 // add the following to article history (only add "ftmain = yes to the main article of the topic):
  111.                     /*
  112.                         |action4 = GTC OR FTC
  113.                         |action4date = DATE AND TIME THAT STEP 1 WAS COMPLETED (the same as 3a's time; format example: 21:38, 5 January 2021)
  114.                         |action4link = Wikipedia:Featured and good topic candidates/NAME OF THE TOPIC/archive1
  115.                         |action4result = promoted
  116.                         |ftname = NAME OF THE TOPIC
  117.                         |ftmain = NO (YES FOR THE MAIN ARTICLE)
  118.                         this should remain below:
  119.                         |currentstatus=Keep this how it is
  120.                         }}
  121.                     */
  122.         // }
  123.     }
  124.    
  125.     static function update_count($objwiki) {
  126.             // $counter_page_title = ( $good_or_featured == "featured" ) ? Wikipedia:Featured topics/count" : "Wikipedia:Good topics/count";
  127.             // TODO:
  128.     }
  129.    
  130.     static function update_topic_list($objwiki) {
  131.    
  132.     }
  133.    
  134.     static function create_and_fill_child_categories($objwiki) {
  135.    
  136.     }
  137.    
  138.     static function create_and_fill_parent_category($objwiki) {
  139.    
  140.     }
  141.    
  142.     static function add_to_log($objwiki) {
  143.    
  144.     }
  145.    
  146.     static function announce_featured_topic($objwiki) {
  147.    
  148.     }
  149. }
  150.  
  151. /** Wrapper for I/O. Makes it easier to test. botclasses.php read and write functions can be replaced with other code when testing. */
  152. class WikiAPIWrapper {
  153.     function __construct($wiki_username, $wiki_password) {
  154.         echo "\nLogging in...\n";
  155.         $this->objwiki = new wikipedia();
  156.         $this->objwiki->http->useragent = '[[en:User:NovemBot]], owner [[en:User:Novem Linguae]], framework [[en:User:RMCD_bot/botclasses.php]]';
  157.         $this->objwiki->login($wiki_username, $wiki_password);
  158.         echo "Done!\n";
  159.     }
  160.  
  161.     function getpage($namespace_and_title) {
  162.         echo "\nReading page $namespace_and_title...\n";
  163.         $output = $this->objwiki->getpage($namespace_and_title);
  164.         echo "...done.\n";
  165.         return $output;
  166.     }
  167.  
  168.     // TODO: does page title need underscores?
  169.     function edit($namespace_and_title, $wikicode, $edit_summary = 'NovemBot Task 2: promote successful featured topic/good topic candidate') {
  170.         echo "\nWriting data to $namespace_and_title...\n";
  171.         /*
  172.         $this->objwiki->edit(
  173.             $namespace_and_title,
  174.             $wikicode,
  175.             $edit_summary
  176.         );
  177.         */
  178.         echo $wikicode . "\n";
  179.         echo "...done.\n";
  180.     }
  181. }
  182.  
  183. // set_time_limit(1440);    # 24 minutes
  184. include("../botclasses.php");
  185. include("../logininfo.php");
  186.  
  187. // Keep randos from running the bot in browser and in bash
  188. if ( ($_GET['password'] ?? '') != $http_get_password && ($argv[1] ?? '') != $http_get_password ) {
  189.     die('Invalid password.');
  190. }
  191.  
  192. echo "PHP version: " . PHP_VERSION . "\n\n";
  193.  
  194. $objwiki = new WikiAPIWrapper($wiki_username, $wiki_password);
  195.  
  196. // TODO: Security. Only an extended confirmed user should be able to trigger the bot.
  197. // TODO: Security. Each foreach loop should have a maximum.
  198. // TODO: logging.
  199.  
  200. // 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.
  201. $page_titles_to_process = ['Wikipedia:Featured and good topic candidates/Fabula Nova Crystallis Final Fantasy/archive1'];
  202. foreach ( $page_titles_to_process as $key => $page_title ) {
  203.     // try {
  204.         $good_or_featured = 'good'; //TODO: logic instead of static value
  205.        
  206.         Promote::make_topic_page($objwiki, $page_title, $good_or_featured);
  207.         Promote::make_topic_talk_page($objwiki);
  208.         Promote::update_talk_pages_of_child_articles($objwiki);
  209.         Promote::update_count($objwiki);
  210.         Promote::update_topic_list($objwiki);
  211.         Promote::create_and_fill_child_categories($objwiki);
  212.         Promote::create_and_fill_parent_category($objwiki);
  213.         Promote::add_to_log($objwiki);
  214.         Promote::announce_featured_topic($objwiki);
  215.        
  216.         // create new page
  217.        
  218.         // edit original page, replace {{Promote topic}} with *: Promotion complete. ~~~~
  219.     // }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement