X-Git-Url: https://scripts.mit.edu/gitweb/autoinstallsdev/mediawiki.git/blobdiff_plain/19e297c21b10b1b8a3acad5e73fc71dcb35db44a..6932310fd58ebef145fa01eb76edf7150284d8ea:/includes/cache/LinkBatch.php diff --git a/includes/cache/LinkBatch.php b/includes/cache/LinkBatch.php new file mode 100644 index 00000000..30d105b2 --- /dev/null +++ b/includes/cache/LinkBatch.php @@ -0,0 +1,246 @@ +addObj( $item ); + } + } + + /** + * Use ->setCaller( __METHOD__ ) to indicate which code is using this + * class. Only used in debugging output. + * @since 1.17 + * + * @param string $caller + */ + public function setCaller( $caller ) { + $this->caller = $caller; + } + + /** + * @param LinkTarget $linkTarget + */ + public function addObj( $linkTarget ) { + if ( is_object( $linkTarget ) ) { + $this->add( $linkTarget->getNamespace(), $linkTarget->getDBkey() ); + } else { + wfDebug( "Warning: LinkBatch::addObj got invalid LinkTarget object\n" ); + } + } + + /** + * @param int $ns + * @param string $dbkey + */ + public function add( $ns, $dbkey ) { + if ( $ns < 0 || $dbkey === '' ) { + return; // T137083 + } + if ( !array_key_exists( $ns, $this->data ) ) { + $this->data[$ns] = []; + } + + $this->data[$ns][strtr( $dbkey, ' ', '_' )] = 1; + } + + /** + * Set the link list to a given 2-d array + * First key is the namespace, second is the DB key, value arbitrary + * + * @param array $array + */ + public function setArray( $array ) { + $this->data = $array; + } + + /** + * Returns true if no pages have been added, false otherwise. + * + * @return bool + */ + public function isEmpty() { + return $this->getSize() == 0; + } + + /** + * Returns the size of the batch. + * + * @return int + */ + public function getSize() { + return count( $this->data ); + } + + /** + * Do the query and add the results to the LinkCache object + * + * @return array Mapping PDBK to ID + */ + public function execute() { + $linkCache = MediaWikiServices::getInstance()->getLinkCache(); + + return $this->executeInto( $linkCache ); + } + + /** + * Do the query and add the results to a given LinkCache object + * Return an array mapping PDBK to ID + * + * @param LinkCache &$cache + * @return array Remaining IDs + */ + protected function executeInto( &$cache ) { + $res = $this->doQuery(); + $this->doGenderQuery(); + $ids = $this->addResultToCache( $cache, $res ); + + return $ids; + } + + /** + * Add a ResultWrapper containing IDs and titles to a LinkCache object. + * As normal, titles will go into the static Title cache field. + * This function *also* stores extra fields of the title used for link + * parsing to avoid extra DB queries. + * + * @param LinkCache $cache + * @param ResultWrapper $res + * @return array Array of remaining titles + */ + public function addResultToCache( $cache, $res ) { + if ( !$res ) { + return []; + } + + $titleFormatter = MediaWikiServices::getInstance()->getTitleFormatter(); + // For each returned entry, add it to the list of good links, and remove it from $remaining + + $ids = []; + $remaining = $this->data; + foreach ( $res as $row ) { + $title = new TitleValue( (int)$row->page_namespace, $row->page_title ); + $cache->addGoodLinkObjFromRow( $title, $row ); + $pdbk = $titleFormatter->getPrefixedDBkey( $title ); + $ids[$pdbk] = $row->page_id; + unset( $remaining[$row->page_namespace][$row->page_title] ); + } + + // The remaining links in $data are bad links, register them as such + foreach ( $remaining as $ns => $dbkeys ) { + foreach ( $dbkeys as $dbkey => $unused ) { + $title = new TitleValue( (int)$ns, (string)$dbkey ); + $cache->addBadLinkObj( $title ); + $pdbk = $titleFormatter->getPrefixedDBkey( $title ); + $ids[$pdbk] = 0; + } + } + + return $ids; + } + + /** + * Perform the existence test query, return a ResultWrapper with page_id fields + * @return bool|ResultWrapper + */ + public function doQuery() { + if ( $this->isEmpty() ) { + return false; + } + + // This is similar to LinkHolderArray::replaceInternal + $dbr = wfGetDB( DB_REPLICA ); + $table = 'page'; + $fields = array_merge( + LinkCache::getSelectFields(), + [ 'page_namespace', 'page_title' ] + ); + + $conds = $this->constructSet( 'page', $dbr ); + + // Do query + $caller = __METHOD__; + if ( strval( $this->caller ) !== '' ) { + $caller .= " (for {$this->caller})"; + } + $res = $dbr->select( $table, $fields, $conds, $caller ); + + return $res; + } + + /** + * Do (and cache) {{GENDER:...}} information for userpages in this LinkBatch + * + * @return bool Whether the query was successful + */ + public function doGenderQuery() { + if ( $this->isEmpty() ) { + return false; + } + + global $wgContLang; + if ( !$wgContLang->needsGenderDistinction() ) { + return false; + } + + $genderCache = MediaWikiServices::getInstance()->getGenderCache(); + $genderCache->doLinkBatch( $this->data, $this->caller ); + + return true; + } + + /** + * Construct a WHERE clause which will match all the given titles. + * + * @param string $prefix The appropriate table's field name prefix ('page', 'pl', etc) + * @param IDatabase $db DB object to use + * @return string|bool String with SQL where clause fragment, or false if no items. + */ + public function constructSet( $prefix, $db ) { + return $db->makeWhereFrom2d( $this->data, "{$prefix}_namespace", "{$prefix}_title" ); + } +}