From: Tim Starling Date: Wed, 4 Jan 2006 23:53:51 +0000 (+0000) Subject: gave LinkBatch its own file X-Git-Tag: 1.6.0~763 X-Git-Url: http://git.cyclocoop.org/%24href?a=commitdiff_plain;h=d2adf8313108dbeab25d79d599008a154a60e21a;p=lhc%2Fweb%2Fwiklou.git gave LinkBatch its own file --- diff --git a/includes/LinkBatch.php b/includes/LinkBatch.php new file mode 100644 index 0000000000..7586cbc2f9 --- /dev/null +++ b/includes/LinkBatch.php @@ -0,0 +1,160 @@ +addObj( $item ); + } + } + + function addObj( $title ) { + if ( is_object( $title ) ) { + $this->add( $title->getNamespace(), $title->getDBkey() ); + } else { + wfDebug( "Warning: LinkBatch::addObj got invalid title object\n" ); + } + } + + function add( $ns, $dbkey ) { + if ( $ns < 0 ) { + return; + } + if ( !array_key_exists( $ns, $this->data ) ) { + $this->data[$ns] = array(); + } + + $this->data[$ns][$dbkey] = 1; + } + + /** + * Set the link list to a given 2-d array + * First key is the namespace, second is the DB key, value arbitrary + */ + function setArray( $array ) { + $this->data = $array; + } + + /** + * Do the query and add the results to a LinkCache object + * Return an array mapping PDBK to ID + */ + function execute( &$cache ) { + $fname = 'LinkBatch::execute'; + wfProfileIn( $fname ); + // Do query + $res = $this->doQuery(); + if ( !$res ) { + wfProfileOut( $fname ); + return array(); + } + + // For each returned entry, add it to the list of good links, and remove it from $remaining + + $ids = array(); + $remaining = $this->data; + while ( $row = $res->fetchObject() ) { + $title = Title::makeTitle( $row->page_namespace, $row->page_title ); + $cache->addGoodLinkObj( $row->page_id, $title ); + $ids[$title->getPrefixedDBkey()] = $row->page_id; + unset( $remaining[$row->page_namespace][$row->page_title] ); + } + $res->free(); + + // The remaining links in $data are bad links, register them as such + foreach ( $remaining as $ns => $dbkeys ) { + foreach ( $dbkeys as $dbkey => $nothing ) { + $title = Title::makeTitle( $ns, $dbkey ); + $cache->addBadLinkObj( $title ); + $ids[$title->getPrefixedDBkey()] = 0; + } + } + wfProfileOut( $fname ); + return $ids; + } + + /** + * Perform the existence test query, return a ResultWrapper with page_id fields + */ + function doQuery() { + $fname = 'LinkBatch::execute'; + $namespaces = array(); + + if ( !count( $this->data ) ) { + return false; + } + wfProfileIn( $fname ); + + // Construct query + // This is very similar to Parser::replaceLinkHolders + $dbr =& wfGetDB( DB_SLAVE ); + $page = $dbr->tableName( 'page' ); + $set = $this->constructSet( 'page', $dbr ); + if ( $set === false ) { + return false; + } + $sql = "SELECT page_id, page_namespace, page_title FROM $page WHERE $set"; + + // Do query + $res = new ResultWrapper( $dbr, $dbr->query( $sql, $fname ) ); + wfProfileOut( $fname ); + return $res; + } + + /** + * Construct a WHERE clause which will match all the given titles. + * Give the appropriate table's field name prefix ('page', 'pl', etc). + * + * @param string $prefix + * @return string + * @access public + */ + function constructSet( $prefix, &$db ) { + $first = true; + $firstTitle = true; + $sql = ''; + foreach ( $this->data as $ns => $dbkeys ) { + if ( !count( $dbkeys ) ) { + continue; + } + + if ( $first ) { + $first = false; + } else { + $sql .= ' OR '; + } + $sql .= "({$prefix}_namespace=$ns AND {$prefix}_title IN ("; + + $firstTitle = true; + foreach( $dbkeys as $dbkey => $nothing ) { + if ( $firstTitle ) { + $firstTitle = false; + } else { + $sql .= ','; + } + $sql .= $db->addQuotes( $dbkey ); + } + + $sql .= '))'; + } + if ( $first && $firstTitle ) { + # No titles added + return false; + } else { + return $sql; + } + } +} + +?> diff --git a/includes/LinkCache.php b/includes/LinkCache.php index d0371e4efa..da2a864650 100644 --- a/includes/LinkCache.php +++ b/includes/LinkCache.php @@ -277,162 +277,4 @@ class LinkCache { swap( $this->mPageLinks, $this->mOldPageLinks ); } } - -/** - * Class representing a list of titles - * The execute() method checks them all for existence and adds them to a LinkCache object - + - * @package MediaWiki - * @subpackage Cache - */ -class LinkBatch { - /** - * 2-d array, first index namespace, second index dbkey, value arbitrary - */ - var $data = array(); - - function LinkBatch( $arr = array() ) { - foreach( $arr as $item ) { - $this->addObj( $item ); - } - } - - function addObj( $title ) { - if ( is_object( $title ) ) { - $this->add( $title->getNamespace(), $title->getDBkey() ); - } else { - wfDebug( "Warning: LinkBatch::addObj got invalid title object\n" ); - } - } - - function add( $ns, $dbkey ) { - if ( $ns < 0 ) { - return; - } - if ( !array_key_exists( $ns, $this->data ) ) { - $this->data[$ns] = array(); - } - - $this->data[$ns][$dbkey] = 1; - } - - /** - * Set the link list to a given 2-d array - * First key is the namespace, second is the DB key, value arbitrary - */ - function setArray( $array ) { - $this->data = $array; - } - - /** - * Do the query and add the results to a LinkCache object - * Return an array mapping PDBK to ID - */ - function execute( &$cache ) { - $fname = 'LinkBatch::execute'; - wfProfileIn( $fname ); - // Do query - $res = $this->doQuery(); - if ( !$res ) { - wfProfileOut( $fname ); - return array(); - } - - // For each returned entry, add it to the list of good links, and remove it from $remaining - - $ids = array(); - $remaining = $this->data; - while ( $row = $res->fetchObject() ) { - $title = Title::makeTitle( $row->page_namespace, $row->page_title ); - $cache->addGoodLinkObj( $row->page_id, $title ); - $ids[$title->getPrefixedDBkey()] = $row->page_id; - unset( $remaining[$row->page_namespace][$row->page_title] ); - } - $res->free(); - - // The remaining links in $data are bad links, register them as such - foreach ( $remaining as $ns => $dbkeys ) { - foreach ( $dbkeys as $dbkey => $nothing ) { - $title = Title::makeTitle( $ns, $dbkey ); - $cache->addBadLinkObj( $title ); - $ids[$title->getPrefixedDBkey()] = 0; - } - } - wfProfileOut( $fname ); - return $ids; - } - - /** - * Perform the existence test query, return a ResultWrapper with page_id fields - */ - function doQuery() { - $fname = 'LinkBatch::execute'; - $namespaces = array(); - - if ( !count( $this->data ) ) { - return false; - } - wfProfileIn( $fname ); - - // Construct query - // This is very similar to Parser::replaceLinkHolders - $dbr =& wfGetDB( DB_SLAVE ); - $page = $dbr->tableName( 'page' ); - $set = $this->constructSet( 'page', $dbr ); - if ( $set === false ) { - return false; - } - $sql = "SELECT page_id, page_namespace, page_title FROM $page WHERE $set"; - - // Do query - $res = new ResultWrapper( $dbr, $dbr->query( $sql, $fname ) ); - wfProfileOut( $fname ); - return $res; - } - - /** - * Construct a WHERE clause which will match all the given titles. - * Give the appropriate table's field name prefix ('page', 'pl', etc). - * - * @param string $prefix - * @return string - * @access public - */ - function constructSet( $prefix, &$db ) { - $first = true; - $firstTitle = true; - $sql = ''; - foreach ( $this->data as $ns => $dbkeys ) { - if ( !count( $dbkeys ) ) { - continue; - } - - if ( $first ) { - $first = false; - } else { - $sql .= ' OR '; - } - $sql .= "({$prefix}_namespace=$ns AND {$prefix}_title IN ("; - - $firstTitle = true; - foreach( $dbkeys as $dbkey => $nothing ) { - if ( $firstTitle ) { - $firstTitle = false; - } else { - $sql .= ','; - } - $sql .= $db->addQuotes( $dbkey ); - } - - $sql .= '))'; - } - if ( $first && $firstTitle ) { - # No titles added - return false; - } else { - return $sql; - } - } -} - ?> diff --git a/includes/Setup.php b/includes/Setup.php index a63cf3fe1b..7409998ec7 100644 --- a/includes/Setup.php +++ b/includes/Setup.php @@ -52,6 +52,7 @@ require_once( 'User.php' ); require_once( 'Skin.php' ); require_once( 'OutputPage.php' ); require_once( 'LinkCache.php' ); +require_once( 'LinkBatch.php' ); require_once( 'Title.php' ); require_once( 'Article.php' ); require_once( 'MagicWord.php' );