Refbase update_2021-01-28_15_58

This commit is contained in:
root
2021-01-28 15:58:21 +01:00
commit 64e7261da6
300 changed files with 164739 additions and 0 deletions

View File

@@ -0,0 +1,144 @@
<?php
/**
* Enum for citation types
*/
abstract class RefbaseCitationType {
// Minimal reference type (author, title, publication, year)
const CT_MINIMAL = 0;
// Request citation from refbase installation (using show.php interface)
const CT_RB = 1;
/**
* Convert string to RefbaseCitationType
*/
static public function decodeCitationType ( $str, & $citeStyle ) {
if ( strtolower( $str ) == 'minimal' ) {
return self::CT_MINIMAL;
} elseif ( preg_match( '/rb-(.*)/', strtolower( $str ),
$citeStyle ) ) {
return self::CT_RB;
} else {
return null;
}
}
}
/**
* Helper class to generate citation text
*/
class RefbaseCitationCreator {
/// Citation type
private $citationType;
/// Citation style (only with $citationType = CT_RB)
private $citationStyle = "";
/// Location of refbase installation (may differ from $dbHost if using https
/// for instance)
protected $refbaseURL = "";
/**
* Constructor
*/
public function __construct( $citationTypeStr ) {
global $wgRefbaseURL;
$this->refbaseURL = $wgRefbaseURL;
$this->citationType =
RefbaseCitationType::decodeCitationType( $citationTypeStr,
$citeStyle );
if ( !empty( $citeStyle ) ) {
$this->citationStyle = $citeStyle[1];
}
wfDebug('refbase-decode-in:' . $citationTypeStr . "\n");
wfDebug('refbase-decode:' . $this->citationType . ", " . var_export($this->citationStyle,true)."\n");
}
/**
* Create citation text
*/
public function createCitation( $entry, & $cite ) {
switch( $this->citationType ) {
case RefbaseCitationType::CT_MINIMAL:
$cite = $entry['author'] . ", " . $entry['title'] . ", " .
$entry['publication'] . ", " . $entry['year'] . ".";
break;
case RefbaseCitationType::CT_RB:
$url = $this->refbaseURL . "show.php?" .
"record=" . "27711"//$entry['serial'] .
"&submit=Cite&exportType=text&citeType=ASCII";
if ( !empty( $this->citationStyle ) ) {
$url .= "&citeStyle=" . $this->citationStyle;
}
wfDebug('refbase-getcite:' . $url . "\n");
// Get citation from url (add http authentication if desired)
global $wgRefbaseURLAuth;
if ( !empty( $wgRefbaseURLAuth ) ) {
if ( strcmp( strtolower( $wgRefbaseURLAuth ),
'default' ) == 0 ) {
if ( isset( $_SERVER['PHP_AUTH_USER'] ) &&
isset( $_SERVER['PHP_AUTH_PW'] ) ) {
$username = $_SERVER['PHP_AUTH_USER'];
$password = $_SERVER['PHP_AUTH_PW'];
$authStr = "Authorization: Basic " .
base64_encode( "$username:$password" );
} else {
$authStr = '';
}
} else {
preg_match( "/([^:]*):(.*)$/", $wgRefbaseURLAuth, $out);
$username = $out[1];
$password = $out[2];
$authStr = "Authorization: Basic " .
base64_encode( "$username:$password" );
}
$param = array( 'http' => array( 'header' => $authStr ) );
$context = stream_context_create( $param );
$cite = trim( file_get_contents( $url, false, $context ) );
} else {
$cite = trim( file_get_contents( $url ) );
}
break;
default:
$cite = wfMessage( 'refbase-error-citation-type' )->text();
}
return true;
}
/*
* Get list of required fields to produce the citation in the desired format
*/
public function getFieldList() {
switch( $this->citationType ) {
case RefbaseCitationType::CT_MINIMAL:
$fieldList = array( 'author',
'title',
'publication',
'year' );
break;
case RefbaseCitationType::CT_RB:
$fieldList = array( 'serial' );
break;
default:
$fieldList = array();
}
return $fieldList;
}
}

View File

@@ -0,0 +1,139 @@
<?php
/**
* Refbase database connector
*/
class RefbaseConnector {
/// Database location
private $dbHost = "";
/// Database name
private $dbName = "";
/// Database user
private $dbUser = "";
/// Database password
private $dbPass = "";
/// Character set
private $dbCharset = "";
/// Reference table
private $dbRefTable = "";
/// User data table (for cite key entry)
private $dbUserDataTable = "";
/// Method to access database (mysql or PDO)
private $dbAccessMethod = "";
/**
* Constructor
*/
public function __construct() {
global $wgRefbaseDbHost;
global $wgRefbaseDbName;
global $wgRefbaseDbUser;
global $wgRefbaseDbPass;
global $wgRefbaseDbRefTable;
global $wgRefbaseDbUserDataTable;
global $wgRefbaseDbCharset;
global $wgRefbaseDbAccessMethod;
// Read from global configuration
$this->dbHost = $wgRefbaseDbHost;
$this->dbName = $wgRefbaseDbName;
$this->dbUser = $wgRefbaseDbUser;
$this->dbPass = $wgRefbaseDbPass;
$this->dbRefTable = $wgRefbaseDbRefTable;
$this->dbUserDataTable = $wgRefbaseDbUserDataTable;
$this->dbCharset = $wgRefbaseDbCharset;
$this->dbAccessMethod = $wgRefbaseDbAccessMethod;
}
/**
* Query by serial number or cite key entry
*/
public function getEntry( $input, $tagTypeList, & $outputEntry,
$fieldList ) {
// List of fields to extract (prefix 'r.' to each element)
$fieldPref = $fieldList;
array_walk( $fieldPref, function ( &$value, $key) {
$value="r.$value";
} );
$fieldPref = join(",", $fieldPref);
$flagFound = false;
for ( $i = 0; $i < count($tagTypeList) && ! $flagFound; $i++ ) {
$tagType = $tagTypeList[$i];
// Query string
$queryStr = "";
if ( $tagType === 'citekey' ) {
$queryStr = "SELECT $fieldPref " .
"FROM " . $this->dbRefTable . " r " .
"INNER JOIN " . $this->dbUserDataTable . " u " .
"ON r.serial = u.record_id " .
"WHERE u.cite_key='$input'";
} else {
$queryStr = "SELECT $fieldPref " .
"FROM " . $this->dbRefTable . " r " .
"WHERE r.serial='$input'";
}
if ( strtolower( $this->dbAccessMethod ) === 'pdo' ) {
// Connect and query
$link = new PDO( 'mysql:host=' . $this->dbHost . ';dbname=' .
$this->dbName . ';charset=' . $this->dbCharset,
$this->dbUser, $this->dbPass );
$dbexec = $link->prepare( $queryStr );
try {
// Perform query
$outputEntry = $dbexec->execute();
} catch( PDOException $ex ) {
$outputEntry = wfMessage( 'refbase-error-dbquery' )->text() .
$ex->getMessage();
return false;
}
$outputEntry = $dbexec->fetch(PDO::FETCH_ASSOC);
} elseif ( strtolower( $this->dbAccessMethod ) === 'mysql' ) {
$link = mysqli_connect( $this->dbHost, $this->dbUser, $this->dbPass, $this->dbName ) or die("db error");
if ( !$link ) {
$outputEntry = wfMessage( 'refbase-error-mysqlconn' )->text();
return false;
}
$result = mysqli_query($link, $queryStr );
if ( !$result ) {
$outputEntry = wfMessage( 'refbase-error-dbquery' )->text() .
mysqli_error($link);
return false;
}
$outputEntry = mysqli_fetch_array($result);
}
if ( !empty( $outputEntry ) ) {
$flagFound = true;
}
}
if ( empty( $outputEntry ) ) {
$outputEntry = wfMessage( 'refbase-error-notfound' )->text();
return false;
}
return true;
}
}

View File

@@ -0,0 +1,113 @@
<?php
/**
* Refbase entry renderer using citation templates (cite_journal only for now)
*/
class RefbaseRendererCitationTemplate extends RefbaseRenderer {
/**
* Constructor (simply inherit from parent)
*/
public function __construct() {
parent::__construct();
}
/**
* List fields required to build template
*/
public function getFieldList() {
return array( 'type',
'serial',
'author',
'year',
'title',
'language',
'publication',
'volume',
'issue',
'pages',
'place',
'publisher',
'issn',
'doi' );
}
/**
* Prepare text for template (supports journal articles only)
*/
public function render( $entry, & $cite, $options ) {
$cite = "";
$ret = true;
if ( $entry["type"] == "Journal Article" ) {
$cite .= "{{cite_journal|url=" . $this->refbaseURL . "show.php?";
$cite .= "record=" . $entry['serial'];
if( !empty( $entry["author"] ) ) {
$author = $entry["author"];
$aulast = RefbaseTools::extractAuthorsLastName
( " *; *", " *, *", 1, $author );
$aufirst = RefbaseTools::extractAuthorsGivenName
( " *; *", " *, *", 1, $author );
if( !empty( $aulast ) ) {
$cite .= "|last=" . $aulast;
}
if( !empty( $aufirst ) ) {
$cite .= "|first=" . $aufirst;
if( !empty( $aulast ) ) {
$cite .= "|authorlink=$aufirst $aulast";
}
}
$authorcount = count( preg_split( "/ *; */", $author ) );
$au = "";
for ( $i=0; $i < $authorcount - 1; $i++ ) {
$aul = RefbaseTools::extractAuthorsLastName
( " *; *", " *, *", $i + 2, $author );
$auf = RefbaseTools::extractAuthorsGivenName
( " *; *", " *, *", $i + 2, $author );
if ( !empty( $aul ) ) {
if ( !empty( $auf ) ) {
$au .= "[[$auf $aul|$aul, $auf]]; ";
}
}
}
if ( !empty( $au ) ) {
$cite .= "|coauthors=" . trim( $au, '; ' );
}
}
if( !empty( $entry["year"] ) ) {
$cite .= "|year=" . $entry['year'];
}
if( !empty( $entry["title"] ) ) {
$title = RefbaseTools::searchReplaceText( $entry['title'],
true );
$cite .= "|title=" . $title;
}
if( !empty( $entry["language"] ) )
$cite .= "|language=" . $entry['language'];
if( !empty( $entry["publication"] ) )
$cite .= "|journal=" . $entry['publication'];
if( !empty( $entry["volume"] ) )
$cite .= "|volume=" . $entry['volume'];
if( !empty( $entry["issue"] ) )
$cite .= "|issue=" . $entry['issue'];
if( !empty( $entry["pages"] ) )
$cite .= "|pages=" . $entry['pages'];
if( !empty( $entry["place"] ) )
$cite .= "|location=" . $entry['place'];
if( !empty( $entry["publiser"] ) )
$cite .= "|publisher=" . $entry['publisher'];
if( !empty( $entry["issn"] ) )
$cite .= "|issn=" . $entry['issn'];
if( !empty( $entry["doi"] ) )
$cite .= "|doi=" . $entry['doi'];
$cite .= "}}";
$ret &= true;
} else {
$cite .= wfMessage( 'refbase-error-cite_journal-type' )->text();
$ret &= false;
}
return $ret;
}
}

View File

@@ -0,0 +1,48 @@
<?php
/**
* Refbase entry renderer using the Cite extension tag (<ref>)
*/
class RefbaseRendererCite extends RefbaseRenderer {
/// Object generating citations
private $citationCreator;
/**
* Constructor (simply inherit from parent)
*/
public function __construct( $citationType ) {
parent::__construct();
$this->citationCreator = new RefbaseCitationCreator( $citationType );
}
/**
* List fields required to build template
*/
public function getFieldList() {
$citeList = $this->citationCreator->getFieldList();
return array_unique( array_merge( array(), $citeList ) );
}
/**
* Render output: add wiki link to refbase page, include citation in tooltip
*/
public function render( $entry, & $cite, $options ) {
$citekey = $options['citekey'];
$cite = "";
// Simply link to refbase, and add tooltip
// (form string [URL <span title="CITATION"> KEY </span>] )
$citation = "";
$this->citationCreator->createCitation( $entry, $citation );
// Use #tag method to properly pass inputs to <ref>
$cite .= "{{#tag:ref|$citation|name=$citekey}}";
return true;
}
}

View File

@@ -0,0 +1,56 @@
<?php
/**
* Refbase entry renderer using simple hyperlink and tooltip
*/
class RefbaseRendererLink extends RefbaseRenderer {
/// Object generating citations
private $citationCreator;
/**
* Constructor (simply inherit from parent)
*/
public function __construct( $citationType ) {
parent::__construct();
$this->citationCreator = new RefbaseCitationCreator( $citationType );
}
/**
* List fields required to build template
*/
public function getFieldList() {
$citeList = $this->citationCreator->getFieldList();
return array_unique( array_merge( array( 'serial' ), $citeList ) );
}
/**
* Render output: add wiki link to refbase page, include citation in tooltip
*/
public function render( $entry, & $cite, $options ) {
$citekey = $options['citekey'];
$cite = "";
// Simply link to refbase, and add tooltip
// (form string [URL <span title="CITATION"> KEY </span>] )
// Display the key (cite_key or serial number as wiki text)
$wikiText = $citekey;
// Add full citation as a tooltip
$toolTip = "";
$this->citationCreator->createCitation( $entry, $toolTip );
// Link to refbase page for current entry
$link = $this->refbaseURL . "show.php?record=" . $entry['serial'];
// Build full string
$cite .= "[" . $link . " ";
$cite .= Html::openElement( 'span', array( 'title' => "\"" . $toolTip . "\"" ) );
$cite .= $wikiText . Html::closeElement( 'span' ) . "]";
return true;
}
}

View File

@@ -0,0 +1,46 @@
<?php
/**
* Refbase entry renderer
*/
abstract class RefbaseRenderer {
/// Location of refbase installation (may differ from $dbHost if using https
/// for instance)
protected $refbaseURL = "";
/**
* Constructor
*/
public function __construct() {
global $wgRefbaseURL;
$this->refbaseURL = $wgRefbaseURL;
}
/**
* Instantiation subclass instances
*/
public static function create( $outputType, $citationType = "" ) {
if ( strtolower( $outputType ) == 'cite_journal' ) {
return new RefbaseRendererCitationTemplate();
} elseif ( strtolower( $outputType ) == 'link' ) {
return new RefbaseRendererLink( $citationType );
} elseif ( strtolower( $outputType ) == 'cite' ) {
return new RefbaseRendererCite( $citationType );
} else {
return false;
}
}
/**
* Returns the list of fields to extract from the database
*/
abstract public function getFieldList();
/**
* Render entries
*/
abstract public function render( $entry, & $cite, $options );
}

View File

@@ -0,0 +1,130 @@
<?php
/**
* Refbase helper functions (cleanup strings, author extraction)
*/
class RefbaseTools {
/// Character translation table
private static $transtab_refbase_html = array(
"/__(?!_)(.+?)__/" => "<u>\\1</u>", // the pattern for underline (__...__) must come before the one for italic (_..._)
"/_(.+?)_/" => "<i>\\1</i>",
"/\\*\\*(.+?)\\*\\*/" => "<b>\\1</b>",
"/\\[super:(.+?)\\]/i" => "<sup>\\1</sup>",
"/\\[sub:(.+?)\\]/i" => "<sub>\\1</sub>",
"/\\[permil\\]/" => "&permil;",
"/\\[infinity\\]/" => "&infin;",
"/\\[alpha\\]/" => "&alpha;",
"/\\[beta\\]/" => "&beta;",
"/\\[gamma\\]/" => "&gamma;",
"/\\[delta\\]/" => "&delta;",
"/\\[epsilon\\]/" => "&epsilon;",
"/\\[zeta\\]/" => "&zeta;",
"/\\[eta\\]/" => "&eta;",
"/\\[theta\\]/" => "&theta;",
"/\\[iota\\]/" => "&iota;",
"/\\[kappa\\]/" => "&kappa;",
"/\\[lambda\\]/" => "&lambda;",
"/\\[mu\\]/" => "&mu;",
"/\\[nu\\]/" => "&nu;",
"/\\[xi\\]/" => "&xi;",
"/\\[omicron\\]/" => "&omicron;",
"/\\[pi\\]/" => "&pi;",
"/\\[rho\\]/" => "&rho;",
"/\\[sigmaf\\]/" => "&sigmaf;",
"/\\[sigma\\]/" => "&sigma;",
"/\\[tau\\]/" => "&tau;",
"/\\[upsilon\\]/" => "&upsilon;",
"/\\[phi\\]/" => "&phi;",
"/\\[chi\\]/" => "&chi;",
"/\\[psi\\]/" => "&psi;",
"/\\[omega\\]/" => "&omega;",
"/\\[Alpha\\]/" => "&Alpha;",
"/\\[Beta\\]/" => "&Beta;",
"/\\[Gamma\\]/" => "&Gamma;",
"/\\[Delta\\]/" => "&Delta;",
"/\\[Epsilon\\]/" => "&Epsilon;",
"/\\[Zeta\\]/" => "&Zeta;",
"/\\[Eta\\]/" => "&Eta;",
"/\\[Theta\\]/" => "&Theta;",
"/\\[Iota\\]/" => "&Iota;",
"/\\[Kappa\\]/" => "&Kappa;",
"/\\[Lambda\\]/" => "&Lambda;",
"/\\[Mu\\]/" => "&Mu;",
"/\\[Nu\\]/" => "&Nu;",
"/\\[Xi\\]/" => "&Xi;",
"/\\[Omicron\\]/" => "&Omicron;",
"/\\[Pi\\]/" => "&Pi;",
"/\\[Rho\\]/" => "&Rho;",
"/\\[Sigma\\]/" => "&Sigma;",
"/\\[Tau\\]/" => "&Tau;",
"/\\[Upsilon\\]/" => "&Upsilon;",
"/\\[Phi\\]/" => "&Phi;",
"/\\[Chi\\]/" => "&Chi;",
"/\\[Psi\\]/" => "&Psi;",
"/\\[Omega\\]/" => "&Omega;",
"/(?:\"|&quot;)(.+?)(?:\"|&quot;)/" => "&ldquo;\\1&rdquo;",
"/ +- +/" => " &#8211; "
);
// EXTRACT AUTHOR'S LAST NAME
// this function takes the contents of the author field and will extract the last name of a particular author (specified by position)
// (e.g., setting '$authorPosition' to "1" will return the 1st author's last name)
// Note: this function assumes that:
// 1. within one author object, there's only *one* delimiter separating author name & initials!
// 2. author objects are stored in the db as "<author_name><author_initials_delimiter><author_initials>", i.e., initials follow *after* the author's name!
// Required Parameters:
// 1. pattern describing delimiter that separates different authors
// 2. pattern describing delimiter that separates author name & initials (within one author)
// 3. position of the author whose last name shall be extracted (e.g., "1" will return the 1st author's last name)
// 4. contents of the author field
public static function extractAuthorsLastName( $oldBetweenAuthorsDelim, $oldAuthorsInitialsDelim, $authorPosition, $authorContents ) {
$authorsArray = preg_split( "/" . $oldBetweenAuthorsDelim . "/", $authorContents ); // get a list of all authors for this record
$authorPosition = $authorPosition - 1; // php array elements start with "0", so we decrease the authors position by 1
$singleAuthor = $authorsArray[$authorPosition]; // for the author in question, extract the full author name (last name & initials)
$singleAuthorArray = preg_split( "/" . $oldAuthorsInitialsDelim . "/", $singleAuthor ); // then, extract author name & initials to separate list items
$singleAuthorsLastName = $singleAuthorArray[0]; // extract this author's last name into a new variable
return $singleAuthorsLastName;
}
// EXTRACT AUTHOR'S GIVEN NAME
// this function takes the contents of the author field and will extract the given name of a particular author (specified by position)
// (e.g., setting '$authorPosition' to "1" will return the 1st author's given name)
// Required Parameters:
// 1. pattern describing delimiter that separates different authors
// 2. pattern describing delimiter that separates author name & initials (within one author)
// 3. position of the author whose last name shall be extracted (e.g., "1" will return the 1st author's last name)
// 4. contents of the author field
public static function extractAuthorsGivenName( $oldBetweenAuthorsDelim, $oldAuthorsInitialsDelim, $authorPosition, $authorContents ) {
$authorsArray = preg_split( "/" . $oldBetweenAuthorsDelim . "/", $authorContents ); // get a list of all authors for this record
$authorPosition = $authorPosition - 1; // php array elements start with "0", so we decrease the authors position by 1
$singleAuthor = $authorsArray[$authorPosition]; // for the author in question, extract the full author name (last name & initials)
$singleAuthorArray = preg_split( "/" . $oldAuthorsInitialsDelim . "/", $singleAuthor ); // then, extract author name & initials to separate list items
if ( !empty($singleAuthorArray[1]) ) {
$singleAuthorsGivenName = $singleAuthorArray[1]; // extract this author's last name into a new variable
} else {
$singleAuthorsGivenName = '';
}
return $singleAuthorsGivenName;
}
// Perform search & replace actions on the given text input:
// ('$includesSearchPatternDelimiters' must be a boolean value that specifies whether the leading and trailing slashes
// are included within the search pattern ['true'] or not ['false'])
public static function searchReplaceText( $sourceString, $includesSearchPatternDelimiters ) {
// apply the search & replace actions defined in '$transtab_refbase_html' to the text passed in '$sourceString':
foreach ( self::$transtab_refbase_html as $searchString => $replaceString ) {
if ( !$includesSearchPatternDelimiters ) {
$searchString = "/" . $searchString . "/"; // add search pattern delimiters
}
if ( preg_match($searchString, $sourceString ) ) {
$sourceString = preg_replace( $searchString, $replaceString, $sourceString );
}
}
return $sourceString;
}
}