Fo0bar has uploaded a new change for review.

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

Change subject: Convert to CSE v2 API, make HTML customizable by 
GoogleSiteSearchHTML hook
......................................................................

Convert to CSE v2 API, make HTML customizable by GoogleSiteSearchHTML hook

Change-Id: I637a1448fefe18297aa2bb3d8f68c38d28240fde
---
D GoogleSiteSearch.content.default.html
M GoogleSiteSearch.hooks.php
M GoogleSiteSearch.php
M extension.json
4 files changed, 31 insertions(+), 30 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/GoogleSiteSearch 
refs/changes/34/305934/1

diff --git a/GoogleSiteSearch.content.default.html 
b/GoogleSiteSearch.content.default.html
deleted file mode 100644
index 7cb654e..0000000
--- a/GoogleSiteSearch.content.default.html
+++ /dev/null
@@ -1,12 +0,0 @@
-<div id="cse" style="width: 100%;">_GSS_LOADING_</div>
-<script src="//www.google.com/jsapi" type="text/javascript"></script>
-<script type="text/javascript">
-  google.load('search', '1', {language : _GSS_LANG_});
-  google.setOnLoadCallback(function() {
-    var customSearchControl = new 
google.search.CustomSearchControl(_GSS_CSE_ID_);
-    
customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
-    customSearchControl.draw('cse');
-    customSearchControl.execute(_GSS_TERM_ESCAPE_);
-  }, true);
-</script>
-<link rel="stylesheet" href="//www.google.com/cse/style/look/default.css" 
type="text/css" />
diff --git a/GoogleSiteSearch.hooks.php b/GoogleSiteSearch.hooks.php
index 109f308..536dad4 100644
--- a/GoogleSiteSearch.hooks.php
+++ b/GoogleSiteSearch.hooks.php
@@ -4,7 +4,7 @@
        public static function searchPrepend( $specialSearch, $output, $term ) {
                global $wgGoogleSiteSearchCSEID;
                global $wgGoogleSiteSearchOnly;
-               global $wgGoogleSiteSearchCharset;
+               global $wgGoogleSiteSearchAttributes;
 
                # Return immediately if the CSE ID is not configured
                if ( !$wgGoogleSiteSearchCSEID ) {
@@ -16,25 +16,38 @@
                        return true;
                }
 
-               $dir = dirname( __FILE__ ) . '/';
-               $lang = $specialSearch->getLanguage();
+               # Default attributes, may be overridden by 
$wgGoogleSiteSearchAttributes.
+               $gcseAttributesDefault = array(
+                       'gname' => 'mw-googlesitesearch',
+                       'linkTarget' => '',
+               );
 
-               # Allow for local overrides of the base HTML
-               if ( file_exists( $dir . 'GoogleSiteSearch.content.html' ) ) {
-                       $outhtml = file_get_contents ( $dir . 
'GoogleSiteSearch.content.html' );
-               } else {
-                       $outhtml = file_get_contents ( $dir . 
'GoogleSiteSearch.content.default.html' );
+               # Attributes which may not be overridden.
+               $gcseAttributesImmutable = array(
+                       'autoSearchOnLoad' => 'false',
+               );
+
+               $gcseAttributes = array_merge( $gcseAttributesDefault, 
$wgGoogleSiteSearchAttributes, $gcseAttributesImmutable );
+
+               # Generate HTML5-compatible <div> attributes.
+               $gcseAttributesDiv = array();
+               foreach ( $gcseAttributes as $key => $value ) {
+                       $gcseAttributesDiv['data-' . $key] = $value;
                }
+               $gcseAttributesDiv['id'] = $gcseAttributes['gname'];
+               $gcseAttributesDiv['class'] = 'gcse-searchresults-only';
 
-               # Replace variable data in the HTML
-               $outhtml = str_replace( '_GSS_CSE_ID_', FormatJson::encode( 
$wgGoogleSiteSearchCSEID ), $outhtml );
-               $outhtml = str_replace( '_GSS_TERM_ESCAPE_', 
FormatJson::encode( $term ), $outhtml );
-               $outhtml = str_replace( '_GSS_LANG_', FormatJson::encode( 
$lang->getCode() ), $outhtml );
-               $outhtml = str_replace( '_GSS_LOADING_', htmlentities( 
wfMessage( 'googlesitesearch-loading', $wgGoogleSiteSearchCharset ) ), $outhtml 
);
+               $html = Html::rawElement( 'div', array( 'id' => 
'mw-googlesitesearch-container' ),
+                       Html::element( 'script', array(), 'var mwGSSCallback = 
function() { google.search.cse.element.getElement(' . FormatJson::encode( 
$gcseAttributes['gname'] ) . ').execute(' . FormatJson::encode( $term ) . '); 
}; window.__gcse = { callback: mwGSSCallback }; (function() { var gcse = 
document.createElement("script"); gcse.type = "text/javascript"; gcse.async = 
true; gcse.src = "https://cse.google.com/cse.js?cx="; + ' . FormatJson::encode( 
$wgGoogleSiteSearchCSEID ) . '; var s = 
document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(gcse, s); 
})();' )
+                       . Html::Element( 'div', $gcseAttributesDiv, wfMessage( 
'googlesitesearch-loading' ) )
+               );
+
+               # Allow hook override of HTML
+               Hooks::run( 'GoogleSiteSearchHTML', [ $specialSearch, $term, 
&$html ] );
 
                # Add it!
                $output->addWikiText( '== ' . wfMessage( 
'googlesitesearch-google-results' ) . ' ==' );
-               $output->AddHTML( $outhtml );
+               $output->AddHTML( $html );
 
                # Do not return wiki results if configured that way
                if ( $wgGoogleSiteSearchOnly ) {
diff --git a/GoogleSiteSearch.php b/GoogleSiteSearch.php
index a516042..2b3cc4b 100644
--- a/GoogleSiteSearch.php
+++ b/GoogleSiteSearch.php
@@ -55,8 +55,8 @@
 if ( !isset( $wgGoogleSiteSearchOnly ) ) {
        $wgGoogleSiteSearchOnly = false;
 }
-if ( !isset( $wgGoogleSiteSearchCharset ) ) {
-       $wgGoogleSiteSearchCharset = 'UTF-8';
+if ( !isset( $wgGoogleSiteSearchAttributes ) ) {
+       $wgGoogleSiteSearchAttributes = [];
 }
 
 # Define internationalizations
diff --git a/extension.json b/extension.json
index 3bca0cd..2448f9c 100644
--- a/extension.json
+++ b/extension.json
@@ -15,12 +15,12 @@
                "GoogleSiteSearch": "GoogleSiteSearch.hooks.php"
        },
        "Hooks": {
-               "SpecialSearchResultsPrepend": 
"GoogleSiteSearch::googleSiteSearchPrepend"
+               "SpecialSearchResultsPrepend": "GoogleSiteSearch::searchPrepend"
        },
        "config": {
                "GoogleSiteSearchCSEID": "",
                "GoogleSiteSearchOnly": false,
-               "GoogleSiteSearchCharset": "UTF-8"
+               "GoogleSiteSearchAttributes": []
        },
        "requires": {
                "MediaWiki": ">= 1.23.0"

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I637a1448fefe18297aa2bb3d8f68c38d28240fde
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/GoogleSiteSearch
Gerrit-Branch: master
Gerrit-Owner: Fo0bar <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to