diff --git a/ApiAskFlexForm.php b/ApiAskFlexForm.php new file mode 100644 index 00000000..f31c13b1 --- /dev/null +++ b/ApiAskFlexForm.php @@ -0,0 +1,312 @@ +checkUserRightsAny( [ 'read' ] ); + $params = $this->extractRequestParams(); + $ret = []; + $ret['results'] = []; + $queryEncoded = $params['query']; + $this->query = base64_decode( str_replace( ' ', '+', $queryEncoded ) ); + $this->q = $params['q']; + $this->returnId = $params['returnid']; + $this->returnText = $params['returntext']; + $this->template = $params['template']; + $this->limit = $params['limit']; + $this->ffform = $params['ffform']; + + $this->setupQuery(); + $this->handleQType(); + $postdata = [ + "action" => "ask", + "format" => "json", + "query" => $this->query + ]; + $mRequest = new Render(); + $results = $this->handleResults( $mRequest->makeRequest( $postdata ), $ret ); + $this->getResult()->addValue( + null, + 'results', + $results['results'] + ); + return true; + } + + /** + * @return void + */ + private function setupQuery(): void { + $filterQuery = false; + if ( str_contains( $this->query, '(' ) && str_contains( $this->query, ')' ) ) { + if ( str_contains( $this->query, '(fquery=' ) ) { + $fQuery = Core::get_string_between( $this->query, '(fquery=', ')' ); + $fQueryOld = $fQuery; + if ( strpos( $fQuery, '__^^__' ) !== false ) { + if ( empty( $this->ffform ) ) { + $fQuery = ''; + } else { + $fQuery = str_replace( '__^^__', base64_decode( $this->ffform ), $fQuery ); + $filterQuery = true; + } + } + $this->query = str_replace( + '(fquery=' . $fQueryOld . ')', + '', + $this->query + ); + } + if ( str_contains( $this->query, '(returntext=' ) ) { + $returnText = Core::get_string_between( $this->query, '(returntext=', ')' ); + $this->query = str_replace( + '(returntext=' . $returnText . ')', + '', + $this->query + ); + } + if ( str_contains( $this->query, '(template=' ) ) { + $template = Core::get_string_between( $this->query, '(template=', ')' ); + $this->query = str_replace( + '(template=' . $template . ')', + '', + $this->query + ); + } + if ( str_contains( $this->query, '(returnid=' ) ) { + $returnId = Core::get_string_between( $this->query, '(returnid=', ')' ); + $this->query = str_replace( + '(returnid=' . $returnId . ')', + '', + $this->query + ); + } + if ( str_contains( $this->query, '(limit=' ) ) { + $limit = Core::get_string_between( $this->query, '(limit=', ')' ); + $this->query = str_replace( + '(limit=' . $limit . ')', + '', + $this->query + ); + } + } + if ( $filterQuery ) { + $this->query .= $fQuery; + } + } + + /** + * @return void + */ + private function handleQType(): void { + if ( $this->q !== null || !empty( $this->q ) ) { + // Are there spaces in the query? + if ( str_contains( $this->q, ' ' ) ) { + $mainQuery = $this->getMainQuery( $this->query ); + $explodedQuery = explode( ' ', $this->q ); + $newQuery = ''; + + foreach ( $explodedQuery as $seperated ) { + if ( !empty( $seperated ) ) { + $newQuery .= '[[' . $mainQuery . '::' . $this->createNewQuery( $seperated ) . ']]'; + } + } + $this->query = str_replace( + '[[' . $mainQuery . '::!!!]]', + $newQuery, + $this->query + ); + } else { + $this->query = str_replace( + '!!!', + $this->createNewQuery( $this->q ), + $this->query + ); + } + } else { + $this->query = str_replace( + '!!!', + '', + $this->query + ); + } + if ( $this->returnId !== null ) { + $this->query .= '|?' . $this->returnId; + } + if ( $this->returnText !== null ) { + $this->query .= '|?' . $this->returnText; + } + if ( $this->limit !== null ) { + $this->query .= '|limit=' . $this->limit; + } else { + $this->query .= '|limit=50'; + } + if ( $this->template !== null ) { + $this->query .= '|template=' . $this->template; + } + } + + /** + * @param string $query + * + * @return string + */ + private function getMainQuery( string $query ): string { + $matches = []; + $mainQuery = ''; + preg_match_all( '/\[\[(.*?)\]\]/', $query, $matches ); + foreach ( $matches[1] as $key => $match ) { + // Looking for the actual query + if ( strpos( $match, '!!!' ) ) { + $matchExploded = explode( '::', $match ); + $mainQuery = $matchExploded[0]; + break; + } + } + return $mainQuery; + } + + /** + * Take a search string or part and add Uppercase first letter and all uppercase to them + * @param string $searchPart + * + * @return string + */ + private function createNewQuery( string $searchPart ): string { + $q2 = ucwords( $searchPart ); + $q3 = strtoupper( $searchPart ); + return '~*' . $searchPart . '*||~*' . $q2 . '*||~*' . $q3 . '*'; + } + + /** + * @param array $data + * @param array $ret + * + * @return array + */ + private function handleResults( array $data, array $ret ): array { + if ( !empty( $data['query']['results'] ) ) { + $data = $data['query']['results']; + + $t = 0; + foreach ( $data as $k => $val ) { + if ( $this->returnText === null ) { + $ret['results'][$t]['text'] = $val['displaytitle']; + } elseif ( isset( $val['printouts'][$this->returnText][0] ) ) { + $ret['results'][$t]['text'] = $val['printouts'][$this->returnText][0]; + } else { + $ret['results'][$t]['text'] = 'Not found'; + } + + if ( $this->returnId === null ) { + $ret['results'][$t]['id'] = $k; + } elseif ( isset( $val['printouts'][$this->returnId][0] ) ) { + $ret['results'][$t]['id'] = $val['printouts'][$this->returnId][0]; + } else { + $ret['results'][$t]['id'] = 'Not found'; + } + $t++; + } + } + return $ret; + } + + public function needsToken() { + return false; + } + + public function isWriteMode() { + return false; + } + + /** + * @return array + */ + public function getAllowedParams(): array { + return [ + 'query' => [ + ParamValidator::PARAM_TYPE => 'string', + ParamValidator::PARAM_REQUIRED => true + ], + 'q' => [ + ParamValidator::PARAM_TYPE => 'string', + ], + 'returnid' => [ + ParamValidator::PARAM_TYPE => 'string', + ], + 'returntext' => [ + ParamValidator::PARAM_TYPE => 'string', + ], + 'template' => [ + ParamValidator::PARAM_TYPE => 'string', + ], + 'ffform' => [ + ParamValidator::PARAM_TYPE => 'string', + ], + 'limit' => [ + ParamValidator::PARAM_TYPE => 'string', + ] + ]; + } + + /** + * @return array + */ + protected function getExamplesMessages() : array { + return [ + 'action=FlexFormAsk&query=base64query&q=search' => 'apihelp-flexform-bot-example-1' + ]; + } + +} \ No newline at end of file diff --git a/ApiFlexForm.php b/ApiFlexForm.php index 279c8824..3a517a46 100644 --- a/ApiFlexForm.php +++ b/ApiFlexForm.php @@ -2,8 +2,10 @@ use FlexForm\Core\Config; use FlexForm\Core\Debug; +use FlexForm\Core\HandleResponse; use FlexForm\Core\Protect; use FlexForm\FlexFormException; +use FlexForm\Processors\Request\Handlers\SemanticAsk; use Wikimedia\ParamValidator\ParamValidator; class ApiFlexForm extends ApiBase { @@ -98,6 +100,17 @@ public function execute() { $result = $messaging->removeUserMessageById( $mId, true ); $output = $this->createResult( "ok", "ok" ); break; + case "ask": + $smwAsk = new SemanticAsk(); + $output = $smwAsk->execute( new HandleResponse() ); + $this->getResult()->addValue( + null, + 'results', + $output['results'] + ); + + return true; + break; case "decrypt": $output = $this->decrypt( $params['titleStartsWith'] ); if ( $output['status'] === "error" ) { diff --git a/FlexForm.hooks.php b/FlexForm.hooks.php index 88a2da74..d21f7a1f 100644 --- a/FlexForm.hooks.php +++ b/FlexForm.hooks.php @@ -257,15 +257,18 @@ public static function onParserFirstCallInit( Parser &$parser ) { if ( !\FlexForm\Core\Config::getConfigStatus() ) { \FlexForm\Core\Config::setConfigFromMW(); } - global $wgFlexFormConfig; - $wgFlexFormConfig['loaders'] = []; - $wgFlexFormConfig['loaders']['css'] = []; - $wgFlexFormConfig['loaders']['javascript'] = []; - $wgFlexFormConfig['loaders']['jsconfigvars'] = []; - $wgFlexFormConfig['loaders']['javascripttag'] = []; - $wgFlexFormConfig['loaders']['csstag'] = []; - $wgFlexFormConfig['loaders']['files'] = []; - + // Only set these config variables if no other form has been rendered. + // Extensions like WikiGuard creates a new parser and thus executes this possibly multiple times. + if ( Core::getRun() === false ) { + global $wgFlexFormConfig; + $wgFlexFormConfig['loaders'] = []; + $wgFlexFormConfig['loaders']['css'] = []; + $wgFlexFormConfig['loaders']['javascript'] = []; + $wgFlexFormConfig['loaders']['jsconfigvars'] = []; + $wgFlexFormConfig['loaders']['javascripttag'] = []; + $wgFlexFormConfig['loaders']['csstag'] = []; + $wgFlexFormConfig['loaders']['files'] = []; + } $formTags = [ 'wsform', '_form', diff --git a/Modules/Handlers/semantic_ask.php b/Modules/Handlers/semantic_ask.php deleted file mode 100644 index 2c6d3e5c..00000000 --- a/Modules/Handlers/semantic_ask.php +++ /dev/null @@ -1,70 +0,0 @@ -= 3) { - $query = html_entity_decode(urldecode($query)); - //var_dump($query); - if($q !== false) { - $q2 = ucwords( $q ); - $q3 = strtoupper( $q ); - $query = str_replace('!!!', '~*' . $q . '*||~*' . $q2 . '*||~*' . $q3 . '*', $query); - } else { - $query = str_replace('!!!', '', $query); - } - if( $returnId !== false ) { - $query .= '|?'.$returnId; - } - if( $returnText !== false ) { - $query .= '|?'.$returnText; - } - if( $limit !== false ) { - $query .= '|limit='.$limit; - } else $query .= '|limit=50'; - - //echo $query."
"; - - - //[[Class::Organization]][[Name::~*ik*]]|?Name|?Contact|limit=99999 - //Process~*hallo*|?Name|?Name|limit=50 - //echo $query."
";
-
-    $postdata = http_build_query([
-        "action" => "ask",
-        "format" => "json",
-        "query" => $query
-    ]);
-    $data = $api->apiPost($postdata, true);
-
-
-    if (isset($data['received']['query']['results']) && !empty( $data['received']['query']['results'] )) {
-        $data = $data['received']['query']['results'];
-
-        $t=0;
-        foreach($data as $k => $val) {
-            if( $returnText === false ) {
-                $ret['results'][$t]['text'] = htmlentities($val['displaytitle'] );
-            } elseif( isset( $val['printouts'][$returnText][0] ) ) {
-                $ret['results'][$t]['text'] = htmlentities( $val['printouts'][$returnText][0] );
-            } else $ret['results'][$t]['text'] = 'Not found';
-
-            if( $returnId === false ) {
-                $ret['results'][$t]['id'] = htmlentities( $k );
-            } elseif( isset( $val['printouts'][$returnId][0] ) ) {
-                $ret['results'][$t]['id'] = htmlentities( $val['printouts'][$returnId][0] );
-            } else $ret['results'][$t]['id'] = 'Not found';
-            $t++;
-        }
-    }
-
-}
-
diff --git a/README.md b/README.md
index 3a1c5332..491aa3b6 100644
--- a/README.md
+++ b/README.md
@@ -86,6 +86,7 @@ Visit this documentation page https://www.open-csp.org/DevOps:Doc/FlexForm/2.0/V
 Visit : https://www.open-csp.org/DevOps:Doc/FlexForm
 
 ### Changelog
+* 2.6.7 : Semantic Ask in tokens quick fix. Will be rewritten as a full API call in 2.7.x
 * 2.6.6 : Tempex now supports select, checkboxes and radiobuttons.
 * 2.6.5 : Update HTMLPurifier and move to composer. Added default user for internal api call.
 * 2.6.4 : fix: A check at MW if a page title is usable was set to early.
diff --git a/extension.json b/extension.json
index 3f1cf8f6..6c3559b2 100644
--- a/extension.json
+++ b/extension.json
@@ -1,6 +1,6 @@
 {
   "name": "FlexForm",
-  "version": "2.6.6",
+  "version": "2.6.7",
   "author": [
     "[https://www.wikibase-solutions.com/author/charlot Sen-Sai]",
     "[https://www.wikibase-solutions.com/author/marijn Marijn]"
@@ -12,6 +12,7 @@
   "AutoloadClasses": {
     "ApiFlexForm": "ApiFlexForm.php",
     "ApiBotFlexForm": "ApiBotFlexForm.php",
+    "ApiAskFlexForm": "ApiAskFlexForm.php",
     "FlexFormHooks": "FlexForm.hooks.php"
   },
   "AutoloadNamespaces": {
@@ -22,7 +23,8 @@
   },
   "APIModules": {
     "flexform": "ApiFlexForm",
-    "FlexFormBot": "ApiBotFlexForm"
+    "FlexFormBot": "ApiBotFlexForm",
+    "FlexFormAsk": "ApiAskFlexForm"
   },
   "Hooks": {
     "LoadExtensionSchemaUpdates": "FlexForm\\Core\\Sql::addTables",
diff --git a/src/Processors/Convert/PandocConverter.php b/src/Processors/Convert/PandocConverter.php
index 83259b2f..6aa521ee 100644
--- a/src/Processors/Convert/PandocConverter.php
+++ b/src/Processors/Convert/PandocConverter.php
@@ -14,8 +14,8 @@
 use FlexForm\Core\Debug;
 use FlexForm\FlexFormException;
 use FlexForm\Processors\Files\Convert;
-use import\classes\Pandoc\Pandoc;
-use import\classes\Pandoc\PandocException;
+use Pandoc\Pandoc;
+use Pandoc\PandocException;
 
 class PandocConverter extends Convert {
 	/**
@@ -36,7 +36,7 @@ private function getPandocMediaPath(): string {
 	}
 
 	/**
-	 * @return import\classes\Pandoc\Pandoc
+	 * @return Pandoc
 	 * @throws FlexFormException
 	 */
 	private function giveMePandoc(): Pandoc {
@@ -91,7 +91,7 @@ public function convertFile(): string {
 		];
 		try {
 			$wiki = $pandoc->runWith( $this->getFile(), $options );
-		} catch ( import\classes\Pandoc\PandocException $e ) {
+		} catch ( PandocException $e ) {
 			$params = [
 				'file'  => $e->getFile(),
 				'line'  => $e->getLine(),
diff --git a/src/Processors/Request/Handlers/SemanticAsk.php b/src/Processors/Request/Handlers/SemanticAsk.php
deleted file mode 100644
index cf9172a3..00000000
--- a/src/Processors/Request/Handlers/SemanticAsk.php
+++ /dev/null
@@ -1,221 +0,0 @@
- $match ) {
-			// Looking for the actual query
-			if ( strpos( $match, '!!!' ) ) {
-				$matchExploded = explode( '::', $match );
-				$mainQuery = $matchExploded[0];
-				break;
-			}
-		}
-		return $mainQuery;
-	}
-
-	/**
-	 * Take a search string or part and add Uppercase first letter and all uppercase to them
-	 * @param string $searchPart
-	 *
-	 * @return string
-	 */
-	private function createNewQuery( string $searchPart ): string {
-		$q2    = ucwords( $searchPart );
-		$q3    = strtoupper( $searchPart );
-		return '~*' . $searchPart . '*||~*' . $q2 . '*||~*' . $q3 . '*';
-	}
-
-	/**
-	 * @param HandleResponse $responseHandler
-	 *
-	 * @return void
-	 * @throws \MWException
-	 */
-	public function execute( HandleResponse $responseHandler ) {
-		$ret            = [];
-		$ret['results'] = [];
-		$queryEncoded = General::getGetString( 'query', true, false );
-		if ( $queryEncoded !== false ) {
-			// $_GET will urldecode automatically. Make sure any spaces return to a +
-			$query = base64_decode( str_replace( ' ', '+', $queryEncoded ) );
-		} else {
-			$query = false;
-		}
-		//$query          = base64_decode( General::getGetString( 'query', true, false ) );
-		$q              = General::getGetString( 'q', true, false );
-		$returnId       = General::getGetString( 'returnid', true, false );
-		$returnText     = General::getGetString( 'returntext', true, false );
-		$template       = General::getGetString( 'template', true, false );
-		$limit          = General::getGetString( 'limit', true, false );
-
-		// if( strlen( $q ) < 3 ) return $ret;
-		if ( $query !== false ) {
-			// $ret = createMsg('No query found.');
-			// test query :  $query = "[[Class::Organization]] [[Name::~*ik*]]|?Name |format=json |limit=99999"
-			// ik kan dat q worden voor select2 door !!! in te vullen in de query, deze wordt dan vervangen.
-			$filterQuery = false;
-			if ( strpos( $query, '(' ) !== false && strpos( $query, ')' ) !== false ) {
-				if ( strpos( $query, '(fquery=' ) !== false ) {
-					$fQuery = Core::get_string_between( $query, '(fquery=', ')' );
-					$fQueryOld = $fQuery;
-					if ( strpos( $fQuery, '__^^__' ) !== false ) {
-						$fform = General::getGetString( 'ffform', true, false );
-						if ( $fform === false ) {
-							$fQuery = '';
-						} else {
-							$fQuery = str_replace( '__^^__', base64_decode( $fform ), $fQuery );
-							$filterQuery = true;
-						}
-					}
-					$query = str_replace(
-						'(fquery=' . $fQueryOld . ')',
-						'',
-						$query
-					);
-				}
-				if ( strpos( $query, '(returntext=' ) !== false ) {
-					$returnText = Core::get_string_between( $query, '(returntext=', ')' );
-					$query = str_replace(
-						'(returntext=' . $returnText . ')',
-						'',
-						$query
-					);
-				}
-				if ( strpos( $query, '(template=' ) !== false ) {
-					$template = Core::get_string_between( $query, '(template=', ')' );
-					$query = str_replace(
-						'(template=' . $template . ')',
-						'',
-						$query
-					);
-				}
-				if ( strpos( $query, '(returnid=' ) !== false ) {
-					$returnId = Core::get_string_between( $query, '(returnid=', ')' );
-					$query = str_replace(
-						'(returnid=' . $returnId . ')',
-						'',
-						$query
-					);
-				}
-				if ( strpos( $query, '(limit=' ) !== false ) {
-					$limit = Core::get_string_between( $query, '(limit=', ')' );
-					$query = str_replace(
-						'(limit=' . $limit . ')',
-						'',
-						$query
-					);
-				}
-			}
-			if ( $filterQuery ) {
-				$query .= $fQuery;
-			}
-
-			if ( $q !== false ) {
-				// Are there spaces in the query?
-				if ( strpos( $q, ' ' ) !== false ) {
-					$mainQuery = $this->getMainQuery( $query );
-					$explodedQuery = explode( ' ', $q );
-					$newQuery = '';
-//
-					foreach ( $explodedQuery as $seperated ) {
-						if ( !empty( $seperated ) ) {
-							$newQuery .= '[[' . $mainQuery . '::' . $this->createNewQuery( $seperated ) . ']]';
-						}
-					}
-					$query = str_replace(
-						'[[' . $mainQuery . '::!!!]]',
-						$newQuery,
-						$query
-					);
-				} else {
-					$query = str_replace(
-						'!!!',
-						$this->createNewQuery( $q ),
-						$query
-					);
-				}
-			} else {
-				$query = str_replace(
-					'!!!',
-					'',
-					$query
-				);
-			}
-			if ( $returnId !== false ) {
-				$query .= '|?' . $returnId;
-			}
-			if ( $returnText !== false ) {
-				$query .= '|?' . $returnText;
-			}
-			if ( $limit !== false ) {
-				$query .= '|limit=' . $limit;
-			} else {
-				$query .= '|limit=50';
-			}
-			if ( $template !== false ) {
-				$query .= '|template=' . $template;
-			}
-
-			// echo $query."
"; - - //[[Class::Organization]][[Name::~*ik*]]|?Name|?Contact|limit=99999 - //Process~*hallo*|?Name|?Name|limit=50 - //echo $query; - - $postdata = [ - "action" => "ask", - "format" => "json", - "query" => $query - ]; - $mRequest = new \FlexForm\Processors\Content\Render(); - $data = $mRequest->makeRequest( $postdata ); - - if ( isset( $data['query']['results'] ) && !empty( $data['query']['results'] ) ) { - $data = $data['query']['results']; - - $t = 0; - foreach ( $data as $k => $val ) { - if ( $returnText === false ) { - $ret['results'][$t]['text'] = $val['displaytitle']; - } elseif ( isset( $val['printouts'][$returnText][0] ) ) { - $ret['results'][$t]['text'] = $val['printouts'][$returnText][0]; - } else { - $ret['results'][$t]['text'] = 'Not found'; - } - - if ( $returnId === false ) { - $ret['results'][$t]['id'] = $k; - } elseif ( isset( $val['printouts'][$returnId][0] ) ) { - $ret['results'][$t]['id'] = $val['printouts'][$returnId][0]; - } else { - $ret['results'][$t]['id'] = 'Not found'; - } - $t++; - } - } - } - header( 'Content-Type: application/json' ); - echo json_encode( - $ret, - JSON_PRETTY_PRINT - ); - die(); - } -} - - diff --git a/src/Render/Themes/Plain/PlainTokenRenderer.php b/src/Render/Themes/Plain/PlainTokenRenderer.php index b9fd4d58..737301a4 100644 --- a/src/Render/Themes/Plain/PlainTokenRenderer.php +++ b/src/Render/Themes/Plain/PlainTokenRenderer.php @@ -197,9 +197,9 @@ private function renderSelectJavascript( global $wgScriptPath, $wgServer; if ( $wgScriptPath !== "" ) { - $smwQueryUrl = "/" . ltrim( $wgScriptPath, '/' ) . '/index.php/Special:FlexForm'; + $smwQueryUrl = "/" . ltrim( $wgScriptPath, '/' ) . '/api.php'; } else { - $smwQueryUrl = '/index.php/Special:FlexForm'; + $smwQueryUrl = '/api.php'; } if ( $smwQuery !== null ) { $filterQuery = $this->checkFilterQuery( $smwQuery ); @@ -209,8 +209,8 @@ private function renderSelectJavascript( } else { $ffFormField = ''; } - $smwQueryUrl .= '?action=handleExternalRequest'; - $smwQueryUrl .= '&script=SemanticAsk&query='; + $smwQueryUrl .= '?action=FlexFormAsk&format=json'; + $smwQueryUrl .= '&query='; $smwQueryUrlQ = base64_encode( $smwQuery ); } else { $smwQueryUrl = null;