/* <?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 */