Skip to content

Commit

Permalink
Implement api configuration options
Browse files Browse the repository at this point in the history
This commit will implement and document multiple new api configuration
options for configuring the search and page fetching.

It also modifies the internal structure of several `wiki-api::search`
structs in order to provide better configuration schemas.

Signed-off-by: Builditluc <[email protected]>
  • Loading branch information
Builditluc committed Dec 2, 2024
1 parent 0928ae8 commit 10b6243
Show file tree
Hide file tree
Showing 14 changed files with 352 additions and 63 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions docs/docs/changelog/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
- Add options for changing the statusbar colors
- Add zen-mode to hide ui elements
- Add multiple new configurable keybindings
- Add multiple new api configuration options
- Add options for configuring the search
- Add an option for configuring page fetching

## Changes

Expand Down
91 changes: 89 additions & 2 deletions docs/docs/configuration/api.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,90 @@
# Api Settings

## Search Settings

### Search results limit
[:octicons-tag-24: 0.9.0][release-0.9.0] · :octicons-milestone-16: Default `10`

You can configure how many total pages to return per search. The value must be between 1 and 500

```toml
api.search_limit = 10
```

### Changing the QiProfile
[:octicons-tag-24: 0.9.0][release-0.9.0] · :octicons-milestone-16: Default `engineautoselect`

Select the query independent profile to use which affects the ranking algorithm of the search.
Available profiles are

| Name | Description | Config Name |
|---------------------|------------------------------------------------------------------------------------------|-----------------------|
| Classic | Ranking based on the number of incoming links, some templates, page language and recency | `classic` |
| ClassicNoBoostLinks | Ranking based on some templates, page language and recency when activated on the wiki | `classicnoboostlinks` |
| WSumIncLinks | Weighted sum based on incoming links | `wsuminclinks` |
| WSumIncLinksPV | Weighted sum based on incoming links and weekly pageviews | `wsuminclinkspv` |
| PopularIncLinksPV | Ranking based primarily on page views | `popularinclinkspv` |
| PopularIncLinks | Ranking based primarily on incoming links | `popularinclinks` |
| EngineAutoselect | Let the search engine decide on the best profile to use | `engineautoselect` |

```toml
api.search_qiprofile = "engineautoselect"
```

### Changing the search type
[:octicons-tag-24: 0.9.0][release-0.9.0] · :octicons-milestone-16: Default `text`

There are multiple types of search that can be performed by wikipedia. Available settings are

- *`nearmatch`* Search just by a match
- *`text`* Search the content of the page
- *`title`* Search the title of the page

```
api.search_type = "text"
```

### Enabling query rewrites
[:octicons-tag-24: 0.9.0][release-0.9.0] · :octicons-milestone-16: Default `false`

Enable interal query rewriting. Wikipedia can rewrite the query into another which is thought to
provide better results by, for instance, correcting spelling errors

```toml
api.search_rewrites = true
```

### Configuring the sort order
[:octicons-tag-24: 0.9.0][release-0.9.0] · :octicons-milestone-16: Default `relevance`

Set the sort order of returned results. Available sort orders are:

- *`createtimestampascending`*: Sort the results by their creation date in ascending order
- *`createtimestampdescending`*: See above, but in descending order
- *`incominglinksascending`*: Sort the results by their amount of pages linking to it in ascending order
- *`incominglinksdescending`*: See above, but in descending order
- *`justmatch`*: Sort the results only by their match to the query
- *`lasteditascending`*: Sort the results by the time of their last edit in ascending order
- *`lasteditdescending`*: See above, but in descending order
- *`nosort`*: Don't sort the search results
- *`random`*: Arrange the results in a random order
- *`relevance`*: Sort the results by relevance

```toml
api.search_sort_order = "relevance"
```

## Page Settings

### Resolving redirects
[:octicons-tag-24: 0.9.0][release-0.9.0] · :octicons-milestone-16: Default `false`

Whether to resolve page redirects directly

```toml
api.page_redirects = false
```

## Changing the Language

:octicons-milestone-16: Default: `en`
Expand Down Expand Up @@ -30,7 +115,7 @@ config file. The default language is English.
You can hide the 'Changed language to ...' popup by disabling the following setting

```toml
api.language_changed_popup = false
ui.popup_search_language_changed = false
```

### Hiding the article language changed popup
Expand All @@ -41,7 +126,7 @@ You can hide the 'Changed the language of your article to ...' popup by
disabling the following setting

```toml
api.article_language_changed_popup = false
ui.popup_page_language_changed = false
```

### Supported Languages
Expand All @@ -65,3 +150,5 @@ endpoint of the site, otherwise wiki-tui won't work.
api.pre_language = "https://"
api.post_language = ".wikipedia.org/w/api.php"
```

[release-0.9.0]: https://github.com/Builditluc/wiki-tui/releases/tag/v0.9
16 changes: 5 additions & 11 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use ratatui::{
widgets::Block,
};
use tracing::warn;
use wiki_api::{languages::Language, Endpoint};


use tokio::sync::mpsc;

Expand Down Expand Up @@ -94,20 +94,14 @@ impl Component for AppComponent {
self.search_bar
.init(action_tx.clone(), config.clone(), theme.clone())?;

self.config = config;
self.theme = theme;

let endpoint = Endpoint::parse("https://en.wikipedia.org/w/api.php").unwrap();
let language = Language::English;

self.page_loader = Some(PageLoader::new(action_tx.clone()));

self.search.endpoint = Some(endpoint);
self.search.language = Some(language);
self.page_loader = Some(PageLoader::new(config.clone(), action_tx.clone()));

action_tx.send(Action::EnterSearchBar).unwrap();
self.action_tx = Some(action_tx);

self.config = config;
self.theme = theme;

Ok(())
}
fn handle_key_events(&mut self, key: KeyEvent) -> ActionResult {
Expand Down
5 changes: 1 addition & 4 deletions src/components/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -641,10 +641,7 @@ impl Component for PageComponent {
matches_binding!(select_prev_link, Action::Page(PageAction::SelectPrevLink));
matches_binding!(select_next_link, Action::Page(PageAction::SelectNextLink));

matches_binding!(open_link, {
self.open_link();
ActionResult::consumed()
});
matches_binding!(open_link, self.open_link());
matches_binding!(toggle_zen_mode, {
self.is_zen_mode = !self.is_zen_mode;
ActionResult::Ignored
Expand Down
9 changes: 6 additions & 3 deletions src/components/page_language_popup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,17 @@ impl Component for PageLanguageSelectionComponent {
fn handle_key_events(&mut self, key: crossterm::event::KeyEvent) -> ActionResult {
if self.config.bindings.global.submit.matches_event(key) {
if let Some(link) = self.list.selected() {
return ActionPacket::single(Action::PopPopup)
.action(Action::PopupMessage(
let mut packet = ActionPacket::single(Action::PopPopup);
if self.config.ui.popup_page_language_changed {
packet = packet.action(Action::PopupMessage(
"Information".to_string(),
format!(
"Changing the language of the page to '{}'",
link.language.name()
),
))
));
}
return packet
.action(Action::LoadLangaugeLink(link.to_owned()))
.into();
}
Expand Down
19 changes: 12 additions & 7 deletions src/components/search.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use anyhow::{anyhow, Result};
use anyhow::{Result};
use crossterm::event::{KeyCode, KeyEvent};
use ratatui::{
prelude::{Alignment, Constraint, Direction, Layout, Rect},
Expand Down Expand Up @@ -73,16 +73,21 @@ impl Default for SearchComponent {

impl SearchComponent {
fn build_search(&self, query: String) -> Result<SearchRequest> {
let endpoint = self
.endpoint
.clone()
.ok_or(anyhow!("No Endpoint configured"))?;
let language = self.language.ok_or(anyhow!("No Language configured"))?;
let api_config = &self.config.api;

let endpoint = self.endpoint.clone().unwrap_or(api_config.endpoint.clone());
let language = self.language.unwrap_or(api_config.language);

Ok(ApiSearch::builder()
.query(query)
.endpoint(endpoint)
.language(language))
.language(language)
.limit(api_config.search_limit)
.qiprofile(api_config.search_qiprofile.clone())
.search_type(api_config.search_type.clone())
.info(api_config.search_info.clone())
.rewrites(api_config.search_rewrites)
.sort_order(api_config.search_sort_order.clone()))
}

fn start_search(&mut self, query: String) -> ActionResult {
Expand Down
13 changes: 9 additions & 4 deletions src/components/search_language_popup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,17 @@ impl Component for SearchLanguageSelectionComponent {
fn handle_key_events(&mut self, key: crossterm::event::KeyEvent) -> ActionResult {
if self.config.bindings.global.submit.matches_event(key) {
if let Some(lang) = self.list.selected() {
return ActionPacket::single(Action::SwitchContextSearch)
.action(Action::PopPopup)
.action(Action::PopupMessage(
let mut packet =
ActionPacket::single(Action::SwitchContextSearch).action(Action::PopPopup);

if self.config.ui.popup_search_language_changed {
packet = packet.action(Action::PopupMessage(
"Information".to_string(),
format!("Changed the language for searches to '{}'", lang.name()),
))
));
}

return packet
.action(Action::Search(SearchAction::ChangeLanguage(
lang.to_owned(),
)))
Expand Down
Loading

0 comments on commit 10b6243

Please sign in to comment.