ocPortal Developer's Guide: Search engine optimisation
» Return to Contents
sources/seo2.php
Global_functions_seo2.php
void seo_meta_erase_storage(ID_TEXT type, ID_TEXT id)
Erase a seo entry… as these shouldn't be left hanging around once content is deleted.
Parameters…
| Name |
type |
| Description |
The type of resource (e.g. download) |
| Type |
ID_TEXT |
| Name |
id |
| Description |
The ID of the resource |
| Type |
ID_TEXT |
(No return value)
function seo_meta_erase_storage($type,$id)
{
$rows=$GLOBALS['SITE_DB']->query_select('seo_meta',array('meta_keywords','meta_description'),array('meta_for_type'=>$type,'meta_for_id'=>$id),'',1);
if (!array_key_exists(0,$rows)) return;
delete_lang($rows[0]['meta_keywords']);
delete_lang($rows[0]['meta_description']);
$GLOBALS['SITE_DB']->query_delete('seo_meta',array('meta_for_type'=>$type,'meta_for_id'=>$id),'',1);
if (function_exists('persistant_cache_delete')) persistant_cache_delete(array('seo',$type,$id));
}
tempcode seo_get_fields(ID_TEXT type, ?ID_TEXT id)
Get template fields to insert into a form page, for manipulation of seo fields.
Parameters…
| Name |
type |
| Description |
The type of resource (e.g. download) |
| Type |
ID_TEXT |
| Name |
id |
| Description |
The ID of the resource (NULL: adding) |
| Default value |
|
| Type |
?ID_TEXT |
Returns…
| Description |
Form page tempcode fragment |
| Type |
tempcode |
function seo_get_fields($type,$id=NULL)
{
require_code('form_templates');
if (is_null($id))
{
list($keywords,$description)=array('','');
} else
{
list($keywords,$description)=seo_meta_get_for($type,$id);
}
$fields=new ocp_tempcode();
if ((get_value('disable_seo')!=='1') && ((get_value('disable_seo')!=='2'/*2 means have it only on edit*/) || (!is_null($id))))
{
$fields->attach(do_template('FORM_SCREEN_FIELD_SPACER',array('SECTION_HIDDEN'=>$keywords=='' && $description=='','TITLE'=>do_lang_tempcode('SEO'),'HELP'=>(get_option('show_docs')==='0')?NULL:protect_from_escaping(symbol_tempcode('URLISE_LANG',array(do_lang('TUTORIAL_ON_THIS'),brand_base_url().'/docs'.strval(ocp_version()).'/pg/tut_seo','tut_seo','1'))))));
$fields->attach(form_input_line_multi(do_lang_tempcode('KEYWORDS'),do_lang_tempcode('DESCRIPTION_META_KEYWORDS'),'meta_keywords[]',array_map('trim',explode(',',preg_replace('#,+#',',',$keywords))),0));
$fields->attach(form_input_line(do_lang_tempcode('META_DESCRIPTION'),do_lang_tempcode('DESCRIPTION_META_DESCRIPTION'),'meta_description',$description,false));
}
return $fields;
}
void seo_meta_set_for_explicit(ID_TEXT type, ID_TEXT id, SHORT_TEXT keywords, SHORT_TEXT description)
Explictly sets the meta information for the specified resource.
Parameters…
| Name |
type |
| Description |
The type of resource (e.g. download) |
| Type |
ID_TEXT |
| Name |
id |
| Description |
The ID of the resource |
| Type |
ID_TEXT |
| Name |
keywords |
| Description |
The keywords to use |
| Type |
SHORT_TEXT |
| Name |
description |
| Description |
The description to use |
| Type |
SHORT_TEXT |
(No return value)
function seo_meta_set_for_explicit($type,$id,$keywords,$description)
{
if ($description==STRING_MAGIC_NULL) return;
if ($keywords==STRING_MAGIC_NULL) return;
$description=str_replace(chr(10),' ',$description);
$rows=$GLOBALS['SITE_DB']->query_select('seo_meta',array('meta_keywords','meta_description'),array('meta_for_type'=>$type,'meta_for_id'=>$id),'',1);
if (array_key_exists(0,$rows))
{
lang_remap($rows[0]['meta_keywords'],$keywords);
lang_remap($rows[0]['meta_description'],$description);
} else
{
$GLOBALS['SITE_DB']->query_insert('seo_meta',array('meta_for_type'=>$type,'meta_for_id'=>$id,'meta_keywords'=>insert_lang($keywords,2),'meta_description'=>insert_lang($description,2)));
}
if (function_exists('decache')) decache('side_tag_cloud');
if (function_exists('persistant_cache_delete')) persistant_cache_delete(array('seo',$type,$id));
}
SHORT_TEXT seo_meta_set_for_implicit(ID_TEXT type, ID_TEXT id, array keyword_sources, SHORT_TEXT description)
Sets the meta information for the specified resource, by auto-summarisation from the given parameters.
Parameters…
| Name |
type |
| Description |
The type of resource (e.g. download) |
| Type |
ID_TEXT |
| Name |
id |
| Description |
The ID of the resource |
| Type |
ID_TEXT |
| Name |
keyword_sources |
| Description |
Array of content strings to summarise from |
| Type |
array |
| Name |
description |
| Description |
The description to use |
| Type |
SHORT_TEXT |
Returns…
| Description |
Keyword string generated (it's also saved in the DB, so usually you won't want to collect this) |
| Type |
SHORT_TEXT |
function seo_meta_set_for_implicit($type,$id,$keyword_sources,$description)
{
if ((!is_null(post_param('meta_keywords',NULL))) && ((post_param('meta_keywords')!='') || (post_param('meta_description')!='')))
{
seo_meta_set_for_explicit($type,$id,post_param('meta_keywords'),post_param('meta_description'));
return '';
}
if (get_value('no_auto_meta')==='1') return '';
if (get_option('automatic_meta_extraction')=='0') return '';
// These characters are considered to be word-characters
require_code('textfiles');
$word_chars=explode(chr(10),read_text_file('word_characters',''));
$strip_chars=array('\''); // These present problems so will be entirely stripped
foreach ($word_chars as $i=>$word_char)
{
$word_chars[$i]=trim($word_char);
}
$common_words=explode(chr(10),read_text_file('too_common_words',''));
foreach ($common_words as $i=>$common_word)
{
$common_words[$i]=trim($common_word);
}
$keywords=array(); // This will be filled
foreach ($keyword_sources as $source) // Look in all our sources
{
$source=strip_comcode($source);
foreach ($strip_chars as $strip_char)
{
$source=strtolower(str_replace($strip_char,'',$source));
}
$source=preg_replace('#\-+#',' ',$source);
$i=0;
$len=strlen($source);
$from=0;
$in_word=false;
while ($i<$len)
{
$at=$source[$i];
$word_char=in_array($at,$word_chars);
if ($in_word)
{
// Exiting word
if (!$word_char)
{
if (($i-$from)>=3)
{
$this_word=substr($source,$from,$i-$from);
if (!in_array($this_word,$common_words))
{
if (!array_key_exists($this_word,$keywords)) $keywords[$this_word]=0;
$keywords[$this_word]++;
}
}
$in_word=false;
}
} else
{
// Entering word
if ($word_char)
{
$from=$i;
$in_word=true;
}
}
$i++;
}
// Finalise
if (($in_word) && (($i-$from)>=3))
{
$this_word=substr($source,$from,$i-$from);
if (!in_array($this_word,$common_words))
{
if (!array_key_exists($this_word,$keywords)) $keywords[$this_word]=0;
$keywords[$this_word]++;
}
}
}
arsort($keywords);
$imp='';
foreach (array_keys($keywords) as $i=>$keyword)
{
if ($imp!='') $imp.=',';
$imp.=$keyword;
if ($i==15) break;
}
require_code('xhtml');
$description=strip_comcode($description);
$description=preg_replace('#\s+---+\s+#',' ',$description);
seo_meta_set_for_explicit($type,$id,$imp,(strlen($description)>1000)?(substr($description,0,1000).'...'):$description);
if (function_exists('decache')) decache('side_tag_cloud');
return $imp;
}
To apply SEO to a module, the following is roughly required for different sections of that modules code. This example is for the download system.
Add (actualisation):
Code (php)
seo_meta_set_for_implicit
('downloads_download',strval($id),array($name,$description,$comments),$description);
Edit (interface):
Code (php)
$fields->attach(seo_get_fields
('downloads_download',strval($id)));
Edit (actualisation):
Code (php)
,post_param
('meta_keywords'),post_param
('meta_description')
,$meta_keywords,$meta_description
seo_meta_set_for_explicit
('downloads_download',strval($id),$meta_keywords,$meta_description);
Delete:
Code (php)
seo_meta_erase_storage('downloads_download',$id);
View:
Code (php)
seo_meta_load_for
('downloads_download',strval($id),$title_to_use);
0 reviews: Unrated (average)
There have been no comments yet