r37322 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r37321‎ | r37322 | r37323 >
Date:15:02, 8 July 2008
Author:btongminh
Status:old
Tags:
Comment:
(bug 13588) Experimentally track link changes if $wgTrackLinkChanges is set to true. Requires schema change.
Modified paths:
  • /trunk/phase3/RELEASE-NOTES (modified) (history)
  • /trunk/phase3/includes/DefaultSettings.php (modified) (history)
  • /trunk/phase3/includes/LinksUpdate.php (modified) (history)
  • /trunk/phase3/maintenance/tables.sql (modified) (history)

Diff [purge]

Index: trunk/phase3/maintenance/tables.sql
@@ -1237,4 +1237,28 @@
12381238 PRIMARY KEY (ul_key)
12391239 ) /*$wgDBTableOptions*/;
12401240
 1241+-- A table to track link changes
 1242+-- Experimental: enable using $wgTrackLinkChanges
 1243+CREATE TABLE /*$wgDBprefix*/recentlinkchanges (
 1244+ rlc_id int unsigned NOT NULL auto_increment,
 1245+
 1246+ -- page, image, category, ...
 1247+ rlc_type varchar(15) binary NOT NULL,
 1248+ rlc_timestamp binary(14) NOT NULL default '',
 1249+ -- 1: insert; 2: deletion;
 1250+ -- should probably make this an enum...
 1251+ rlc_action tinyint(1) NOT NULL default 0,
 1252+
 1253+ -- page where the links are on
 1254+ rlc_from int NOT NULL,
 1255+
 1256+ rlc_to_namespace int,
 1257+ rlc_to_title varchar(255) binary,
 1258+ rlc_to_blob blob,
 1259+
 1260+ PRIMARY KEY(rlc_id),
 1261+ KEY from_timestamp (rlc_type, rlc_timestamp),
 1262+ KEY to_timestamp (rlc_to_namespace, rlc_to_title, rlc_timestamp)
 1263+) /*$wgDBTableOptions*/;
 1264+
12411265 -- vim: sw=2 sts=2 et
Index: trunk/phase3/includes/LinksUpdate.php
@@ -12,12 +12,19 @@
1313 var $mId, //!< Page ID of the article linked from
1414 $mTitle, //!< Title object of the article linked from
1515 $mLinks, //!< Map of title strings to IDs for the links in the document
 16+ $mExistingLinks,
1617 $mImages, //!< DB keys of the images used, in the array key only
 18+ $mExistingImages,
1719 $mTemplates, //!< Map of title strings to IDs for the template references, including broken ones
 20+ $mExistingTemplates,
1821 $mExternals, //!< URLs of external links, array key only
 22+ $mExistingExternals,
1923 $mCategories, //!< Map of category names to sort keys
 24+ $mExistingCategories,
2025 $mInterlangs, //!< Map of language codes to titles
 26+ $mExistingInterlangs,
2127 $mProperties, //!< Map of arbitrary name to value
 28+ $mExistingProperties,
2229 $mDb, //!< Database connection reference
2330 $mOptions, //!< SELECT options to be used (array)
2431 $mRecursive; //!< Whether to queue jobs for recursive updates
@@ -75,7 +82,7 @@
7683 * Update link tables with outgoing links from an updated article
7784 */
7885 function doUpdate() {
79 - global $wgUseDumbLinkUpdate;
 86+ global $wgUseDumbLinkUpdate, $wgTrackLinkChanges;
8087
8188 wfRunHooks( 'LinksUpdate', array( &$this ) );
8289 if ( $wgUseDumbLinkUpdate ) {
@@ -83,6 +90,8 @@
8491 } else {
8592 $this->doIncrementalUpdate();
8693 }
 94+ if ( $wgTrackLinkChanges )
 95+ $this->makeRecentlinkchanges();
8796 wfRunHooks( 'LinksUpdateComplete', array( &$this ) );
8897
8998 }
@@ -565,6 +574,9 @@
566575 * @private
567576 */
568577 function getExistingLinks() {
 578+ if ( is_array( $this->mExistingLinks ) )
 579+ return $this->mExistingLinks;
 580+
569581 $res = $this->mDb->select( 'pagelinks', array( 'pl_namespace', 'pl_title' ),
570582 array( 'pl_from' => $this->mId ), __METHOD__, $this->mOptions );
571583 $arr = array();
@@ -575,7 +587,7 @@
576588 $arr[$row->pl_namespace][$row->pl_title] = 1;
577589 }
578590 $this->mDb->freeResult( $res );
579 - return $arr;
 591+ return $this->mExistingLinks = $arr;
580592 }
581593
582594 /**
@@ -583,6 +595,9 @@
584596 * @private
585597 */
586598 function getExistingTemplates() {
 599+ if ( is_array( $this->mExistingTemplates ) )
 600+ return $this->mExistingTemplates;
 601+
587602 $res = $this->mDb->select( 'templatelinks', array( 'tl_namespace', 'tl_title' ),
588603 array( 'tl_from' => $this->mId ), __METHOD__, $this->mOptions );
589604 $arr = array();
@@ -593,7 +608,7 @@
594609 $arr[$row->tl_namespace][$row->tl_title] = 1;
595610 }
596611 $this->mDb->freeResult( $res );
597 - return $arr;
 612+ return $this->mExistingTemplates = $arr;
598613 }
599614
600615 /**
@@ -601,6 +616,9 @@
602617 * @private
603618 */
604619 function getExistingImages() {
 620+ if ( is_array( $this->mExistingImages ) )
 621+ return $this->mExistingImages;
 622+
605623 $res = $this->mDb->select( 'imagelinks', array( 'il_to' ),
606624 array( 'il_from' => $this->mId ), __METHOD__, $this->mOptions );
607625 $arr = array();
@@ -608,7 +626,7 @@
609627 $arr[$row->il_to] = 1;
610628 }
611629 $this->mDb->freeResult( $res );
612 - return $arr;
 630+ return $this->mExistingImages = $arr;
613631 }
614632
615633 /**
@@ -616,6 +634,9 @@
617635 * @private
618636 */
619637 function getExistingExternals() {
 638+ if ( is_array( $this->mExistingExternals ) )
 639+ return $this->mExistingExternals;
 640+
620641 $res = $this->mDb->select( 'externallinks', array( 'el_to' ),
621642 array( 'el_from' => $this->mId ), __METHOD__, $this->mOptions );
622643 $arr = array();
@@ -623,7 +644,7 @@
624645 $arr[$row->el_to] = 1;
625646 }
626647 $this->mDb->freeResult( $res );
627 - return $arr;
 648+ return $this->mExistingExternals = $arr;
628649 }
629650
630651 /**
@@ -631,6 +652,9 @@
632653 * @private
633654 */
634655 function getExistingCategories() {
 656+ if ( is_array( $this->mExistingCategories ) )
 657+ return $this->mExistingCategories;
 658+
635659 $res = $this->mDb->select( 'categorylinks', array( 'cl_to', 'cl_sortkey' ),
636660 array( 'cl_from' => $this->mId ), __METHOD__, $this->mOptions );
637661 $arr = array();
@@ -638,7 +662,7 @@
639663 $arr[$row->cl_to] = $row->cl_sortkey;
640664 }
641665 $this->mDb->freeResult( $res );
642 - return $arr;
 666+ return $this->mExistingCategories = $arr;
643667 }
644668
645669 /**
@@ -647,13 +671,16 @@
648672 * @private
649673 */
650674 function getExistingInterlangs() {
 675+ if ( is_array( $this->mExistingInterlangs ) )
 676+ return $this->mExistingInterlangs;
 677+
651678 $res = $this->mDb->select( 'langlinks', array( 'll_lang', 'll_title' ),
652679 array( 'll_from' => $this->mId ), __METHOD__, $this->mOptions );
653680 $arr = array();
654681 while ( $row = $this->mDb->fetchObject( $res ) ) {
655682 $arr[$row->ll_lang] = $row->ll_title;
656683 }
657 - return $arr;
 684+ return $this->mExistingInterlangs = $arr;
658685 }
659686
660687 /**
@@ -661,6 +688,9 @@
662689 * @private
663690 */
664691 function getExistingProperties() {
 692+ if ( is_array( $this->mExistingProperties ) )
 693+ return $this->mExistingProperties;
 694+
665695 $res = $this->mDb->select( 'page_props', array( 'pp_propname', 'pp_value' ),
666696 array( 'pp_page' => $this->mId ), __METHOD__, $this->mOptions );
667697 $arr = array();
@@ -668,7 +698,7 @@
669699 $arr[$row->pp_propname] = $row->pp_value;
670700 }
671701 $this->mDb->freeResult( $res );
672 - return $arr;
 702+ return $this->mExistingProperties = $arr;
673703 }
674704
675705
@@ -698,4 +728,124 @@
699729 }
700730 }
701731 }
 732+
 733+ // Recentlinkchanges constants
 734+ const INSERTION = 1;
 735+ const DELETION = 2;
 736+ private static $rlcFields = array(
 737+ 'rlc_type',
 738+ 'rlc_timestamp',
 739+ 'rlc_action',
 740+ 'rlc_from',
 741+ 'rlc_to_namespace',
 742+ 'rlc_to_title',
 743+ 'rlc_to_blob'
 744+ );
 745+ /*
 746+ * Insert items to recentlinkchanges
 747+ */
 748+ function makeRecentlinkchanges() {
 749+ $insert = array();
 750+ $now = $this->mDb->timestamp();
 751+
 752+ // Category changes
 753+ $existing = array_keys( $this->getExistingCategories() );
 754+ $current = array_keys( $this->mCategories );
 755+ $this->simpleAddToLinkchanges( $insert, 'category', $now, $existing, $current, NS_CATEGORY );
 756+
 757+ // External links
 758+ $existing = array_keys( $this->getExistingExternals() );
 759+ $current = array_keys( $this->mExternals );
 760+ $insertions = array_diff( $current, $existing );
 761+ foreach ( $insertions as $item )
 762+ $insert[] = array(
 763+ 'external', $now, self::INSERTION,
 764+ $this->mId, null, null, $item );
 765+ $deletions = array_diff( $existing, $current );
 766+ foreach ( $deletions as $item )
 767+ $insert[] = array(
 768+ 'external', $now, self::DELETION,
 769+ $this->mId, null, null, $item );
 770+
 771+ // Image changes
 772+ $existing = array_keys( $this->getExistingImages() );
 773+ $current = array_keys( $this->mImages );
 774+ $this->simpleAddToLinkchanges( $insert, 'image', $now, $existing, $current, NS_IMAGE );
 775+
 776+ // Interlangs
 777+ $existing = $this->getExistingInterlangs();
 778+ $current = $this->mInterlangs;
 779+ $this->assocAddToLinkchanges( $insert, 'interlang', $existing, $current );
 780+
 781+ // Page links
 782+ $existing = $this->getExistingLinks();
 783+ $current = $this->mLinks;
 784+ $this->addToLinkChangesByNamespace( $insert, 'page', $now, $existing, $current);
 785+
 786+ // Properties
 787+ $existing = $this->getExistingProperties();
 788+ $current = $this->mProperties;
 789+ $this->assocAddToLinkchanges( $insert, 'property', $existing, $current );
 790+
 791+ // Templates
 792+ $existing = $this->getExistingTemplates();
 793+ $current = $this->mTemplates;
 794+ $this->addToLinkChangesByNamespace( $insert, 'template', $now, $existing, $current);
 795+
 796+ $this->mDb->insert( 'recentlinkchanges', $insert, __METHOD__ );
 797+
 798+ }
 799+
 800+ /*
 801+ * Compute the difference for arrays of titles with namespace $ns and add
 802+ * them to $insert.
 803+ */
 804+ private function simpleAddToLinkchanges( &$insert, $type, $now, $existing, $current, $ns ) {
 805+
 806+ $insertions = array_diff( $current, $existing );
 807+ foreach ( $insertions as $item )
 808+ $insert[] = array_combine(self::$rlcFields, array(
 809+ $type, $now, self::INSERTION,
 810+ $this->mId, $ns, $item, null
 811+ ) );
 812+ $deletions = array_diff( $existing, $current );
 813+ foreach ( $deletions as $item )
 814+ $insert[] = array_combine(self::$rlcFields, array(
 815+ $type, $now, self::DELETION,
 816+ $this->mId, $ns, $item, null
 817+ ) );
 818+
 819+ }
 820+
 821+ /*
 822+ * Compute the difference for associative arrays and insert them to
 823+ * $insert as title => blob.
 824+ */
 825+ function assocAddToLinkChanges( &$insert, $type, $now, $existing, $current ) {
 826+ $insertions = array_diff_assoc( $current, $existing );
 827+ foreach ( $insertions as $key => $value )
 828+ $insert[] = array_combine(self::$rlcFields, array(
 829+ $type, $now, self::INSERTION,
 830+ $this->mId, null, $key, $value
 831+ ) );
 832+ $deletions = array_diff_assoc( $existing, $current );
 833+ foreach ( $deletions as $key => $value )
 834+ $insert[] = array_combine(self::$rlcFields, array(
 835+ $type, $now, self::DELETION,
 836+ $this->mId, null, $key, $value
 837+ ) );
 838+ }
 839+
 840+ /*
 841+ * Format arrays in the form $namespace => $titleArray for use in
 842+ * simpleAddLinkLinkChanges
 843+ */
 844+ function addToLinkChangesByNamespace( &$insert, $type, $now, $existing, $current ) {
 845+ $namespaces = array_merge( array_keys( $existing ), array_keys( $current ) );
 846+ foreach ( $namespaces as $ns )
 847+ $this->simpleAddToLinkchanges( $insert, $type, $now,
 848+ isset( $existing[$ns] ) ? array_keys( $existing[$ns] ) : array(),
 849+ isset( $current[$ns] ) ? array_keys( $current[$ns] ) : array(),
 850+ $ns );
 851+ }
702852 }
Index: trunk/phase3/includes/DefaultSettings.php
@@ -3262,3 +3262,8 @@
32633263 * ting this variable false.
32643264 */
32653265 $wgUseAutomaticEditSummaries = true;
 3266+
 3267+/*
 3268+ * Track link changes
 3269+ */
 3270+$wgTrackLinkChanges = false;
Index: trunk/phase3/RELEASE-NOTES
@@ -185,7 +185,9 @@
186186 * Add a new hook NormalizeMessageKey to allow extensions to replace messages before
187187 the database is potentially queried
188188 * (bug 9736) Redirects on Special:Fewestrevisions are now marked as such.
189 -
 189+* (bug 13588) Experimentally track link changes if $wgTrackLinkChanges is set
 190+ to true. Requires schema change.
 191+
190192 === Bug fixes in 1.13 ===
191193
192194 * (bug 10677) Add link to the file description page on the shared repository

Follow-up revisions

RevisionCommit summaryAuthorDate
r37542Hold back $wgTrackLinkChanges for a little bit; reverting r37322, 37430, 3743...brion22:11, 10 July 2008

Status & tagging log