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

Commit 8825cca

Browse files
authored
Add phar build, and move config to ~/.config
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
1 parent 3d5f62d commit 8825cca

14 files changed

+136
-30
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
/contribs/
55
/.phpunit.result.cache
66
/build/
7+
/website/mwcli.phar

.phpcs.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
<file>.</file>
99
<exclude-pattern>temp/</exclude-pattern>
1010
<exclude-pattern>vendor/</exclude-pattern>
11-
<exclude-pattern>config.php</exclude-pattern>
11+
<exclude-pattern>website/simple.min.css</exclude-pattern>
1212
</ruleset>

README.md

+11-11
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Add a new site to the config file.
5858
sites:add [-c|--config [CONFIG]] [--url URL]
5959

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

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

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

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

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

8585
### sites:remove
8686

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

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

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

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

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

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

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

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

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

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

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

bin/mwcli

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use Samwilson\MediaWikiCLI\Command\UploadFilesCommand;
1818
use Samwilson\MediaWikiCLI\Command\UploadPagesCommand;
1919
use Symfony\Component\Console\Application;
2020

21-
$application = new Application( 'mwcli', '1.3.0' );
21+
$application = new Application( 'mwcli', '@git-tag@' );
2222
$application->add(new SitesAddCommand());
2323
$application->add(new SitesInfoCommand());
2424
$application->add(new SitesListCommand());

box.json

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"output": "website/mwcli.phar",
3+
"compression": "GZ",
4+
"git-tag": "git-tag",
5+
"directories": [
6+
"src/",
7+
"i18n/"
8+
],
9+
"finder": [
10+
{
11+
"in": "vendor",
12+
"name": "*.php",
13+
"exclude": [
14+
"CHANGELOG",
15+
"CONTRIBUTING",
16+
"README",
17+
"Tests",
18+
"behat",
19+
"ext",
20+
"bin",
21+
"build",
22+
"doc",
23+
"docs",
24+
"doc-template",
25+
"fixtures",
26+
"test",
27+
"tests",
28+
"test_old",
29+
"vendor-bin"
30+
]
31+
}
32+
]
33+
}

composer.json

+3
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@
5555
"minus-x check .",
5656
"./bin/mwcli readme",
5757
"phpunit"
58+
],
59+
"build": [
60+
"box compile"
5861
]
5962
}
6063
}

i18n/en.json

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"using-config": "Using config file $1",
33
"option-config-desc": "Path of the Yaml config file to use.",
44
"saved-config": "Configuration file saved.",
5+
"old-config-exists": "A config file exists at $1 that you may wish to move to $2",
56

67
"command-sites-add-desc": "Add a new site to the config file.",
78
"command-sites-list-desc": "List all configured sites.",

src/Command/CommandBase.php

+25-13
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Console\Output\OutputInterface;
1818
use Symfony\Component\Console\Style\SymfonyStyle;
1919
use Symfony\Component\Yaml\Yaml;
20+
use XdgBaseDir\Xdg;
2021

2122
abstract class CommandBase extends Command {
2223

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

33+
/** @var mixed|null Runtime config of the config file's data. */
34+
private $config;
35+
3236
public function configure() {
3337
// Set up i18n.
3438
$this->intuition = new Intuition( 'mwcli' );
3539
$this->intuition->registerDomain( 'mwcli', dirname( __DIR__, 2 ) . '/i18n' );
36-
37-
$default = $this->getConfigDirDefault() . 'config.yml';
40+
$default = ( new Xdg() )->getHomeConfigDir() . '/mwcli/config.yml';
3841
$this->addOption( 'config', 'c', InputOption::VALUE_OPTIONAL, $this->msg( 'option-config-desc' ), $default );
3942
}
4043

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

79-
/**
80-
* Get the default config directory (the root directory of mwcli).
81-
* @return string The full filesystem path, always with a trailing slash.
82-
*/
83-
protected function getConfigDirDefault(): string {
84-
return rtrim( dirname( __DIR__, 2 ), DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR;
85-
}
86-
8782
/**
8883
* @param InputInterface $input
89-
* @return mixed[][]
84+
* @return mixed
9085
*/
9186
protected function getConfig( InputInterface $input ): array {
87+
if ( $this->config ) {
88+
return $this->config;
89+
}
9290
$configPath = $input->getOption( 'config' );
91+
92+
// Backwards compatibility check: if there's a config.yaml file in the
93+
// current directory, tell the user. It may not actually be an mwcli one
94+
// so we don't try to move it ourselves.
95+
$cwdConfig = getcwd() . '/config.yml';
96+
if ( file_exists( $cwdConfig ) ) {
97+
$this->io->warning( $this->msg( 'old-config-exists', [ $cwdConfig, $configPath ] ) );
98+
}
99+
93100
if ( !file_exists( $configPath ) ) {
94101
// Create an empty config file.
95102
$this->saveConfig( $input, [] );
96103
}
97104
$this->io->block( $this->msg( 'using-config', [ $configPath ] ) );
98-
$config = Yaml::parseFile( $configPath );
99-
return $config;
105+
$this->config = Yaml::parseFile( $configPath );
106+
return $this->config;
100107
}
101108

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

113125
protected function getApi( array $siteInfo, ?AuthMethod $authMethod = null ): ActionApi {

src/Command/ExportCategoryCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function configure() {
3131
$this->addOption( 'wiki', 'w', InputOption::VALUE_REQUIRED, $this->msg( 'option-wiki-desc' ) );
3232
$this->addOption( 'category', 'a', InputOption::VALUE_REQUIRED, $this->msg( 'option-category-desc' ) );
3333
$this->addOption( 'dest', 'd', InputOption::VALUE_REQUIRED, $this->msg( 'option-dest-desc' ),
34-
$this->getConfigDirDefault() . 'categories' );
34+
getcwd() . '/categories' );
3535
}
3636

3737
public function execute( InputInterface $input, OutputInterface $output ) {

src/Command/ExportContribsCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function configure() {
2020
$this->addOption( 'wiki', 'w', InputOption::VALUE_REQUIRED, $this->msg( 'option-wiki-desc' ) );
2121
$this->addOption( 'user', 'u', InputOption::VALUE_REQUIRED, $this->msg( 'option-user-desc' ) );
2222
$this->addOption( 'dest', 'd', InputOption::VALUE_REQUIRED, $this->msg( 'option-dest-desc' ),
23-
$this->getConfigDirDefault() . 'contribs' );
23+
getcwd() . '/contribs' );
2424
$this->addOption( 'only-author', 'o', InputOption::VALUE_NONE, $this->msg( 'option-only-author-desc' ) );
2525
}
2626

src/Command/ExportWikitextCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function configure() {
2727
$this->setDescription( $this->msg( 'command-export-wikitext-desc' ) );
2828
$this->addOption( 'wiki', 'w', InputOption::VALUE_REQUIRED, $this->msg( 'option-wiki-desc' ) );
2929
$this->addOption( 'dest', 'd', InputOption::VALUE_REQUIRED, $this->msg( 'option-dest-desc' ),
30-
$this->getConfigDirDefault() . 'wikitext' );
30+
getcwd() . '/wikitext' );
3131
$this->addOption( 'ext', 'e', InputOption::VALUE_REQUIRED, $this->msg( 'option-ext-desc' ), 'txt' );
3232
}
3333

src/Command/ReadmeGenCommand.php

+19-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Symfony\Component\Console\Input\InputInterface;
66
use Symfony\Component\Console\Output\OutputInterface;
7+
use XdgBaseDir\Xdg;
78

89
class ReadmeGenCommand extends CommandBase {
910

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

4041
// Remove local paths.
41-
$commandInfo = str_replace( getcwd(), '[CWD]', $commandInfo );
42+
$xdg = new Xdg();
43+
$commandInfo = str_replace(
44+
[
45+
getcwd(),
46+
$xdg->getHomeCacheDir(),
47+
$xdg->getHomeConfigDir(),
48+
$xdg->getHomeDataDir(),
49+
$xdg->getHomeDir(),
50+
],
51+
[
52+
'[CWD]',
53+
'[CACHE]',
54+
'[CONFIG]',
55+
'[DATA]',
56+
'[HOME]',
57+
],
58+
$commandInfo
59+
);
4260

4361
// Write new contents to README.md.
4462
$readmePath = dirname( __DIR__, 2 ) . '/README.md';

website/index.html

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<link rel="stylesheet" href="simple.min.css">
7+
<title>mwcli: a MediaWiki CLI tool</title>
8+
</head>
9+
<body>
10+
<header>
11+
<h1>MediaWiki CLI</h1>
12+
<p>A command line client for <a href="https://www.mediawiki.org">MediaWiki</a> wikis.</p>
13+
</header>
14+
15+
<main>
16+
<p>
17+
MediaWiki CLI (or <code>mwcli</code> for short) is a multilingual cross-platform PHP command-line tool
18+
for interacting with MediaWiki installations,
19+
to carry out tasks such as uploading and downloading files, and exporting pages.
20+
</p>
21+
22+
<p>
23+
<a class="button" href="mwcli.phar">Download mwcli.phar</a>
24+
</p>
25+
26+
<p>Other useful links:</p>
27+
28+
<ul>
29+
<li><a href="https://github.com/samwilson/mwcli/blob/main/README.md">README.md</a></li>
30+
<li><a href="https://github.com/samwilson/mwcli/">Source code</a></li>
31+
<li><a href="https://github.com/samwilson/mwcli/issues">Issue tracker</a></li>
32+
</ul>
33+
34+
</main>
35+
</body>
36+
</html>

0 commit comments

Comments
 (0)