Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F4506
patch.patch
Public
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Authored By
•
bzimport
Nov 21 2014, 9:59 PM
2014-11-21 21:59:09 (UTC+0)
Size
8 KB
Referenced Files
None
Subscribers
None
patch.patch
View Options
Index: SiteMatrix.php
===================================================================
--- SiteMatrix.php (revision 26031)
+++ SiteMatrix.php (working copy)
@@ -21,3 +21,5 @@
}
extAddSpecialPage( dirname(__FILE__) . '/SiteMatrix_body.php', 'SiteMatrix', 'SiteMatrixPage' );
+$wgAutoloadClasses['ApiQuerySiteMatrix'] = dirname( __FILE__ ) . '/SiteMatrix_body.php';
+$wgAPIModules['sitematrix'] = 'ApiQuerySiteMatrix';
Index: SiteMatrix_body.php
===================================================================
--- SiteMatrix_body.php (revision 26031)
+++ SiteMatrix_body.php (working copy)
@@ -14,21 +14,20 @@
global $IP;
require_once( $IP.'/languages/Names.php' );
-class SiteMatrixPage extends SpecialPage {
-
- function SiteMatrixPage() {
- SpecialPage::SpecialPage('SiteMatrix');
- }
-
- function execute( $par ) {
- global $wgOut, $wgLocalDatabases;
- $this->setHeaders();
-
- $langlist = array_map( 'trim', file( '/home/wikipedia/common/langlist' ) );
- sort( $langlist );
- $xLanglist = array_flip( $langlist );
-
- $sites = array(
+class SiteMatrix
+{
+ public $langlist, $sites, $names, $hosts;
+ public $wikipediaSpecial, $hidden, $specials, $matrix;
+
+ public function __construct()
+ {
+ global $wgLocalDatabases, $IP;
+
+ $this->langlist = array_map( 'trim', file( '/home/wikipedia/common/langlist' ) );
+ sort( $this->langlist );
+ $xLanglist = array_flip( $this->langlist );
+
+ $this->sites = array(
'wiki',
'wiktionary',
'wikibooks',
@@ -37,7 +36,7 @@
'wikiquote',
'wikiversity',
);
- $names = array(
+ $this->names = array(
'wiki' => 'Wikipedia<br />w',
'wiktionary' => 'Wiktionary<br />wikt',
'wikibooks' => 'Wikibooks<br />b',
@@ -46,7 +45,7 @@
'wikisource' => 'Wikisource<br />s',
'wikiversity' => 'Wikiversity<br />v',
);
- $hosts = array(
+ $this->hosts = array(
'wiki' => 'wikipedia.org',
'wiktionary' => 'wiktionary.org',
'wikibooks' => 'wikibooks.org',
@@ -57,58 +56,107 @@
);
# Special wikis that should point to wikiPedia, not wikiMedia
- $wikipediaSpecial = array(
+ $this->wikipediaSpecial = array(
'dk', 'sources', 'species', 'test',
);
# Some internal databases for other domains.
- $hidden = array(
+ $this->hidden = array(
'foundation', 'mediawiki',
);
-
+
# Tabulate the matrix
- $specials = array();
- $matrix = array();
+ $this->specials = array();
+ $this->matrix = array();
foreach( $wgLocalDatabases as $db ) {
# Find suffix
- foreach ( $sites as $site ) {
+ foreach ( $this->sites as $site ) {
$m = array();
if ( preg_match( "/(.*)$site\$/", $db, $m ) ) {
$lang = str_replace( '_', '-', $m[1] );
if ( !isset( $xLanglist[$lang] ) && $site == 'wiki' ) {
- $specials[] = $lang;
+ $this->specials[] = $lang;
} else {
- $matrix[$site][$lang] = 1;
+ $this->matrix[$site][$lang] = 1;
}
break;
}
}
}
+ }
+}
+class SiteMatrixPage extends SpecialPage {
+
+ function SiteMatrixPage() {
+ SpecialPage::SpecialPage('SiteMatrix');
+ }
+
+ function execute( $par ) {
+ global $wgOut, $wgRequest, $wgLanguageNames;
+ $this->setHeaders();
+
+ $matrix = new SiteMatrix();
+
+ if ($wgRequest->getVal( 'action' ) == "raw")
+ {
+ $wgOut->disable();
+ header("Content-Type: text/xml; charset=utf-8");
+ echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
+ echo "<sitematrix>\n";
+ echo "\t<matrix>\n";
+ foreach ( $matrix->langlist as $lang ) {
+ $langhost = str_replace("_", "-", $lang);
+ echo "\t\t<language code=\"{$langhost}\" name=\"".htmlspecialchars($wgLanguageNames[$lang])."\">\n";
+ foreach ( $matrix->sites as $site ) {
+ if ( isset($matrix->matrix[$site][$lang]) ) {
+ $url = "http://{$langhost}.{$matrix->hosts[$site]}/";
+ echo "\t\t\t<site code=\"{$site}\" url=\"{$url}\" />\n";
+ }
+ }
+ echo "\t\t</language>\n";
+ }
+ echo "\t</matrix>\n";
+ echo "\t<specials>\n";
+ foreach ( $matrix->specials as $lang ) {
+ if ( in_array($lang, $matrix->hidden) ) {
+ continue;
+ }
+
+ $langhost = str_replace("_", "-", $lang);
+ $domain = in_array($lang, $matrix->wikipediaSpecial) ? ".wikipedia.org" : ".wikimedia.org";
+ $url = "http://{$langhost}{$domain}/";
+
+ echo "\t\t<special code=\"{$langhost}\" url=\"{$url}\" />\n";
+ }
+ echo "\t</specials>\n";
+ echo "</sitematrix>";
+ return;
+ }
+
# Construct the HTML
# Header row
$s = '<table>
<tr>
<th>' . wfMsg( 'sitematrix-language' ) . '</th>
- <th colspan="' . count( $sites ) . '">' . wfMsg( 'sitematrix-project' ) . '</th>
+ <th colspan="' . count( $matrix->sites ) . '">' . wfMsg( 'sitematrix-project' ) . '</th>
</tr><tr>
<th> </th>';
- foreach ( $names as $name ) {
+ foreach ( $matrix->names as $name ) {
$s .= '<th>' . $name . '</th>';
}
$s .= "</tr>\n";
- global $wgLanguageNames;
# Bulk of table
- foreach ( $langlist as $lang ) {
+ foreach ( $matrix->langlist as $lang ) {
$anchor = strtolower( '<a id="' . htmlspecialchars( $lang ) . '" name="' . htmlspecialchars( $lang ) . '"></a>' );
$s .= '<tr>';
$s .= '<td>' . $anchor . '<strong>' . $wgLanguageNames[$lang] . '</strong></td>';
$langhost = str_replace( '_', '-', $lang );
- foreach ( $names as $site => $name ) {
- $url = "http://$langhost." . $hosts[$site] . '/';
- if ( empty( $matrix[$site][$lang] ) ) {
+ foreach ( $matrix->names as $site => $name ) {
+ $url = "http://$langhost." . $matrix->hosts[$site] . '/';
+ if ( empty( $matrix->matrix[$site][$lang] ) ) {
# Non-existent wiki
$s .= '<td><a href="' . $url . '" class="new">' . $lang . '</a></td>';
} else {
@@ -123,17 +171,17 @@
# Specials
$s .= '<h2>' . wfMsg( 'sitematrix-others' ) . '</h2>';
$s .= '<ul>';
- foreach ( $specials as $lang ) {
+ foreach ( $matrix->specials as $lang ) {
# Skip "hidden" databases:
- if( in_array($lang, $hidden) ) {
+ if( in_array($lang, $matrix->hidden) ) {
continue;
}
$langhost = str_replace( '_', '-', $lang );
# Handle special wikipedia projects:
- if( in_array($lang, $wikipediaSpecial) ) {
+ if( in_array($lang, $matrix->wikipediaSpecial) ) {
$domain = '.wikipedia.org';
} else{
$domain = '.wikimedia.org';
@@ -144,3 +192,89 @@
$wgOut->addHTML( $s );
}
}
+
+/**
+ * Query module to get site matrix
+ * @addtogroup API
+ */
+class ApiQuerySiteMatrix extends ApiQueryBase {
+
+ public function __construct($query, $moduleName) {
+ parent :: __construct($query, $moduleName, 'sm');
+ }
+
+ public function execute() {
+ global $wgLanguageNames;
+ $params = $this->extractRequestParams();
+ $result = $this->getResult();
+ $matrix = new SiteMatrix();
+
+ $matrix_out = array();
+ foreach ( $matrix->langlist as $lang ) {
+ $langhost = str_replace("_", "-", $lang);
+ $language = array(
+ 'code' => $langhost,
+ 'name' => $wgLanguageNames[$lang],
+ 'site' => array(),
+ );
+
+ foreach ( $matrix->sites as $site ) {
+ if (isset($matrix->matrix[$site][$lang])) {
+ $url = "http://{$langhost}.{$matrix->hosts[$site]}/";
+ $site_out = array(
+ 'url' => $url,
+ 'code' => $site,
+ );
+ $language['site'][] = $site_out;
+ }
+ }
+
+ $result->setIndexedTagName($language['site'], 'site');
+ $matrix_out[] = $language;
+ }
+ $result->setIndexedTagName($matrix_out, 'language');
+ $result->addValue(null, "matrix", $matrix_out);
+
+ $specials = array();
+ foreach ( $matrix->specials as $lang )
+ {
+ if ( in_array($lang, $matrix->hidden) ) continue;
+
+ $langhost = str_replace( '_', '-', $lang );
+ $domain = in_array($lang, $matrix->wikipediaSpecial) ? ".wikipedia.org" : ".wikimedia.org";
+ $url = "http://{$langhost}{$domain}/";
+
+ $wiki = array();
+ $wiki['url'] = $url;
+ $wiki['code'] = $langhost;
+ $specials[] = $wiki;
+ }
+
+ $result->setIndexedTagName($specials, 'special');
+ $result->addValue(null, "specials", $specials);
+ }
+
+ protected function getAllowedParams() {
+ return array (
+ );
+ }
+
+ protected function getParamDescription() {
+ return array (
+ );
+ }
+
+ protected function getDescription() {
+ return 'Get Wikimedia sites list';
+ }
+
+ protected function getExamples() {
+ return array (
+ 'api.php?action=sitematrix',
+ );
+ }
+
+ public function getVersion() {
+ return __CLASS__ . ': $Id: SiteMatrix_body.php ????? 2007-09-23 20:11:29Z ????? $';
+ }
+}
\ No newline at end of file
File Metadata
Details
Attached
Mime Type
text/x-diff
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
4071
Default Alt Text
patch.patch (8 KB)
Attached To
Mode
T13431: API for Extension:SiteMatrix
Attached
Detach File
Event Timeline
Log In to Comment