We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 30269
    • 119 Posts
    To document our products, we could use a Wiki. We installed MediaWiki but that seemed overkill. Besides, we like to keep everything within Modx for maintenance reasons. Maybe there is a Modx Wiki snippet or something, but I could not find it, so we created a simple solutions that works for us.

    As I see it, a very basic Wiki needs
    1) easy linking without relying on any kind of navigational structure and
    2) roll back of revisions. Although this is only needed if the Wiki is open to the general public. For well-informed editors like in our case, we can do without.

    For easy linking, it would be great to be able to just type text in Quickedit and mark the words/items that would need further explanation, hence could use a link to a new page. So that’s where this snippet come in.

    Tags

    For unfriendliness it would be great to use tags, like in MediaWiki. They use [[ ]], we can’t so I suggest {[ ]} (unless there’s a better idea?)
    An editor would then write text like:

    ** "A good way of transportation is a {[bicycle]}. Instead of a {[car]} it does not use fuel." **
    This would inflict 3 pages already: the one this text is written on and two extra with titile "bicycle" and "car"


    Typical call: Call only one per page - in the template.

    [[WikiX? &wikiTree=`190` &template = `Wiki_Master` &toEmptyClass=`redlink`]]

    Parameters:
    &wikiTree - parent of the new created pages
    &template - template for these new pages
    &toEmptyClass - class added to link if target page does not have any content yet

    Function:

    WikiX will find any word tagged between {[itemToLinkTo]}

    WikiX will search for a page (called $wkPage in code) that has a Title == itemToLinkTo

    If not found, WikiX will create a new page and set Title to ’itemToLinkTo’

    WikiX will replace the tag with a Modx-page-link :
    With an additional highlight class ($pageEmptyClass) if taget page’s content is empty
    Standard link if content is not empty
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Sounds like modifications to NewsManager. It already does a lot of what you are saying here. Studying how it’s done would be very helpful in a project like this.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 30269
        • 119 Posts
        Indeed, after some cut and paste, this seem to do the trick:
        /* <?php */
        /* WikiX v0.6  Basic Wiki - Page link and creator for Modx
           By Tvld, Oct 13, 2006. Licensed under GPL
           Modified 
             Oct 16, 2006 - 
             Oct 19 - error checking for right use of tags to avoid database errors
        
        To document our products, we could use a Wiki. We installed MediaWiki but that seemed overkill. Besides, we like to keep everything within Modx for maintenance reasons. Maybe there is a Modx Wiki snippet or something, but I could not find it, so we created a simple solutions that works for us.
        
        As I see it, a very basic Wiki needs 
        	1) easy linking without relying on any kind of navigational structure and 
        	2) roll back of revisions. Although this is only needed if the Wiki is open to the general public. For well-informed editors like in our case, we can do without.
        	
        For easy linking, it would be great to be able to just type text in Quickedit and mark the words/items that would need further explanation, hence could use a link to a new page. So that's where this snippet come in.
        
        [b]Tags[/b]
        
        For unfriendliness it would be great to use tags, like in MediaWiki. They use [[ ]], we can't so I suggest {[ ]}  (unless there's a better idea?)
        An editor would then write text like:
        
         ** "A good way of transportation is a {[bicycle]}. Instead of a {[car]} it does not use fuel." **
         This would inflict 3 pages already: the one this text is written on and two extra with title "bicycle" and "car"
        
        [b]Typical call[/b]: Call only one per page - in the template.
        
        [[WikiX? &wikiTree=`190` &template = `Wiki_Master` &toEmptyClass=`redlink` &jsDir=`[(base_url)]assets/js/`]]
        
        Parameters:
         &wikiTree 	- parent of the new created pages
         &template	- template for these new pages
         &toEmptyClass - class added to link if target page does not have any content yet
         &jsDir - directory where to find wikix_js
         
        [b]Function[/b]: 
        
        WikiX will find any word tagged between {[itemToLinkTo]}
        
        WikiX will search for a page (called $wkPage in code) that has a Title == itemToLinkTo
        
        If not found, WikiX will create a new page and set Title to 'itemToLinkTo'
        
        WikiX will replace the tag with a Modx-page-link :
        	With an additional highlight class ($pageEmptyClass) if taget page's content is empty
        	Standard link if content is not empty
        
        ****TO DO
        
        * Search under directory only (FlexSearch also)
        * Article submit by user - seperate pane, store older version for later recall
        * Article history browser with later version recall option
        
        
        	
        */
        
        if (isset($wikiTree)){ // directory to limit search to (<-DOES NOT WORK YET ), as well as parent for new page inserts
        	if ($wikiTree=='nl' ){ $wikiTree= 190; }
        } else {
        	$wikiTree= 1;
        }
        
        $jsDir = isset($jsDir)? $jsDir : '';
        
        $template = isset($template) ? $modx->db->getValue('SELECT id FROM '.$modx->getFullTableName('site_templates').' WHERE templatename=\''.mysql_escape_string($template).'\''):$modx->config['default_template'];
        
        $tbl = $modx->dbConfig['dbase'] . "." . $modx->dbConfig['table_prefix'] . "site_content";
        
        // get details of this page
        $html = $modx->documentObject['content'];
        
        // Loop first to find errors...
        $s= strpos($html,'{[');
        $i=0;
        $lastTag = '(no tag yet)';
        while ( $s ){
        	$t= strpos($html,']}',$s);
        	if ( !$t ) {return "<br /><strong>WikiX Error 1: End tag missing after position $s.<br/> Parsing stopped after</strong> '$lastTag' <strong>tag #$i</strong><br />";}
        	$lkLen = $t-$s;
        	  if ( $lkLen > 50 ){ return "<br /><strong>WikiX Error 2: title size large than 50 characters. Found ']}' $lkLen characters after position $s.<br/>  Parsing stopped after</strong> '$lastTag' <strong>tag #$i</strong><br />";}
        	$ns = strpos($html,'{[',$s+1);
        	  if ($ns && $ns<$t ) { return "<br /><strong>WikiX Error 3 : tags can not be nested. Found '{[' on $ns after position $s.<br/>  Parsing stopped after</strong> '$lastTag' <strong>tag #$i</strong><br />";}
        	$htmlTag = strpos($html,'<',$s);
        	  if ($htmlTag && $htmlTag<$t ) { return "<br /><strong>WikiX Error 4: no HTML tags in Wiki parameter allowed! Found '<' at position: $htmlTag, between $s and $t.<br/>  Parsing stopped after</strong> '$lastTag' <strong>tag #$i</strong><br />";}
            $lastTag= substr($html,$s+2,$t-$s-2);	  
            $i=$i+1;	  
        	$s= strpos($html,'{[',$s+2);
        }
        // now do the real tag replacing
        $s= strpos($html,'{[');
        while ( $s ){
        	$t= strpos($html,']}',$s);
        	$linkItem= substr($html,$s+2,$t-$s-2);
        	$html = substr_replace($html,wikiLink($linkItem,$wikiTree,$template,$toEmptyClass),$s,$t-$s+2);
        	$s= strpos($html,'{[');
        }
        
        $wikixjs='<script type="text/javascript" src="'.$jsDir .'wikix_v02.js"></script>';
        $modx->setPlaceholder('wikixjs', $wikixjs); 
        
        return $html;
        
        /*********** Functions */
        
        function wikiLink($linkItem,$wikiTree,$template,$toEmptyClass){
        
        	global $modx;
        	
        	$tbl = $modx->dbConfig['dbase'] . "." . $modx->dbConfig['table_prefix'] . "site_content";
        	
        	$sql = "SELECT id, pagetitle, content ";
        	$sql .= "FROM $tbl WHERE ";
        	$sql .= "( pagetitle='$linkItem'";
        	$sql .= " OR description LIKE '% $linkItem %')";
        	$sql .= " AND $tbl.published = 1 AND $tbl.searchable=1 AND $tbl.deleted=0";
        	$sql .= " AND $tbl.parent = $wikiTree";
        	
        	$rslt = $modx->dbQuery($sql);
        	if ( $modx->recordCount($rslt)>0 ){ 
        		$row = $modx->fetchRow($rslt);
        		$pageId = $row['id'];
        		$content = $row['content'];
        		$longTitle = $row['longtitle'];
        	} else {   // create new page under $wikiTree
        		$userid = $modx->getLoginUserID();
        	    if(!$user) $user = 'anonymous';
        	    
        	    // post content from newspublisher snippet
        	    $flds = array(
        	        'pagetitle'     => ucfirst($linkItem),
        	        'longtitle'     => $linkItem,
        	        'description' => '',
        	        'introtext'     => '',
        	        'alias'         => '',
        	        'parent'        => $wikiTree, 
        	        'createdon'     => time(),
        	        'createdby'     => ($userid>0 ? $userid * -1:0),
        	        'editedon'      => '0',
        	        'editedby'      => '0',
        	        'published'     => '1',
        	        'pub_date'      => '0',
        	        'unpub_date'    => '0',
        	        'deleted'       => '0',
        	        'hidemenu'      => '1',
        	        'menuindex'     => '0',
        	        'template'      => $template,
        	        'content'       => ''
        	    );
        	    $pageId = $modx->db->insert($flds,$modx->getFullTableName('site_content'));
        
        		$content = '';
        		$longTitle = $linkItem;
        	}
        
        	if (strlen($content)==0) {
        		$addClass= isset($toEmptyClass) ? 'class="'.$toEmptyClass.'"' : '';
        	} else {
        		$addClass='';	
        	}
        	$url = $modx->makeURL($pageId, '', '');	
        	$linkHtml ='<a '.$addClass.' href="'.$url.'" title="'.$longTitle.'">'.$linkItem.'</a>';	
        	
        	return $linkHtml;
        }
        
        /* End */
          • 22815
          • 1,097 Posts
          You may also like to look at my LinkToCreatePage snippet:
          http://modxcms.com/forums/index.php/topic,7795.0.html
          which provides the other wiki function of being able to add a page in the frontend using the URL that you tried to visit, by adding manager-user only links in the 404 page.

          (Or you could probably just take the idea and clone your code to do the same thing so they’re consistent).
            No, I don&#39;t know what OpenGeek&#39;s saying half the time either.
            MODx Documentation: The Wiki | My Wiki contributions | Main MODx Documentation
            Forum: Where to post threads about add-ons | Forum Rules
            Like MODx? donate (and/or share your resources)
            Like me? See my Amazon wishlist
            MODx "Most Promising CMS" - so appropriate!
            • 30269
            • 119 Posts
            Just updated the code above - Newspublisher was indeed a great snippet to learn from when making it.

            I really to love Modx - with just a very short snippet, from a not experienced programmer and Jot (wow great tool - installation less than 30 minutes) all works like a charm.

            If you’re interested, have a look at http://www.l20.eu/...

            As soon as we need it I’ll add the roll back of articles. For now, it does what a wiki needs to do for us smiley Now de-installing MediaWiki... wink
              • 30269
              • 119 Posts
              Just update version above to 0.6. Added some error checking as when parameter tags were wrong, the snippet could get lost.

              Our WikiX is working quite nice. Now, for the article history functions, it would be nice to have a PHP tool that show the differences between two versions of the text...