r50973 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r50972‎ | r50973 | r50974 >
Date:10:53, 25 May 2009
Author:nikerabbit
Status:ok
Tags:
Comment:
* Work on fuzzytag
Modified paths:
  • /trunk/extensions/Translate/Translate.php (modified) (history)
  • /trunk/extensions/Translate/revtags.sql (modified) (history)
  • /trunk/extensions/Translate/scripts/populateFuzzy.php (added) (history)

Diff [purge]

Index: trunk/extensions/Translate/scripts/populateFuzzy.php
@@ -0,0 +1,63 @@
 2+<?php
 3+/**
 4+ * A script to populate fuzzy tags.
 5+ *
 6+ * @author Niklas Laxstrom
 7+ *
 8+ * @copyright Copyright © 2009, Niklas Laxström
 9+ * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
 10+ * @file
 11+ */
 12+require( dirname( __FILE__ ) . '/cli.inc' );
 13+
 14+$db = wfGetDB( DB_MASTER );
 15+
 16+$id = $db->selectField( 'revtag_type', 'rtt_id', array( 'rtt_name' => 'fuzzy' ), __METHOD__ );
 17+if ( $id === false ) {
 18+ echo "Fuzzy tag is not registered\n";
 19+ exit();
 20+}
 21+
 22+
 23+$count = $db->selectField( 'page', 'count(*)', array( 'page_namespace' => $wgTranslateMessageNamespaces ), __METHOD__ );
 24+if ( !$count ) {
 25+ echo "Nothing to update";
 26+ exit();
 27+}
 28+
 29+
 30+$tables = array( 'page', 'text', 'revision' );
 31+$fields = array( 'page_id', 'page_title', 'page_namespace', 'rev_id', 'old_text', 'old_flags' );
 32+$conds = array(
 33+ 'page_latest = rev_id',
 34+ 'old_id = rev_text_id',
 35+ 'page_namespace' => $wgTranslateMessageNamespaces,
 36+);
 37+
 38+$insert = array();
 39+
 40+$limit = max(1000,intval($count/100));
 41+$offset = 0;
 42+while (true) {
 43+ echo "$offset/$count\n";
 44+ $options = array( 'LIMIT' => $limit, 'OFFSET' => $offset );
 45+ $res = $db->select( $tables, $fields, $conds, __METHOD__, $options );
 46+ if ( !$res->numRows() ) break;
 47+ foreach ( $res as $r ) {
 48+ $text = Revision::getRevisionText( $r );
 49+ if ( strpos( $text, TRANSLATE_FUZZY ) !== false ) {
 50+ $inserts[] = array(
 51+ 'rt_page' => $r->page_id,
 52+ 'rt_revision' => $r->rev_id,
 53+ 'rt_type' => $id
 54+ );
 55+ }
 56+ }
 57+ $offset += $limit;
 58+
 59+ $db->replace( 'revtag', 'rt_type_page_revision', $inserts, __METHOD__ );
 60+}
 61+
 62+
 63+
 64+echo $db->affectedRows() . "\n";
\ No newline at end of file
Index: trunk/extensions/Translate/revtags.sql
@@ -21,7 +21,7 @@
2222 rt_value blob null
2323 ) /*$wgDBTableOptions*/;
2424 -- Index for finding all revisions in a page with a given tag
25 -CREATE INDEX /*i*/rt_type_page_revision ON /*$wgDBprefix*/revtag
 25+CREATE UNIQUE INDEX /*i*/rt_type_page_revision ON /*$wgDBprefix*/revtag
2626 (rt_type, rt_page, rt_revision);
2727 -- Index for finding the tags on a given revision
2828 CREATE INDEX /*i*/rt_revision_type ON /*$wgDBprefix*/revtag (rt_revision, rt_type);
\ No newline at end of file
Index: trunk/extensions/Translate/Translate.php
@@ -183,11 +183,24 @@
184184 function wfMemOut() { }
185185
186186 function efTranslateInit() {
187 - global $wgTranslatePHPlot, $wgAutoloadClasses;
 187+ global $wgTranslatePHPlot, $wgAutoloadClasses, $wgHooks;
188188 if ( $wgTranslatePHPlot ) {
189189 $wgAutoloadClasses['PHPlot'] = $wgTranslatePHPlot;
190190 }
191191
 192+ // Database schema
 193+ $wgHooks['LoadExtensionSchemaUpdates'][] = 'PageTranslationHooks::schemaUpdates';
 194+
 195+ // Do not activate hooks if not setup properly
 196+ if ( !efTranslateCheckPT() ) {
 197+ $wgEnablePageTranslation = false;
 198+ return true;
 199+ }
 200+
 201+ // Fuzzy tags for speed
 202+ $wgHooks['ArticleSaveComplete'][] = 'efTranslateAddFuzzy';
 203+
 204+
192205 global $wgEnablePageTranslation;
193206 if ( $wgEnablePageTranslation ) {
194207
@@ -213,18 +226,6 @@
214227 $wgTranslateMessageNamespaces[] = NS_TRANSLATIONS;
215228
216229 // Page translation hooks
217 - global $wgHooks;
218 -
219 - // Database schema
220 - $wgHooks['LoadExtensionSchemaUpdates'][] = 'PageTranslationHooks::schemaUpdates';
221 -
222 - // Do not activate hooks if not setup properly
223 - if ( !efTranslateCheckPT() ) {
224 - $wgEnablePageTranslation = false;
225 - return true;
226 - }
227 -
228 -
229230 // Register our css, is there a better place for this?
230231 $wgHooks['OutputPageBeforeHTML'][] = 'PageTranslationHooks::injectCss';
231232
@@ -259,7 +260,7 @@
260261 function efTranslateCheckPT() {
261262 global $wgHooks, $wgMemc;
262263
263 - $version = "2"; // Must be a string
 264+ $version = "3"; // Must be a string
264265 global $wgMemc;
265266 $memcKey = wfMemcKey( 'pt' );
266267 $ok = $wgMemc->get( $memcKey );
@@ -272,7 +273,7 @@
273274
274275 // Add our tags if they are not registered yet
275276 // tp:tag is called also the ready tag
276 - $tags = array( 'tp:mark', 'tp:tag', 'tp:transver' );
 277+ $tags = array( 'tp:mark', 'tp:tag', 'tp:transver', 'fuzzy' );
277278
278279 $dbw = wfGetDB( DB_MASTER );
279280 if ( !$dbw->tableExists('revtag_type') ) {
@@ -310,3 +311,34 @@
311312 function STDOUT() {}
312313 function STDERR() {}
313314 }
 315+
 316+function efTranslateAddFuzzy( $article, $user, $text, $summary,
 317+ $minor, $_, $_, $flags, $revision ) {
 318+
 319+ global $wgTranslateMessageNamespaces;
 320+
 321+ $ns = $article->getTitle()->getNamespace();
 322+ if ( !in_array( $ns, $wgTranslateMessageNamespaces) ) return true;
 323+
 324+ // We are not interested in null revisions
 325+ if ( $revision === null ) {
 326+ $rev = $article->getTitle()->getLatestRevId();
 327+ } else {
 328+ $rev = $revision->getID();
 329+ }
 330+
 331+ // Add the ready tag
 332+ $dbw = wfGetDB( DB_MASTER );
 333+
 334+ $id = $dbw->selectField( 'revtag_type', 'rtt_id', array( 'rtt_name' => 'fuzzy' ), __METHOD__ );
 335+
 336+ $conds = array(
 337+ 'rt_page' => $article->getTitle()->getArticleId(),
 338+ 'rt_type' => $id,
 339+ 'rt_revision' => $rev
 340+ );
 341+ $dbw->delete( 'revtag', $conds, __METHOD__ );
 342+ $dbw->insert( 'revtag', $conds, __METHOD__ );
 343+
 344+ return true;
 345+}
\ No newline at end of file

Status & tagging log