http://www.mediawiki.org/wiki/Special:Code/MediaWiki/84591

Revision: 84591
Author:   aaron
Date:     2011-03-23 03:13:37 +0000 (Wed, 23 Mar 2011)
Log Message:
-----------
* Made BeforeParserMakeImageLinkObj/BeforeGalleryFindFile let hooks set sha1 
parameter
* Made FlaggedRevs specify files by sha1,timestamp to handle renames with no 
redirects. This makes them handled as well as templates in this regard. (bug 
27836) 
* Moved BeforeGalleryFindFile hook to proper place (don't trigger for 
non-NS_FILE titles)
* Removed unused mRevisionId field from ImageGallery
* Removed old hotfix from makeMediaLinkObj(); all the current callers would 
crash beforehand if the title was null anyway
* Updated hook docs (some prior params were missing)
* Broke some long lines and cleaned up some whitespace
* TODO: track file info in core rather than fr_fileSHA1Keys and ugly, 
duplicated, queries. This should be easy to do now.

Modified Paths:
--------------
    trunk/extensions/FlaggedRevs/FlaggedRevs.hooks.php
    trunk/phase3/docs/hooks.txt
    trunk/phase3/includes/ImageGallery.php
    trunk/phase3/includes/Linker.php
    trunk/phase3/includes/parser/Parser.php

Modified: trunk/extensions/FlaggedRevs/FlaggedRevs.hooks.php
===================================================================
--- trunk/extensions/FlaggedRevs/FlaggedRevs.hooks.php  2011-03-23 00:53:02 UTC 
(rev 84590)
+++ trunk/extensions/FlaggedRevs/FlaggedRevs.hooks.php  2011-03-23 03:13:37 UTC 
(rev 84591)
@@ -334,7 +334,7 @@
        * (b) Set specified versions in fr_fileSHA1Keys
        */
        public static function parserFetchStableFile(
-               $parser, Title $nt, &$skip, &$time, &$query = false
+               $parser, Title $nt, &$skip, &$time, &$query, &$sha1
        ) {
                if ( !( $parser instanceof Parser ) ) {
                        return true; // nothing to do
@@ -363,7 +363,7 @@
        * (a) Select the desired images based on the selected stable version 
time/SHA-1
        * (b) Set specified versions in fr_fileSHA1Keys
        */
-       public static function galleryFetchStableFile( $ig, Title $nt, &$time, 
&$query = false ) {
+       public static function galleryFetchStableFile( $ig, Title $nt, &$time, 
&$query, &$sha1 ) {
                $parser =& $ig->mParser; // convenience
                if ( !( $parser instanceof Parser ) || $nt->getNamespace() != 
NS_FILE ) {
                        return true; // nothing to do
@@ -387,7 +387,7 @@
        */
        protected static function parserFindStableFile( Parser $parser, Title 
$title ) {
                $time = false; // current version
-               $sha1 = null; // corresponds to $time
+               $sha1 = false; // corresponds to $time
                # Check for the version of this file used when reviewed.
                $incManager = FRInclusionManager::singleton();
                list( $maybeTS, $maybeSha1 ) = 
$incManager->getReviewedFileVersion( $title );
@@ -430,15 +430,20 @@
                }
                # Fetch the current timestamps of the images.
                foreach ( $pOutput->mImages as $filename => $x ) {
-                       # FIXME: it would be nice not to double fetch these!
-                       $time = false; // current
                        # Stable output with versions specified
                        if ( isset( $pOutput->fr_fileSHA1Keys[$filename] ) ) {
                                // Fetch file with $time to confirm the 
specified version exists
                                $time = 
$pOutput->fr_fileSHA1Keys[$filename]['ts'];
+                               $sha1 = 
$pOutput->fr_fileSHA1Keys[$filename]['sha1'];
+                               # FIXME: don't double fetch these (Parser just 
did)!!!
+                               $file = RepoGroup::singleton()->findFileFromKey(
+                                       $sha1, array( 'time' => $time ) );
+                       } else {
+                               $title = Title::makeTitleSafe( NS_FILE, 
$filename );
+                               # FIXME: don't double fetch these (Parser just 
did)!!!
+                               $file = wfFindFile( $title ); 
                        }
-                       $title = Title::makeTitleSafe( NS_FILE, $filename );
-                       $file = wfFindFile( $title, array( 'time' => $time ) );
+                       # Update tracking vars...
                        $pOutput->fr_fileSHA1Keys[$filename] = array();
                        if ( $file ) {
                                $pOutput->fr_fileSHA1Keys[$filename]['ts'] = 
$file->getTimestamp();

Modified: trunk/phase3/docs/hooks.txt
===================================================================
--- trunk/phase3/docs/hooks.txt 2011-03-23 00:53:02 UTC (rev 84590)
+++ trunk/phase3/docs/hooks.txt 2011-03-23 03:13:37 UTC (rev 84591)
@@ -553,7 +553,9 @@
 'BeforeGalleryFindFile': before an image is fetched for a gallery
 &$gallery,: the gallery object
 &$nt: the image title
-&$time: image timestamp
+&$time: image timestamp (used to specify the file)
+&$descQuery: query string to add to thumbnail URL
+&$sha1: image base 36 sha1 (used to specify the file, $nt will be ignored if 
this is set)
 
 'BeforeInitialize': before anything is initialized in performRequestForTitle()
 &$title: Title being used for request
@@ -578,6 +580,8 @@
 &$nt: the image title
 &$skip: skip this image and link it?
 &$time: the image timestamp
+&$descQuery: query string to add to thumbnail URL
+&$sha1: image base 36 sha1 (used to specify the file, $nt will be ignored if 
this is set)
 
 'BeforeParserrenderImageGallery': before an image gallery is rendered by Parser
 &$parser: Parser object

Modified: trunk/phase3/includes/ImageGallery.php
===================================================================
--- trunk/phase3/includes/ImageGallery.php      2011-03-23 00:53:02 UTC (rev 
84590)
+++ trunk/phase3/includes/ImageGallery.php      2011-03-23 03:13:37 UTC (rev 
84591)
@@ -14,7 +14,6 @@
        var $mImages, $mShowBytes, $mShowFilename;
        var $mCaption = false;
        var $mSkin = false;
-       var $mRevisionId = 0;
 
        /**
         * Hide blacklisted images?
@@ -253,12 +252,16 @@
                        $nt = $pair[0];
                        $text = $pair[1]; # "text" means "caption" here
 
-                       # Give extensions a chance to select the file revision 
for us
-                       $time = $descQuery = false;
-                       wfRunHooks( 'BeforeGalleryFindFile', array( &$this, 
&$nt, &$time, &$descQuery ) );
-
                        if ( $nt->getNamespace() == NS_FILE ) {
-                               $img = wfFindFile( $nt, array( 'time' => $time 
) );
+                               # Give extensions a chance to select the file 
revision for us
+                               $time = $sha1 = $descQuery = false;
+                               wfRunHooks( 'BeforeGalleryFindFile',
+                                       array( &$this, &$nt, &$time, 
&$descQuery, &$sha1 ) );
+                               # Get the file and register it
+                               $img = $this->mParser->fetchFile( $nt, $time, 
$sha1 );
+                               if ( $img ) {
+                                       $nt = $img->getTitle(); // file title 
may be different (via hooks)
+                               }
                        } else {
                                $img = false;
                        }

Modified: trunk/phase3/includes/Linker.php
===================================================================
--- trunk/phase3/includes/Linker.php    2011-03-23 00:53:02 UTC (rev 84590)
+++ trunk/phase3/includes/Linker.php    2011-03-23 03:13:37 UTC (rev 84591)
@@ -794,31 +794,39 @@
         *
         * @param $title Title object.
         * @param $text String: pre-sanitized HTML
-        * @param $time string: time image was created
+        * @param $time string: MW timestamp of file creation time
         * @return String: HTML
+        */
+       public function makeMediaLinkObj( $title, $text = '', $time = false ) {
+               $img = wfFindFile( $title, array( 'time' => $time ) );
+               return $this->makeMediaLinkFile( $title, $img, $text );
+       }
+
+       /**
+        * Create a direct link to a given uploaded file.
+        * This will make a broken link if $file is false.
         *
+        * @param $title Title object.
+        * @param $file mixed File object or false
+        * @param $text String: pre-sanitized HTML
+        * @return String: HTML
+        *
         * @todo Handle invalid or missing images better.
         */
-       public function makeMediaLinkObj( $title, $text = '', $time = false ) {
-               if ( is_null( $title ) ) {
-                       # # # HOTFIX. Instead of breaking, return empty string.
-                       return $text;
+       public function makeMediaLinkFile( Title $title, $file, $text = '' ) {
+               if ( $file && $file->exists() ) {
+                       $url = $file->getURL();
+                       $class = 'internal';
                } else {
-                       $img  = wfFindFile( $title, array( 'time' => $time ) );
-                       if ( $img ) {
-                               $url  = $img->getURL();
-                               $class = 'internal';
-                       } else {
-                               $url = $this->getUploadUrl( $title );
-                               $class = 'new';
-                       }
-                       $alt = htmlspecialchars( $title->getText(),  ENT_QUOTES 
);
-                       if ( $text == '' ) {
-                               $text = $alt;
-                       }
-                       $u = htmlspecialchars( $url );
-                       return "<a href=\"{$u}\" class=\"$class\" 
title=\"{$alt}\">{$text}</a>";
+                       $url = $this->getUploadUrl( $title );
+                       $class = 'new';
                }
+               $alt = htmlspecialchars( $title->getText(), ENT_QUOTES );
+               if ( $text == '' ) {
+                       $text = $alt;
+               }
+               $u = htmlspecialchars( $url );
+               return "<a href=\"{$u}\" class=\"$class\" 
title=\"{$alt}\">{$text}</a>";
        }
 
        /**

Modified: trunk/phase3/includes/parser/Parser.php
===================================================================
--- trunk/phase3/includes/parser/Parser.php     2011-03-23 00:53:02 UTC (rev 
84590)
+++ trunk/phase3/includes/parser/Parser.php     2011-03-23 03:13:37 UTC (rev 
84591)
@@ -1862,11 +1862,11 @@
                                                        $holders->merge( 
$this->replaceInternalLinks2( $text ) );
                                                }
                                                # cloak any absolute URLs 
inside the image markup, so replaceExternalLinks() won't touch them
-                                               $s .= $prefix . 
$this->armorLinks( $this->makeImage( $nt, $text, $holders ) ) . $trail;
+                                               $s .= $prefix . 
$this->armorLinks(
+                                                       $this->makeImage( $nt, 
$text, $holders ) ) . $trail;
                                        } else {
                                                $s .= $prefix . $trail;
                                        }
-                                       $this->mOutput->addImage( 
$nt->getDBkey() );
                                        wfProfileOut( __METHOD__."-image" );
                                        continue;
 
@@ -1910,16 +1910,22 @@
                        if ( $ns == NS_MEDIA ) {
                                wfProfileIn( __METHOD__."-media" );
                                # Give extensions a chance to select the file 
revision for us
-                               $skip = $time = false;
-                               wfRunHooks( 'BeforeParserMakeImageLinkObj', 
array( &$this, &$nt, &$skip, &$time ) );
+                               $skip = $time = $sha1 = $descQuery = false;
+                               wfRunHooks( 'BeforeParserMakeImageLinkObj',
+                                       array( &$this, &$nt, &$skip, &$time, 
&$descQuery, &$sha1 ) );
                                if ( $skip ) {
+                                       $this->mOutput->addImage( 
$nt->getDBkey() ); // register
                                        $link = $sk->link( $nt );
                                } else {
-                                       $link = $sk->makeMediaLinkObj( $nt, 
$text, $time );
+                                       # Fetch and register the file
+                                       $file = $this->fetchFile( $nt, $time, 
$sha1 );
+                                       if ( $file ) {
+                                               $nt = $file->getTitle(); // 
file title may be different (via hooks)
+                                       }
+                                       $link = $sk->makeMediaLinkFile( $nt, 
$file, $text );
                                }
                                # Cloak with NOPARSE to avoid replacement in 
replaceExternalLinks
                                $s .= $prefix . $this->armorLinks( $link ) . 
$trail;
-                               $this->mOutput->addImage( $nt->getDBkey() );
                                wfProfileOut( __METHOD__."-media" );
                                continue;
                        }
@@ -3340,14 +3346,16 @@
                for ( $i = 0; $i < 2 && is_object( $title ); $i++ ) {
                        # Give extensions a chance to select the revision 
instead
                        $id = false; # Assume current
-                       wfRunHooks( 'BeforeParserFetchTemplateAndtitle', array( 
$parser, &$title, &$skip, &$id ) );
+                       wfRunHooks( 'BeforeParserFetchTemplateAndtitle',
+                               array( $parser, &$title, &$skip, &$id ) );
 
                        if ( $skip ) {
                                $text = false;
                                $deps[] = array(
-                                       'title' => $title,
-                                       'page_id' => $title->getArticleID(),
-                                       'rev_id' => null );
+                                       'title'         => $title,
+                                       'page_id'       => 
$title->getArticleID(),
+                                       'rev_id'        => null
+                               );
                                break;
                        }
                        $rev = $id ? Revision::newFromId( $id ) : 
Revision::newFromTitle( $title );
@@ -3359,9 +3367,9 @@
                        }
 
                        $deps[] = array(
-                               'title' => $title,
-                               'page_id' => $title->getArticleID(),
-                               'rev_id' => $rev_id );
+                               'title'         => $title,
+                               'page_id'       => $title->getArticleID(),
+                               'rev_id'        => $rev_id );
 
                        if ( $rev ) {
                                $text = $rev->getText();
@@ -3390,6 +3398,23 @@
        }
 
        /**
+        * Fetch a file and register a reference to it.
+        * @TODO: register and track file version info too
+        */
+       function fetchFile( $title, $time = false, $sha1 = false ) {
+               if ( $sha1 ) { // get by (sha1,timestamp)
+                       $file = RepoGroup::singleton()->findFileFromKey( $sha1, 
array( 'time' => $time ) );
+                       if ( $file ) {
+                               $title = $file->getTitle(); // file title may 
not match $title
+                       }
+               } else { // get by (name,timestamp)
+                       $file = wfFindFile( $title, array( 'time' => $time ) );
+               }
+               $this->mOutput->addImage( $title->getDBkey() );
+               return $file;
+       }
+
+       /**
         * Transclude an interwiki link.
         */
        function interwikiTransclude( $title, $action ) {
@@ -4506,7 +4531,6 @@
                $ig->setHideBadImages();
                $ig->setAttributes( Sanitizer::validateTagAttributes( $params, 
'table' ) );
                $ig->useSkin( $this->mOptions->getSkin( $this->mTitle ) );
-               $ig->mRevisionId = $this->mRevisionId;
 
                if ( isset( $params['showfilename'] ) ) {
                        $ig->setShowFilename( true );
@@ -4560,11 +4584,6 @@
                        $html = $this->recursiveTagParse( trim( $label ) );
 
                        $ig->add( $nt, $html );
-
-                       # Only add real images (bug #5586)
-                       if ( $nt->getNamespace() == NS_FILE ) {
-                               $this->mOutput->addImage( $nt->getDBkey() );
-                       }
                }
                return $ig->toHTML();
        }
@@ -4615,6 +4634,7 @@
         * @param $title Title
         * @param $options String
         * @param $holders LinkHolderArray
+        * @return string HTML
         */
        function makeImage( $title, $options, $holders = false ) {
                # Check if the options text is of the form "options|alt text"
@@ -4646,15 +4666,18 @@
                $sk = $this->mOptions->getSkin( $this->mTitle );
 
                # Give extensions a chance to select the file revision for us
-               $skip = $time = $descQuery = false;
-               wfRunHooks( 'BeforeParserMakeImageLinkObj', array( &$this, 
&$title, &$skip, &$time, &$descQuery ) );
-
+               $skip = $time = $sha1 = $descQuery = false;
+               wfRunHooks( 'BeforeParserMakeImageLinkObj',
+                       array( &$this, &$title, &$skip, &$time, &$descQuery, 
&$sha1 ) );
                if ( $skip ) {
+                       $this->mOutput->addImage( $title->getDBkey() ); // 
register
                        return $sk->link( $title );
                }
-
-               # Get the file
-               $file = wfFindFile( $title, array( 'time' => $time ) );
+               # Fetch and register the file
+               $file = $this->fetchFile( $title, $time, $sha1 );
+               if ( $file ) {
+                       $title = $file->getTitle(); // file title may be 
different (via hooks)
+               }
                # Get parameter map
                $handler = $file ? $file->getHandler() : false;
 
@@ -4809,7 +4832,8 @@
                wfRunHooks( 'ParserMakeImageParams', array( $title, $file, 
&$params ) );
 
                # Linker does the rest
-               $ret = $sk->makeImageLink2( $title, $file, $params['frame'], 
$params['handler'], $time, $descQuery, $this->mOptions->getThumbSize() );
+               $ret = $sk->makeImageLink2( $title, $file, $params['frame'], 
$params['handler'],
+                       $time, $descQuery, $this->mOptions->getThumbSize() );
 
                # Give the handler a chance to modify the parser object
                if ( $handler ) {


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to