r20718 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r20717‎ | r20718 | r20719 >
Date:22:33, 26 March 2007
Author:aaron
Status:old
Tags:
Comment:
*Get image stablizing to work; clean up some things
Modified paths:
  • /branches/jhb/phase3/extensions/FlaggedRevs.php (modified) (history)
  • /branches/jhb/phase3/extensions/FlaggedRevs.sql (modified) (history)
  • /branches/jhb/phase3/extensions/FlaggedRevsPage.body.php (modified) (history)
  • /branches/jhb/phase3/extensions/FlaggedRevsPage.i18n.php (modified) (history)

Diff [purge]

Index: branches/jhb/phase3/extensions/FlaggedRevs.php
@@ -54,6 +54,8 @@
5555 $wgLogActions['review/unapprove'] = 'review-logentryrevoke';
5656
5757 class FlaggedRevs {
 58+ /* 50MB allows fixing those huge pages */
 59+ const MAX_INCLUDE_SIZE = 50000000;
5860
5961 function __construct() {
6062 $this->dimensions = array( 'accuracy' => array( 0=>'acc-0',
@@ -69,12 +71,53 @@
7072 2=>'style-2',
7173 3=>'style-3') );
7274 }
 75+
 76+ function pageOverride() {
 77+ global $wgFlaggedRevsAnonOnly, $wgUser;
 78+ return !( $wgFlaggedRevsAnonOnly && !$wgUser->isAnon() );
 79+ }
 80+
 81+ function getFlaggedRevision( $rev_id ) {
 82+ $db = wfGetDB( DB_SLAVE );
 83+ // select a row, this should be unique
 84+ $result = $db->select( 'flaggedrevs', array('*'), array('fr_rev_id' => $rev_id) );
 85+ if ( $row = $db->fetchObject($result) ) {
 86+ return $row;
 87+ }
 88+ return NULL;
 89+ }
 90+
 91+ function getFlaggedRevText( $rev_id ) {
 92+ $db = wfGetDB( DB_SLAVE );
 93+ // select a row, this should be unique
 94+ $result = $db->select( 'flaggedcache', array('fc_cache'), array('fc_rev_id' => $rev_id) );
 95+ if ( $row = $db->fetchObject($result) ) {
 96+ return $row->fc_cache;
 97+ }
 98+ return NULL;
 99+ }
 100+
 101+ /**
 102+ * @param string $text
 103+ * @returns string
 104+ * All included pages are expanded out to keep this text frozen
 105+ */
 106+ function expandText( $text ) {
 107+ global $wgParser, $wgTitle;
 108+ // Do not treat this as paring an article on normal view
 109+ // enter the title object as wgTitle
 110+ $options = new ParserOptions;
 111+ $options->setRemoveComments( true );
 112+ $options->setMaxIncludeSize( self::MAX_INCLUDE_SIZE );
 113+ $output = $wgParser->preprocess( $text, $wgTitle, $options );
 114+ return $output;
 115+ }
73116
74117 function getFlagsForRevision( $rev_id ) {
75118 // Set default blank flags
76119 $flags = array( 'accuracy' => 0, 'depth' => 0, 'style' => 0 );
77120
78 - $db = wfGetDB( DB_MASTER );
 121+ $db = wfGetDB( DB_SLAVE );
79122 // select a row, this should be unique
80123 $result = $db->select( 'flaggedrevs', array('*'), array('fr_rev_id' => $rev_id) );
81124 if ( $row = $db->fetchObject($result) ) {
@@ -83,7 +126,9 @@
84127 return $flags;
85128 }
86129
87 - function getLatestRev( $page_id ) {
 130+ function getLatestFlaggedRev( $page_id ) {
 131+ # Call getReviewedRevs(),
 132+ # this will prune things out if needed
88133 if ( $row = $this->getReviewedRevs($page_id) ) {
89134 return $row[0];
90135 }
@@ -105,17 +150,19 @@
106151 while ( $row = $db->fetchObject( $result ) ) {
107152 // Purge deleted revs from flaggedrev table
108153 if ( $row->rev_deleted ) {
109 - $cache_text = $this->expandText( $row->fr_cache );
110 - $db->delete( 'flaggedrevs', array( 'fr_rev_id' => $rev->getId ) );
 154+ $cache_text = $this->expandText( $this->getFlaggedRevText( $row->fr_rev_id ) );
 155+ $db->delete( 'flaggedrevs', array( 'fr_rev_id' => $row->rev_id ) );
111156 // Delete stable images if needed
112 - $images = $this->findLocalImages( $cache_text );
 157+ list($images,$thumbs) = $this->findLocalImages( $cache_text );
113158 $copies = $this->deleteStableImages( $images );
114159 // Update stable image table
115160 $this->removeStableImages( $row->rev_id, $copies );
 161+ $this->deleteStableThumbnails( $thumbs );
116162 // Clear cache...
117163 $this->updatePage( Title::newFromID($page_id) );
 164+ } else {
 165+ $rows[] = $row;
118166 }
119 - $rows[] = $row;
120167 }
121168 return $rows;
122169 }
@@ -133,11 +180,6 @@
134181 // Return count of revisions
135182 return $db->numRows($result);
136183 }
137 -
138 - function pageOverride() {
139 - global $wgFlaggedRevsAnonOnly, $wgUser;
140 - return !( $wgFlaggedRevsAnonOnly && !$wgUser->isAnon() );
141 - }
142184
143185 function setPageContent( &$out ) {
144186 global $wgArticle, $wgRequest, $wgTitle, $wgOut, $action;
@@ -157,7 +199,7 @@
158200 // Set new body html text as that of now
159201 $flaghtml = ''; $newbodytext = $out->mBodytext;
160202 // Check the newest stable version
161 - $top_frev = $this->getLatestRev( $wgArticle->getId() );
 203+ $top_frev = $this->getLatestFlaggedRev( $wgArticle->getId() );
162204 if ( $wgRequest->getVal('diff') ) {
163205 // Do not clutter up diffs any further...
164206 } else if ( $top_frev ) {
@@ -176,25 +218,40 @@
177219 $diff = ($revid > $top_frev->fr_rev_id) ? $revid : $top_frev->fr_rev_id;
178220 $flaghtml = wfMsgExt('revreview-newest', array('parse'), $top_frev->fr_rev_id, $oldid, $diff, $time );
179221 }
180 - } # Viewing the page normally
 222+ } # Viewing the page normally: override the page
181223 else {
182 - global $wgUser, $wgUploadDirectory;
183 - // We will be looking at the reviewed revision...
 224+ global $wgUser, $wgUploadDirectory, $wgUseSharedUploads, $wgUploadPath;
 225+ # We will be looking at the reviewed revision...
184226 $visible_id = $top_frev->fr_rev_id;
185227 $revs_since = $this->getUnreviewedRevCount( $wgArticle->getId(), $visible_id );
186228 $flaghtml = wfMsgExt('revreview-replaced', array('parse'), $visible_id, $wgArticle->getLatest(), $revs_since, $time );
187 - # Hack...temporarily change image dir
 229+
 230+ # Hack...temporarily change image directories
 231+ # There is no nice option to set this for each parse
188232 # This lets the parser know where to look...
 233+ $uploadPath = $wgUploadPath;
189234 $uploadDir = $wgUploadDirectory;
190 - $wgUploadDirectory = "{$wgUploadDirectory}/stable";
 235+ $useSharedUploads = $wgUseSharedUploads;
 236+ # Stable thumbnails need to have the right path
 237+ $wgUploadPath = ($wgUploadPath) ? "{$uploadPath}/stable" : false;
 238+ # Create <img> tags with the right url
 239+ $wgUploadDirectory = "{$uploadDir}/stable";
 240+ # Stable images are never stored at commons
 241+ $wgUseSharedUploads = false;
 242+
191243 $parse_ops = ParserOptions::newFromUser($wgUser);
192244 # Don't show section-edit links
 245+ # They can be old and misleading
193246 $parse_ops->setEditSection( false );
194 - // Parse the new body, wikitext -> html
195 - $newbody = $wgParser->parse( $top_frev->fr_cache, $wgTitle, $parse_ops );
 247+ # Parse the new body, wikitext -> html
 248+ $text = $this->getFlaggedRevText( $top_frev->fr_rev_id );
 249+ $newbody = $wgParser->parse( $text, $wgTitle, $parse_ops );
196250 $newbodytext = $newbody->getText();
197 - // Reset image dir
 251+
 252+ # Reset image directories
 253+ $wgUploadPath = $uploadPath;
198254 $wgUploadDirectory = $uploadDir;
 255+ $wgUseSharedUploads = $useSharedUploads;
199256 }
200257 // Construct some tagging
201258 $flaghtml .= "<table align='center' cellspadding=\'0\'><tr>";
@@ -205,7 +262,8 @@
206263 $flaghtml .= '</tr></table>';
207264 // Should use CSS?
208265 $flaghtml = "<small>$flaghtml</small>";
209 - // Copy over the old body
 266+
 267+ // Set the new body HTML! and place the tag on top
210268 $out->mBodytext = '<div class="mw-warning plainlinks">' . $flaghtml . '</div>' . $newbodytext;
211269 } else {
212270 $flaghtml = wfMsgExt('revreview-noflagged', array('parse'));
@@ -216,7 +274,11 @@
217275 // Show review links for the VISIBLE revision
218276 // We cannot review deleted revisions
219277 if ( is_object($wgArticle->mRevision) && $wgArticle->mRevision->mDeleted ) return;
220 - $this->addQuickReview( $visible_id, false, $out );
 278+ // Add quick review links IF we did not override, otherwise, they might
 279+ // review a revision that parses out newer templates/images than what they say
 280+ // Note: overrides are never done when viewing with "oldid="
 281+ if ( $visible_id==$revid || !$this->pageOverride() )
 282+ $this->addQuickReview( $visible_id, false, $out );
221283 }
222284
223285 function addToEditView( &$editform ) {
@@ -235,7 +297,7 @@
236298 // Set new body html text as that of now
237299 $flaghtml = '';
238300 // Check the newest stable version
239 - $top_frev = $this->getLatestRev( $editform->mArticle->getId() );
 301+ $top_frev = $this->getLatestFlaggedRev( $editform->mArticle->getId() );
240302 if ( is_object($top_frev) ) {
241303 global $wgParser, $wgLang;
242304 $time = $wgLang->timeanddate( wfTimestamp(TS_MW, $top_frev->fr_timestamp), true );
@@ -253,7 +315,7 @@
254316 } # Editing the page normally
255317 else {
256318 if ( $revid==$top_frev->rev_id )
257 - $flaghtml = wfMsgExt('revreview-isnewest', array('parse'));
 319+ $flaghtml = wfMsgExt('revreview-isnewest', array('parse'), $time);
258320 else
259321 $flaghtml = wfMsgExt('revreview-newest', array('parse'), $top_frev->fr_rev_id, $top_frev->fr_rev_id, $revid, $time );
260322 }
@@ -286,7 +348,7 @@
287349 // If we are viewing a page normally, and it was overrode
288350 // change the edit tab to a "current revision" tab
289351 if ( !$wgRequest->getVal('oldid') ) {
290 - $top_frev = $this->getLatestRev( $wgArticle->getId() );
 352+ $top_frev = $this->getLatestFlaggedRev( $wgArticle->getId() );
291353 // Note that revisions may not be set to override for users
292354 if ( is_object($top_frev) && $this->pageOverride() ) {
293355 # Remove edit option altogether
Index: branches/jhb/phase3/extensions/FlaggedRevs.sql
@@ -1,4 +1,5 @@
 2+-- (c) Joerg Baach, Aaron Schulz, 2007
 3+
24 -- Table structure for table `revisiontags`
35 -- This stores expanded revision wikitext caches
46 -- along with rating/user/notes data
@@ -6,7 +7,6 @@
78 fr_id int(10) NOT NULL auto_increment,
89 fr_page_id int(10) NOT NULL,
910 fr_rev_id int(10) NOT NULL,
10 - fr_cache mediumblob NOT NULL default '',
1111 fr_acc int(2) NOT NULL,
1212 fr_dep int(2) NOT NULL,
1313 fr_sty int(2) NOT NULL,
@@ -18,8 +18,18 @@
1919 UNIQUE KEY (fr_id),
2020 INDEX fr_page_rev (fr_page_id,fr_rev_id),
2121 INDEX fr_acc_dep_sty (fr_acc,fr_dep,fr_sty)
22 -) ENGINE=MyISAM AUTO_INCREMENT=9 DEFAULT CHARSET=latin1 COMMENT='Revision Tags Extension' AUTO_INCREMENT=0;
 22+) TYPE=InnoDB;
2323
 24+-- This stores cached, expanded revisions text
 25+CREATE TABLE /*$wgDBprefix*/flaggedcache (
 26+ fc_id int(10) NOT NULL auto_increment,
 27+ fc_rev_id int(10) NOT NULL,
 28+ fc_cache mediumblob NOT NULL default '',
 29+
 30+ PRIMARY KEY fc_rev_id (fc_rev_id),
 31+ UNIQUE KEY (fc_id)
 32+) TYPE=InnoDB;
 33+
2434 -- This stores image usage for the stable image directory
2535 -- along with rating/user/notes data
2636 CREATE TABLE /*$wgDBprefix*/flaggedimages (
Index: branches/jhb/phase3/extensions/FlaggedRevsPage.body.php
@@ -8,8 +8,6 @@
99
1010 class Revisionreview extends SpecialPage
1111 {
12 - /* 50MB allows fixing those huge pages */
13 - const MAX_INCLUDE_SIZE = 50000000;
1412
1513 function Revisionreview() {
1614 SpecialPage::SpecialPage('Revisionreview', 'review');
@@ -124,7 +122,7 @@
125123 }
126124 $form .= '</td></tr></table></fieldset>';
127125
128 - $images = $this->findLocalImages( $this->expandText( $rev->getText() ) );
 126+ list($images,$thumbs) = $this->findLocalImages( FlaggedRevs::expandText( $rev->getText() ) );
129127 if ( $images ) {
130128 $form .= wfMsg('revreview-images') . "\n";
131129 $form .= "<ul>";
@@ -213,12 +211,11 @@
214212 $user = $wgUser->getId();
215213 $timestamp = wfTimestampNow();
216214
217 - $cache_text = $this->expandText( $rev->getText() );
 215+ $cache_text = FlaggedRevs::expandText( $rev->getText() );
218216 // Add or update entry for this revision
219217 $set = array(
220218 'fr_page_id' => $rev->getPage(),
221219 'fr_rev_id' => $rev->getId(),
222 - 'fr_cache' => $cache_text,
223220 'fr_acc' => $this->dimensions['accuracy'],
224221 'fr_dep' => $this->dimensions['depth'],
225222 'fr_sty' => $this->dimensions['style'],
@@ -226,12 +223,17 @@
227224 'fr_timestamp' => $timestamp,
228225 'fr_comment'=> $this->notes
229226 );
 227+ $set2 = array('fc_rev_id' => $rev->getId(), 'fc_cache' => $cache_text);
 228+ // Update flagrevisions table
230229 $db->replace( 'flaggedrevs', array( array('fr_page_id','fr_rev_id') ), $set, __METHOD__ );
 230+ // Store/update the text
 231+ $db->replace( 'flaggedcache', array('fc_rev_id'), $set2, __METHOD__ );
231232 // Update the article review log
232233 $this->updateLog( $this->page, $this->dimensions, $this->comment, $this->oldid, true );
233234 // Clone images to stable dir
234 - $images = $this->findLocalImages( $cache_text );
 235+ list($images,$thumbs) = $this->findLocalImages( $cache_text );
235236 $copies = $this->makeStableImages( $images );
 237+ $this->deleteStableThumbnails( $thumbs );
236238 // Update stable image table
237239 $this->insertStableImages( $rev->getId(), $copies );
238240 // Clear cache...
@@ -250,14 +252,22 @@
251253 $db = wfGetDB( DB_MASTER );
252254 $user = $wgUser->getId();
253255 $timestamp = wfTimestampNow();
254 -
255 - $cache_text = $this->expandText( $rev->getText() );
 256+ // get the flagged revision to access its cache text
 257+ $frev = FlaggedRevs::getFlaggedRevision( $rev->getId );
 258+ if( !$frev ) {
 259+ // This shouldn't happen...
 260+ return;
 261+ }
256262 $db->delete( 'flaggedrevs', array( 'fr_rev_id' => $rev->getId ) );
257263 // Update the article review log
258264 $this->updateLog( $this->page, $this->dimensions, $this->comment, $this->oldid, false );
 265+
 266+ $cache_text = FlaggedRevs::getFlaggedRevText( $rev->getId ) ;
259267 // Delete stable images if needed
260 - $images = $this->findLocalImages( $cache_text );
 268+ list($images,$thumbs) = $this->findLocalImages( $cache_text );
261269 $copies = $this->deleteStableImages( $images );
 270+ // Stable versions must remake this thumbnail
 271+ $this->deleteStableThumbnails( $thumbs );
262272 // Update stable image table
263273 $this->removeStableImages( $rev->getId(), $copies );
264274 // Clear cache...
@@ -274,31 +284,17 @@
275285 function updatePage( $title ) {
276286 $title->invalidateCache();
277287 }
278 -
279 - /**
280 - * @param string $text
281 - * @returns string
282 - * All included pages are expanded out to keep this text frozen
283 - */
284 - function expandText( $text ) {
285 - global $wgParser, $wgTitle;
286 - // Do not treat this as paring an article on normal view
287 - // enter the title object as wgTitle
288 - $options = new ParserOptions;
289 - $options->setRemoveComments( true );
290 - $options->setMaxIncludeSize( self::MAX_INCLUDE_SIZE );
291 - $output = $wgParser->preprocess( $text, $wgTitle, $options );
292 - return $output;
293 - }
294288
295289 /**
296290 * Get all local image files and generate an array of them
297291 * @param string $s, wikitext
298 - * $output array, string titles
299 - */
 292+ * $output array, (string title array, string thumbnail array)
 293+ */
300294 function findLocalImages( $s ) {
 295+ global $wgUploadPath;
 296+
301297 $fname = 'findLocalImages';
302 - $imagelist = array();
 298+ $imagelist = array(); $thumblist = array();
303299
304300 if ( !$s || !strval($s) ) return $imagelist;
305301
@@ -313,25 +309,39 @@
314310 $e1_img = "/^([:{$tc}]+)(.+)$/sD";
315311 # Loop for each link
316312 for ($k = 0; isset( $a[$k] ); $k++) {
317 - if ( preg_match( $e1_img, $a[$k], $m ) ) {
 313+ if( preg_match( $e1_img, $a[$k], $m ) ) {
318314 # page with normal text or alt of form x or ns:x
319315 $nt = Title::newFromText( $m[1] );
320316 $ns = $nt->getNamespace();
321317 # add if this is an image
322 - if ( $ns == NS_IMAGE )
 318+ if( $ns == NS_IMAGE ) {
323319 $imagelist[] = $nt->getPrefixedText();
 320+ }
 321+ $image = $nt->getDBKey();
 322+ # check for data for thumbnails
 323+ $part = array_map( 'trim', explode( '|', $m[2]) );
 324+ foreach( $part as $val ) {
 325+ if( preg_match( '/^([0-9]+)px$/', $val, $n ) ) {
 326+ $width = intval( $n[1] );
 327+ $thumblist[$image] = $width;
 328+ } else if( preg_match( '/^([0-9]+)x([0-9]+)(px|)$/', $val, $n ) ) {
 329+ $width = intval( $n[1] );
 330+ $thumblist[$image] = $width;
 331+ }
 332+ }
324333 }
325334 }
326 - return $imagelist;
 335+ return array( $imagelist, $thumblist );
327336 }
328337
329338 /**
330339 * Showtime! Copy all used images to a stable directory
331 - * Get all local image files and generate an array of them
 340+ * This updates (overwrites) any existing stable images
 341+ * Won't work for sites with unhashed dirs that have subfolders protected
332342 * The future FileStore migration might effect this, not sure...
333343 * @param array $imagelist, list of string names
334344 * $output array, list of string names of images sucessfully cloned
335 - */
 345+ */
336346 function makeStableImages( $imagelist ) {
337347 global $wgUploadDirectory, $wgSharedUploadDirectory;
338348 // All stable images are local, not shared
@@ -361,7 +371,6 @@
362372 if( !is_dir($stableDir . $hash) ) {
363373 wfMkdirParents($stableDir . $hash);
364374 }
365 - // Copy over our files
366375 copy("{$path}{$name}","{$stableDir}{$hash}{$name}");
367376 $usedimages[] = $name;
368377 }
@@ -385,9 +394,9 @@
386395 * Delete an a list of stable image files
387396 * @param array $imagelist, list of string names
388397 * $output array, list of string names of images to be deleted
389 - */
 398+ */
390399 function deleteStableImages( $imagelist ) {
391 - global $wgUploadDirectory, $wgSharedUploadDirectory;
 400+ global $wgSharedUploadDirectory;
392401 // All stable images are local, not shared
393402 // Otherwise, we could have some nasty cross language/wiki conflicts
394403 $stableDir = "$wgUploadDirectory/stable";
@@ -419,6 +428,26 @@
420429 }
421430
422431 /**
 432+ * Delete an a list of stable image thumbnails
 433+ * New thumbnails don't normally override old ones, causing outdated images
 434+ * This allows for tagged revisions to be re-reviewed with newer images
 435+ * @param array $imagelist, list of string names
 436+ * $output array, list of string names of images to be deleted
 437+ */
 438+ function deleteStableThumbnails( $thumblist ) {
 439+ global $wgUploadDirectory;
 440+ // We need valid input
 441+ if ( !is_array($thumblist) ) return false;
 442+ foreach ( $thumblist as $name => $width ) {
 443+ $thumburl = "{$wgUploadDirectory}/stable/thumb" . wfGetHashPath( $name, false ) . "$name/". $width."px-".$name;
 444+ if ( is_file($thumburl) ) {
 445+ unlink($thumburl);
 446+ }
 447+ }
 448+ return true;
 449+ }
 450+
 451+ /**
423452 * Update the stable image usage table
424453 * Add some images if not redundant
425454 * @param array $imagelist, list of string names
Index: branches/jhb/phase3/extensions/FlaggedRevsPage.i18n.php
@@ -16,8 +16,8 @@
1717 ([{{fullurl:{{FULLPAGENAMEE}}|oldid=$2&diff=$3}} compare]) was [{{fullurl:Special:Log/review|page={{FULLPAGENAMEE}}}} approved]
1818 on <i>$4</i>, rated as:',
1919 'revreview-replaced' => 'This is the latest [[Help:Article validation|reviewed]] revision of this page,
20 - [{{fullurl:Special:Log/review|page={{FULLPAGENAMEE}}}} approved] on <i>$4</i>. The [{{fullurl:{{FULLPAGENAMEE}}|oldid=$2}} current revision]
21 - can be found in the [{{fullurl:{{FULLPAGENAMEE}}|action=history}} page history]. There {{plural:$3|is $3 revision|are $3 revisions}}
 20+ [{{fullurl:Special:Log/review|page={{FULLPAGENAMEE}}}} approved] on <i>$4</i>. The [{{fullurl:{{FULLPAGENAMEE}}|oldid=$2}} current revision]
 21+ is editable and may be more up to date. There {{plural:$3|is $3 revision|are $3 revisions}}
2222 ([{{fullurl:{{FULLPAGENAMEE}}|oldid=$1&diff=$2}} changes]) awaiting review.',
2323
2424 'revisionreview' => 'Review revisions',