r72022 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r72021‎ | r72022 | r72023 >
Date:13:16, 31 August 2010
Author:platonides
Status:ok (Comments)
Tags:
Comment:
Move FileUpload hook out of the transaction.
FileSearch relied on the article not existing to detect if it's a but it was broken: always detected it as a reupload.
Add a new parameter to the hook to detect reuploads.
Fixed other issues on FileSearch extension.
Modified paths:
  • /trunk/extensions/FileSearch/FileSearchIndexer.php (modified) (history)
  • /trunk/phase3/docs/hooks.txt (modified) (history)
  • /trunk/phase3/includes/filerepo/LocalFile.php (modified) (history)

Diff [purge]

Index: trunk/phase3/docs/hooks.txt
@@ -788,6 +788,8 @@
789789
790790 'FileUpload': When a file upload occurs
791791 $file : Image object representing the file that was uploaded
 792+$reupload : Boolean indicating if there was a previously another image there or not (since 1.17)
 793+$hasDescription : Boolean indicating that there was already a description page and a new one from the comment wasn't created (since 1.17)
792794
793795 'FileUndeleteComplete': When a file is undeleted
794796 $title: title object to the file
Index: trunk/phase3/includes/filerepo/LocalFile.php
@@ -928,9 +928,6 @@
929929 $article->doEdit( $pageText, $comment, EDIT_NEW | EDIT_SUPPRESS_RC );
930930 }
931931
932 - # Hooks, hooks, the magic of hooks...
933 - wfRunHooks( 'FileUpload', array( $this ) );
934 -
935932 # Commit the transaction now, in case something goes wrong later
936933 # The most important thing is that files don't get lost, especially archives
937934 $dbw->commit();
@@ -941,6 +938,9 @@
942939 # which in fact doesn't really exist (bug 24978)
943940 $this->saveToCache();
944941
 942+ # Hooks, hooks, the magic of hooks...
 943+ wfRunHooks( 'FileUpload', array( $this, $reupload, $descTitle->exists() ) );
 944+
945945 # Invalidate cache for all pages using this file
946946 $update = new HTMLCacheUpdate( $this->getTitle(), 'imagelinks' );
947947 $update->doUpdate();
Index: trunk/extensions/FileSearch/FileSearchIndexer.php
@@ -19,20 +19,21 @@
2020 *
2121 * @param Image $file
2222 */
23 - public static function upload( $file ) {
24 - $title = $file->getTitle();
25 - $article = new Article( $title );
26 - # If the description page doesn't exist at this time, then we're
27 - # dealing with a fresh upload; in this case, the index update will
28 - # be done when the page is deleted. On the other hand, if it *does*
29 - # exist, then there won't be a search index update, so we have to
30 - # trigger one ourselves. Not the cleanest of methods...
31 - if( $title->exists() ) {
 23+ public static function upload( $file, $reupload, $hasDescription ) {
 24+ # If we create the description page with the upload, there will be
 25+ # a SearchUpdate when the page is created.
 26+ # Otherwise, if the page exists there won't be a search index update,
 27+ # so we have to trigger one ourselves
 28+ if( $hasDescription ) {
3229 global $wgDeferredUpdateList;
 30+ $title = $file->getTitle();
 31+ $article = new Article( $title );
 32+
3333 $update = new SearchUpdate( $title->getArticleId(),
3434 $title->getPrefixedText(), $article->getContent() );
3535 array_push( $wgDeferredUpdateList, $update );
3636 }
 37+ return true;
3738 }
3839
3940 /**
@@ -48,9 +49,9 @@
4950 wfDebugLog( 'filesearch', "Update called for `{$title}`" );
5051 $titleObj = Title::makeTitle( NS_IMAGE, $title );
5152 $image = wfFindFile( $titleObj );
52 - if ( !$image->exists() ) {
 53+ if ( !$image || !$image->exists() ) {
5354 wfDebugLog( 'filesearch', "Image does not exist: $title" );
54 - return;
 55+ return true;
5556 }
5657 $extractor = self::getExtractor( ( $mime = $image->getMimeType() ) );
5758 if( $extractor instanceof Extractor ) {
@@ -62,6 +63,7 @@
6364 wfDebugLog( 'filesearch', "No suitable extractor found for `{$mime}`" );
6465 }
6566 }
 67+ return true;
6668 }
6769
6870 /**

Comments

#Comment by Bryan (talk | contribs)   21:57, 1 December 2010

Would there be any reason for extensions to assume they are actually inside a transaction, and would such an extension break by this change?

#Comment by Platonides (talk | contribs)   23:05, 1 December 2010

I doubt so. The extension would need to perform several queries, getting just the last one rollbacked (if it's just one query then a rollback -always unlikely- would look like the extension not working, but mediawiki would still be consistent). Since the file is placed there, a rollback before would have been worse.

Status & tagging log