Ferreteria/archive/mw-base.php
Code
<php> <?php /* PURPOSE: MediaWiki interface functions/classes -- functionality that MW forgot to include HISTORY:
2013-01-03 created for MWX_User 2013-01-24 extracted clsMWData and clsDataResult_MW from smw-base.php
- /
/*%%%%
PURPOSE: clsDatabase that uses MediaWiki's db
- /
abstract class clsMWData extends clsDatabase_abstract {
protected $mwDB;
public function __construct(DatabaseBase $iWikiDB) {
$this->mwDB = $iWikiDB;
}
protected function DefaultEngine() {
$obj = new clsDataEngine_MW(); $obj->MW_Object($this->mwDB); return $obj;
}
/* ****
SECTION: STATIC (GENERAL)
*/
public static function Spawn(DatabaseBase $iWikiDB=NULL) {
static $objSelf;
if (!isset($objSelf)) { $mwoDB = (is_null($iWikiDB)) ? (wfGetDB( DB_SLAVE )) : $iWikiDB;
$strClass = static::SelfClass(); // this requires PHP 5.3 $objSelf = new $strClass($mwoDB); $objSelf->Open(); } return $objSelf;
}
/*
/SECTION: END OTHER STATIC
**** */
/* ****
SECTION: abstract overrides
*/
public function Open() { } // do nothing; MW handles this
public function Shut() { } // do nothing; MW handles this
public function SafeParam($iVal) {
return mysql_escape_string($iVal); // this is engine-specific
}
public function Exec($iSQL) {
// to be written
}
public function DataSet($iSQL=NULL,$iClass=NULL) {
$mwo = $this->mwDB->query($iSQL); // $mwo is a ResultWrapper http://svn.wikimedia.org/doc/classResultWrapper.html // $re = $mwo->result; $objRes = new clsDataResult_MW(); // if (is_resource($re)) { $objRes->Result($mwo); $rs = new clsDataSet_bare(NULL); $rs->sqlMake = $iSQL; $rs->ResultHandler($objRes); $this->PrepareItem($rs); return $rs; /* } else { echo 'RE is '.get_class($re); throw new exception('MediaWiki query failed. SQL: '.$iSQL); }
- /
}
/*
/SECTION: END OTHER STATIC
**** */
protected function PrepareItem(clsRecs_abstract $iItem) {
$iItem->objDB = $this;
}
}
class clsDataEngine_MW extends clsDataEngine {
private $mwo;
public function MW_Object(DatabaseBase $iDB=NULL) {
if (!is_null($iDB)) { $this->mwo = $iDB; } return $iDB;
}
/* ****
SECTION: abstract overrides
*/
public function db_open() {} // do nothing -- handled by MW
public function db_shut() {} // do nothing -- handled by MW
/* ****
SECTION: non-abstract overrides
*/
public function db_do_query($iSQL) {
$obj = $this->Spawn_ResultObject(); $obj->do_query($this->mwo,$iSQL); return $obj;
}
public function db_get_new_id() {
die(__METHOD__.' not yet implemented.');
}
public function db_safe_param($iVal) {
die(__METHOD__.' not yet implemented.');
}
public function db_get_error() {
die(__METHOD__.' not yet implemented.');
}
public function db_get_qty_rows_chgd() {
die(__METHOD__.' not yet implemented.');
}
/*
/SECTION: end abstract overrides
**** */
protected function Spawn_ResultObject() {
return new clsDataResult_MW();
}
}
class clsDataResult_MW extends clsDataResult {
/*----
NOTE: Must be public so new objects can be initialized with ResultWrapper
*/
public function Result(ResultWrapper $iRes=NULL) {
if (!is_null($iRes)) { $this->box['res'] = $iRes; } return $this->box['res'];
}
protected function ResultClear() {
$this->box['res'] = NULL;
}
public function do_query(DatabaseBase $iDB_MW,$iSQL) {
$mwoRes = $iDB_MW->query($iSQL); if (is_object($mwoRes)) { $this->Result($mwoRes); $this->box['ok'] = TRUE; } else { echo 'SQL ['.$iSQL.'] did not create a result object.'; $this->ResultClear(); $this->box['ok'] = FALSE; }
}
public function is_okay() {
return $this->box['ok'];
}
/*----
ACTION: set the record pointer so the first row in the set will be read next
*/
public function do_rewind() {
return $this->Result()->rewing(); // not tested
}
/*----
ACTION: Fetch the first/next row of data from a result set
*/
public function get_next() {
$objRow = $this->Result()->next(); $arRow = (array)$objRow; return $arRow;
}
/*----
ACTION: Return the number of rows in the result set
*/
public function get_count() {
return $this->Result()->numRows();
}
/*----
ACTION: Return whether row currently has data.
*/
public function is_filled() {
return $this->Result()->valid(); // not sure if this is exactly equivalent
}
}
/* %%%%
PURPOSE: extension/helper for MediaWiki "User" class
- /
class MWX_User {
private $mwoUser;
/* ****
STATIC
*/
public static function NewFromID($idUser) {
$mwoUser = User::newFromId($idUser); $mwxUser = new MWX_User($mwoUser); return $mwxUser;
}
public static function TextFromID_ht($idUser) {
if ($idUser == 0) { return 'unknown'; } else { $mwxUser = static::NewFromID($idUser); return $mwxUser->PageLink_ht(); }
}
/* ****
DYNAMIC
*/
public function __construct(User $iUser) {
$this->mwoUser = $iUser;
}
public function MW_Object() {
return $this->mwoUser;
}
public function PageLink_wt($iText=NULL) {
$mwoUser = $this->MW_Object(); $strText = (is_null($iText))?($mwoUser->getName()):$iText; $mwoTitle = $mwoUser->getUserPage(); $wpage = $mwoTitle->getPrefixedText(); return "$strText";
}
public function PageLink_ht($iText=NULL,$iPopup=NULL) {
$mwoUser = $this->MW_Object(); $strText = (is_null($iText))?($mwoUser->getName()):$iText; $mwoTitle = $mwoUser->getUserPage(); $url = $mwoTitle->getFullURL(); $out = '<a href="'.$url.'"'; if (!is_null($iPopup)) { $out .= ' title="'.$iPopup.'"'; } $out .= ">$strText</a>"; return $out;
}
} </php>