Hiong3-eng5 has uploaded a new change for review. https://gerrit.wikimedia.org/r/89388
Change subject: added API owAddToCollection ...................................................................... added API owAddToCollection Change-Id: I1fb2d151a608820dd8d77a97078a3a30ab6f91d2 --- M OmegaWiki/WikiDataAPI.php M includes/api/OmegaWikiExt.php A includes/api/owAddToCollection.php 3 files changed, 301 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikiLexicalData refs/changes/88/89388/1 diff --git a/OmegaWiki/WikiDataAPI.php b/OmegaWiki/WikiDataAPI.php index af22f95..00a6295 100644 --- a/OmegaWiki/WikiDataAPI.php +++ b/OmegaWiki/WikiDataAPI.php @@ -1142,7 +1142,10 @@ ), __METHOD__ ); + if ( $collectionId ) { return $collectionId; + } + return null; } function addCollection( $definedMeaningId, $collectionType ) { diff --git a/includes/api/OmegaWikiExt.php b/includes/api/OmegaWikiExt.php index 3775452..ee43827 100644 --- a/includes/api/OmegaWikiExt.php +++ b/includes/api/OmegaWikiExt.php @@ -32,6 +32,7 @@ $wgAutoloadClasses['Express'] = dirname( __FILE__ ) . '/owExpress.php'; $wgAutoloadClasses['AddSyntrans'] = dirname( __FILE__ ) . '/owAddSyntrans.php'; $wgAutoloadClasses['AddAnnotation'] = dirname( __FILE__ ) . '/owAddAnnotation.php'; + $wgAutoloadClasses['AddToCollection'] = dirname( __FILE__ ) . '/owAddToCollection.php'; // Map module name to class name $wgAPIModules['ow_syntrans'] = 'SynonymTranslation'; @@ -39,6 +40,7 @@ $wgAPIModules['ow_express'] = 'Express'; $wgAPIModules['ow_add_syntrans'] = 'AddSyntrans'; $wgAPIModules['ow_add_annotation'] = 'AddAnnotation'; + $wgAPIModules['ow_add_to_collection'] = 'AddToCollection'; // Load the internationalization file $wgExtensionMessagesFiles['myextension'] diff --git a/includes/api/owAddToCollection.php b/includes/api/owAddToCollection.php new file mode 100644 index 0000000..d0941f7 --- /dev/null +++ b/includes/api/owAddToCollection.php @@ -0,0 +1,296 @@ +<?php + +/** OmegaWiki API's add to collection class + * Created on October 11, 2013 + */ + +require_once( 'extensions/WikiLexicalData/OmegaWiki/WikiDataAPI.php' ); +require_once( 'extensions/WikiLexicalData/OmegaWiki/Transaction.php' ); + +class AddToCollection extends ApiBase { + + private $objectId, $attributeId, $optionId; + + public function __construct( $main, $action ) { + parent :: __construct( $main, $action, null); + } + + public function execute() { + + $result = array(); + global $params; + + // limit access to bots + if ( !$this->getUser()->isAllowed( 'bot' ) ) { + $this->dieUsage( 'you must have a bot flag to use this API function', 'bot_only' ); + } + + // keep blocked bots out + if ( $this->getUser()->isBlocked() ) { + $this->dieUsage( 'your account is blocked.', 'blocked' ); + } + + // Get the parameters + $params = $this->extractRequestParams(); + + // set test status + $this->test = false; + + if ( isset( $params['test'] ) ) { + if ( $params['test'] == '1' OR $params['test'] == null ) { + $this->test = true; + } + } + + // If wikipage, use batch processing + if ( $params['wikipage'] ) { + $text = $this->processBatch( $params['wikipage'] ); + return true; + } + + // if not, add just one syntrans + + // Parameter checks + + // * optional parameter + if ( !isset( $params['int_memb_id'] ) ) { + $this->internalMemberId = ''; + } else { + $this->internalMemberId = $params['int_memb_id']; + } + + // * required parameters + if ( !isset( $params['dm'] ) ) { + $this->dieUsage( 'parameter dm to add is missing', 'param dm is missing' ); + } + if ( !isset( $params['coll_mid'] ) ) { + $this->dieUsage( 'parameter col_mid to add to is missing', 'param col_mid missing' ); + } + $this->definedMeaningId = $params['dm']; + $this->collectionMid = $params['coll_mid']; + + if ( !is_numeric( $this->collectionMid ) || !is_numeric( $this->definedMeaningId ) ) { + if($ctr == 1) { + $result = array ( 'note' => "either $this->collectionMid or $this->definedMeaningId is not an int or probably just the CSV header"); + } else { + $result = array ( 'note' => "either $this->collectionMid or $this->definedMeaningId is not an int"); + } + } else { + $result = $this->processAddToCollection(); + } + + if ( !isset ( $result['error'] ) ) { + } + + $this->getResult()->addValue( null, $this->getModuleName(), $result ); + return true; + } + + // Version + public function getVersion() { + return __CLASS__ . ': $Id$'; + } + + // Description + public function getDescription() { + return 'Add annotations to Omegawiki.' ; + } + + // Parameters. + public function getAllowedParams() { + return array( + 'dm' => array ( + ApiBase::PARAM_TYPE => 'integer' + ), + 'coll_mid' => array ( + ApiBase::PARAM_TYPE => 'integer' + ), + 'int_memb_id' => array ( + ApiBase::PARAM_TYPE => 'integer' + ), + 'wikipage' => array ( + ApiBase::PARAM_TYPE => 'string' + ), + 'test' => array ( + ApiBase::PARAM_TYPE => 'string' + ), + ); + } + + // Describe the parameter + public function getParamDescription() { + return array( + 'dm' => 'Defined meaning id to add to the Collection', + 'coll_mid' => 'Defined meaning Id of the Collection' , + 'int_memb_id' => 'Internal member id, A further string that can be used to identify the member inside the collection', + 'wikipage' => 'The wikipage to process. (csv format, using wiki page)', + 'test' => 'test mode. No changes are made.' + ); + } + + // Get examples + public function getExamples() { + return array( + 'Add a defined meaning id to a collection', + 'api.php?action=ow_add_to_collection&dm=194&coll_mid=725301&format=xml', + 'or to test it', + 'api.php?action=ow_add_to_collection&dm=837820&coll_mid=725301&format=xml&test', + 'You can also add defined meaning id using a CSV file format', + 'saved in a wikiPage. The file must contain at least 3 columns:', + ' dm_id (int)', + ' coll_mid (int)', + ' int_memb_id (int)', + 'api.php?action=ow_add_to_collection&wikipage=User:Hiong3-eng5/addToCollection.csv&format=xml', + 'or to test it', + 'api.php?action=ow_add_to_collection&wikipage=User:Hiong3-eng5/addToCollection.csv&format=xml&test' + ); + } + + public function processBatch( $wikiPage ) { + global $params; + + $csvWikiPageTitle = Title::newFromText( $wikiPage ); + $csvWikiPage = new WikiPage ( $csvWikiPageTitle ); + + if ( !$wikiText = $csvWikiPage->getContent( Revision::RAW ) ) + return $this->getResult()->addValue( null, $this->getModuleName(), + array ( 'result' => array ( + 'error' => "WikiPage ( $csvWikiPageTitle ) does not exist" + ) ) + ); + + $text = $wikiText->mText; + + // Check if the page is redirected, + // then adjust accordingly. + preg_match( "/REDIRECT \[\[(.+)\]\]/", $text, $match2 ); + if ( isset($match2[1]) ) { + $redirectedText = $match2[1]; + $csvWikiPageTitle = Title::newFromText( $redirectedText ); + $csvWikiPage = new WikiPage ( $csvWikiPageTitle ); + $wikiText = $csvWikiPage->getContent( Revision::RAW ); + $text = $wikiText->mText; + } + + $this->getResult()->addValue( null, $this->getModuleName(), + array ( 'process' => array ( + 'text' => 'wikipage', + 'type' => 'batch processing' + ) ) + ); + + $inputLine = explode("\n", $text); + $ctr = 0; + while ( $inputData = array_shift( $inputLine ) ) { + $ctr = $ctr + 1; + $inputData = trim( $inputData ); + if ( $inputData == "" ) { + $result = array ( 'note' => "skipped blank line"); + $this->getResult()->addValue( null, $this->getModuleName(), + array ( 'result' . $ctr => $result ) + ); + continue; + } + + $inputDataArray = explode(',',$inputData); + $inputDataCount = count( $inputDataArray ); + if ( $inputDataCount == 1 ) { + $result = array ( 'note' => "skipped blank line"); + $this->getResult()->addValue( null, $this->getModuleName(), + array ( 'result' . $ctr => $result ) + ); + continue; + } + + if ( $inputDataCount <> 3 ) { + $result = array ( 'note' => "The line `$inputData` can not be processed because it has $inputDataCount columns instead of 3."); + $this->getResult()->addValue( null, $this->getModuleName(), + array ( 'result' . $ctr => $result ) + ); + continue; + } + $this->definedMeaningId = $inputDataArray[0]; + $this->collectionMid = $inputDataArray[1]; + $this->internalMemberId = $inputDataArray[2]; + + if ( !is_numeric( $this->collectionMid ) || !is_numeric( $this->definedMeaningId ) ) { + if($ctr == 1) { + $result = array ( 'note' => "either $this->collectionMid or $this->definedMeaningId is not an int or probably just the CSV header"); + } else { + $result = array ( 'note' => "either $this->collectionMid or $this->definedMeaningId is not an int"); + } + } else { + $result = $this->processAddToCollection(); + } + $this->getResult()->addValue( null, $this->getModuleName(), + array ( 'result' . $ctr => $result ) + ); + + } + return true; + } + + private function processAddToCollection() { + $dc = wdGetDataSetContext(); + + // Get CollectionId + $this->collectionId = getCollectionId( $this->collectionMid ); + if ( !$this->collectionId ) { + return array( 'error' => array ( + 'code' => 'param coll_mid does not exist', + 'info' => "No coll_mid found" + ) + ); + } + + // check that defined_meaning_id exists + if ( !verifyDefinedMeaningId( $this->definedMeaningId ) ) { + return array( 'error' => array ( + 'code' => 'param dm does not exist', + 'info' => "No dm found" + ) + ); + } + + $collection = getDefinedMeaningSpellingForLanguage( $this->collectionMid, WLD_ENGLISH_LANG_ID ); + $expression = getDefinedMeaningSpellingForLanguage( $this->definedMeaningId, WLD_ENGLISH_LANG_ID ); + if ( !$expression ) { + $expression = getDefinedMeaningSpellingForAnyLanguage( $this->definedMeaningId, WLD_ENGLISH_LANG_ID ); + if ( !$expression ) { + return array( 'error' => array ( + 'code' => 'param dm does not have any expression associated with it', + 'info' => "No dm with expression found" + ) + ); + } + } + // Check if defined meaning id already exist in the collection + // If so add else warn + if ( !definedMeaningInCollection( $this->definedMeaningId, $this->collectionId ) ) { + $note = array( 'result' => array( + 'status' => "added `$expression` (dm:$this->definedMeaningId) to `$collection`." + ) ); + + if ( !$this->test ) { + startNewTransaction( $this->getUser()->getID(), "0.0.0.0", "Added using API function add_to_collection", $dc ); + addDefinedMeaningToCollection( $this->definedMeaningId, $this->collectionId, $this->internalMemberId ); + } else { + $note['result']['note'] = 'test run only'; + } + + return $note; + } else { + $note = array( 'result' => array( + 'status' => "`$expression` exists in `$collection`." + ) + ); + if ( $this->test ) { + $note['result']['note'] = 'test run only'; + } + + return $note; + } + + } + +} -- To view, visit https://gerrit.wikimedia.org/r/89388 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I1fb2d151a608820dd8d77a97078a3a30ab6f91d2 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/WikiLexicalData Gerrit-Branch: master Gerrit-Owner: Hiong3-eng5 <hiong3.e...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits