Various edit stash fixes
authorAaron Schulz <aschulz@wikimedia.org>
Tue, 9 Dec 2014 21:48:17 +0000 (13:48 -0800)
committerAaron Schulz <aschulz@wikimedia.org>
Tue, 9 Dec 2014 21:50:00 +0000 (13:50 -0800)
* Unlock the key at the right point, so checkCache actually sees the result
* Turn CRLF to LF just as EditPage does via getText(), this avoids misses
* Added a bit more debug logging

Change-Id: I5c296325ebee2501e5de59b8090e1ddde8689f17

includes/api/ApiStashEdit.php

index 00f6814..fd5bd7e 100644 (file)
@@ -48,7 +48,8 @@ class ApiStashEdit extends ApiBase {
                        $this->dieUsage( "Unsupported content model/format", 'badmodelformat' );
                }
 
-               $text = trim( $params['text'] ); // needed so the key SHA1's match
+               // Trim and fix newlines so the key SHA1's match (see RequestContext::getText())
+               $text = rtrim( str_replace( "\r\n", "\n", $params['text'] ) );
                $textContent = ContentHandler::makeContent(
                        $text, $title, $params['contentmodel'], $params['contentformat'] );
 
@@ -109,8 +110,11 @@ class ApiStashEdit extends ApiBase {
                } elseif ( $wgMemc->lock( $key, 0, 30 ) ) {
                        $contentFormat = $content->getDefaultFormat();
                        $editInfo = $page->prepareContentForEdit( $content, null, $user, $contentFormat );
-                       $wgMemc->unlock( $key );
                        $status = 'error'; // default
+                       $unlocker = new ScopedCallback( function() use ( $key ) {
+                               global $wgMemc;
+                               $wgMemc->unlock( $key );
+                       } );
                } else {
                        $editInfo = false;
                        $status = 'busy';
@@ -195,12 +199,13 @@ class ApiStashEdit extends ApiBase {
                        if ( $wgMemc->lock( $key, 30, 30 ) ) {
                                $editInfo = $wgMemc->get( $key );
                                $wgMemc->unlock( $key );
-                               $sec = microtime( true ) - $start;
-                               wfDebugLog( 'StashEdit', "Waited $sec seconds on '$key'." );
                        }
+                       $sec = microtime( true ) - $start;
+                       wfDebugLog( 'StashEdit', "Waited $sec seconds on '$key'." );
                }
 
                if ( !is_object( $editInfo ) || !$editInfo->output ) {
+                       wfDebugLog( 'StashEdit', "No cache value for key '$key'." );
                        return false;
                }