+ $errbit = htmlspecialchars( substr($contents, 1) );
+ switch( $retval ) {
+ case 'E':
+ $errmsg = $this->_error( 'math_lexing_error', $errbit );
+ break;
+ case 'S':
+ $errmsg = $this->_error( 'math_syntax_error', $errbit );
+ break;
+ case 'F':
+ $errmsg = $this->_error( 'math_unknown_function', $errbit );
+ break;
+ default:
+ $errmsg = $this->_error( 'math_unknown_error', $errbit );
+ }
+ }
+
+ if ( !$errmsg ) {
+ $this->hash = substr ($contents, 1, 32);
+ }
+
+ wfRunHooks( 'MathAfterTexvc', array( &$this, &$errmsg ) );
+
+ if ( $errmsg ) {
+ return $errmsg;
+ }
+
+ if (!preg_match("/^[a-f0-9]{32}$/", $this->hash)) {
+ return $this->_error( 'math_unknown_error' );
+ }
+
+ if( !file_exists( "$wgTmpDirectory/{$this->hash}.png" ) ) {
+ return $this->_error( 'math_image_error' );
+ }
+
+ if( filesize( "$wgTmpDirectory/{$this->hash}.png" ) == 0 ) {
+ return $this->_error( 'math_image_error' );
+ }
+
+ $hashpath = $this->_getHashPath();
+ if( !file_exists( $hashpath ) ) {
+ if( !@wfMkdirParents( $hashpath, 0755 ) ) {
+ return $this->_error( 'math_bad_output' );
+ }
+ } elseif( !is_dir( $hashpath ) || !is_writable( $hashpath ) ) {
+ return $this->_error( 'math_bad_output' );
+ }
+
+ if( !rename( "$wgTmpDirectory/{$this->hash}.png", "$hashpath/{$this->hash}.png" ) ) {
+ return $this->_error( 'math_output_error' );
+ }
+
+ # Now save it back to the DB:
+ if ( !wfReadOnly() ) {
+ $outmd5_sql = pack('H32', $this->hash);
+
+ $md5_sql = pack('H32', $this->md5); # Binary packed, not hex
+
+ $dbw = wfGetDB( DB_MASTER );
+ $dbw->replace( 'math', array( 'math_inputhash' ),
+ array(
+ 'math_inputhash' => $dbw->encodeBlob($md5_sql),
+ 'math_outputhash' => $dbw->encodeBlob($outmd5_sql),
+ 'math_html_conservativeness' => $this->conservativeness,
+ 'math_html' => $this->html,
+ 'math_mathml' => $this->mathml,
+ ), __METHOD__
+ );
+ }
+
+ // If we're replacing an older version of the image, make sure it's current.
+ global $wgUseSquid;
+ if ( $wgUseSquid ) {
+ $urls = array( $this->_mathImageUrl() );
+ $u = new SquidUpdate( $urls );
+ $u->doUpdate();