Jump to content

loadFromUrl on \IPS\forums\Topic\Post


TSP

Recommended Posts

Posted

Probably an issue with other comment classes, but I've only tested with \IPS\forums\Topic\Post.

The behaviour on loadFromUrl doesn't produce an expected result. 

Let's say you installed the following code in the root folder of this community:

<?php
require_once( __DIR__ . '/init.php' );
$url = \IPS\Http\Url::createFromString(
	'https://invisioncommunity.com/forums/topic/444948-cache-page-output-for-guests/?do=findComment&comment=2809267'
);
$post = \IPS\forums\Topic\Post::loadFromUrl($url);
echo "Loaded postId {$post->pid}: {$post->url()}";

What I would expect: It should load the post with postId 2809267. Alternatively fail if it's unable to identify a commentId from the URL.

What actually happens: It loads the post with postId 444948 (if it exists). 444948 in the given URL refers to the topicId. 

You should

Extend the method loadFromUrl in comment classes. Make it load commentId 2809267 in the above case.
If the link given is a link to the topic, but there is no sign of an attempt to pass a commentId in the URL, you could load the first post of that topic.
If it's unable to identify a commentId it should load, then it should throw an exception indicating this. (So not an \OutOfRangeException, something else)

Regardless of how you choose to resolve this, the code shouldn't continue doing what it does today at least. 

Posted

I've made an internal note to see if we can look into this at some point, however please note that it may be a while (or it may never happen) so you should look into alternative solutions in the mean time.

Posted
17 hours ago, bfarber said:

I've made an internal note to see if we can look into this at some point, however please note that it may be a while (or it may never happen) so you should look into alternative solutions in the mean time.

Yes, I'm not patient enough to wait for you to sort this one out anyway 😛

I did the following in the script where I wished to be able to load from URL:

<?php
// lots of other code
		while ( $reply = readline(' > Enter an URL or postId to the post you wish to attempt to rebuild: ') )
		{
			$this->checkAndCleanMemory();
			try
			{
				$postId = null;

				if ( is_numeric( $reply ) )
				{
					$postId = (int) $reply;
				}
				else
				{
					$url = \IPS\Http\Url::createFromString( $reply );
					$postId = (int) $url->queryString['comment'] ?? false;
					
					if ( !$postId )
					{
						if ( $fragment = $url->data['fragment'] ?? false )
						{
							$postId = (int) str_replace('comment-', '', $fragment);
						}
					}				
				}
				if ( !is_int( $postId ) )
				{
					throw new \Exception('Found no integer');
				}
// lots of other code

Would obviously need to be a more proper implementation in loadFromUrl, and my code doesn't cover the first post in a topic if no commentID is given, but for my use case atm. this is good enough.

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...