XF 2.2 Article Constraints

Ozzy47

Well-known member
Trying to extend XF\ThreadType\Article to add settings for max characters, images and media. Settings are in place. Now If I set the XF setting messageMaxLength to a value of say 100, and my setting to a value of 150000 then trying to post an article less than 150000 characters, I get the following error message, The submitted message is too long to be processed. Please shorten it. Im sure there is a problem with the way I am trying to extend the function.
PHP:
<?php

namespace OzzModz\ArticleEnhance\XF\ThreadType;

use XF\Entity\Post;
use XF\Entity\Thread;
use XF\Http\Request;
use XF\Mvc\Entity\Entity as Entity;

/**
* Class Article
*
* Extends \XF\ThreadType\Article
*
* @package OzzModz\ArticleEnhance\XF\ThreadType
*/

class Article extends XFCP_Article
{
    public function setupMessagePreparer(
        Thread $thread,
        Post $post,
        \XF\Service\Message\Preparer $preparer
    )
    {
        $parent = parent::setupMessagePreparer($thread, $post, $preparer);
     
        // Get options
        $options = \XF::options();

        // Get variables
        $character = $options->ozzmodzArticlemessageMaxLength;
        $images = $options->ozzmodzArticlemessageMaxImages;
        $media = $options->ozzmodzArticlemessageMaxMedia;

        if (!$post->isFirstPost())
        {
            return;
        }

        // articles are significant content, so give them more relaxed constraints than an average post
        //$relaxFactor = 3;

        //$character = intval($preparer->getConstraint('character'));
        if ($character > 0)
        {
            //$preparer->setConstraint('maxLength', $relaxFactor * $maxLength);
            $preparer->setConstraint('maxLength', $character);
        }

        //$maxImages = intval($preparer->getConstraint('maxImages'));
        if ($images > 0)
        {
            //$preparer->setConstraint('maxImages', $relaxFactor * $maxImages);
            $preparer->setConstraint('maxImages', $images);
             
        }

        //$maxMedia = intval($preparer->getConstraint('maxMedia'));
        if ($media > 0)
        {
            //$preparer->setConstraint('maxMedia', $relaxFactor * $maxMedia);
            $preparer->setConstraint('maxMedia', $media);
        }    
     
        return $parent;
    }
}
 
Why is it that the article limits don’t need to extend that? They simply x3 the XF setting.
 
Why is it that the article limits don’t need to extend that? They simply x3 the XF setting.
Good question. XF\Service\Message\Preparer.php:

PHP:
    public function checkValidity($message)
    {
        $this->errors = [];

        /** @var \XF\BbCode\ProcessorAction\AnalyzeUsage $usage */
        $usage = $this->bbCodeProcessor->getAnalyzer('usage');

        $maxLength = $this->constraints['maxLength'];
        if ($maxLength && utf8_strlen($message) > $maxLength)
        {
            $this->errors[] = \XF::phraseDeferred(
                'please_enter_message_with_no_more_than_x_characters',
                ['count' => $maxLength]
            );
        }
        
        ...
    }

The above shouldn't need to be extended as the constraints are already set to the higher limits. Maybe this is a bug?
 
Even with that commented out, I still get the same message.

Even though this has nothing to do with your error, I would change these parts as another add-on may extend this as well:

PHP:
        // $parent = parent::setupMessagePreparer($thread, $post, $preparer);
    
        // Get options
        $options = \XF::options();

        // Get variables
        $character = $options->ozzmodzArticlemessageMaxLength;
        $images = $options->ozzmodzArticlemessageMaxImages;
        $media = $options->ozzmodzArticlemessageMaxMedia;

        if (!$post->isFirstPost())
        {
           return parent::setupMessagePreparer($thread, $post, $preparer);
        }

Have you tried to dump $this->constraints in message preparer->checkValidity?
 
Hmm, with the addon disabled, I still get that error message if the article length is more than X3 times the max character setting.
 
Seems like it might be working. I think setting such a low limit on the message character legenth was causing issues. I increased that to 5000, and set the article characters to 18,000 then was able to post an article with ~16,000 characters successfully. Trying to post one with more then 18,000 threw the error correctly.
 
Seems like it might be working. I think setting such a low limit on the message character legenth was causing issues. I increased that to 5000, and set the article characters to 18,000 then was able to post an article with ~16,000 characters successfully. Trying to post one with more then 18,000 threw the error correctly.

I spoke too soon. Seems I still get stopped by XF:Editor::convertToBbCode() if my characters are more than 6 times the XFmax character setting.

This, $htmlMaxLength = 6 * $this->options()->messageMaxLength;
PHP:
    public function convertToBbCode($html, $htmlMaxLength = -1)
    {
        if ($htmlMaxLength < 0)
        {
            $htmlMaxLength = 6 * $this->options()->messageMaxLength;
            // increase the limit as HTML can be a lot more verbose and some scenarios might relax the limit
        }

        if ($htmlMaxLength && utf8_strlen($html) > $htmlMaxLength)
        {
            throw \XF::phrasedException('submitted_message_is_too_long_to_be_processed');
        }

        $bbCode = \XF\Html\Renderer\BbCode::renderFromHtml($html);
        return \XF::cleanString($bbCode);
    }

Now If I edit the XF/ControllerPlugin/Editor.php file and change $htmlMaxLength = 6 * $this->options()->messageMaxLength; to 60, $htmlMaxLength = 60 * $this->options()->messageMaxLength; I don't get the error.

Now I try to extend that function:
PHP:
<?php

namespace OzzModz\ArticleEnhance\XF\ControllerPlugin;

/**
 * Class Editor
 *
 * Extends \XF\ControllerPlugin\Editor
 *
 * @package  OzzModz\ArticleEnhance\XF\ControllerPlugin
 */

class Editor extends XFCP_Editor
{
    public function convertToBbCode($html, $htmlMaxLength = -1)
    {
          $parent = parent::convertToBbCode($html, $htmlMaxLength = -1);
      
        if ($htmlMaxLength < 0)
        {
            $htmlMaxLength = 60 * $this->options()->messageMaxLength;
            // increase the limit as HTML can be a lot more verbose and some scenarios might relax the limit
        }

        if ($htmlMaxLength && utf8_strlen($html) > $htmlMaxLength)
        {
            throw \XF::phrasedException('submitted_message_is_too_long_to_be_processed');
        }

        $bbCode = \XF\Html\Renderer\BbCode::renderFromHtml($html);
        return \XF::cleanString($bbCode);
      
          return $parent;
    }
}
 
You want to replace the $htmlMaxLength value before calling parent::convertToBbCode($html, $htmlMaxLength) to avoid getting the exception throw.

Something like this:
PHP:
    public function convertToBbCode($html, $htmlMaxLength = -1)
    {
        if ($conditionToCheckIfHtmlMaxLengthNeedsToBeUpdated)
        {
            $htmlMaxLength *= 60
        }
      
        return parent::convertToBbCode($html, $htmlMaxLength = -1);
    }
 
Okay, not sure what condition to check for though. I suppose if it’s article thread somehow.
 
Even doing something as simple as:
PHP:
    public function convertToBbCode($html, $htmlMaxLength = -1)
    {
        if ($htmlMaxLength < 0)
        {
            $htmlMaxLength *= 60;
        }
      
        return parent::convertToBbCode($html, $htmlMaxLength = -1);
    }

Still throws the error.
:unsure::unsure:
 
PHP:
return parent::convertToBbCode($html, $htmlMaxLength = -1);
That wouldn't really work as you are setting the $htmlMaxLength value to -1 there which in-turn will do it's own weirdery
 
Well this don't even work, still get the error:
PHP:
    public function convertToBbCode($html, $htmlMaxLength = -1)
    {
        //if ($htmlMaxLength < 0)
        //{
            $htmlMaxLength *= 60;
        //}
      
        return parent::convertToBbCode($html, $htmlMaxLength);
    }
 
What message are you getting now? There are two different messages related to maximum message length, you get:
The submitted message is too long to be processed. Please shorten it.
when the maximum HTML character length is reached and you get
Please enter a message with no more than {count} characters.
when the message preparer says you have entered more than allowed max character length.
 
Back
Top Bottom