/**
* See docs/deferred.txt
*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
* @todo document (e.g. one-sentence top-level class description).
*/
class LinksUpdate {
$mLinks, //!< Map of title strings to IDs for the links in the document
$mImages, //!< DB keys of the images used, in the array key only
$mTemplates, //!< Map of title strings to IDs for the template references, including broken ones
+ $mDistantTemplates,//!< Map of title strings to IDs for the distant template references, including broken ones
$mExternals, //!< URLs of external links, array key only
$mCategories, //!< Map of category names to sort keys
$mInterlangs, //!< Map of language codes to titles
$this->mLinks = $parserOutput->getLinks();
$this->mImages = $parserOutput->getImages();
$this->mTemplates = $parserOutput->getTemplates();
+ $this->mDistantTemplates = $parserOutput->getDistantTemplates();
$this->mExternals = $parserOutput->getExternalLinks();
$this->mCategories = $parserOutput->getCategories();
$this->mProperties = $parserOutput->getProperties();
$existing = $this->getExistingImages();
$imageDeletes = $this->getImageDeletions( $existing );
- $this->incrTableUpdate( 'imagelinks', 'il', $imageDeletes, $this->getImageInsertions( $existing ) );
+ $this->incrTableUpdate( 'imagelinks', 'il', $imageDeletes,
+ $this->getImageInsertions( $existing ) );
# Invalidate all image description pages which had links added or removed
$imageUpdates = $imageDeletes + array_diff_key( $this->mImages, $existing );
# External links
$existing = $this->getExistingExternals();
$this->incrTableUpdate( 'externallinks', 'el', $this->getExternalDeletions( $existing ),
- $this->getExternalInsertions( $existing ) );
+ $this->getExternalInsertions( $existing ) );
# Language links
$existing = $this->getExistingInterlangs();
$this->incrTableUpdate( 'templatelinks', 'tl', $this->getTemplateDeletions( $existing ),
$this->getTemplateInsertions( $existing ) );
+ # Distant template links
+ global $wgGlobalDatabase;
+ if ( $wgGlobalDatabase ) {
+ $existing = $this->getDistantExistingTemplates();
+ $this->incrSharedTableUpdate( 'globaltemplatelinks', 'gtl',
+ $this->getDistantTemplateDeletions( $existing ),
+ $this->getDistantTemplateInsertions( $existing ) );
+ }
+
# Category links
$existing = $this->getExistingCategories();
$categoryDeletes = $this->getCategoryDeletions( $existing );
- $this->incrTableUpdate( 'categorylinks', 'cl', $categoryDeletes, $this->getCategoryInsertions( $existing ) );
+ $this->incrTableUpdate( 'categorylinks', 'cl', $categoryDeletes,
+ $this->getCategoryInsertions( $existing ) );
# Invalidate all categories which were added, deleted or changed (set symmetric difference)
$categoryInserts = array_diff_assoc( $this->mCategories, $existing );
$propertiesDeletes = $this->getPropertyDeletions( $existing );
- $this->incrTableUpdate( 'page_props', 'pp', $propertiesDeletes, $this->getPropertyInsertions( $existing ) );
+ $this->incrTableUpdate( 'page_props', 'pp', $propertiesDeletes,
+ $this->getPropertyInsertions( $existing ) );
# Invalidate the necessary pages
$changed = $propertiesDeletes + array_diff_assoc( $this->mProperties, $existing );
foreach ( $batches as $batch ) {
list( $start, $end ) = $batch;
$params = array(
+ 'table' => 'templatelinks',
'start' => $start,
'end' => $end,
);
if ( $where ) {
$this->mDb->delete( $table, $where, __METHOD__ );
}
+ if ( isset( $insertions['globaltemplatelinks'] ) ) {
+ $this->mDb->insert( 'globaltemplatelinks', $insertions['globaltemplatelinks'], __METHOD__, 'IGNORE' );
+ unset( $insertions['globaltemplatelinks'] );
+ }
+ if ( isset( $insertions['globalnamespaces'] ) ) {
+ $this->mDb->insert( 'globalnamespaces', $insertions['globalnamespaces'], __METHOD__, 'IGNORE' );
+ unset( $insertions['globalnamespaces'] );
+ }
+ if ( isset( $insertions['globalinterwiki'] ) ) {
+ $this->mDb->insert( 'globalinterwiki', $insertions['globalinterwiki'], __METHOD__, 'IGNORE' );
+ unset( $insertions['globalinterwiki'] );
+ }
if ( count( $insertions ) ) {
$this->mDb->insert( $table, $insertions, __METHOD__, 'IGNORE' );
}
}
+ /**
+ * Update a shared table by doing a delete query then an insert query
+ * @private
+ */
+ function incrSharedTableUpdate( $table, $prefix, $deletions, $insertions ) {
+ global $wgGlobalDatabase;
+
+ if ( $wgGlobalDatabase ) {
+ $dbw = wfGetDB( DB_MASTER, array(), $wgGlobalDatabase );
+ $where = array( "{$prefix}_from_wiki" => wfWikiID(),
+ "{$prefix}_from_page" => $this->mId
+ );
+ $baseKey = "{$prefix}_to_wiki";
+ $middleKey = "{$prefix}_to_namespace";
+
+ $clause = $dbw->makeWhereFrom3d( $deletions, $baseKey, $middleKey, "{$prefix}_to_title" );
+ if ( $clause ) {
+ $where[] = $clause;
+ } else {
+ $where = false;
+ }
+
+ if ( $where ) {
+ $dbw->delete( $table, $where, __METHOD__ );
+ }
+ if ( count( $insertions ) ) {
+ $dbw->insert( $table, $insertions, __METHOD__, 'IGNORE' );
+ }
+ }
+ }
/**
* Get an array of pagelinks insertions for passing to the DB
function getLinkInsertions( $existing = array() ) {
$arr = array();
foreach( $this->mLinks as $ns => $dbkeys ) {
- $diffs = isset( $existing[$ns] ) ? array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
+ $diffs = isset( $existing[$ns] )
+ ? array_diff_key( $dbkeys, $existing[$ns] )
+ : $dbkeys;
foreach ( $diffs as $dbk => $id ) {
$arr[] = array(
'pl_from' => $this->mId,
return $arr;
}
+ /**
+ * Get an array of distant template insertions. Like getLinkInsertions()
+ * @private
+ */
+ function getDistantTemplateInsertions( $existing = array() ) {
+ $arr = array();
+ foreach( $this->mDistantTemplates as $prefix => $templatesToNS ) {
+ foreach( $templatesToNS as $ns => $dbkeys ) {
+ $diffs = isset( $existing[$prefix] ) && isset( $existing[$prefix][$ns] )
+ ? array_diff_key( $dbkeys, $existing[$prefix][$ns] )
+ : $dbkeys;
+ $interwiki = Interwiki::fetch( $prefix );
+ $wikiid = $interwiki->getWikiID();
+ foreach ( $diffs as $dbk => $id ) {
+ $arr['globaltemplatelinks'][] = array(
+ 'gtl_from_wiki' => wfWikiID(),
+ 'gtl_from_page' => $this->mId,
+ 'gtl_from_namespace' => $this->mTitle->getNamespace(),
+ 'gtl_from_title' => $this->mTitle->getText(),
+ 'gtl_to_wiki' => $wikiid,
+ 'gtl_to_namespace' => $ns,
+ 'gtl_to_title' => $dbk
+ );
+ $arr['globalinterwiki'][] = array(
+ 'giw_wikiid' => $wikiid,
+ 'giw_prefix' => $prefix,
+ );
+ $arr['globalnamespaces'][] = array(
+ 'gn_wiki' => wfWikiID( ),
+ 'gn_namespace' => $this->mTitle->getNamespace(),
+ 'gn_namespacetext' => $this->mTitle->getNsText(),
+ );
+ }
+ }
+ }
+ return $arr;
+ }
+
/**
* Get an array of image insertions
* Skips the names specified in $existing
* @private
*/
function getInterlangInsertions( $existing = array() ) {
- $diffs = array_diff_assoc( $this->mInterlangs, $existing );
- $arr = array();
- foreach( $diffs as $lang => $title ) {
- $arr[] = array(
- 'll_from' => $this->mId,
- 'll_lang' => $lang,
- 'll_title' => $title
- );
- }
- return $arr;
+ $diffs = array_diff_assoc( $this->mInterlangs, $existing );
+ $arr = array();
+ foreach( $diffs as $lang => $title ) {
+ $arr[] = array(
+ 'll_from' => $this->mId,
+ 'll_lang' => $lang,
+ 'll_title' => $title
+ );
+ }
+ return $arr;
}
/**
return $del;
}
+ /**
+ * Given an array of existing templates, returns those templates which are not in $this
+ * and thus should be deleted.
+ * @private
+ */
+ function getDistantTemplateDeletions( $existing ) {
+ $del = array();
+ foreach ( $existing as $wikiid => $templatesForNS ) {
+ if ( isset( $this->mDistantTemplates[$wikiid] ) ) {
+ $del[$wikiid] = array_diff_key( $existing[$wikiid], $this->mDistantTemplates[$wikiid] );
+ } else {
+ $del[$wikiid] = $existing[$wikiid];
+ }
+ foreach ( $templatesForNS as $ns => $dbkeys ) {
+ if ( isset( $this->mDistantTemplates[$wikiid][$ns] ) ) {
+ $del[$wikiid][$ns] = array_diff_key( $existing[$wikiid][$ns], $this->mDistantTemplates[$wikiid][$ns] );
+ } else {
+ $del[$wikiid][$ns] = $existing[$wikiid][$ns];
+ }
+ }
+ }
+ return $del;
+ }
+
/**
* Given an array of existing images, returns those images which are not in $this
* and thus should be deleted.
* @private
*/
function getInterlangDeletions( $existing ) {
- return array_diff_assoc( $existing, $this->mInterlangs );
+ return array_diff_assoc( $existing, $this->mInterlangs );
}
/**
return $arr;
}
+ /**
+ * Get an array of existing distant templates, as a 3-D array
+ * @private
+ */
+ function getDistantExistingTemplates() {
+ global $wgGlobalDatabase;
+
+ $arr = array();
+ if ( $wgGlobalDatabase ) {
+ $dbr = wfGetDB( DB_SLAVE, array(), $wgGlobalDatabase );
+ $res = $dbr->select(
+ 'globaltemplatelinks',
+ array( 'gtl_to_wiki', 'gtl_to_namespace', 'gtl_to_title' ),
+ array( 'gtl_from_wiki' => wfWikiID(), 'gtl_from_page' => $this->mId ),
+ __METHOD__,
+ $this->mOptions
+ );
+ foreach ( $res as $row ) {
+ if ( !isset( $arr[$row->gtl_to_wiki] ) ) {
+ $arr[$row->gtl_to_wiki] = array();
+ }
+ if ( !isset( $arr[$row->gtl_to_wiki][$row->gtl_to_namespace] ) ) {
+ $arr[$row->gtl_to_wiki][$row->gtl_to_namespace] = array();
+ }
+ $arr[$row->gtl_to_wiki][$row->gtl_to_namespace][$row->gtl_to_title] = 1;
+ }
+ }
+ return $arr;
+ }
+
/**
* Get an array of existing images, image names in the keys
* @private
function getTitle() {
return $this->mTitle;
}
-
+
/**
* Return the list of images used as generated by the parser
*/