Skip to content
This repository has been archived by the owner on Jan 28, 2025. It is now read-only.

Commit

Permalink
Add phar build, and move config to ~/.config
Browse files Browse the repository at this point in the history
Add Box configuration for building a Phar file at a new website
directory: website/mwcl.phar

Also update the location of the config.yml file to be in the
XDG config directory, so it's accessible wherever the Phar is
run from. Add warning if a config file is found in the current
working directory.

Bug: GH #241
Bug: GH #246
  • Loading branch information
samwilson authored Jan 28, 2025
1 parent 3d5f62d commit 8825cca
Show file tree
Hide file tree
Showing 14 changed files with 136 additions and 30 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/contribs/
/.phpunit.result.cache
/build/
/website/mwcli.phar
2 changes: 1 addition & 1 deletion .phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
<file>.</file>
<exclude-pattern>temp/</exclude-pattern>
<exclude-pattern>vendor/</exclude-pattern>
<exclude-pattern>config.php</exclude-pattern>
<exclude-pattern>website/simple.min.css</exclude-pattern>
</ruleset>
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Add a new site to the config file.
sites:add [-c|--config [CONFIG]] [--url URL]

* `--config` `-c` — Path of the Yaml config file to use.
Default: '[CWD]/config.yml'
Default: '[CONFIG]/mwcli/config.yml'
* `--url` — The URL of the wiki. Can be to any page or even api.php.
*Required.*

Expand All @@ -69,7 +69,7 @@ Get general information about a wiki.
sites:info [-c|--config [CONFIG]] [-w|--wiki WIKI]

* `--config` `-c` — Path of the Yaml config file to use.
Default: '[CWD]/config.yml'
Default: '[CONFIG]/mwcli/config.yml'
* `--wiki` `-w` — The mwcli name of the wiki to use. Use <info>sites:list</info> to list all.
*Required.*

Expand All @@ -80,7 +80,7 @@ List all configured sites.
sites:list [-c|--config [CONFIG]]

* `--config` `-c` — Path of the Yaml config file to use.
Default: '[CWD]/config.yml'
Default: '[CONFIG]/mwcli/config.yml'

### sites:remove

Expand All @@ -89,7 +89,7 @@ Remove a site from the config file.
sites:remove [-c|--config [CONFIG]] [-w|--wiki WIKI]

* `--config` `-c` — Path of the Yaml config file to use.
Default: '[CWD]/config.yml'
Default: '[CONFIG]/mwcli/config.yml'
* `--wiki` `-w` — The mwcli name of the wiki to use. Use <info>sites:list</info> to list all.
*Required.*

Expand All @@ -100,7 +100,7 @@ Export pages and files in a category (and its subcategories).
export:category [-c|--config [CONFIG]] [-w|--wiki WIKI] [-a|--category CATEGORY] [-d|--dest DEST]

* `--config` `-c` — Path of the Yaml config file to use.
Default: '[CWD]/config.yml'
Default: '[CONFIG]/mwcli/config.yml'
* `--wiki` `-w` — The mwcli name of the wiki to use. Use <info>sites:list</info> to list all.
*Required.*
* `--category` `-a` — Name of the category to export (with or without the leading 'Category:', and in any language).
Expand All @@ -115,7 +115,7 @@ Export a user's contributions.
export:contribs [-c|--config [CONFIG]] [-w|--wiki WIKI] [-u|--user USER] [-d|--dest DEST] [-o|--only-author]

* `--config` `-c` — Path of the Yaml config file to use.
Default: '[CWD]/config.yml'
Default: '[CONFIG]/mwcli/config.yml'
* `--wiki` `-w` — The mwcli name of the wiki to use. Use <info>sites:list</info> to list all.
*Required.*
* `--user` `-u` — Export contributions of this username.
Expand All @@ -131,7 +131,7 @@ Export a wiki's pages as text files.
export:wikitext [-c|--config [CONFIG]] [-w|--wiki WIKI] [-d|--dest DEST] [-e|--ext EXT]

* `--config` `-c` — Path of the Yaml config file to use.
Default: '[CWD]/config.yml'
Default: '[CONFIG]/mwcli/config.yml'
* `--wiki` `-w` — The mwcli name of the wiki to use. Use <info>sites:list</info> to list all.
*Required.*
* `--dest` `-d` — The destination directory for exported files.
Expand All @@ -146,7 +146,7 @@ Install an extension into a local wiki. Requires 'install_path' to be set in a s
extension:install [-c|--config [CONFIG]] [-w|--wiki WIKI] [-g|--git] [-u|--gituser GITUSER] [--] <extension-name>

* `--config` `-c` — Path of the Yaml config file to use.
Default: '[CWD]/config.yml'
Default: '[CONFIG]/mwcli/config.yml'
* `--wiki` `-w` — The mwcli name of the wiki to use. Use <info>sites:list</info> to list all.
*Required.*
* `--git` `-g` — Use Git to install the extension, instead of the default tarball method.
Expand All @@ -161,7 +161,7 @@ Shows a list of installed extensions that have updates available, including thei
extension:outdated [-c|--config [CONFIG]] [-w|--wiki WIKI]

* `--config` `-c` — Path of the Yaml config file to use.
Default: '[CWD]/config.yml'
Default: '[CONFIG]/mwcli/config.yml'
* `--wiki` `-w` — The mwcli name of the wiki to use. Use <info>sites:list</info> to list all.
*Required.*

Expand All @@ -172,7 +172,7 @@ Upload local files to a wiki.
upload:files [-c|--config [CONFIG]] [-w|--wiki WIKI] [-m|--comment COMMENT] [--] [<files>...]

* `--config` `-c` — Path of the Yaml config file to use.
Default: '[CWD]/config.yml'
Default: '[CONFIG]/mwcli/config.yml'
* `--wiki` `-w` — The mwcli name of the wiki to use. Use <info>sites:list</info> to list all.
*Required.*
* `--comment` `-m` — Revision comment.
Expand All @@ -186,7 +186,7 @@ Upload local text files as wiki pages.
upload:pages [-c|--config [CONFIG]] [-w|--wiki WIKI] [-m|--comment [COMMENT]] [-t|--watch] [--] <pages-dir>

* `--config` `-c` — Path of the Yaml config file to use.
Default: '[CWD]/config.yml'
Default: '[CONFIG]/mwcli/config.yml'
* `--wiki` `-w` — The mwcli name of the wiki to use. Use <info>sites:list</info> to list all.
*Required.*
* `--comment` `-m` — Revision comment.
Expand Down
2 changes: 1 addition & 1 deletion bin/mwcli
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use Samwilson\MediaWikiCLI\Command\UploadFilesCommand;
use Samwilson\MediaWikiCLI\Command\UploadPagesCommand;
use Symfony\Component\Console\Application;

$application = new Application( 'mwcli', '1.3.0' );
$application = new Application( 'mwcli', '@git-tag@' );
$application->add(new SitesAddCommand());
$application->add(new SitesInfoCommand());
$application->add(new SitesListCommand());
Expand Down
33 changes: 33 additions & 0 deletions box.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"output": "website/mwcli.phar",
"compression": "GZ",
"git-tag": "git-tag",
"directories": [
"src/",
"i18n/"
],
"finder": [
{
"in": "vendor",
"name": "*.php",
"exclude": [
"CHANGELOG",
"CONTRIBUTING",
"README",
"Tests",
"behat",
"ext",
"bin",
"build",
"doc",
"docs",
"doc-template",
"fixtures",
"test",
"tests",
"test_old",
"vendor-bin"
]
}
]
}
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
"minus-x check .",
"./bin/mwcli readme",
"phpunit"
],
"build": [
"box compile"
]
}
}
1 change: 1 addition & 0 deletions i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"using-config": "Using config file $1",
"option-config-desc": "Path of the Yaml config file to use.",
"saved-config": "Configuration file saved.",
"old-config-exists": "A config file exists at $1 that you may wish to move to $2",

"command-sites-add-desc": "Add a new site to the config file.",
"command-sites-list-desc": "List all configured sites.",
Expand Down
38 changes: 25 additions & 13 deletions src/Command/CommandBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Yaml\Yaml;
use XdgBaseDir\Xdg;

abstract class CommandBase extends Command {

Expand All @@ -29,12 +30,14 @@ abstract class CommandBase extends Command {
/** @var LoggerInterface */
protected $logger;

/** @var mixed|null Runtime config of the config file's data. */
private $config;

public function configure() {
// Set up i18n.
$this->intuition = new Intuition( 'mwcli' );
$this->intuition->registerDomain( 'mwcli', dirname( __DIR__, 2 ) . '/i18n' );

$default = $this->getConfigDirDefault() . 'config.yml';
$default = ( new Xdg() )->getHomeConfigDir() . '/mwcli/config.yml';
$this->addOption( 'config', 'c', InputOption::VALUE_OPTIONAL, $this->msg( 'option-config-desc' ), $default );
}

Expand Down Expand Up @@ -76,27 +79,31 @@ protected function msg( string $msg, ?array $vars = [] ): string {
] );
}

/**
* Get the default config directory (the root directory of mwcli).
* @return string The full filesystem path, always with a trailing slash.
*/
protected function getConfigDirDefault(): string {
return rtrim( dirname( __DIR__, 2 ), DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR;
}

/**
* @param InputInterface $input
* @return mixed[][]
* @return mixed
*/
protected function getConfig( InputInterface $input ): array {
if ( $this->config ) {
return $this->config;
}
$configPath = $input->getOption( 'config' );

// Backwards compatibility check: if there's a config.yaml file in the
// current directory, tell the user. It may not actually be an mwcli one
// so we don't try to move it ourselves.
$cwdConfig = getcwd() . '/config.yml';
if ( file_exists( $cwdConfig ) ) {
$this->io->warning( $this->msg( 'old-config-exists', [ $cwdConfig, $configPath ] ) );
}

if ( !file_exists( $configPath ) ) {
// Create an empty config file.
$this->saveConfig( $input, [] );
}
$this->io->block( $this->msg( 'using-config', [ $configPath ] ) );
$config = Yaml::parseFile( $configPath );
return $config;
$this->config = Yaml::parseFile( $configPath );
return $this->config;
}

/**
Expand All @@ -106,8 +113,13 @@ protected function getConfig( InputInterface $input ): array {
*/
protected function saveConfig( InputInterface $input, array $config ): void {
$configPath = $input->getOption( 'config' );
if ( !file_exists( dirname( $configPath ) ) ) {
mkdir( dirname( $configPath ), 0700, true );
}
file_put_contents( $configPath, Yaml::dump( $config, 3 ) );
$this->io->success( $this->msg( 'saved-config', [ $configPath ] ) );
// Set the runtime cache to null so it will be refreshed next time the config is accessed.
$this->config = null;
}

protected function getApi( array $siteInfo, ?AuthMethod $authMethod = null ): ActionApi {
Expand Down
2 changes: 1 addition & 1 deletion src/Command/ExportCategoryCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function configure() {
$this->addOption( 'wiki', 'w', InputOption::VALUE_REQUIRED, $this->msg( 'option-wiki-desc' ) );
$this->addOption( 'category', 'a', InputOption::VALUE_REQUIRED, $this->msg( 'option-category-desc' ) );
$this->addOption( 'dest', 'd', InputOption::VALUE_REQUIRED, $this->msg( 'option-dest-desc' ),
$this->getConfigDirDefault() . 'categories' );
getcwd() . '/categories' );
}

public function execute( InputInterface $input, OutputInterface $output ) {
Expand Down
2 changes: 1 addition & 1 deletion src/Command/ExportContribsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function configure() {
$this->addOption( 'wiki', 'w', InputOption::VALUE_REQUIRED, $this->msg( 'option-wiki-desc' ) );
$this->addOption( 'user', 'u', InputOption::VALUE_REQUIRED, $this->msg( 'option-user-desc' ) );
$this->addOption( 'dest', 'd', InputOption::VALUE_REQUIRED, $this->msg( 'option-dest-desc' ),
$this->getConfigDirDefault() . 'contribs' );
getcwd() . '/contribs' );
$this->addOption( 'only-author', 'o', InputOption::VALUE_NONE, $this->msg( 'option-only-author-desc' ) );
}

Expand Down
2 changes: 1 addition & 1 deletion src/Command/ExportWikitextCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function configure() {
$this->setDescription( $this->msg( 'command-export-wikitext-desc' ) );
$this->addOption( 'wiki', 'w', InputOption::VALUE_REQUIRED, $this->msg( 'option-wiki-desc' ) );
$this->addOption( 'dest', 'd', InputOption::VALUE_REQUIRED, $this->msg( 'option-dest-desc' ),
$this->getConfigDirDefault() . 'wikitext' );
getcwd() . '/wikitext' );
$this->addOption( 'ext', 'e', InputOption::VALUE_REQUIRED, $this->msg( 'option-ext-desc' ), 'txt' );
}

Expand Down
20 changes: 19 additions & 1 deletion src/Command/ReadmeGenCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use XdgBaseDir\Xdg;

class ReadmeGenCommand extends CommandBase {

Expand Down Expand Up @@ -38,7 +39,24 @@ public function execute( InputInterface $input, OutputInterface $output ) {
}

// Remove local paths.
$commandInfo = str_replace( getcwd(), '[CWD]', $commandInfo );
$xdg = new Xdg();
$commandInfo = str_replace(
[
getcwd(),
$xdg->getHomeCacheDir(),
$xdg->getHomeConfigDir(),
$xdg->getHomeDataDir(),
$xdg->getHomeDir(),
],
[
'[CWD]',
'[CACHE]',
'[CONFIG]',
'[DATA]',
'[HOME]',
],
$commandInfo
);

// Write new contents to README.md.
$readmePath = dirname( __DIR__, 2 ) . '/README.md';
Expand Down
36 changes: 36 additions & 0 deletions website/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="simple.min.css">
<title>mwcli: a MediaWiki CLI tool</title>
</head>
<body>
<header>
<h1>MediaWiki CLI</h1>
<p>A command line client for <a href="https://www.mediawiki.org">MediaWiki</a> wikis.</p>
</header>

<main>
<p>
MediaWiki CLI (or <code>mwcli</code> for short) is a multilingual cross-platform PHP command-line tool
for interacting with MediaWiki installations,
to carry out tasks such as uploading and downloading files, and exporting pages.
</p>

<p>
<a class="button" href="mwcli.phar">Download mwcli.phar</a>
</p>

<p>Other useful links:</p>

<ul>
<li><a href="https://github.com/samwilson/mwcli/blob/main/README.md">README.md</a></li>
<li><a href="https://github.com/samwilson/mwcli/">Source code</a></li>
<li><a href="https://github.com/samwilson/mwcli/issues">Issue tracker</a></li>
</ul>

</main>
</body>
</html>
Loading

0 comments on commit 8825cca

Please sign in to comment.