Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6942119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:58:26+00:00 2026-05-27T12:58:26+00:00

I have a somewhat interesting query.. I may have over simplified the example, but

  • 0

I have a somewhat interesting query..

I may have over simplified the example, but ill do my best to describe my problem.

I am building a very simple implementation of a wiki from scratch, everything going well until I realized I need Cycle Detection to prevent endless loops of data populating the page and well Overflowing the stack heap.

Database structure is basic, well its more intricate that what is shown but for the purposes of this post the two columns is all we need.

The Content field is straight forward, it stores the Content of the Page or WikiPart links i.e [[n]] to link to another part and includes, links are shopwn as [[n]] and includes are {{n}}.

+---------------------------+
| id    |  Content          |
+---------------------------+
|  1    | see {{2}} here    | 
+---------------------------+
|  2    | {{1}} here [[4]]  | 
+---------------------------+
|  4    | {{1}}             | 
+---------------------------+



$html_for_screen = readData($this->Content);

function readData($wikipage) {

    $str = "";

    //Convert any wiki links to HTML Links
    $wikipage = Converter::convertWikink($wikipage);

    //Get ALL Include Link matches into array
    $wiki_inc = RegEx::getMatches(wikipage); 

    //Iterate through the Matches
    foreach($wiki_inc as $wiki) {
         //traverse through each match. 
         //but I assume here is where I would eventually have the trouble
         //With infinant loops
         $str .= readData($wiki);
    }

    return $str;

}

The Question:
How would I prevent Wiki parts endlessly including eachother.
i.e WikiPart 1 includes WikiPart2.. but WikiPart 2 includes WikiPart1

The parse or readData() function would just continue looping.

regards

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:58:27+00:00Added an answer on May 27, 2026 at 12:58 pm

    Actually if you encounter a cycle, you can’t resolve any longer. Example:

    1: {{2}}
    2: {{1}}
    

    This will create an endless loop:

    1 -> 2 -> 1 -> 2 -> ...
    

    As any computers resources are limited, endless loops will result in a crash.

    So what can you do? You could detect that and then error out by using a stack:

    function readData($wikipage)
    {
        static $stack = array();
        if (in_array($wikipage, $stack))
        {
            throw new Exception(sprintf('Circular reference detected: %s -> %s', implode(' -> ', $stack), $wikipage));
        }
        $stack[] = $wikipage;
    
        ... (your existing code)
    
        array_pop($stack);
    }
    

    Additionally you can control recursion limit by using count($stack) to determine the nesting level.

    Actually throwing an exception might not be the proper reaction to the cyclic reference, but it shows how the detection work. You can decide on your own how you’d like to deal with the case, e.g. returning FALSE or not resolving the field any longer etc..

    Edit: Getting creative here:

    If the output is HTML you could make the user resolve the problem as well. If such a cyclic reference is detected, some AJAX marker could be inserted that would request in some form of overlay within the browser that snippet which was unable to obtain on the server side. Such an overlay will then contain the cyclic reference again (being able to overlay again) so that the user would be able to see the cyclic reference interactively.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have somewhat interesting development situation. The client and deployment server are inside a
I have a somewhat simple Client/Server solution running over C# remoting (System.Runtime.Remoting). The MarshalByRef
I have somewhat of a problem. We have a centralized interface engine that will
I have interesting problem with my UiimageViews. I have an application (game with 2
I have somewhat of a strange question that is not really technical, but I
I have an interesting problem. I am developing for a .Net2.0 solution. I have
I have somewhat of a problem. My old webserver (windows 2003) have been replaced,
I have an interesting problem, however I don't know quite how to articulate it
I have somewhat deep Json structure like below incoming over the wire into C#
I have a somewhat complex SQL query that is pulling from a table of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.