Extension:Add HTML Meta and Title
| This extension stores its source code on a wiki page. Please be aware that this code may be unreviewed or maliciously altered. They may contain security holes, outdated interfaces that are no longer compatible etc. Note: No localisation updates are provided for this extension by translatewiki.net. |
|
Add HTML Meta and Title Release status: beta |
|||
|---|---|---|---|
| Implementation | Tag | ||
| Description | Allows for easier SEO (search engine optimization) with MediaWiki. | ||
| Author(s) | Vladimir Radulovski, Jim Wilson (Владимир Радуловскиtalk) | ||
| Last version | 0.5.1 | ||
| MediaWiki | 1.6.x, 1.8.x, 1.9.x or higher (not tested by author on most recent MW versions - i.e. > 1.12) | ||
| Database changes | no | ||
| License | MIT | ||
| Download | http://velko.org/Add_HTML_Meta_and_Title.phps - save file and rename phps to php | ||
|
|||
| Check usage and version matrix; HTML Meta and Title stats | |||
This extension allows for easier SEO (search engine optimization) with MediaWiki.
Contents |
Usage[edit]
When you enter the following in a wiki page:
<seo title="word1,word2" metakeywords="word3,word4" metadescription="word5,word6" />
...or the shorter...
<seo title="word1,word2" metak="word3,word4" metad="word5,word6" />
...these words are added to the HTML title and meta headers. This makes SEO (search engine optimization) with MediaWiki easier.
For example, the above would become:
<title>Original title, word1,word2</title> (the string ", word1,word2,..." is added) <meta name="word3,word4" content="word5,word6" />
(This is a new meta tag - existing metas are left untouched.)
Code structure[edit]
This extension uses two MediaWiki hooks: OutputPageBeforeHTML (for the meta tag) and BeforePageDisplay (for the title tag).
This extension is based in part on the extension MetaKeywordsTag.
License[edit]
Add HTML Meta and Title is released under the MIT License.
Installation and Download[edit]
- Download source from http://velko.org/Add_HTML_Meta_and_Title.phps - download the attached .phps file and rename it to .php, and create a file extensions/Add_HTML_Meta_and_Title.php .
- Enable the extension by adding this line to your LocalSettings.php:
require_once('extensions/Add_HTML_Meta_and_Title.php');
Changelog[edit]
v0.5.1[edit]
- modified to work with MW versions 1.1.2 and up (Revision by Mic)
v0.4[edit]
- have put
$emt="";in the parseSEO function because I got some nasty PHP notice for an unitialised variable...
v0.2[edit]
- added htmlspecialchars() as a filter to the text that is displayed in the title and meta - anything else needed to prevent malicious people? I think no. (If you are an English speaker you may want to use the htmlentities PHP function, which is more restrictive)
v0.1[edit]
- Initital version - everything works.
The code for version 0.5.1 (MW 1.1.2 and up)[edit]
<?php /* * * Extension homepage is at http://www.mediawiki.org/wiki/Extension:Add_HTML_Meta_and_Title * * --------------- Begin Jim R. Wilson's License Data -------------------------------------------------------- * Copyright (c) 2007 Jim R. Wilson * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do * so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * --------------- End of Jim R. Wilson's License Data -------------------------------------------------------- */ /* Version 0.5a Modified by Mic, Added support for newer versions ie. 1.1.8 */ # Confirm MW environment if (defined('MEDIAWIKI')) { # Credits $wgExtensionCredits['parserhook'][] = array( 'name'=>'Add_HTML_Meta_and_Title', 'author'=>'Vladimir Radulovski, Jim Wilson, Mic', 'url'=>'https://www.mediawiki.org/wiki/Extension:Add_HTML_Meta_and_Title', 'description'=> htmlentities ('Adds the <seo title="word1,word2,..." metakeywords="word1,word2,..." metadescription="word1,word2,..." /> tag so you can add to the meta keywords and HTML-title of a wiki-page. If you are lazy just use the short version: <seo title="1 , 2" metak="m1 m2,m3" metad="word1, word2" /> .'), 'version'=>'0.5.1' ); # Add Extension Function $wgExtensionFunctions[] = 'setupSEOParserHooks'; /** * Sets up the MetaKeywordsTag Parser hook and system messages */ function setupSEOParserHooks() { global $wgParser, $wgExtensionMessages; # meta if empty $wgParser->setHook( 'seo', 'renderSEO' ); $wgExtensionMessages['seo-empty-attr'] ='Error: <seo> tag must contain at least one non-empty "title" or "metak(eywords)" or "metad(escription)" attribute.'; return true; } /** * Renders the <keywords> tag. * @param String $text Incomming text - should always be null or empty (passed by value). * @param Array $params Attributes specified for tag - must contain 'content' (passed by value). * @param Parser $parser Reference to currently running parser (passed by reference). * @return String Always empty. */ function renderSEO( $text, array $params, Parser $parser, PPFrame $frame ) { # Short-circuit with error message if content is not specified. $emt=""; if ( (isset($params['title'])) || (isset($params['metak'])) || (isset($params['metad'])) || (isset($params['metakeywords'])) || (isset($params['metadescription'])) ) { if (isset($params['title'])) {$emt .= "<!-- ADDTITLE ".base64_encode($params['title'])." -->";} if (isset($params['metak'])) {$emt .= "<!-- ADDMETAK ".base64_encode($params['metak'])." -->";} if (isset($params['metakeywords'])) {$emt .= "<!-- ADDMETAK ".base64_encode($params['metakeywords'])." -->";} if (isset($params['metad'])) {$emt .= "<!-- ADDMETAD ".base64_encode($params['metad'])." -->";} if (isset($params['metadescription'])) {$emt .= "<!-- ADDMETAD ".base64_encode($params['metadescription'])." -->";} return $emt; //$encoded_metas_and_title; } else {return '<div class="errorbox">'. wfMsgForContent('seo-empty-attr'). '</div>'; } } # Attach post-parser hook to extract metadata and alter headers $wgHooks['OutputPageBeforeHTML'][] = 'insertMeta'; #$wgHooks['BeforePageDisplay'][] = 'insertMeta'; $wgHooks['BeforePageDisplay'][] = 'insertTitle'; #$wgHooks['OutputPageBeforeHTML'][] = 'insertTitle'; /** * Adds the <meta> keywords to document head. * Usage: $wgHooks['OutputPageBeforeHTML'][] = 'insertMetaKeywords'; * @param OutputPage $out Handle to an OutputPage object - presumably $wgOut (passed by reference). * @param String $text Output text. * @return Boolean Always true to allow other extensions to continue processing. */ function insertTitle ( $out ) { global $wgSitename; # Extract meta keywords // public function getHTML() { return $this->mBodytext; } if (preg_match_all( '/<!-- ADDTITLE ([0-9a-zA-Z\\+\\/]+=*) -->/m', $out->mBodytext, $matches)===false ) return true; $data = $matches[1]; // print_r ($data) ; # Merge keyword data into OutputPage as meta tags foreach ($data as $item) { $content = @base64_decode($item); $content = htmlspecialchars($content, ENT_QUOTES); if ($content) { // print "DA $content\n"; $new_title = $out->mHTMLtitle; $new_title = "$content - ".$wgSitename; $out->setHTMLTitle( $new_title ); } else { // print "TZ\n"; } } return true; } function insertMeta( $out, $text ) { # Extract meta keywords if (preg_match_all( '/<!-- ADDMETAK ([0-9a-zA-Z\\+\\/]+=*) -->/m', $text, $matches)===false ) return true; $data = $matches[1]; # Merge keyword data into OutputPage as meta tags foreach ($data AS $item) { $content = @base64_decode($item); $content = htmlspecialchars($content, ENT_QUOTES); if ($content) { $out->addMeta( 'keywords', $content ); } } // Now for desc # Extract meta keywords if (preg_match_all( '/<!-- ADDMETAD ([0-9a-zA-Z\\+\\/]+=*) -->/m', $text, $matches)===false ) return true; $data = $matches[1]; # Merge keyword data into OutputPage as meta tags foreach ($data AS $item) { $content = @base64_decode($item); $content = htmlspecialchars($content, ENT_QUOTES); #preg_replace($pattern, $replacement, $string); if ($content) { $out->addMeta( 'description', $content ); } } return true; } } # End MW env wrapper ?>
The code for version 0.5[edit]
<?php /* * * Extension homepage is at http://www.mediawiki.org/wiki/Extension:Add_HTML_Meta_and_Title * * --------------- Begin Jim R. Wilson's License Data -------------------------------------------------------- * Copyright (c) 2007 Jim R. Wilson * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do * so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * --------------- End of Jim R. Wilson's License Data -------------------------------------------------------- */ # Confirm MW environment if (defined('MEDIAWIKI')) { # Credits $wgExtensionCredits['parserhook'][] = array( 'name'=>'Add_HTML_Meta_and_Title', 'author'=>'Vladimir Radulovski - vladradulov<at>gmail.com, based on the work of Jim Wilson - wilson.jim.r<at>gmail.com', 'url'=>'http://www.mediawiki.org/wiki/Extension:Add_HTML_Meta_and_Title', 'description'=> htmlentities ('Adds the <seo title="word1,word2,..." metakeywords="word1,word2,..." metadescription="word1,word2,..." /> tag so you can add to the meta keywords and HTML-title of a wiki-page. If you are lazy just use the short version: <seo title="1 , 2" metak="m1 m2,m3" metad="word1, word2" /> .'), 'version'=>'0.5' ); # Add Extension Function $wgExtensionFunctions[] = 'setupSEOParserHooks'; /** * Sets up the MetaKeywordsTag Parser hook and system messages */ function setupSEOParserHooks() { global $wgParser, $wgMessageCache; # meta if empty $wgParser->setHook( 'seo', 'renderSEO' ); $wgMessageCache->addMessage( 'seo-empty-attr', 'Error: <seo> tag must contain at least one non-empty "title" or "metak(eywords)" or "metad(escription)" attribute.' ); } /** * Renders the <keywords> tag. * @param String $text Incomming text - should always be null or empty (passed by value). * @param Array $params Attributes specified for tag - must contain 'content' (passed by value). * @param Parser $parser Reference to currently running parser (passed by reference). * @return String Always empty. */ function renderSEO( $text, $params = array(), &$parser ) { # Short-circuit with error message if content is not specified. $emt=""; if ( (isset($params['title'])) || (isset($params['metak'])) || (isset($params['metad'])) || (isset($params['metakeywords'])) || (isset($params['metadescription'])) ) { if (isset($params['title'])) {$emt .= "<!-- ADDTITLE ".base64_encode($params['title'])." -->";} if (isset($params['metak'])) {$emt .= "<!-- ADDMETAK ".base64_encode($params['metak'])." -->";} if (isset($params['metakeywords'])) {$emt .= "<!-- ADDMETAK ".base64_encode($params['metakeywords'])." -->";} if (isset($params['metad'])) {$emt .= "<!-- ADDMETAD ".base64_encode($params['metad'])." -->";} if (isset($params['metadescription'])) {$emt .= "<!-- ADDMETAD ".base64_encode($params['metadescription'])." -->";} return $emt; //$encoded_metas_and_title; } else {return '<div class="errorbox">'. wfMsgForContent('seo-empty-attr'). '</div>'; } } # Attach post-parser hook to extract metadata and alter headers $wgHooks['OutputPageBeforeHTML'][] = 'insertMeta'; #$wgHooks['BeforePageDisplay'][] = 'insertMeta'; $wgHooks['BeforePageDisplay'][] = 'insertTitle'; #$wgHooks['OutputPageBeforeHTML'][] = 'insertTitle'; /** * Adds the <meta> keywords to document head. * Usage: $wgHooks['OutputPageBeforeHTML'][] = 'insertMetaKeywords'; * @param OutputPage $out Handle to an OutputPage object - presumably $wgOut (passed by reference). * @param String $text Output text. * @return Boolean Always true to allow other extensions to continue processing. */ function insertTitle ( $out ) { # Extract meta keywords // public function getHTML() { return $this->mBodytext; } if (preg_match_all( '/<!-- ADDTITLE ([0-9a-zA-Z\\+\\/]+=*) -->/m', $out->mBodytext, $matches)===false ) return true; $data = $matches[1]; // print_r ($data) ; # Merge keyword data into OutputPage as meta tags foreach ($data as $item) { $content = @base64_decode($item); $content = htmlspecialchars($content, ENT_QUOTES); if ($content) { // print "DA $content\n"; $new_title = $out->mHTMLtitle; $new_title .= ", $content"; $out->setHTMLTitle( $new_title ); } else { // print "TZ\n"; } } return true; } function insertMeta( $out, $text ) { # Extract meta keywords if (preg_match_all( '/<!-- ADDMETAK ([0-9a-zA-Z\\+\\/]+=*) -->/m', $text, $matches)===false ) return true; $data = $matches[1]; # Merge keyword data into OutputPage as meta tags foreach ($data AS $item) { $content = @base64_decode($item); $content = htmlspecialchars($content, ENT_QUOTES); if ($content) { $out->addMeta( 'keywords', $content ); } } // Now for desc # Extract meta keywords if (preg_match_all( '/<!-- ADDMETAD ([0-9a-zA-Z\\+\\/]+=*) -->/m', $text, $matches)===false ) return true; $data = $matches[1]; # Merge keyword data into OutputPage as meta tags foreach ($data AS $item) { $content = @base64_decode($item); $content = htmlspecialchars($content, ENT_QUOTES); #preg_replace($pattern, $replacement, $string); if ($content) { $out->addMeta( 'description', $content ); } } return true; } } # End MW env wrapper
See also[edit]
- Extension:WikiSEO is based on this extension, but rewritten to modern MediaWiki standards and under active development.
- Extension:MetaKeywordsTag for meta tags only
- If you only want to override the title on pages (not append words to it), you might also look at the DISPLAYTITLE tag in combination with the Manual:$wgAllowDisplayTitle and Manual:$wgRestrictDisplayTitle settings.
- Extension:Advanced Meta for easily setting robots, meta and keywords on entire namespaces or individual pages
| Language: | English • русский |
|---|