Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: CI

on: push

jobs:
build:

strategy:
matrix:
# All supported PHP versions https://www.php.net/supported-versions.php
php: [ '8.2', '8.3', '8.4', '8.5' ]

runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{matrix.php}}

- name: Install
run: |
composer install

- name: Test
run: |
composer test
30 changes: 1 addition & 29 deletions .phpcs.xml
Original file line number Diff line number Diff line change
@@ -1,34 +1,6 @@
<?xml version="1.0"?>
<ruleset>
<!--
Piwigo coding conventions:
http://piwigo.org/doc/doku.php?id=dev:coding_conventions
-->

<rule ref="Generic.Arrays.ArrayIndent">
<properties>
<property name="indent" value="2" />
</properties>
</rule>
<rule ref="Generic.WhiteSpace.ScopeIndent">
<properties>
<property name="exact" value="true" />
<property name="indent" value="2" />
</properties>
</rule>
<rule ref="Generic.Functions.OpeningFunctionBraceBsdAllman" />
<rule ref="Generic.Arrays.DisallowShortArraySyntax" />
<rule ref="Squiz.Strings.ConcatenationSpacing" />
<rule ref="Generic.Files.LineEndings" />
<rule ref="Generic.Files.LineLength">
<properties>
<property name="absoluteLineLimit" value="79" />
</properties>
</rule>
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace" />

<rule ref="vendor/samwilson/piwigo-coding-standards/Piwigo/ruleset.xml" />
<file>.</file>

<exclude-pattern>./vendor</exclude-pattern>

</ruleset>
20 changes: 0 additions & 20 deletions .scrutinizer.yml

This file was deleted.

14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
Piwigo to MediaWiki
===================
Piwigo to Wikimedia Commons
===========================

* Code: https://github.com/samwilson/piwigo2mediawiki
* Bug reports: https://github.com/samwilson/piwigo2mediawiki/issues
* License: GPL 3.0 or later
* [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/samwilson/Piwigo2MediaWiki/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/samwilson/Piwigo2MediaWiki/?branch=master)
This is a Piwigo plugin for copying photos to Wikimedia Commons.

* Code: https://github.com/samwilson/Piwigo-WikimediaCommons
* Bug reports: https://github.com/samwilson/Piwigo-WikimediaCommons/issues
* Licence: GPL 3.0 or later
* Commons logo: CC-BY-SA 3.0 https://commons.wikimedia.org/wiki/File:Media_Viewer_Icon_-_Commons_Gray.svg
111 changes: 9 additions & 102 deletions admin.php
Original file line number Diff line number Diff line change
@@ -1,108 +1,15 @@
<?php

// Make sure we're already in Piwigo.
defined('PHPWG_ROOT_PATH') or exit(1);

require_once __DIR__.'/vendor/autoload.php';
include_once PHPWG_ROOT_PATH.'admin/include/tabsheet.class.php';

use Mediawiki\Api\ApiUser;
use Mediawiki\Api\FluentRequest;
use Mediawiki\Api\MediawikiApi;
use Mediawiki\Api\UsageException;

// Prepare the template.
$url = isset($_REQUEST['url']) ? $_REQUEST['url'] : '';
$p2mConf = isset($conf[PIWIGO2MEDIAWIKI_ID])
? $conf[PIWIGO2MEDIAWIKI_ID]
: array();
$template->assign(array(
'admin_url' => PIWIGO2MEDIAWIKI_ADMIN,
'piwigo2mediawiki_page' => PIWIGO2MEDIAWIKI_PAGE,
'mediawiki_url' => $url,
'p2m_conf' => $p2mConf,
));

// Delete if requested.
if (isset($_POST['action']) && $_POST['action']==='delete'
&& isset($_POST['id']) && isset($p2mConf[$_POST['id']])
) {
unset($p2mConf[$_POST['id']]);
conf_update_param(PIWIGO2MEDIAWIKI_ID, $p2mConf);
redirect(PIWIGO2MEDIAWIKI_ADMIN);
}

// Load one wiki's data for editing if an ID is specified.
if (isset($_REQUEST['id']) && isset($p2mConf[$_REQUEST['id']])) {
$info = $p2mConf[$_REQUEST['id']];
$info['id'] = $_REQUEST['id'];
$template->assign(array(
'info' => $info,
));
}

// Save (create or edit) a single wiki's data.
if ($url && isset($_POST['action']) && $_POST['action'] === 'add'
&& isset($_POST['username']) && $_POST['username']
&& isset($_POST['password']) && $_POST['password']
) {
// Find the API URL or show an error.
$validUrl = true;
try {
$api = MediawikiApi::newFromPage($url);
$url = $api->getApiUrl();
} catch (Exception $exception) {
$msg = l10n(
'MediaWiki API discovery failed for: %s<br />Error: %s',
$url,
$exception->getMessage()
);
$template->assign(array(
'warnings' => $msg,
'info' => $_POST,
));
$validUrl = false;
}
// If we've got a valid API URL, find the site name and save all data.
if ($validUrl) {

$username = $_POST['username'];
$password = $_POST['password'];

$loggedIn = false;
try
{
$loggedIn = $api->login(new ApiUser($username, $password));
} catch (UsageException $e) {
$msg = l10n('Authentication failed.<br />Error: %s', $e->getMessage());
$template->assign(array(
'warnings' => $msg,
'info' => $_POST,
));
}

// If logging in worked, get some more information and save the config.
if ($loggedIn)
{
$siteInfo = $api->getRequest(
FluentRequest::factory()
->setAction('query')
->setParam('meta', 'siteinfo')
);
$p2mConf[$info['id']] = array(
'sitename' => $siteInfo['query']['general']['sitename'],
'url' => $url,
'username' => $username,
'password' => $password,
'wikitext' => isset($_POST['wikitext']) ? $_POST['wikitext'] : '',
);
conf_update_param(PIWIGO2MEDIAWIKI_ID, $p2mConf);
redirect(PIWIGO2MEDIAWIKI_ADMIN);
}
}
defined('PHPWG_ROOT_PATH') or exit(1);

// Main navigation.
$tab = $_GET['tab'] ?? 'settings';
if (in_array($tab, array('settings', 'oauth'))) {
$admin_page = __DIR__.'/admin/'.$tab.'.php';
} else {
$admin_page = __DIR__.'/admin/photo.php';
}

$template_handle = PIWIGO2MEDIAWIKI_ID.'admin';
$template_file = PIWIGO2MEDIAWIKI_DIR.'admin.tpl';
$template->set_filename($template_handle, $template_file);
$template->assign_var_from_handle('ADMIN_CONTENT', $template_handle);
require_once $admin_page;
91 changes: 0 additions & 91 deletions admin.tpl

This file was deleted.

14 changes: 14 additions & 0 deletions admin/commons.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 52 additions & 0 deletions admin/oauth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

defined('PHPWG_ROOT_PATH') or exit(1);

use MediaWiki\OAuthClient\Client;
use MediaWiki\OAuthClient\ClientConfig;
use MediaWiki\OAuthClient\Consumer;
use MediaWiki\OAuthClient\Token;

check_status(ACCESS_ADMINISTRATOR);
if (!isset($conf[WIKIMEDIACOMMONS_ID]))
{
access_denied();
}

$conf[WIKIMEDIACOMMONS_ID] = safe_unserialize($conf[WIKIMEDIACOMMONS_ID]);

$oauth_client_conf = new ClientConfig($conf[WIKIMEDIACOMMONS_ID]['endpoint']);
$oauth_client_conf->setConsumer(new Consumer(
$conf[WIKIMEDIACOMMONS_ID]['key'],
$conf[WIKIMEDIACOMMONS_ID]['secret']
));
$oauth_client_conf->setUserAgent(get_root_url());
$client = new Client($oauth_client_conf);

if (isset($_GET['logout'])) {
userprefs_update_param(WIKIMEDIACOMMONS_ID, null);
$logged_out_msg = 'You have been logged out of Wikimedia Commons.';
$_SESSION['page_infos'][] = l10n($logged_out_msg);
redirect(get_root_url().'admin.php');
}

if (!isset($_GET['oauth_verifier'])) {
// Send them off to Commons to authorise Piwigo.
list( $auth_url, $request_token ) = $client->initiate();
$_SESSION['request_secret'] = $request_token->secret;
redirect($auth_url);

} else {
// When they come back, reconstruct the token from the session,
// and fetch an access token to store.
$request_token = new Token($_GET['oauth_token'], $_SESSION['request_secret']);
$access_token = $client->complete( $request_token, $_GET['oauth_verifier'] );
$ident = $client->identify( $access_token );
userprefs_update_param(WIKIMEDIACOMMONS_ID, array(
'username' => $ident->username,
'access_key' => $access_token->key,
'access_secret' => $access_token->secret,
));
$_SESSION['page_infos'][] = l10n('You are now logged in to Wikimedia Commons.');
redirect(get_root_url().'admin.php');
}
Loading
Loading