r82539 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r82538‎ | r82539 | r82540 >
Date:21:57, 20 February 2011
Author:nikola
Status:deferred
Tags:
Comment:
* Ability to read links directly from a foreign database instead of the
* API.
* Ability to use local namespace names in interlanguage title.
* Do away with $wgInterlanguageExtensionPrefix.
* Took care of suggestions by other devs.
Modified paths:
  • /trunk/extensions/Interlanguage/Interlanguage.i18n.magic.php (modified) (history)
  • /trunk/extensions/Interlanguage/Interlanguage.i18n.php (modified) (history)
  • /trunk/extensions/Interlanguage/Interlanguage.php (modified) (history)
  • /trunk/extensions/Interlanguage/InterlanguageCentral.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Interlanguage/InterlanguageCentral.php
@@ -1,24 +1,27 @@
22 <?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
 3+/**
 4+ * MediaWiki InterlanguageCentral extension v1.1
 5+ *
 6+ * Copyright © 2010 Nikola Smolenski <smolensk@eunet.rs>
 7+ * @version 1.1
 8+ *
 9+ * This program is free software; you can redistribute it and/or modify
 10+ * it under the terms of the GNU General Public License as published by
 11+ * the Free Software Foundation; either version 2 of the License, or
 12+ * (at your option) any later version.
 13+ *
 14+ * This program is distributed in the hope that it will be useful,
 15+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 17+ * GNU General Public License for more details.
 18+ *
 19+ * You should have received a copy of the GNU General Public License
 20+ * along with this program; if not, write to the Free Software
 21+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 22+ *
 23+ * For more information,
 24+ * @see http://www.mediawiki.org/wiki/Extension:Interlanguage
 25+ */
2326
2427 $wgExtensionFunctions[]="wfInterlanguageCentralExtension";
2528 $wgJobClasses['purgeDependentWikis'] = 'InterlanguageCentralExtensionPurgeJob';
@@ -26,7 +29,7 @@
2730 'name' => 'Interlanguage Central',
2831 'author' => 'Nikola Smolenski',
2932 'url' => 'http://www.mediawiki.org/wiki/Extension:Interlanguage',
30 - 'version' => '1.0',
 33+ 'version' => '1.1',
3134 'descriptionmsg' => 'interlanguagecentral-desc',
3235 );
3336 $wgExtensionMessagesFiles['Interlanguagecentral'] = dirname(__FILE__) . '/InterlanguageCentral.i18n.php';
@@ -44,17 +47,14 @@
4548 //ILL = InterLanguageLinks
4649 var $oldILL = array();
4750
48 - function onArticleSave() {
49 - global $wgTitle;
50 - $this->oldILL = $this->getILL($wgTitle);
 51+ function onArticleSave( $article ) {
 52+ $this->oldILL = $this->getILL($article->mTitle);
5153 return true;
5254 }
5355
54 - function onArticleSaveComplete() {
55 - global $wgTitle;
 56+ function onArticleSaveComplete( $article ) {
 57+ $newILL = $this->getILL($article->mTitle);
5658
57 - $newILL = $this->getILL($wgTitle);
58 -
5959 //Compare ILLs before and after the save; if nothing changed, there is no need to purge
6060 if(
6161 count(array_udiff_assoc(
@@ -68,7 +68,7 @@
6969 ))
7070 ) {
7171 $ill = array_merge_recursive($this->oldILL, $newILL);
72 - $job = new InterlanguageCentralExtensionPurgeJob( $wgTitle, array('ill' => $ill) );
 72+ $job = new InterlanguageCentralExtensionPurgeJob( $article->mTitle, array('ill' => $ill) );
7373 $job->insert();
7474 }
7575
@@ -80,14 +80,12 @@
8181 $dbr = wfGetDB( DB_SLAVE );
8282 $res = $dbr->select( 'langlinks', array( 'll_lang', 'll_title' ), array( 'll_from' => $title->mArticleID), __FUNCTION__);
8383 $a = array();
84 - while ( $row = $dbr->fetchObject( $res ) ) {
 84+ foreach( $res as $row ) {
8585 if(!isset($a[$row->ll_lang])) {
8686 $a[$row->ll_lang] = array();
8787 }
8888 $a[$row->ll_lang][$row->ll_title] = true;
8989 }
90 - $dbr->freeResult( $res );
91 -
9290 return $a;
9391 }
9492
@@ -117,10 +115,18 @@
118116 //TODO: error handling
119117 $baseURL = sprintf($wgInterlanguageCentralExtensionIndexUrl, $lang) .
120118 "?action=purge&title=";
121 - foreach($pages as $page => $dummy) {
 119+ foreach($pages as $page => $dummy) {
122120 $url = $baseURL . urlencode(strtr($page, ' ', '_'));
123121 Http::post( $url );
124122 }
 123+ //TODO: activate when becomes possible
 124+ /*
 125+ global $wgInterlanguageCentralExtensionApiUrl;
 126+ $url = sprintf($wgInterlanguageCentralExtensionApiUrl, $lang) .
 127+ "?action=purge&title=" .
 128+ implode( "|", array_walk( array_keys( $pages ), 'urlencode' ) );
 129+ Http::post( $url );
 130+ */
125131 }
126132
127133 return true;
Index: trunk/extensions/Interlanguage/Interlanguage.i18n.magic.php
@@ -77,6 +77,16 @@
7878 'interlanguage' => array( 0, 'межязык', 'interlanguage' ),
7979 );
8080
 81+/** Serbian Cyrillic ekavian (Српски (ћирилица)>) */
 82+$messages['sr-ec'] = array(
 83+ 'interlanguage' => array( '0', 'међујезичје', 'interlanguage' ),
 84+);
 85+
 86+/** Serbian Latin ekavian (Srpski (latinica)) */
 87+$messages['sr-el'] = array(
 88+ 'interlanguage' => array( '0', 'меđujezičje', 'interlanguage' ),
 89+);
 90+
8191 /** Ukrainian (Українська) */
8292 $magicWords['uk'] = array(
8393 'interlanguage' => array( 0, 'міжмова', 'межязык', 'interlanguage' ),
@@ -85,4 +95,4 @@
8696 /** Vietnamese (Tiếng Việt) */
8797 $magicWords['vi'] = array(
8898 'interlanguage' => array( 0, 'liênngônngữ' ),
89 -);
\ No newline at end of file
 99+);
Index: trunk/extensions/Interlanguage/Interlanguage.i18n.php
@@ -401,6 +401,7 @@
402402 */
403403 $messages['sr-ec'] = array(
404404 'interlanguage-desc' => 'Преузима међувики са другог пројекта',
 405+ 'interlanguage-pagelinksexplanation' => 'Странице са међујезичким везама:',
405406 );
406407
407408 /** Serbian Latin ekavian (‪Srpski (latinica)‬) */
Index: trunk/extensions/Interlanguage/Interlanguage.php
@@ -1,9 +1,9 @@
22 <?php
33 /**
4 - * MediaWiki Interlanguage extension v1.3
 4+ * MediaWiki Interlanguage extension v1.4
55 *
66 * Copyright © 2008-2010 Nikola Smolenski <smolensk@eunet.rs> and others
7 - * @version 1.3
 7+ * @version 1.4
88 *
99 * This program is free software; you can redistribute it and/or modify
1010 * it under the terms of the GNU General Public License as published by
@@ -29,7 +29,7 @@
3030 'name' => 'Interlanguage',
3131 'author' => 'Nikola Smolenski',
3232 'url' => 'http://www.mediawiki.org/wiki/Extension:Interlanguage',
33 - 'version' => '1.3',
 33+ 'version' => '1.4',
3434 'descriptionmsg' => 'interlanguage-desc',
3535 );
3636 $wgExtensionMessagesFiles['Interlanguage'] = dirname(__FILE__) . '/Interlanguage.i18n.php';
@@ -39,14 +39,15 @@
4040 global $wgParser, $wgHooks, $wgInterlanguageExtension;
4141
4242 $wgInterlanguageExtension = new InterlanguageExtension();
43 - $wgHooks['LanguageGetMagic'][] = array( &$wgInterlanguageExtension, 'onLanguageGetMagic' );
44 - $wgHooks['EditPage::showEditForm:fields'][] = array( &$wgInterlanguageExtension, 'pageLinks' );
 43+ $wgHooks['LanguageGetMagic'][] = $wgInterlanguageExtension;
 44+ $wgHooks['EditPage::showEditForm:fields'][] = array( $wgInterlanguageExtension, 'pageLinks' );
4545 $wgHooks['ArticleSaveComplete'][] = $wgInterlanguageExtension;
46 - $wgParser->setFunctionHook( 'interlanguage', array( &$wgInterlanguageExtension, 'interlanguage' ), SFH_NO_HASH );
 46+ $wgParser->setFunctionHook( 'interlanguage', array( $wgInterlanguageExtension, 'interlanguage' ), SFH_NO_HASH );
4747 }
4848
4949 class InterlanguageExtension {
5050 var $pageLinks = array();
 51+ var $foreignDbr = false;
5152
5253 function onLanguageGetMagic( &$magicWords, $langCode ) {
5354 $magicWords['interlanguage'] = array(0, 'interlanguage');
@@ -60,30 +61,90 @@
6162 * @param $param - parameter passed to {{interlanguage:}}.
6263 */
6364 function interlanguage( &$parser, $param ) {
64 - global $wgInterlanguageExtensionApiUrl, $wgInterlanguageExtensionSort,
65 - $wgInterlanguageExtensionPrefix, $wgInterlanguageExtensionInterwiki,
66 - $wgLanguageCode, $wgTitle, $wgMemc;
67 -
68 - if(isset($wgInterlanguageExtensionPrefix)) {
69 - $param = "$wgInterlanguageExtensionPrefix$param";
70 - }
 65+ global $wgInterlanguageExtensionApiUrl, $wgInterlanguageExtensionDB,
 66+ $wgInterlanguageExtensionSort, $wgInterlanguageExtensionInterwiki, $wgLanguageCode, $wgTitle, $wgMemc;
7167
7268 //This will later be used by pageLinks() and onArticleSave()
7369 $this->pageLinks[$param] = true;
7470
75 - $url = $wgInterlanguageExtensionApiUrl . "?action=query&prop=langlinks&" .
76 - "lllimit=500&format=php&redirects&titles=" . urlencode(strtr( $param, ' ', '_' ));
7771 $key = wfMemcKey( 'Interlanguage', md5( $param ) );
7872 $res = $wgMemc->get( $key );
7973
8074 if ( !$res ) {
81 - // be sure to set $res back to bool false, we do a strict compare below
 75+ // Be sure to set $res back to bool false, we do a strict compare below
8276 $res = false;
83 - $a = Http::get( $url );
84 - $a = @unserialize( $a );
 77+
 78+ if ( isset( $wgInterlanguageExtensionDB ) ) {
 79+ // Get the links from a foreign database
 80+ if( !$this->foreignDbr ) {
 81+ $foreignDbrClass = 'Database' . ucfirst( $wgInterlanguageExtensionDB['dbType'] );
 82+ $this->foreignDbr = new $foreignDbrClass(
 83+ $wgInterlanguageExtensionDB['dbServer'],
 84+ $wgInterlanguageExtensionDB['dbUser'],
 85+ $wgInterlanguageExtensionDB['dbPassword'],
 86+ $wgInterlanguageExtensionDB['dbName'],
 87+ $wgInterlanguageExtensionDB['dbFlags'],
 88+ $wgInterlanguageExtensionDB['tablePrefix']
 89+ );
 90+ }
 91+
 92+ $paramTitle = Title::newFromText( $param );
 93+ if( $paramTitle ) {
 94+ $dbKey = $paramTitle->mDbkeyform;
 95+ $namespace = $paramTitle->mNamespace;
 96+ } else {
 97+ //If the title is malformed, try at least this
 98+ $dbKey = strtr( $param, ' ', '_' );
 99+ $namespace = 0;
 100+ }
 101+
 102+ $a = array( 'query' => array( 'pages' => array() ) );
 103+
 104+ $res = $this->foreignDbr->select(
 105+ 'page',
 106+ array( 'page_id', 'page_is_redirect' ),
 107+ array(
 108+ 'page_title' => $dbKey,
 109+ 'page_namespace' => $namespace
 110+ ),
 111+ __FUNCTION__
 112+ );
 113+ $res = $res->fetchObject();
 114+
 115+ if( $res === false ) {
 116+ // There is no such article on the central wiki
 117+ $a['query']['pages'][-1] = array( 'missing' => "" );
 118+ } else {
 119+ if( $res->page_is_redirect ) {
 120+ $res = $this->foreignDbr->select(
 121+ array( 'redirect', 'page' ),
 122+ 'page_id',
 123+ array(
 124+ 'rd_title = page_title',
 125+ 'rd_from' => $res->page_id
 126+ ),
 127+ __FUNCTION__
 128+ );
 129+ $res = $res->fetchObject();
 130+ }
 131+
 132+ $a['query']['pages'][0] = array( 'langlinks' => $this->readLinksFromDB( $this->foreignDbr, $res->page_id ) );
 133+ }
 134+ } else {
 135+ // Get the links from an API
 136+ $title = $this->translateNamespace( $param );
 137+
 138+ $url = $wgInterlanguageExtensionApiUrl .
 139+ "?action=query&prop=langlinks&" .
 140+ "lllimit=500&format=php&redirects&titles=" .
 141+ urlencode( $title );
 142+ $a = Http::get( $url );
 143+ $a = @unserialize( $a );
 144+ }
 145+
85146 if(isset($a['query']['pages']) && is_array($a['query']['pages'])) {
86147 $a = array_shift($a['query']['pages']);
87 -
 148+
88149 if(isset($a['missing'])) {
89150 // There is no such article on the central wiki
90151 $linker = new Linker();
@@ -101,16 +162,11 @@
102163 }
103164 }
104165 }
105 -
 166+
106167 if($res === false) {
107168 // An API error has occured; preserve the links that are in the article
108169 $dbr = wfGetDB( DB_SLAVE );
109 - $res = $dbr->select( 'langlinks', array( 'll_lang', 'll_title' ), array( 'll_from' => $wgTitle->mArticleID), __FUNCTION__);
110 - $a = array();
111 - while ( $row = $dbr->fetchObject( $res ) ) {
112 - $a[] = array( 'lang' => $row->ll_lang, '*' => $row->ll_title );
113 - }
114 - $dbr->freeResult( $res );
 170+ $a = $this->readLinksFromDB( $dbr, $wgTitle->mArticleID );
115171 $res = true;
116172 }
117173
@@ -159,7 +215,7 @@
160216 if( count( $pagelinks ) ) {
161217 $linker = new Linker(); $pagelinktexts = array();
162218 foreach( $pagelinks as $page => $dummy ) {
163 - $title = Title::newFromText( $wgInterlanguageExtensionInterwiki . strtr($page,'_',' ') );
 219+ $title = Title::newFromText( $wgInterlanguageExtensionInterwiki . strtr($this->translateNamespace( $page ),'_',' ') );
164220 $link = $linker->link( $title );
165221 if( strlen( $link ) ) {
166222 $pagelinktexts[] = $link;
@@ -188,6 +244,23 @@
189245 }
190246
191247 /**
 248+ * Translate namespace from localized to the canonical form.
 249+ *
 250+ * @param $param - Page title
 251+ */
 252+ function translateNamespace( $param ) {
 253+ $paramTitle = Title::newFromText( $param );
 254+ if( $paramTitle ) {
 255+ $canonicalNamespaces = MWNamespace::getCanonicalNamespaces();
 256+ $title = $canonicalNamespaces[$paramTitle->mNamespace] . ":" . $paramTitle->mDbkeyform;
 257+ } else {
 258+ //If the title is malformed, try at least this
 259+ $title = strtr( $param, ' ', '_' );
 260+ }
 261+ return $title;
 262+ }
 263+
 264+ /**
192265 * Saves names of pages on the central wiki which are linked to from the saved page
193266 * by {{interlanguage:}} magic.
194267 *
@@ -221,14 +294,38 @@
222295 $dbr = wfGetDB( DB_SLAVE );
223296 $res = $dbr->select( 'page_props', 'pp_value', array( 'pp_page' => $articleid, 'pp_propname' => 'interlanguage_pages' ), __FUNCTION__);
224297 $pagelinks = array();
225 - if ( $row = $dbr->fetchObject( $res ) ) {
 298+ $row = $res->fetchObject();
 299+ if ( $row ) {
226300 $pagelinks = @unserialize( $row->pp_value );
227301 }
228 - $dbr->freeResult( $res );
 302+
229303 return $pagelinks;
230304 }
231305
232306 /**
 307+ * Read interlanguage links from a database, and return them in the same format that API
 308+ * uses.
 309+ *
 310+ * @param $dbr - Database.
 311+ * @param $articleid - ID of the article whose links should be returned.
 312+ * @returns The array with the links. If there are no links, an empty array is returned.
 313+ */
 314+ function readLinksFromDB( $dbr, $articleid ) {
 315+ $res = $dbr->select(
 316+ array( 'langlinks' ),
 317+ array( 'll_lang', 'll_title' ),
 318+ array( 'll_from' => $articleid ),
 319+ __FUNCTION__
 320+ );
 321+ $a = array();
 322+ foreach( $res as $row ) {
 323+ $a[] = array( 'lang' => $row->ll_lang, '*' => $row->ll_title );
 324+ }
 325+ $res->free();
 326+ return $a;
 327+ }
 328+
 329+ /**
233330 * Compare two interlanguage links by order of alphabet, based on language code.
234331 */
235332 static function compareCode($a, $b) {

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r74204July 30 2010: Display link(s) to the page(s) on the central wiki with...nikola14:20, 3 October 2010
r74208Pair to the Interlanguage extension....nikola14:47, 3 October 2010

Status & tagging log