r74208 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r74207‎ | r74208 | r74209 >
Date:14:47, 3 October 2010
Author:nikola
Status:deferred (Comments)
Tags:
Comment:
Pair to the Interlanguage extension.

When a new interlanguage link is added to the central wiki, the
Interlanguage Central extension purges the articles on the dependant
wikis and updates their interlanguage links.
Modified paths:
  • /trunk/extensions/Interlanguage/InterlanguageCentral.i18n.php (added) (history)
  • /trunk/extensions/Interlanguage/InterlanguageCentral.php (added) (history)

Diff [purge]

Index: trunk/extensions/Interlanguage/InterlanguageCentral.i18n.php
@@ -0,0 +1,12 @@
 2+<?php
 3+/**
 4+ * Internationalisation file for InterlanguageCentral extension.
 5+ *
 6+ * @addtogroup Extensions
 7+ */
 8+
 9+$messages = array();
 10+
 11+$messages['en'] = array(
 12+ 'interlanguagecentral-desc' => 'Purges page caches on dependent wikis upon interlanguage links change',
 13+);
Property changes on: trunk/extensions/Interlanguage/InterlanguageCentral.i18n.php
___________________________________________________________________
Added: svn:eol-style
114 + native
Index: trunk/extensions/Interlanguage/InterlanguageCentral.php
@@ -0,0 +1,130 @@
 2+<?php
 3+# MediaWiki InterlanguageCentral extension v1.0
 4+#
 5+# Copyright © 2010 Nikola Smolenski <smolensk@eunet.rs>
 6+#
 7+# This program is free software; you can redistribute it and/or modify
 8+# it under the terms of the GNU General Public License as published by
 9+# the Free Software Foundation; either version 2 of the License, or
 10+# (at your option) any later version.
 11+#
 12+# This program is distributed in the hope that it will be useful,
 13+# but WITHOUT ANY WARRANTY; without even the implied warranty of
 14+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 15+# GNU General Public License for more details.
 16+#
 17+# You should have received a copy of the GNU General Public License
 18+# along with this program; if not, write to the Free Software
 19+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 20+#
 21+# For more information see
 22+# http://www.mediawiki.org/wiki/Extension:Interlanguage
 23+
 24+$wgExtensionFunctions[]="wfInterlanguageCentralExtension";
 25+$wgJobClasses['purgeDependentWikis'] = 'InterlanguageCentralExtensionPurgeJob';
 26+$wgExtensionCredits['parserhook'][] = array(
 27+ 'name' => 'Interlanguage Central',
 28+ 'author' => 'Nikola Smolenski',
 29+ 'url' => 'http://www.mediawiki.org/wiki/Extension:Interlanguage',
 30+ 'version' => '1.0',
 31+ 'descriptionmsg' => 'interlanguagecentral-desc',
 32+);
 33+$wgExtensionMessagesFiles['Interlanguagecentral'] = dirname(__FILE__) . '/InterlanguageCentral.i18n.php';
 34+
 35+function wfInterlanguageCentralExtension() {
 36+ global $wgHooks, $wgInterlanguageCentralExtension;
 37+
 38+ $wgInterlanguageCentralExtension = new InterlanguageCentralExtension();
 39+ $wgHooks['ArticleSave'][] = $wgInterlanguageCentralExtension;
 40+ $wgHooks['ArticleSaveComplete'][] = $wgInterlanguageCentralExtension;
 41+ //TODO: ArticleDelete etc.
 42+}
 43+
 44+class InterlanguageCentralExtension {
 45+ //ILL = InterLanguageLinks
 46+ var $oldILL = array();
 47+
 48+ function onArticleSave() {
 49+ global $wgTitle;
 50+ $this->oldILL = $this->getILL($wgTitle);
 51+ return true;
 52+ }
 53+
 54+ function onArticleSaveComplete() {
 55+ global $wgTitle;
 56+
 57+ $newILL = $this->getILL($wgTitle);
 58+
 59+ //Compare ILLs before and after the save; if nothing changed, there is no need to purge
 60+ if(
 61+ count(array_udiff_assoc(
 62+ $this->oldILL,
 63+ $newILL,
 64+ "InterlanguageCentralExtension::arrayCompareKeys"
 65+ )) || count(array_udiff_assoc(
 66+ $newILL,
 67+ $this->oldILL,
 68+ "InterlanguageCentralExtension::arrayCompareKeys"
 69+ ))
 70+ ) {
 71+ $ill = array_merge_recursive($this->oldILL, $newILL);
 72+ $job = new InterlanguageCentralExtensionPurgeJob( $wgTitle, array('ill' => $ill) );
 73+ $job->insert();
 74+ }
 75+
 76+ return true;
 77+
 78+ }
 79+
 80+ function getILL($title) {
 81+ $dbr = wfGetDB( DB_SLAVE );
 82+ $res = $dbr->select( 'langlinks', array( 'll_lang', 'll_title' ), array( 'll_from' => $title->mArticleID), __FUNCTION__);
 83+ $a = array();
 84+ while ( $row = $dbr->fetchObject( $res ) ) {
 85+ if(!isset($a[$row->ll_lang])) {
 86+ $a[$row->ll_lang] = array();
 87+ }
 88+ $a[$row->ll_lang][$row->ll_title] = true;
 89+ }
 90+ $dbr->freeResult( $res );
 91+
 92+ return $a;
 93+ }
 94+
 95+ static function arrayCompareKeys($a, $b) {
 96+ return count(array_diff_key($a, $b))? 1: (count(array_diff_key($b, $a))? -1: 0);
 97+ }
 98+}
 99+
 100+//Based on http://www.mediawiki.org/wiki/Manual:Job_queue/For_developers
 101+class InterlanguageCentralExtensionPurgeJob extends Job {
 102+ public function __construct( $title, $params ) {
 103+ parent::__construct( 'purgeDependentWikis', $title, $params );
 104+ }
 105+
 106+ /**
 107+ * Execute the job
 108+ *
 109+ * @return bool
 110+ */
 111+ public function run() {
 112+ global $wgInterlanguageCentralExtensionIndexUrl;
 113+
 114+ //sleep() could be added here to reduce unnecessary use
 115+ $ill = $this->params['ill'];
 116+
 117+ foreach($ill as $lang => $pages) {
 118+ //TODO: error handling
 119+ $baseURL = sprintf($wgInterlanguageCentralExtensionIndexUrl, $lang) .
 120+ "?action=purge&title=";
 121+ foreach($pages as $page => $dummy) {
 122+ $url = $baseURL . urlencode(strtr($page, ' ', '_'));
 123+ Http::post( $url );
 124+ }
 125+ }
 126+
 127+ return true;
 128+ }
 129+
 130+ //TODO: custom insert with duplicate merging
 131+}
Property changes on: trunk/extensions/Interlanguage/InterlanguageCentral.php
___________________________________________________________________
Added: svn:eol-style
1132 + native

Follow-up revisions

RevisionCommit summaryAuthorDate
r82539* Ability to read links directly from a foreign database instead of the...nikola21:57, 20 February 2011

Comments

#Comment by Nikerabbit (talk | contribs)   16:20, 3 October 2010
+	function onArticleSave() {	
+		global $wgTitle;

Don't use $wgTitle in this hook. Take the title from the Article object passed to the hook. Same for onArticleSaveComplete.

+		while ( $row = $dbr->fetchObject( $res ) ) {
+		$dbr->freeResult( $res );

You can use foreach instead of while loop. FreeResult doesn't usually need to be called explicitely.

#Comment by Catrope (talk | contribs)   16:21, 3 October 2010

For clarity: the latter comment means you can use foreach ( $res as $row )

#Comment by Nikola Smolenski (talk | contribs)   10:30, 21 February 2011

I fixed both issues in r82539.

#Comment by Brion VIBBER (talk | contribs)   22:21, 3 October 2010

Suggestion on the file structure... if this needs a separate include in LocalSettings.php, it should probably be in its own directory to keep things clear and automation-friendly. If it's meant to be part of the Interlanguage extension, then it needs to be pulled in from Interlanguage.php.

#Comment by Nikola Smolenski (talk | contribs)   10:33, 21 February 2011

This is a separate extension, but I assume that in future the two extensions may share some functions or so, which is why I would like to keep them together.

Status & tagging log