Hiong3-eng5 has uploaded a new change for review.
https://gerrit.wikimedia.org/r/70325
Change subject: SpecialPage:OWDownloads
......................................................................
SpecialPage:OWDownloads
* issue: the updated date might be a little off by the nature of the job queue.
* Kindly check for corrections, merge if ok.
Change-Id: If21ee9db67d025891ee851e68a96646124dbe14a
---
M App.php
A includes/jobs/OWExpressionListJob.php
A includes/setup/OWSpecials.php
R includes/specials/SpecialDatasearch.php
A includes/specials/SpecialOWDownloads.php
R includes/specials/SpecialOWStatistics.php
6 files changed, 402 insertions(+), 44 deletions(-)
git pull
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikiLexicalData
refs/changes/25/70325/1
diff --git a/App.php b/App.php
index 018b151..bee04bd 100644
--- a/App.php
+++ b/App.php
@@ -2,11 +2,39 @@
if ( !defined( 'MEDIAWIKI' ) ) die( 'Invalid entry point.' );
-$dir = dirname( __FILE__ ) . '/';
+$dir = dirname( __FILE__ );
-require_once( $dir . 'OmegaWiki/Wikidata.php' );
-require_once( $dir . 'SpecialLanguages.php' );
-require_once( $dir . 'includes/api/OmegaWikiExt.php');
+if ( preg_match( '/\//', $dir ) ) {
+ $slash = '/';
+} else {
+ $slash = '\\';
+}
+
+$dir = $dir . $slash;
+
+$wldScriptPath = dirname( __FILE__ ) . $slash;
+$owScriptPath = $wldScriptPath . "OmegaWiki" . $slash;
+$wldDownloadScriptPath = $IP . $slash . "downloads" . $slash;
+$wldIncludesScriptPath = $wldScriptPath . "includes" . $slash;
+$wldSpecialsScriptPath = $wldIncludesScriptPath . "specials" . $slash;
+$wldAPIScriptPath = $wldIncludesScriptPath . "api" . $slash;
+$wldSetupScriptPath = $wldIncludesScriptPath . "setup" . $slash;
+$wldJobsScriptPath = $wldIncludesScriptPath . "jobs" . $slash;
+
+require_once( $owScriptPath . 'Wikidata.php' );
+require_once( $wldScriptPath . 'SpecialLanguages.php' );
+
+/** The broken API,
+ * $wgAPIModules['wikidata'] = 'ApiWikiData';
+ *
+ * The broken api wikidata is disabled since it does not work
+ * $wgAutoloadClasses['ApiWikiData' ] = $dir .
'includes/api/ApiWikiData.php';
+ * $wgAutoloadClasses['ApiWikiDataFormatBase'] = $dir .
'includes/api/ApiWikiDataFormatBase.php';
+ * $wgAutoloadClasses['ApiWikiDataFormatXml' ] = $dir .
'includes/api/ApiWikiDataFormatXml.php';
+ */
+
+// Current API
+require_once( $wldAPIScriptPath . 'OmegaWikiExt.php');
$wgExtensionCredits['other'][] = array(
'path' => __FILE__,
@@ -23,24 +51,7 @@
'descriptionmsg' => 'wikidata-desc',
);
-$wgExtensionCredits['specialpage'][] = array(
- 'name' => 'SpecialConceptMapping',
- 'author' => 'Kim Bruning',
-);
-
-$wgExtensionCredits['specialpage'][] = array(
- 'name' => 'SpecialCopy',
- 'author' => 'Alan Smithee',
-);
-
-/* the API "wikidata" does not work.
- * Should be repaired, if someone has the courage to go through
- * the undocumented code...
- */
-// $wgAPIModules['wikidata'] = 'ApiWikiData';
-
$wgExtensionMessagesFiles['Wikidata'] = $dir . 'Wikidata.i18n.php';
-
// Resource modules
@@ -73,12 +84,7 @@
$wgAutoloadClasses['WikidataArticle' ] = $dir .
'includes/WikidataArticle.php';
$wgAutoloadClasses['WikidataEditPage' ] = $dir .
'includes/WikidataEditPage.php';
-$wgAutoloadClasses['WikidataPageHistory' ] = $dir .
'includes/WikidataPageHistory.php';
-// api wikidata is disabled since it does not work
-// $wgAutoloadClasses['ApiWikiData' ] = $dir .
'includes/api/ApiWikiData.php';
-// $wgAutoloadClasses['ApiWikiDataFormatBase'] = $dir .
'includes/api/ApiWikiDataFormatBase.php';
-// $wgAutoloadClasses['ApiWikiDataFormatXml' ] = $dir .
'includes/api/ApiWikiDataFormatXml.php';
-
+$wgAutoloadClasses['WikidataPageHistory' ] = $dir .
'includes/WikidataPageHistoxry.php';
# FIXME: Rename this to reduce chance of collision.
$wgAutoloadClasses['OmegaWiki'] = $dir . 'OmegaWiki/OmegaWiki.php';
@@ -91,15 +97,14 @@
$wgAutoloadClasses['SpecialSuggest'] = $dir . 'OmegaWiki/SpecialSuggest.php';
$wgAutoloadClasses['SpecialSelect'] = $dir . 'OmegaWiki/SpecialSelect.php';
-$wgAutoloadClasses['SpecialDatasearch'] = $dir .
'OmegaWiki/SpecialDatasearch.php';
// $wgAutoloadClasses['SpecialTransaction'] = $dir .
'OmegaWiki/SpecialTransaction.php';
$wgAutoloadClasses['SpecialNeedsTranslation'] = $dir .
'OmegaWiki/SpecialNeedsTranslation.php';
$wgAutoloadClasses['SpecialImportLangNames'] = $dir .
'OmegaWiki/SpecialImportLangNames.php';
$wgAutoloadClasses['SpecialAddCollection'] = $dir .
'OmegaWiki/SpecialAddCollection.php';
$wgAutoloadClasses['SpecialConceptMapping'] = $dir .
'OmegaWiki/SpecialConceptMapping.php';
$wgAutoloadClasses['SpecialCopy'] = $dir . 'OmegaWiki/SpecialCopy.php';
-$wgAutoloadClasses['SpecialOWStatistics'] = $dir .
'OmegaWiki/SpecialOWStatistics.php';
+require_once( $wldSetupScriptPath . "OWSpecials.php" );
# FIXME: These should be modified to make Wikidata more reusable.
$wgAvailableRights[] = 'editwikidata-uw';
@@ -164,17 +169,14 @@
# in a single database.
if ( !isset( $wdSiteContext ) ) $wdSiteContext = "uw";
-
$wgSpecialPages['Suggest'] = 'SpecialSuggest';
$wgSpecialPages['Select'] = 'SpecialSelect';
-$wgSpecialPages['Datasearch'] = 'SpecialDatasearch';
// $wgSpecialPages['Transaction'] = 'SpecialTransaction';
$wgSpecialPages['NeedsTranslation'] = 'SpecialNeedsTranslation';
$wgSpecialPages['ImportLangNames'] = 'SpecialImportLangNames';
$wgSpecialPages['AddCollection'] = 'SpecialAddCollection';
$wgSpecialPages['ConceptMapping'] = 'SpecialConceptMapping';
$wgSpecialPages['Copy'] = 'SpecialCopy';
-$wgSpecialPages['ow_statistics'] = 'SpecialOWStatistics';
#
## Hooks
@@ -191,6 +193,10 @@
$wgHooks['PageContentLanguage'][] =
'WikiLexicalDataHooks::onPageContentLanguage';
$wgHooks['SkinTemplateNavigation'][] =
'WikiLexicalDataHooks::onSkinTemplateNavigation';
+// Job Classes
+$wgAutoloadClasses['CreateExpressionListJob'] = $wldJobsScriptPath .
'OWExpressionListJob.php';
+$wgJobClasses['CreateExpressionList'] = 'CreateExpressionListJob';
+
// LocalApp.php is optional. Its function is like LocalSettings.php,
// if you want to separate the MediaWiki configuration from the Wikidata
configuration
if ( file_exists ( dirname(__FILE__) . "LocalApp.php" )) {
diff --git a/includes/jobs/OWExpressionListJob.php
b/includes/jobs/OWExpressionListJob.php
new file mode 100644
index 0000000..1d3ed2f
--- /dev/null
+++ b/includes/jobs/OWExpressionListJob.php
@@ -0,0 +1,164 @@
+<?php
+/**
+ * Create a list of Expressions
+ *
+ */
+global $owScriptPath;
+require_once( $owScriptPath . 'languages.php' );
+
+Class CreateExpressionListJob extends Job {
+
+ public function __construct( $title, $params ) {
+ parent::__construct( 'CreateExpressionList', $title, $params );
+ }
+
+ /**
+ * Execute the job
+ *
+ * @return bool
+ */
+ public function run() {
+ // Load data from $this->params and $this->title
+ if ( isset( $this->params['langcode'] ) ) {
+ $languageId = $this->params['langcode'];
+ }
+
+ if ( isset( $this->params['type'] ) ) {
+ $type = $this->params['type'];
+ }
+
+ if ( isset( $this->params['format'] ) ) {
+ $format = $this->params['format'];
+ }
+
+ if ( $languageId && $format ) {
+ // $this->createList( 'exp', 'fr', 'csv' );
+ $this->createList( $type, $languageId, $format );
+ return true;
+ }
+
+ // Perform your updates
+
+ return false;
+ }
+
+ protected function createList( $type, $code, $format ) {
+ global $wldDownloadScriptPath;
+
+ // language specifics
+ // Question: What if there is no wikimedia code for the
+ // language?
+ $languageId = getLanguageIdForCode( $code );
+ $languageExpressions = getLanguageIdExpressions( $languageId );
+
+ // create File name
+ $fileName = $wldDownloadScriptPath;
+ $fileName .= $type . '_' . $code . '.' . $format;
+
+ // When someone updates the file while someone is
+ // downloading the file, the file may ( in my mind ),
+ // be corrupted. So process it first as a temporary file,
+ // delete the original file, and rename the temporary file ~he
+ $tempFileName = UIDGenerator::newUUIDv4();
+
+ $fh = fopen( $tempFileName, 'w' );
+ fwrite( $fh, '"Expressions"' . "\n" );
+ foreach( $languageExpressions as $row ) {
+ $spelling = formatCSVcolumn( $row->spelling );
+ fwrite( $fh, $spelling . "\n" );
+ }
+ fclose( $fh );
+
+ if ( file_exists( $fileName ) ) {
+ unlink( $fileName );
+ }
+ rename( $tempFileName, $fileName );
+ }
+
+}
+
+/**
+ * Simple formatting
+ */
+function formatCSVcolumn( $column ) {
+
+ if( is_numeric( $column ) ) {
+ return $column;
+ }
+ if( preg_match( '/\"/', $column ) ) {
+ $column = str_replace( '"', '""', $column );
+ }
+ return '"' . $column . '"';
+}
+
+/** Get language code
+ *
+ * returns null if code does not exist
+ */
+function getLanguageIdCode( $languageId ) {
+ $dbr = wfGetDB( DB_SLAVE );
+
+ // get wikimedia_key
+ $languageId = $dbr->selectField(
+ 'language',
+ 'wikimedia_key',
+ array(
+ 'language_id' => $languageId
+ ), __METHOD__
+ );
+
+ if ( $languageId ) {
+ return $code;
+ }
+
+ // else get iso639_3
+ $languageId = $dbr->selectField(
+ 'language',
+ 'wikimedia_key',
+ array(
+ 'language_id' => $languageId
+ ), __METHOD__
+ );
+
+ if ( $languageId ) {
+ return $code;
+ }
+
+ return null;
+}
+
+/**
+ * returns an array of "Expression" objects
+ * for a language
+ *
+ * else returns null
+ */
+function getLanguageIdExpressions( $languageId, $dc = null ) {
+ if ( is_null( $dc ) ) {
+ $dc = wdGetDataSetContext();
+ }
+ $dbr = wfGetDB( DB_SLAVE );
+
+ $queryResult = $dbr->select(
+ "{$dc}_expression",
+ 'spelling',
+ array(
+ 'language_id' => $languageId,
+ 'remove_transaction_id' => null
+ ),
+ __METHOD__,
+ array(
+ 'ORDER BY' => 'spelling'
+ )
+ );
+
+ $expression = array();
+ foreach ( $queryResult as $exp ) {
+ $expression[] = $exp;
+ }
+
+ if ( $expression ) {
+ return $expression;
+ }
+ return null;
+}
diff --git a/includes/setup/OWSpecials.php b/includes/setup/OWSpecials.php
new file mode 100644
index 0000000..122522e
--- /dev/null
+++ b/includes/setup/OWSpecials.php
@@ -0,0 +1,58 @@
+<?php
+/**
+ * A Special Page where one can find files to download
+ */
+global $wldScriptPath, $wldSpecialsScriptPath;
+
+ # Alert the user that this is not a valid access point to MediaWiki if they
try to access the special pages file directly.
+if ( !defined( 'MEDIAWIKI' ) ) {
+ echo
+ 'To install my extension, put the following line in LocalSettings.php:'
.
+ "\n" .
+ 'require_once( "' . $wldScriptPath . 'OWSpecials.php" );
+ ';
+ exit( 1 );
+}
+
+$wgExtensionCredits['specialpage'][] = array(
+ 'name' => 'SpecialConceptMapping',
+ 'author' => 'Kim Bruning',
+);
+
+$wgExtensionCredits['specialpage'][] = array(
+ 'name' => 'SpecialCopy',
+ 'author' => 'Alan Smithee',
+);
+
+$wgExtensionCredits['specialpage'][] = array(
+ 'name' => 'SpecialDatasearch',
+ 'author' => 'Kipcool',
+);
+
+$wgExtensionCredits['specialpage'][] = array(
+ 'name' => 'SpecialOWStatistics',
+ 'author' => 'Kipcool',
+);
+
+$wgExtensionCredits['specialpage'][] = array(
+ 'name' => 'SpecialOWDownloads',
+ 'author' => 'Hiong3-eng5',
+);
+
+# Location of the SpecialMyExtension class (Tell MediaWiki to load this file)
+$wgAutoloadClasses['SpecialDatasearch'] = $wldSpecialsScriptPath .
'SpecialDatasearch.php';
+$wgAutoloadClasses['SpecialOWStatistics'] = $wldSpecialsScriptPath .
'SpecialOWStatistics.php';
+$wgAutoloadClasses[ 'SpecialOWDownloads' ] = $wldSpecialsScriptPath .
'SpecialOWDownloads.php';
+
+# Tell MediaWiki about the new special page and its class name
+$wgSpecialPages['ow_data_search'] = 'SpecialDatasearch';
+$wgSpecialPages['ow_statistics'] = 'SpecialOWStatistics';
+$wgSpecialPages['ow_downloads'] = 'SpecialOWDownloads';
+
+# Tell MediaWiki about which group the new special page belongs to
+$wgSpecialPageGroups[ 'ow_data_search' ] = 'wiki';
+$wgSpecialPageGroups[ 'ow_statistics' ] = 'wiki';
+$wgSpecialPageGroups[ 'ow_downloads' ] = 'wiki';
+
+# Location of an aliases file (Tell MediaWiki to load this file)
+//$wgExtensionMessagesFiles[ 'OWSpecialsAlias' ] = #dir .
'../i18n/OWSpecials.alias.php';
diff --git a/OmegaWiki/SpecialDatasearch.php
b/includes/specials/SpecialDatasearch.php
similarity index 95%
rename from OmegaWiki/SpecialDatasearch.php
rename to includes/specials/SpecialDatasearch.php
index 81d943e..90736ee 100644
--- a/OmegaWiki/SpecialDatasearch.php
+++ b/includes/specials/SpecialDatasearch.php
@@ -2,15 +2,15 @@
if ( !defined( 'MEDIAWIKI' ) ) die();
-require_once( "Wikidata.php" );
-require_once( "WikiDataGlobals.php" );
-require_once( "WikiDataAPI.php" );
-require_once( "forms.php" );
-require_once( "type.php" );
-require_once( "ViewInformation.php" );
-require_once( "OmegaWikiAttributes.php" );
-require_once( "OmegaWikiRecordSets.php" );
-require_once( "OmegaWikiEditors.php" );
+require_once( __DIR__ . "/../../OmegaWiki/Wikidata.php" );
+require_once( __DIR__ . "/../../OmegaWiki/WikiDataGlobals.php" );
+require_once( __DIR__ . "/../../OmegaWiki/WikiDataAPI.php" );
+require_once( __DIR__ . "/../../OmegaWiki/forms.php" );
+require_once( __DIR__ . "/../../OmegaWiki/type.php" );
+require_once( __DIR__ . "/../../OmegaWiki/ViewInformation.php" );
+require_once( __DIR__ . "/../../OmegaWiki/OmegaWikiAttributes.php" );
+require_once( __DIR__ . "/../../OmegaWiki/OmegaWikiRecordSets.php" );
+require_once( __DIR__ . "/../../OmegaWiki/OmegaWikiEditors.php" );
class SpecialDatasearch extends SpecialPage {
protected $externalIdentifierAttribute;
@@ -39,7 +39,7 @@
private $show;
function SpecialDatasearch() {
- parent::__construct( 'DataSearch' );
+ parent::__construct( 'ow_data_search' );
$request = $this->getRequest();
$this->collectionId = $request->getInt( "collection" ); //
default is 0
diff --git a/includes/specials/SpecialOWDownloads.php
b/includes/specials/SpecialOWDownloads.php
new file mode 100644
index 0000000..b7bc590
--- /dev/null
+++ b/includes/specials/SpecialOWDownloads.php
@@ -0,0 +1,130 @@
+<?php
+/**
+ * Special download page. Currently regenerated files are formatted
+ * using standard csv with headers and commas as separator and quotation
+ * marks, when appropriate as text delimiter (but actually, I guess the
+ * all non numeric fields are text delimited, to be remedied in future
+ * revisions).
+ *
+ * Question:How to deal with language without a wikimedia key? ~he
+ */
+global $wldJobsScriptPath;
+require_once( $wldJobsScriptPath . 'OWExpressionListJob.php');
+
+class SpecialOWDownloads extends SpecialPage {
+
+ function __construct() {
+ parent::__construct( 'ow_downloads' );
+ }
+
+ function execute( $par ) {
+ global $wldDownloadScriptPath;
+ $request = $this->getRequest();
+ $output = $this->getOutput();
+ $this->setHeaders();
+
+ # Get requests
+ $fileName = $request->getText( 'update' );
+
+ $wikitext = array();
+ $definitions = array();
+ $expressions = array();
+ $translations = array();
+
+ $downloadDir = scandir( $wldDownloadScriptPath ) ;
+
+ if ( $fileName ) {
+ // update Expression
+ if ( preg_match( '/^exp_/', $fileName ) ) {
+ preg_match( '/_(.+)\.(.+)/', $fileName, $match
);
+ $jobParams = array( 'type' => 'exp', 'langcode'
=> $match[1], 'format' => $match[2] );
+ $title =
Title::newFromText('User:jobQuery/CreateExpressionList');
+ $job = new CreateExpressionListJob( $title,
$jobParams );
+ JobQueueGroup::singleton()->push( $job ); //
mediawiki >= 1.21
+ // $job->insert(); // mediawiki < 1.21
+ }
+ }
+
+ // Segregate by groups
+ foreach( $downloadDir as $files ) {
+ // definitions
+ if ( preg_match( '/^def_/', $files ) ) {
+ $definitions[] = "$files";
+ }
+ // definitions
+ if ( preg_match( '/^exp_/', $files ) ) {
+ $expressions[] = "$files";
+ }
+ // translations
+ if ( preg_match( '/^trans_/', $files ) ) {
+ $translations[] = "$files";
+ }
+ }
+
+ // Process Definitions
+ if ( $definitions ) {
+ $wikitext[] = '===Definitions===';
+ $myLine = $this->processText( $definitions );
+ $wikitext[] = $myLine . "\n|}\n";
+ }
+
+ // Process Expressions
+ if ( $expressions ) {
+ $wikitext[] = '====List of Expressions====';
+ $myLine = $this->processText( $expressions );
+ $wikitext[] = $myLine . "\n|}\n";
+ }
+
+ // Process Translations
+ if ( $translations ) {
+ $wikitext[] = '===Translations===';
+ $myLine = $this->processText( $translations );
+ $wikitext[] = $myLine . "\n|}\n";
+ }
+
+ // output wikitext
+ foreach( $wikitext as $wikilines ) {
+ $output->addWikiText( $wikilines );
+ }
+ }
+
+ protected function processText( $text ) {
+ global $wgScriptPath, $wldDownloadScriptPath;
+
+ $presetMyLine = "{| class=\"wikitable\"\n! Language !! Date !!
Size (bytes) !! Action \n|-\n";
+ $myLine = $presetMyLine;
+ foreach( $text as $line ) {
+ if ( preg_match( '/^exp_(.+)\./', $line, $match ) ) {
+ $language = $match[1];
+ }
+ $languageId = getLanguageIdForCode( $language );
+ // TODO: How to internationalize $nameLanguageId?
+ $nameLanguageId = WLD_ENGLISH_LANG_ID;
+
+ $languageName = getLanguageIdLanguageNameFromIds(
$languageId, $nameLanguageId );
+
+ $wikiRow = '';
+ // if( $myLine != "{|\n" ) {
+ if( $myLine != $presetMyLine ) {
+ $wikiRow = "|-\n";
+ }
+ $myLink = '[http://' . $_SERVER['HTTP_HOST'] .
$wgScriptPath . "/downloads/$line $languageName]";
+ $myLine .= "$wikiRow| $myLink ";
+ $filestats = stat( $wldDownloadScriptPath . $line );
+
+ $date = new DateTime();
+ $date->setTimestamp( $filestats['mtime'] );
+ $dateTime = $date->format('Y-m-d H:i:s') . "\n";
+
+ $myLine .= "|| " . $dateTime . " ";
+ $myLine .= "| align=\"right\" | " . $filestats['size']
. " ";
+
+ // ability to update the file
+ // TO CHECK: Check if the construction of this URL is
correct
+ $myLine .= '|| [http://' . $_SERVER['HTTP_HOST'] .
$wgScriptPath . '/index.php?title=Special:Ow_downloads&update=' . $line . '
Regenerate]';
+ $myLine .= "\n";
+ }
+ return $myLine;
+ }
+
+}
diff --git a/OmegaWiki/SpecialOWStatistics.php
b/includes/specials/SpecialOWStatistics.php
similarity index 98%
rename from OmegaWiki/SpecialOWStatistics.php
rename to includes/specials/SpecialOWStatistics.php
index 06419ab..4ff47f3 100644
--- a/OmegaWiki/SpecialOWStatistics.php
+++ b/includes/specials/SpecialOWStatistics.php
@@ -1,8 +1,8 @@
<?php
if ( !defined( 'MEDIAWIKI' ) ) die();
-require_once( "Wikidata.php" );
-require_once( 'languages.php' );
+require_once( __DIR__ . '/../../OmegaWiki/Wikidata.php' );
+require_once( __DIR__ . '/../../OmegaWiki/languages.php' );
class SpecialOWStatistics extends SpecialPage {
function __construct() {
--
To view, visit https://gerrit.wikimedia.org/r/70325
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: If21ee9db67d025891ee851e68a96646124dbe14a
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikiLexicalData
Gerrit-Branch: master
Gerrit-Owner: Hiong3-eng5 <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits