-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "Add to AI chat" links to search results (Special:Search)
- Loading branch information
1 parent
043d8c9
commit b2e3eda
Showing
5 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
/** | ||
* Implements AskAI extension for MediaWiki. | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along | ||
* with this program; if not, write to the Free Software Foundation, Inc., | ||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
* http://www.gnu.org/copyleft/gpl.html | ||
* | ||
* @file | ||
*/ | ||
|
||
namespace MediaWiki\AskAI; | ||
|
||
use MediaWiki\Hook\SpecialSearchResultsPrependHook; | ||
use OutputPage; | ||
use SpecialSearch; | ||
|
||
/** | ||
* Hooks of Extension:AskAI. | ||
*/ | ||
class Hooks implements SpecialSearchResultsPrependHook { | ||
/** | ||
* Add our JavaScript module to Special:Search. | ||
* @param SpecialSearch $specialSearch | ||
* @param OutputPage $out | ||
* @param string $term | ||
* @return bool|void | ||
*/ | ||
public function onSpecialSearchResultsPrepend( $specialSearch, $out, $term ) { | ||
$out->addModules( 'ext.askai.search' ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* Shows "Add to AI chat" link near every search result on Special:Search. */ | ||
|
||
$( function () { | ||
const $results = $( '.mw-search-result' ); | ||
if ( !$results.length ) { | ||
return; | ||
} | ||
|
||
const addedSources = []; | ||
|
||
/* Redirect to Special:AI, providing it with the previously added sources */ | ||
function goToSpecial() { | ||
const title = new mw.Title( 'Special:AI' ); | ||
window.location.href = title.getUrl( { | ||
wpPages: addedSources.join( '\n' ) | ||
} ); | ||
} | ||
|
||
/* Handler of "Add to AI chat" link */ | ||
function addToAI() { | ||
const $addLink = $( this ), | ||
$res = $addLink.parents( '.mw-search-result' ), | ||
pageLink = $res.find( 'a' )[ 0 ], | ||
matchedSentences = $res.find( '.searchresult' ).text().trim().split( /\.\s/ ); | ||
|
||
const $loading = $( '<span>' ) | ||
.attr( 'class', 'mw-askai-search-adding' ) | ||
.append( mw.msg( 'askai-search-adding' ) ); | ||
|
||
$addLink.replaceWith( $loading ); | ||
|
||
// Download the article and find the paragraph(s) that contain "matchedText". | ||
$.get( pageLink.href ).done( ( html ) => { | ||
const parNumbers = []; | ||
|
||
$( '<div>' ).append( html ).find( '.mw-parser-output > p' ).each( ( idx, p ) => { | ||
for ( const sentence of matchedSentences ) { | ||
if ( p.innerText.indexOf( sentence ) !== -1 ) { | ||
parNumbers.push( idx ); | ||
return; | ||
} | ||
} | ||
} ); | ||
|
||
if ( !parNumbers.length ) { | ||
$loading.replaceWith( mw.msg( 'askai-search-add-not-found' ) ); | ||
return; | ||
} | ||
|
||
addedSources.push( pageLink.title + '#p' + parNumbers.join( ',' ) ); | ||
|
||
$loading.replaceWith( $( '<a>' ) | ||
.attr( 'class', 'mw-askai-search-view' ) | ||
.append( mw.msg( 'askai-search-view' ) ) | ||
.click( goToSpecial ) | ||
); | ||
} ).fail( () => { | ||
$loading.replaceWith( mw.msg( 'askai-search-add-failed' ) ); | ||
} ); | ||
} | ||
|
||
// Every result should have "Add to AI chat" link | ||
$results.each( ( idx, result ) => { | ||
$( result ).find( '.mw-search-result-data' ).append( ' ', $( '<a>' ) | ||
.attr( 'class', 'mw-askai-search-add' ) | ||
.append( mw.msg( 'askai-search-add' ) ) | ||
.click( addToAI ) | ||
); | ||
} ); | ||
} ); |