MaxSem has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/174889

Change subject: Random page API
......................................................................

Random page API

Change-Id: Ie9b91ad0908f635ec9ef4688e254c22b3eaf8598
---
M WikiGrok.php
M i18n/en.json
M i18n/qqq.json
A includes/api/QueryRandom.php
4 files changed, 97 insertions(+), 2 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikiGrok 
refs/changes/89/174889/1

diff --git a/WikiGrok.php b/WikiGrok.php
index 0dc1b87..a9e069c 100644
--- a/WikiGrok.php
+++ b/WikiGrok.php
@@ -17,6 +17,7 @@
 
 $autoloadClasses = array(
        'Api\ApiResponse' => 'api/ApiResponse',
+       'Api\QueryRandom' => 'api/QueryRandom',
        'Campaign' => 'campaigns/Campaign',
        'ConfirmationCampaign' => 'campaigns/ConfirmationCampaign',
        'EventLogger' => 'EventLogger',
@@ -34,6 +35,7 @@
 }
 
 $wgAPIModules['wikigrokresponse'] = 'WikiGrok\Api\ApiResponse';
+$wgAPIListModules['wikigrokrandom'] = $wgAPIGeneratorModules['wikigrokrandom'] 
= 'WikiGrok\Api\QueryRandom';
 
 $wgHooks['OutputPageParserOutput'][] = 
'WikiGrok\Hooks::onOutputPageParserOutput';
 $wgHooks['LinksUpdate'][] = 'WikiGrok\Hooks::onLinksUpdate';
diff --git a/i18n/en.json b/i18n/en.json
index 1ed12e7..2d9475a 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -2,5 +2,9 @@
        "@metadata": {
                "authors": ["Max Semenik"]
        },
-       "wikigrok-desc": "Creates an easy way to fill in missing Wikidata 
information"
+       "wikigrok-desc": "Creates an easy way to fill in missing Wikidata 
information",
+       "apihelp-query+wikigrokrandom-description": "Returns a random page 
belonging to a WikiGrok campaign",
+       "apihelp-query+wikigrokrandom-example-1": "Get a random page",
+       "apihelp-query+wikigrokrandom-example-2": "Get a random page belonging 
to a particular campaign",
+       "apihelp-query+wikigrokrandom-param-campaign": "Campaign to pick from. 
If not set, returns a page belonging to any campaign."
 }
\ No newline at end of file
diff --git a/i18n/qqq.json b/i18n/qqq.json
index 19a28a0..e17a6cb 100644
--- a/i18n/qqq.json
+++ b/i18n/qqq.json
@@ -4,5 +4,9 @@
                        "Max Semenik"
                ]
        },
-       "wikigrok-desc": "{{desc}}"
+       "wikigrok-desc": "{{desc}}",
+       "apihelp-query+wikigrokrandom-description": 
"{{doc-apihelp-description|query+wikigrokrandom}}",
+       "apihelp-query+wikigrokrandom-example-1": 
"{{doc-apihelp-example|query+wikigrokrandom}}",
+       "apihelp-query+wikigrokrandom-example-2": 
"{{doc-apihelp-example|query+wikigrokrandom}}",
+       "apihelp-query+wikigrokrandom-param-campaign": 
"{{doc-apihelp-param|query+wikigrokrandom|campaign}}"
 }
diff --git a/includes/api/QueryRandom.php b/includes/api/QueryRandom.php
new file mode 100644
index 0000000..6282082
--- /dev/null
+++ b/includes/api/QueryRandom.php
@@ -0,0 +1,85 @@
+<?php
+
+namespace WikiGrok\Api;
+
+use ApiBase;
+use ApiPageSet;
+use ApiQueryGeneratorBase;
+use ProfileSection;
+use WikiGrok\QuestionStore;
+
+class QueryRandom extends ApiQueryGeneratorBase {
+
+       public function __construct( $query, $moduleName ) {
+               parent::__construct( $query, $moduleName, 'wgr' );
+       }
+
+       public function execute() {
+               $this->run();
+       }
+
+       public function getCacheMode( $params ) {
+               return 'public';
+       }
+
+       public function executeGenerator( $resultPageSet ) {
+               $this->run( $resultPageSet );
+       }
+
+       /**
+        * @param ApiPageSet $resultPageSet
+        */
+       public function run( $resultPageSet = null ) {
+               $profileSection = new ProfileSection( __METHOD__ );
+
+               $params = $this->extractRequestParams();
+               $campaign = $params['campaign'];
+
+               $store = new QuestionStore();
+               $title = $store->getRandomPage( $campaign );
+
+               if ( !$title ) {
+                       return;
+               }
+
+               if ( !$resultPageSet ) {
+                       $vals = array(
+                               'pageid' => $title->getArticleID(),
+                               'ns' => $title->getNamespace(),
+                               'title' => $title->getPrefixedText(),
+                       );
+                       $this->getResult()->addValue( array( 'query', 
$this->getModuleName() ), null, $vals );
+               } else {
+                       $resultPageSet->populateFromTitles( array( $title ) );
+               }
+       }
+
+       public function getAllowedParams() {
+               return array(
+                       'campaign' => array(
+                               ApiBase::PARAM_TYPE => 'string',
+                       ),
+               );
+       }
+
+       protected function getExamplesMessages() {
+               return array(
+                       'action=query&list=wikigrokrandom'
+                               => 'apihelp-query+wikigrokrandom-example-1',
+                       'action=query&list=wikigrokrandom&campaign=awesomeness'
+                       => 'apihelp-query+wikigrokrandom-example-2',
+               );
+       }
+
+       public function getHelpUrls() {
+               return 'https://www.mediawiki.org/wiki/Extension:WikiGrok#API';
+       }
+
+       /**
+        * This module is considered unstable
+        * @return bool
+        */
+       public function isInternal() {
+               return true;
+       }
+}
\ No newline at end of file

-- 
To view, visit https://gerrit.wikimedia.org/r/174889
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie9b91ad0908f635ec9ef4688e254c22b3eaf8598
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikiGrok
Gerrit-Branch: master
Gerrit-Owner: MaxSem <maxsem.w...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to