1

Is it possible to have my wiki load a line of text from a *.txt file and display it somewhere on the Main Page? Something like a "daily message" that comes straight from the txt file and does not involve editing the wiki manually?

My wiki is running on Ubuntu Mate and is as follows:

enter image description here

UPDATE: Based on Alexander's answer I have this working fine, but caching is not behaving as expected. I want the external data to show up immediately upon clicking browser refresh button (so I have set cache times to zero) - but this does not work. The only way I have found to force the external text to be refreshed is to A) edit the wiki page (which forces a reload), or B) stop/start the apache server.

In my LocalSettings.php file I have:

wfLoadExtension( 'ExternalData' );
$edgCacheExpireTime=0;
$edgFilePath['inject'] = '/home/rw/my_external_data.txt';

In my main page wiki i have:

{{#get_file_data:
  file=inject
  |format=text
  |data=inject=text
  |cache seconds=0
 }}{{#external_value:inject}}
relayman357
  • 793
  • 1
  • 6
  • 30

1 Answers1

2

You can install ''External Data'', then:

Old style:

define in your LocalSettings.php

wfLoadExtension( 'ExternalData' );
$edgFilePath['inject'] = '/path/to/your/file.txt';
$edgCacheExpireTime = 10; // only ten seconds.

and add to your Main page:

{{#get_file_data: file = inject
  | format = text
  | data = inject=__text
  | cache seconds = 10
 }}{{#external_value:inject}}

New style:

define in your LocalSettings.php

wfLoadExtension( 'ExternalData' );
$wgExternalDataSources['inject'] = [
    'path' => '/path/to/your/file.txt',
    'min cache seconds' => 10
];

and add to your Main page:

{{#get_external_data: file = inject
  | format = text
  | data = inject=__text
  | cache seconds = 10
 }}{{#external_value:inject}}
Alexander Mashin
  • 3,892
  • 1
  • 9
  • 15
  • Thanks Alexander. I don't know how to run `composer update` in the ExternalData directory. There is a `composer.json` file in that directory but I don't know how to run `composer update` as specified [here](https://www.mediawiki.org/wiki/Extension:External_Data#Installation). – relayman357 Feb 10 '21 at 03:18
  • Provided you have composer installed, simply type `composer update` or `php composer.phar update` in the terninal while in `extensions/ExternalData`. – Alexander Mashin Feb 10 '21 at 03:31
  • I don’t know what composer is. – relayman357 Feb 10 '21 at 03:33
  • Ok, nice! Thank you Alexander. Works like a charm. When i run `composer update` it says there is a version 2.0 available (the version I have after `apt-get install composer` is 1.10.10). I am leaving it at version 1.10.10 since the composer wiki page you link above says, "Currently, only Composer 1.x is supported by MediaWiki. " – relayman357 Feb 10 '21 at 14:54
  • I added an update to my post concerning how to set the cache delay so changes to the external data text file show up immediately (with browser refresh). I can't make that happen. – relayman357 Feb 10 '21 at 18:45
  • I updated my answer to set caching time to ten seconds. Browser refresh may not be enough anyway, you may need `?action=purge`. – Alexander Mashin Feb 11 '21 at 04:37
  • Hi @alexandermashin I made the changes to `cache seconds` and to `$edgCacheExpireTime` and neither appear to have any affect. I added the following at bottom of page to do a purge but purging does nothing either: `{{fullurl:MediaWiki:mainpage|action=purge}}`. The only thing that actually refreshes is to edit the page or stop/start mysql and apache2 services. – relayman357 Feb 18 '21 at 00:18
  • By "purge" I meant that you click on http://your.wiki/Your_page?action=purge link on your page in the browser to invalidate parser cache and web server cache. – Alexander Mashin Feb 18 '21 at 02:44
  • Thank you. I found `purge` under special pages and then put in `Main_Page` and the purge worked as you said. Not sure why the `cache seconds` and `edgCacheExpireTime` don't do it, maybe something weird in my setup. Anyway, thanks again sir! – relayman357 Feb 18 '21 at 14:11
  • Looked promising, but I get `Error: no local variable "inject" has been set.`, even though it is all exactly as described here for LocalSettings.php and in the page. – mivk Apr 01 '22 at 21:49
  • See the corrected answer: `text` → `__text`. – Alexander Mashin Apr 02 '22 at 06:49
  • @AlexanderMashin: Unfortunately, still the same error. In case it matters, using ED v. 3.1 in Mediawiki 1.31.10 on Debian 10 with Apache, PostgreSQL, and PHP version 7.4.3. – mivk Apr 02 '22 at 15:33
  • Did you use double underscore? Are there any other error messages? – Alexander Mashin Apr 02 '22 at 15:54
  • @AlexanderMashin: Yes: `{{#get_external_data: file=inject|format=text|data=inject=__text}}{{#external_value:inject}}`. Full error is "Unable to get the contents of file /docs/www-wiki/popular_pages.txt. Error: no local variable "inject" has been set.". (My PHP version is actually 7.3.19) – mivk Apr 02 '22 at 16:17
  • The error `Unable to get the contents of file…` is issued when a file exists but cannot be read -- a rare situation, possibly caused by wrong file permissions. What is strange is that the message contains a real filename, while it should contain the alias `inject`. – Alexander Mashin Apr 02 '22 at 17:03
  • Do you really have `$wgExternalDataSources['inject'] = [ 'path' = '/docs/www-wiki/popular_pages.txt', 'min cache seconds' => 10 ];` in your `LocalSettings.php`? – Alexander Mashin Apr 02 '22 at 17:12
  • @AlexanderMashin: It works! After replacing `'path'=...` with `'path'=>...`. I also edited it in your answer. Before, it was giving "PHP Parse error: syntax error, unexpected '=', expecting ']'" in the Apache logs. Took me a while to find it... All good now. Thanks a lot. – mivk Apr 02 '22 at 20:09
  • Thank you. It was stupid of me to confuse `=` and `=>`. – Alexander Mashin Apr 03 '22 at 08:57