http://www.mediawiki.org/wiki/Special:Code/MediaWiki/89188
Revision: 89188
Author: foxtrott
Date: 2011-05-30 21:07:43 +0000 (Mon, 30 May 2011)
Log Message:
-----------
refactoring, bugfixes (only uses 50 entries; not sorted)
Modified Paths:
--------------
trunk/extensions/SemanticGlossary/SemanticGlossary.php
trunk/extensions/SemanticGlossary/SemanticGlossaryParser.php
trunk/extensions/SemanticGlossary/SpecialSemanticGlossaryBrowser.php
Added Paths:
-----------
trunk/extensions/SemanticGlossary/SemanticGlossaryBackend.php
Removed Paths:
-------------
trunk/extensions/SemanticGlossary/SemanticGlossarySettings.php
Modified: trunk/extensions/SemanticGlossary/SemanticGlossary.php
===================================================================
--- trunk/extensions/SemanticGlossary/SemanticGlossary.php 2011-05-30
21:00:35 UTC (rev 89187)
+++ trunk/extensions/SemanticGlossary/SemanticGlossary.php 2011-05-30
21:07:43 UTC (rev 89188)
@@ -35,7 +35,7 @@
$wgExtensionCredits[defined( 'SEMANTIC_EXTENSION_TYPE' ) ? 'semantic' :
'other'][] = array(
'path' => __FILE__,
'name' => 'Semantic Glossary',
- 'author' => '[http://www.mediawiki.org/wiki/User:F.trott Stephan
Gambke]',
+ 'author' => '[[mw:User:F.trott|Stephan Gambke]]',
'url' => 'http://www.mediawiki.org/wiki/Extension:Semantic_Glossary',
'descriptionmsg' => 'semanticglossary-desc',
'version' => SG_VERSION,
@@ -53,6 +53,7 @@
$wgAutoloadClasses['SemanticGlossaryParser'] = $dir .
'/SemanticGlossaryParser.php';
$wgAutoloadClasses['SemanticGlossaryTree'] = $dir .
'/SemanticGlossaryTree.php';
$wgAutoloadClasses['SemanticGlossaryElement'] = $dir .
'/SemanticGlossaryElement.php';
+$wgAutoloadClasses['SemanticGlossaryBackend'] = $dir .
'/SemanticGlossaryBackend.php';
$wgAutoloadClasses['SemanticGlossaryMessageLog'] = $dir .
'/SemanticGlossaryMessageLog.php';
$wgAutoloadClasses['SpecialSemanticGlossaryBrowser'] = $dir .
'/SpecialSemanticGlossaryBrowser.php';
@@ -96,9 +97,6 @@
// Create new permission 'editglossary' and assign it to usergroup 'user' by
default
$wgGroupPermissions['user']['editglossary'] = true;
-// create and initialize settings object
-$sggSettings = new SemanticGlossarySettings();
-
/**
* Handler for late setup of Semantic Glossary
*/
Added: trunk/extensions/SemanticGlossary/SemanticGlossaryBackend.php
===================================================================
--- trunk/extensions/SemanticGlossary/SemanticGlossaryBackend.php
(rev 0)
+++ trunk/extensions/SemanticGlossary/SemanticGlossaryBackend.php
2011-05-30 21:07:43 UTC (rev 89188)
@@ -0,0 +1,102 @@
+<?php
+
+/**
+ * File holding the SemanticGlossaryBackend class
+ *
+ * @author Stephan Gambke
+ * @file
+ * @ingroup SemanticGlossary
+ */
+if ( !defined( 'SG_VERSION' ) ) {
+ die( 'This file is part of the SemanticGlossary extension, it is not a
valid entry point.' );
+}
+
+/**
+ * The SemanticGlossaryBackend class.
+ *
+ * @ingroup SemanticGlossary
+ */
+class SemanticGlossaryBackend {
+
+ protected $mQueryResult;
+ protected $mResultLine;
+ protected $mMessageLog;
+ protected $mTerm;
+ protected $mDefinition;
+ protected $mLink;
+ protected $mSource;
+
+ public function __construct ( SemanticGlossaryMessageLog &$messages =
null ) {
+
+ $this -> mMessageLog = $messages;
+
+ $store = smwfGetStore(); // default store
+ // Create query
+ $desc = new SMWSomeProperty( new SMWDIProperty( '___glt' ), new
SMWThingDescription() );
+ $desc -> addPrintRequest( new SMWPrintRequest(
SMWPrintRequest::PRINT_PROP, null, SMWPropertyValue::makeProperty( '___glt' ) )
);
+ $desc -> addPrintRequest( new SMWPrintRequest(
SMWPrintRequest::PRINT_PROP, null, SMWPropertyValue::makeProperty( '___gld' ) )
);
+ $desc -> addPrintRequest( new SMWPrintRequest(
SMWPrintRequest::PRINT_PROP, null, SMWPropertyValue::makeProperty( '___gll' ) )
);
+
+ $query = new SMWQuery( $desc, false, false );
+ $query -> sort = true;
+ $query -> sortkeys[ '___glt' ] = 'ASC';
+
+ // get the query result
+ $this -> mQueryResult = $store -> getQueryResult( $query );
+ }
+
+ public function next () {
+
+ // find next line
+ while ( $resultline = $this -> mQueryResult -> getNext() ) {
+ $this -> mTerm = $resultline[ 0 ] -> getNextText(
SMW_OUTPUT_HTML );
+ $this -> mDefinition = $resultline[ 1 ] -> getNextText(
SMW_OUTPUT_HTML );
+ $this -> mLink = $resultline[ 2 ] -> getNextText(
SMW_OUTPUT_HTML );
+ $this -> mSource = $resultline[ 0 ] ->
getResultSubject() -> getTitle() -> getPrefixedText();
+
+ $nextTerm = $resultline[ 0 ] -> getNextText(
SMW_OUTPUT_HTML );
+ $nextDefinition = $resultline[ 1 ] -> getNextText(
SMW_OUTPUT_HTML );
+ $nextLink = $resultline[ 2 ] -> getNextText(
SMW_OUTPUT_HTML );
+
+
+ // FIXME: SMW has a bug that right after storing data
this data
+ // might be available twice. The workaround here is to
compare the
+ // first and second result and if they are identical
assume that
+ // it is because of the bug. (2nd condition in the if
below)
+ // skip if more then one term or more than one
definition present
+ if ( ( $nextTerm || $nextDefinition || $nextLink ) &&
+ !( $nextTerm == $this -> mTerm &&
$nextDefinition == $this -> mDefinition && $nextLink == $this -> mLink ) ) {
+
+ if ( $this -> mMessageLog ) {
+ $this -> mMessageLog -> addMessage(
+ wfMsg(
'semanticglossary-termdefinedtwice', array( $subject -> getTitle() ->
getPrefixedText() ) ),
+
SemanticGlossaryMessageLog::SG_WARNING );
+ }
+
+ continue;
+ }
+
+ return true;
+ }
+
+ return $resultline != null;
+ }
+
+ function &getTerm () {
+ return $this -> mTerm;
+ }
+
+ function &getDefinition () {
+ return $this -> mDefinition;
+ }
+
+ function &getLink () {
+ return $this -> mLink;
+ }
+
+ function &getSource () {
+ return $this -> mSource;
+ }
+
+}
+
Property changes on:
trunk/extensions/SemanticGlossary/SemanticGlossaryBackend.php
___________________________________________________________________
Added: svn:eol-style
+ native
Modified: trunk/extensions/SemanticGlossary/SemanticGlossaryParser.php
===================================================================
--- trunk/extensions/SemanticGlossary/SemanticGlossaryParser.php
2011-05-30 21:00:35 UTC (rev 89187)
+++ trunk/extensions/SemanticGlossary/SemanticGlossaryParser.php
2011-05-30 21:07:43 UTC (rev 89188)
@@ -32,36 +32,40 @@
* @param $text
* @return Boolean
*/
- static function parse( &$parser, &$text ) {
+ static function parse ( &$parser, &$text ) {
wfProfileIn( __METHOD__ );
if ( !self::$parserSingleton ) {
self::$parserSingleton = new SemanticGlossaryParser();
}
- self::$parserSingleton->realParse( $parser, $text );
+ self::$parserSingleton -> realParse( $parser, $text );
wfProfileOut( __METHOD__ );
return true;
}
+ function getBackend () {
+ return new SemanticGlossaryBackend();
+ }
+
/**
* Returns the list of terms applicable in the current context
*
* @return Array an array mapping terms (keys) to descriptions (values)
*/
- function getGlossaryArray( SemanticGlossaryMessageLog &$messages = null
) {
+ function getGlossaryArray ( SemanticGlossaryMessageLog &$messages =
null ) {
wfProfileIn( __METHOD__ );
// build glossary array only once per request
- if ( !$this->mGlossaryArray ) {
- $this->buildGlossary( $messages );
+ if ( !$this -> mGlossaryArray ) {
+ $this -> buildGlossary( $messages );
}
wfProfileOut( __METHOD__ );
- return $this->mGlossaryArray;
+ return $this -> mGlossaryArray;
}
/**
@@ -69,90 +73,44 @@
*
* @return Array an array mapping terms (keys) to descriptions (values)
*/
- function getGlossaryTree( SemanticGlossaryMessageLog &$messages = null
) {
+ function getGlossaryTree ( SemanticGlossaryMessageLog &$messages = null
) {
wfProfileIn( __METHOD__ );
// build glossary array only once per request
- if ( !$this->mGlossaryTree ) {
- $this->buildGlossary( $messages );
+ if ( !$this -> mGlossaryTree ) {
+ $this -> buildGlossary( $messages );
}
wfProfileOut( __METHOD__ );
- return $this->mGlossaryTree;
+ return $this -> mGlossaryTree;
}
- protected function buildGlossary( SemanticGlossaryMessageLog &$messages
= null ) {
+ protected function buildGlossary ( SemanticGlossaryMessageLog
&$messages = null ) {
wfProfileIn( __METHOD__ );
- $this->mGlossaryTree = new SemanticGlossaryTree();
+ $this -> mGlossaryTree = new SemanticGlossaryTree();
- $store = smwfGetStore(); // default store
- // Create query
- $desc = new SMWSomeProperty( new SMWDIProperty( '___glt' ), new
SMWThingDescription() );
- $desc->addPrintRequest( new SMWPrintRequest(
SMWPrintRequest::PRINT_PROP, null, SMWPropertyValue::makeProperty( '___glt' ) )
);
- $desc->addPrintRequest( new SMWPrintRequest(
SMWPrintRequest::PRINT_PROP, null, SMWPropertyValue::makeProperty( '___gld' ) )
);
- $desc->addPrintRequest( new SMWPrintRequest(
SMWPrintRequest::PRINT_PROP, null, SMWPropertyValue::makeProperty( '___gll' ) )
);
+ $backend = $this -> getBackEnd();
- $query = new SMWQuery( $desc, true, false );
- $query->querymode = SMWQuery::MODE_INSTANCES;
-
- global $smwgQDefaultLimit;
- $query->setLimit( $smwgQDefaultLimit );
- $query->sortkeys[SG_PROP_GLT] = 'ASC';
-
- // get the query result
- $queryresult = $store->getQueryResult( $query );
-
// assemble the result array
- $this->mGlossaryArray = array();
- while ( ( $resultline = $queryresult->getNext() ) ) {
- $term = $resultline[0]->getNextText( SMW_OUTPUT_HTML );
- $definition = $resultline[1]-> getNextText(
SMW_OUTPUT_HTML );
- $link = $resultline[2]->getNextText( SMW_OUTPUT_HTML );
- $subject = $resultline[0]->getResultSubject();
+ $this -> mGlossaryArray = array( );
+ while ( $backend -> next() ) {
- // FIXME: SMW has a bug that right after storing data
this data
- // might be available twice. The workaround here is to
compare the
- // first and second result and if they are identical
assume that
- // it is because of the bug. (2nd condition in the if
below)
-
- $nextTerm = $resultline[0]->getNextText(
SMW_OUTPUT_HTML );
- $nextDefinition = $resultline[1]->getNextText(
SMW_OUTPUT_HTML );
-
- // skip if more then one term or more than one
definition present
- if ( ( $nextTerm || $nextDefinition ) &&
- !( $nextTerm == $term && $nextDefinition ==
$definition ) ) {
-
- if ( $messages ) {
- $messages->addMessage(
- wfMsg(
'semanticglossary-termdefinedtwice', array(
$subject->getTitle()->getPrefixedText() ) ),
-
SemanticGlossaryMessageLog::SG_WARNING );
- }
-
- continue;
- }
-
- $source = array(
- $subject->getDBkey(),
- $subject->getNamespace(),
- $subject->getInterwiki()
- );
-
$elementData = array(
- SemanticGlossaryElement::SG_TERM => $term,
- SemanticGlossaryElement::SG_DEFINITION =>
$definition,
- SemanticGlossaryElement::SG_LINK => $link,
- SemanticGlossaryElement::SG_SOURCE => $source
+ SemanticGlossaryElement::SG_TERM => ($term =
$backend -> getTerm()),
+ SemanticGlossaryElement::SG_DEFINITION =>
$backend -> getDefinition(),
+ SemanticGlossaryElement::SG_LINK => $backend ->
getLink(),
+ SemanticGlossaryElement::SG_SOURCE => $backend
-> getSource()
);
- if ( array_key_exists( $term, $this->mGlossaryArray ) )
{
- $this->mGlossaryArray[$term]->addDefinition(
$elementData );
+ if ( array_key_exists( $term, $this -> mGlossaryArray )
) {
+ $this -> mGlossaryArray[ $term ] ->
addDefinition( $elementData );
} else {
- $this->mGlossaryArray[$term] = new
SemanticGlossaryElement( $elementData );
+ $this -> mGlossaryArray[ $term ] = new
SemanticGlossaryElement( $elementData );
}
- $this->mGlossaryTree->addTerm( $term, $elementData );
+ $this -> mGlossaryTree -> addTerm( $term, $elementData
);
}
wfProfileOut( __METHOD__ );
@@ -167,18 +125,18 @@
* @param $text
* @return Boolean
*/
- protected function realParse( &$parser, &$text ) {
- global $wgRequest, $sggSettings;
+ protected function realParse ( &$parser, &$text ) {
+ global $wgRequest;
wfProfileIn( __METHOD__ );
- $action = $wgRequest->getVal( 'action', 'view' );
+ $action = $wgRequest -> getVal( 'action', 'view' );
if ( $text == null ||
$text == '' ||
$action == 'edit' ||
$action == 'ajax' ||
- isset( $_POST['wpPreview'] )
+ isset( $_POST[ 'wpPreview' ] )
) {
wfProfileOut( __METHOD__ );
@@ -186,7 +144,7 @@
}
// Get array of terms
- $glossary = $this->getGlossaryTree();
+ $glossary = $this -> getGlossaryTree();
if ( $glossary == null ) {
wfProfileOut( __METHOD__ );
@@ -199,7 +157,7 @@
wfSuppressWarnings();
$doc = DOMDocument::loadHTML(
- '<html><meta http-equiv="content-type"
content="charset=utf-8"/>' . $text . '</html>'
+ '<html><meta http-equiv="content-type"
content="charset=utf-8"/>' . $text . '</html>'
);
wfRestoreWarnings();
@@ -208,55 +166,55 @@
wfProfileIn( __METHOD__ . ' 2 xpath' );
// Find all text in HTML.
$xpath = new DOMXpath( $doc );
- $elements = $xpath->query(
- "//*[not(ancestor-or-self::*[@class='noglossary'] or
ancestor-or-self::a)][text()!=' ']/text()"
+ $elements = $xpath -> query(
+
"//*[not(ancestor-or-self::*[@class='noglossary'] or
ancestor-or-self::a)][text()!=' ']/text()"
);
wfProfileOut( __METHOD__ . ' 2 xpath' );
// Iterate all HTML text matches
- $nb = $elements->length;
+ $nb = $elements -> length;
$changedDoc = false;
for ( $pos = 0; $pos < $nb; $pos++ ) {
- $el = $elements->item( $pos );
+ $el = $elements -> item( $pos );
- if ( strlen( $el->nodeValue ) <
$glossary->getMinTermLength() ) {
+ if ( strlen( $el -> nodeValue ) < $glossary ->
getMinTermLength() ) {
continue;
}
wfProfileIn( __METHOD__ . ' 3 lexer' );
- $matches = array();
+ $matches = array( );
preg_match_all(
'/[[:alpha:]]+|[^[:alpha:]]/u',
- $el->nodeValue,
+ $el -> nodeValue,
$matches,
PREG_OFFSET_CAPTURE | PREG_PATTERN_ORDER
);
wfProfileOut( __METHOD__ . ' 3 lexer' );
- if ( count( $matches ) == 0 || count( $matches[0] ) ==
0 ) {
+ if ( count( $matches ) == 0 || count( $matches[ 0 ] )
== 0 ) {
continue;
}
- $lexemes = &$matches[0];
+ $lexemes = &$matches[ 0 ];
$countLexemes = count( $lexemes );
- $parent = &$el->parentNode;
+ $parent = &$el -> parentNode;
$index = 0;
$changedElem = false;
while ( $index < $countLexemes ) {
wfProfileIn( __METHOD__ . ' 4 findNextTerm' );
- list( $skipped, $used, $definition ) =
$glossary->findNextTerm( $lexemes, $index, $countLexemes );
+ list( $skipped, $used, $definition ) =
$glossary -> findNextTerm( $lexemes, $index, $countLexemes );
wfProfileOut( __METHOD__ . ' 4 findNextTerm' );
wfProfileIn( __METHOD__ . ' 5 insert' );
if ( $used > 0 ) { // found a term
if ( $skipped > 0 ) { // skipped some
text, insert it as is
- $parent->insertBefore(
- $doc->createTextNode(
- substr(
$el->nodeValue,
-
$currLexIndex = $lexemes[$index][1],
-
$lexemes[$index + $skipped][1] - $currLexIndex )
+ $parent -> insertBefore(
+ $doc -> createTextNode(
+ substr( $el ->
nodeValue,
+
$currLexIndex = $lexemes[ $index ][ 1 ],
+
$lexemes[ $index + $skipped ][ 1 ] - $currLexIndex )
),
$el
);
@@ -269,22 +227,22 @@
$span -> setAttribute( 'class',
'tooltip' );
// Wrap abbreviation in <span> tags,
hidden
- $lastLex = $lexemes[$index + $used - 1];
- $spanTerm = $doc->createElement( 'span',
- substr( $el->nodeValue,
- $currLexIndex =
$lexemes[$index][1],
- $lastLex[1] -
$currLexIndex + strlen( $lastLex[0] ) )
+ $lastLex = $lexemes[ $index + $used - 1
];
+ $spanTerm = $doc -> createElement(
'span',
+ substr( $el ->
nodeValue,
+ $currLexIndex =
$lexemes[ $index ][ 1 ],
+ $lastLex[ 1 ] -
$currLexIndex + strlen( $lastLex[ 0 ] ) )
);
- $spanTerm->setAttribute( 'class',
'tooltip_abbr' );
+ $spanTerm -> setAttribute( 'class',
'tooltip_abbr' );
// Wrap definition in <span> tags,
hidden
- $spanDefinition =
$definition->getFullDefinition( $doc );
- $spanDefinition->setAttribute( 'class',
'tooltip_tip' );
+ $spanDefinition = $definition ->
getFullDefinition( $doc );
+ $spanDefinition -> setAttribute(
'class', 'tooltip_tip' );
// insert term and definition
- $span->appendChild( $spanTerm );
- $span->appendChild( $spanDefinition );
- $parent->insertBefore( $span, $el );
+ $span -> appendChild( $spanTerm );
+ $span -> appendChild( $spanDefinition );
+ $parent -> insertBefore( $span, $el );
$changedElem = true;
} else { // did not find term, just use the
rest of the text
@@ -293,9 +251,9 @@
// element at all.
// Only change element if found term
before
if ( $changedElem ) {
- $parent->insertBefore(
- $doc->createTextNode(
- substr(
$el->nodeValue, $lexemes[$index][1] )
+ $parent -> insertBefore(
+ $doc -> createTextNode(
+ substr( $el ->
nodeValue, $lexemes[ $index ][ 1 ] )
),
$el
);
@@ -315,21 +273,20 @@
}
if ( $changedElem ) {
- $parent->removeChild( $el );
+ $parent -> removeChild( $el );
$changedDoc = true;
}
-
}
if ( $changedDoc ) {
- $body = $xpath->query( '/html/body' );
+ $body = $xpath -> query( '/html/body' );
$text = '';
- foreach ( $body->item( 0 )->childNodes as $child ) {
- $text .= $doc->saveXML( $child );
+ foreach ( $body -> item( 0 ) -> childNodes as $child ) {
+ $text .= $doc -> saveXML( $child );
}
- $this->loadModules( $parser );
+ $this -> loadModules( $parser );
}
wfProfileOut( __METHOD__ );
@@ -337,20 +294,20 @@
return true;
}
- protected function loadModules( &$parser ) {
+ protected function loadModules ( &$parser ) {
global $wgOut, $wgScriptPath;
if ( defined( 'MW_SUPPORTS_RESOURCE_MODULES' ) ) {
if ( !is_null( $parser ) ) {
- $parser->getOutput()->addModules(
'ext.SemanticGlossary' );
+ $parser -> getOutput() -> addModules(
'ext.SemanticGlossary' );
} else {
- $wgOut->addModules( 'ext.SemanticGlossary' );
+ $wgOut -> addModules( 'ext.SemanticGlossary' );
}
} else {
- if ( !is_null( $parser ) && ( $wgOut->isArticle() ) ) {
- $parser->getOutput()->addHeadItem( '<link
rel="stylesheet" href="' . $wgScriptPath .
'/extensions/SemanticGlossary/skins/SemanticGlossary.css" />',
'ext.SemanticGlossary.css' );
+ if ( !is_null( $parser ) && ( $wgOut -> isArticle() ) )
{
+ $parser -> getOutput() -> addHeadItem( '<link
rel="stylesheet" href="' . $wgScriptPath .
'/extensions/SemanticGlossary/skins/SemanticGlossary.css" />',
'ext.SemanticGlossary.css' );
} else {
- $wgOut->addHeadItem(
'ext.SemanticGlossary.css', '<link rel="stylesheet" href="' . $wgScriptPath .
'/extensions/SemanticGlossary/skins/SemanticGlossary.css" />' );
+ $wgOut -> addHeadItem(
'ext.SemanticGlossary.css', '<link rel="stylesheet" href="' . $wgScriptPath .
'/extensions/SemanticGlossary/skins/SemanticGlossary.css" />' );
}
}
}
Deleted: trunk/extensions/SemanticGlossary/SemanticGlossarySettings.php
===================================================================
--- trunk/extensions/SemanticGlossary/SemanticGlossarySettings.php
2011-05-30 21:00:35 UTC (rev 89187)
+++ trunk/extensions/SemanticGlossary/SemanticGlossarySettings.php
2011-05-30 21:07:43 UTC (rev 89188)
@@ -1,26 +0,0 @@
-<?php
-
-/**
- * File holding the default settings for the Semantic Glossary extension
- *
- * @author Stephan Gambke
- *
- * @file
- * @ingroup SemanticGlossary
- */
-if ( !defined( 'SG_VERSION' ) ) {
- die( 'This file is part of the Semantic Glossary extension, it is not a
valid entry point.' );
-}
-
-/**
- * Class to encapsulate Semantic Glossary settings
- * @ingroup SemanticGlossary
- */
-class SemanticGlossarySettings {
-
- /**
- * @var Contains the characters that may not be part of a term.
- */
- public $punctuationCharacters = '\.(),;:?!';
-}
-
Modified: trunk/extensions/SemanticGlossary/SpecialSemanticGlossaryBrowser.php
===================================================================
--- trunk/extensions/SemanticGlossary/SpecialSemanticGlossaryBrowser.php
2011-05-30 21:00:35 UTC (rev 89187)
+++ trunk/extensions/SemanticGlossary/SpecialSemanticGlossaryBrowser.php
2011-05-30 21:07:43 UTC (rev 89188)
@@ -73,8 +73,7 @@
foreach ( $glossaryarray as $term => $glossaryElement ) {
// One term may have several definitions. Include them
all.
while ( ( $key = $glossaryElement->getCurrentKey() )
!== null ) {
- $sourceArray = $glossaryElement->getSource(
$key );
- $source = $sourceArray[2] . ':' .
$sourceArray[1] . ':' . $sourceArray[0];
+ $source = $glossaryElement->getSource( $key );
$definition = $glossaryElement->getDefinition(
$key );
$link = $glossaryElement->getLink( $key );
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs