samwilson /
Piwigo2MediaWiki
| 1 | <?php |
||
| 2 | |||
| 3 | // Make sure we're already in Piwigo. |
||
| 4 | defined('PHPWG_ROOT_PATH') or exit(1); |
||
|
0 ignored issues
–
show
|
|||
| 5 | |||
| 6 | require_once __DIR__.'/vendor/autoload.php'; |
||
| 7 | |||
| 8 | use Mediawiki\Api\ApiUser; |
||
| 9 | use Mediawiki\Api\FluentRequest; |
||
| 10 | use Mediawiki\Api\MediawikiApi; |
||
| 11 | use Mediawiki\Api\UsageException; |
||
| 12 | |||
| 13 | // Prepare the template. |
||
| 14 | $url = isset($_REQUEST['url']) ? $_REQUEST['url'] : ''; |
||
| 15 | $p2mConf = isset($conf[PIWIGO2MEDIAWIKI_ID]) |
||
| 16 | ? $conf[PIWIGO2MEDIAWIKI_ID] |
||
| 17 | : array(); |
||
| 18 | $template->assign(array( |
||
| 19 | 'admin_url' => PIWIGO2MEDIAWIKI_ADMIN, |
||
| 20 | 'piwigo2mediawiki_page' => PIWIGO2MEDIAWIKI_PAGE, |
||
| 21 | 'mediawiki_url' => $url, |
||
| 22 | 'p2m_conf' => $p2mConf, |
||
| 23 | )); |
||
| 24 | |||
| 25 | // Delete if requested. |
||
| 26 | if (isset($_POST['action']) && $_POST['action']==='delete' |
||
| 27 | && isset($_POST['id']) && isset($p2mConf[$_POST['id']]) |
||
| 28 | ) { |
||
| 29 | unset($p2mConf[$_POST['id']]); |
||
| 30 | conf_update_param(PIWIGO2MEDIAWIKI_ID, $p2mConf); |
||
| 31 | redirect(PIWIGO2MEDIAWIKI_ADMIN); |
||
| 32 | } |
||
| 33 | |||
| 34 | // Load one wiki's data for editing if an ID is specified. |
||
| 35 | if (isset($_REQUEST['id']) && isset($p2mConf[$_REQUEST['id']])) { |
||
| 36 | $info = $p2mConf[$_REQUEST['id']]; |
||
| 37 | $info['id'] = $_REQUEST['id']; |
||
| 38 | $template->assign(array( |
||
| 39 | 'info' => $info, |
||
| 40 | )); |
||
| 41 | } |
||
| 42 | |||
| 43 | // Save (create or edit) a single wiki's data. |
||
| 44 | if ($url && isset($_POST['action']) && $_POST['action'] === 'add' |
||
| 45 | && isset($_POST['username']) && $_POST['username'] |
||
| 46 | && isset($_POST['password']) && $_POST['password'] |
||
| 47 | ) { |
||
| 48 | // Find the API URL or show an error. |
||
| 49 | $validUrl = true; |
||
| 50 | try { |
||
| 51 | $api = MediawikiApi::newFromPage($url); |
||
| 52 | $url = $api->getApiUrl(); |
||
| 53 | } catch (Exception $exception) { |
||
| 54 | $msg = l10n( |
||
| 55 | 'MediaWiki API discovery failed for: %s<br />Error: %s', |
||
| 56 | $url, |
||
| 57 | $exception->getMessage() |
||
| 58 | ); |
||
| 59 | $template->assign(array( |
||
| 60 | 'warnings' => $msg, |
||
| 61 | 'info' => $_POST, |
||
| 62 | )); |
||
| 63 | $validUrl = false; |
||
| 64 | } |
||
| 65 | // If we've got a valid API URL, find the site name and save all data. |
||
| 66 | if ($validUrl) { |
||
| 67 | |||
| 68 | $username = $_POST['username']; |
||
| 69 | $password = $_POST['password']; |
||
| 70 | |||
| 71 | $loggedIn = false; |
||
| 72 | try |
||
| 73 | { |
||
| 74 | $loggedIn = $api->login(new ApiUser($username, $password)); |
||
| 75 | } catch (UsageException $e) { |
||
| 76 | $msg = l10n('Authentication failed.<br />Error: %s', $e->getMessage()); |
||
| 77 | $template->assign(array( |
||
| 78 | 'warnings' => $msg, |
||
| 79 | 'info' => $_POST, |
||
| 80 | )); |
||
| 81 | } |
||
| 82 | |||
| 83 | // If logging in worked, get some more information and save the config. |
||
| 84 | if ($loggedIn) |
||
| 85 | { |
||
| 86 | $siteInfo = $api->getRequest( |
||
| 87 | FluentRequest::factory() |
||
| 88 | ->setAction('query') |
||
| 89 | ->setParam('meta', 'siteinfo') |
||
| 90 | ); |
||
| 91 | $p2mConf[$info['id']] = array( |
||
| 92 | 'sitename' => $siteInfo['query']['general']['sitename'], |
||
| 93 | 'url' => $url, |
||
| 94 | 'username' => $username, |
||
| 95 | 'password' => $password, |
||
| 96 | 'wikitext' => isset($_POST['wikitext']) ? $_POST['wikitext'] : '', |
||
| 97 | ); |
||
| 98 | conf_update_param(PIWIGO2MEDIAWIKI_ID, $p2mConf); |
||
| 99 | redirect(PIWIGO2MEDIAWIKI_ADMIN); |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | } |
||
| 104 | |||
| 105 | $template_handle = PIWIGO2MEDIAWIKI_ID.'admin'; |
||
| 106 | $template_file = PIWIGO2MEDIAWIKI_DIR.'admin.tpl'; |
||
| 107 | $template->set_filename($template_handle, $template_file); |
||
| 108 | $template->assign_var_from_handle('ADMIN_CONTENT', $template_handle); |
||
| 109 |
PHP has two types of connecting operators (logical operators, and boolean operators):
and&&or||The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&, or||.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
dieintroduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrowat this point:These limitations lead to logical operators rarely being of use in current PHP code.