Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adds a convenience method that validates a MediaWiki API URL and endp…

…oint.
  • Loading branch information...
commit 5bd3a2a56bdc6a1eeaf2c26d7fe0ef73671d7f05 1 parent 67b7445
Jim Safley authored
Showing with 38 additions and 5 deletions.
  1. +7 −2 lib/Scripto.php
  2. +31 −3 lib/Scripto/Service/MediaWiki.php
View
9 lib/Scripto.php
@@ -11,6 +11,11 @@
require_once 'Scripto/Exception.php';
/**
+ * Require Scripto_Service_MediaWiki.
+ */
+require_once 'Scripto/Service/MediaWiki.php';
+
+/**
* Represents a Scripto application. Serves as a connector object between the
* external system API and MediaWiki API.
*
@@ -58,7 +63,7 @@ public function __construct(Scripto_Adapter_Interface $adapter, $mediawiki)
if (!isset($mediawiki['pass_cookies'])) {
$mediawiki['pass_cookies'] = true;
}
- require_once 'Scripto/Service/MediaWiki.php';
+
$this->_mediawiki = new Scripto_Service_MediaWiki($mediawiki['api_url'],
$mediawiki['db_name'],
(bool) $mediawiki['pass_cookies']);
@@ -405,5 +410,5 @@ public function getRevision($revisionId)
'wikitext' => $page['revisions'][0]['*'],
'html' => $response['parse']['text']['*']);
return $revision;
- }
+ }
}
View
34 lib/Scripto/Service/MediaWiki.php
@@ -89,14 +89,13 @@ class Scripto_Service_MediaWiki extends Zend_Service_Abstract
/**
* Constructs the MediaWiki API client.
*
+ * @link http://www.mediawiki.org/wiki/API:Main_page
* @param string $apiUrl The URL to the MediaWiki API.
* @param string $dbName The name of the MediaWiki database.
* @param bool $passCookies Pass cookies to the web browser.
*/
public function __construct($apiUrl, $dbName, $passCookies = true)
{
- require_once 'Zend/Http/Cookie.php';
-
$this->_dbName = (string) $dbName;
$this->_passCookies = (bool) $passCookies;
@@ -109,6 +108,7 @@ public function __construct($apiUrl, $dbName, $passCookies = true)
// from the browser and add them to the HTTP client cookie jar. Doing so
// maintains state between browser requests.
if ($this->_passCookies) {
+ require_once 'Zend/Http/Cookie.php';
foreach ($this->_cookieSuffixes as $cookieSuffix) {
$cookieName = self::COOKIE_PREFIX . $this->_dbName . $cookieSuffix;
if (array_key_exists($cookieName, $_COOKIE)) {
@@ -122,7 +122,7 @@ public function __construct($apiUrl, $dbName, $passCookies = true)
}
/**
- * Gets information about the currently logged-in user.
+ * Gets information about the current user.
*
* @link http://www.mediawiki.org/wiki/API:Meta#userinfo_.2F_ui
* @param string $uiprop
@@ -570,4 +570,32 @@ protected function _request($action, array $params = array())
}
return $response;
}
+
+ /**
+ * Determine whether the provided MediaWiki API URL is valid.
+ *
+ * @param string $apiUrl
+ * @return bool
+ */
+ static public function isValidApiUrl($apiUrl)
+ {
+ // Check for valid API URL string.
+ if (!Zend_Uri::check($apiUrl) || !preg_match('#/api\.php$#', $apiUrl)) {
+ return false;
+ }
+
+ // Ping the API endpoint for a valid response.
+ $response = self::getHttpClient()->setUri($apiUrl)
+ ->setParameterPost('action', 'query')
+ ->setParameterPost('meta', 'siteinfo')
+ ->setParameterPost('format', 'json')
+ ->request('POST')->getBody();
+
+ $response = json_decode($response, true);
+ if (!is_array($response) || !isset($response['query']['general'])) {
+ return false;
+ }
+
+ return true;
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.