Difference between revisions of "MediaWiki notes"

From Noah.org
Jump to navigationJump to search
Line 64: Line 64:
 
|}
 
|}
 
</pre>
 
</pre>
 +
 +
== Change a user's password ==
 +
 +
If a user forgets a password you must reset it directly in the database. The user_password is the MD5 hash of the user_id + '-' + the MD5 hash of the plaintext password. The following examples set the password for user #1 to 'plaintext_password'.
 +
 +
MySQL:
 +
 +
<pre>
 +
UPDATE user SET user_password=md5(CONCAT('1-',md5('plaintext_password'))) WHERE user_id=1;
 +
</pre>
 +
 +
Postgres:
 +
 +
<pre>
 +
UPDATE user SET user_password=md5('1-'||md5('plaintext_password')) WHERE user_id=1;
 +
</pre>
 +
 +
MediaWiki makes it clear that user rights and security are not a high priority, but some of the user management borders on absurd!
  
 
== Import WikiPedia database dumps ==
 
== Import WikiPedia database dumps ==
  
It isn't too hard to create your own local copy of WikiPedia. You just need to install MediaWiki and MySQL; download one of the [http://meta.wikimedia.org/wiki/Data_dumps WikiPedia data dumps]; then import using importDump.php. There is one small problem. By default MediaWiki is configured to not allow + signs in article titles, but many WikiPedia article titles have + signs. This will cause the import to fail after a few hundred articles. The trick is to edit LocalSettings.php and add this line:
+
It isn't too hard to create your own local copy of WikiPedia. You just need to install MediaWiki and MySQL; download one of the [http://meta.wikimedia.org/wiki/Data_dumps WikiPedia data dumps]; then import using importDump.php.  
 +
 
 +
=== IMPORTANT change to default settings before import ===
 +
 
 +
By default, the MediaWiki software is configured to not allow + signs in article titles, but many WikiPedia article titles have + signs. This will cause the import to fail after a few hundred articles. The trick is to edit LocalSettings.php and add this line:
  
 
<pre>
 
<pre>

Revision as of 12:06, 3 September 2008


MediaWiki documentation

Editor's Help

Developer's Manual contains good stuff on PHP source and SQL schema.

Quick table overview

Wikitext markup is not as easy and smart as it should be. In particular, tables are only slightly less annoying than they are in HTML. The following short review is copied from the MediaWiki Help:Tables page.

{| table start
|+ table caption, optional; only between table start and first table row
|- table row,
! table header cell, optional. Consecutive table header cells may be added on same line separated by double marks (!!) or start on new lines, each with its own single mark (!).
| table data cell, required! Consecutive table data cells may be added on same line separated by double marks (||) or start on new lines, each with its own single mark (|).
|} table end

Simple Table Examples

{|
|  Orange    ||   Apple   ||   more
|-
|   Bread    ||   Pie     ||   more
|-
|   Butter   || Ice cream ||  and more
|}
{|
|Orange
|Apple
|-
|Bread
|Pie
|-
|Butter
|Ice cream 
|}
{| border="1"
|Orange
|Apple
|align="right"|12,333.00
|-
|Bread
|Pie
|align="right"|500.00
|-
|Butter
|Ice cream
|align="right"|1.00
|}

Change a user's password

If a user forgets a password you must reset it directly in the database. The user_password is the MD5 hash of the user_id + '-' + the MD5 hash of the plaintext password. The following examples set the password for user #1 to 'plaintext_password'.

MySQL:

UPDATE user SET user_password=md5(CONCAT('1-',md5('plaintext_password'))) WHERE user_id=1;

Postgres:

UPDATE user SET user_password=md5('1-'||md5('plaintext_password')) WHERE user_id=1;

MediaWiki makes it clear that user rights and security are not a high priority, but some of the user management borders on absurd!

Import WikiPedia database dumps

It isn't too hard to create your own local copy of WikiPedia. You just need to install MediaWiki and MySQL; download one of the WikiPedia data dumps; then import using importDump.php.

IMPORTANT change to default settings before import

By default, the MediaWiki software is configured to not allow + signs in article titles, but many WikiPedia article titles have + signs. This will cause the import to fail after a few hundred articles. The trick is to edit LocalSettings.php and add this line:

$wgLegalTitleChars = " +%!\"$&'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF";

Selecting articles in SQL directly from the database

Replace ARTICLE_TITLE with the name of the article you want to retrieve. The old_text column a misnomer, but it holds the latest article text in this context.

select old_text from revision,page,text where revision.rev_id=page.page_latest and text.old_id=revision.rev_text_id and page.page_title='ARTICLE_TITLE';

mod_rewrite for redirecting 404 errors to wiki search

No more 404 errors!

If someone requests a page on your site that does not exist (HTTP 404 error) then you can redirect that request to the MediaWiki search page and search for the URI. This also lets you search for articles just by requesting them in the URI. For example, I have an article on "Pexpect" under my MediaWiki: http://www.noah.org/wiki/Pexpect, but you can also simply request with this URL: http://www.noah.org/pexpect. This mod_rewrite rule will redirect to the Search page which will find an exact match on the name and then redirect again to the article with that name.

Basically this looks for requests that don't map to a directory or a file -- ones that would generate a 404 error. The request is then redirected to the MediaWiki search. This also strips off any .html or .htm to give a search a better chance of success. Put this after any other redirect rules you may already have.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteRule ^(.+?)(\.html|\.htm)?$ http://%{SERVER_NAME}/wiki/Special:Search?search=$1&go=Go [R=301,NC,L]

mod_rewrite for MediaWiki friendly URLs

This is a pain in the ass to get right. There are not many pieces, but I had to keep fiddling with them to get it right. Plus, it's easy to get it almost but not quite right.

Assuming you want the root URI to be /wiki (just like Wikipedia). For example:

http://www.noah.org/wiki/Htaccess

Move wiki root directory

First move your wiki directory from to w/. This is the filesystem path, not the URI. For example:

mv /var/www/htdocs/wiki /var/www/htdocs/w

Just to be clear, the wiki/ directory should no longer exist in the filesystem. It will not just be a virtual directory handled by MediaWiki scripts.

MediaWiki LocalSettings.php

$wgScriptPath       = "/w";
$wgArticlePath      = "/wiki/$1";

.htaccess (above the MediaWiki directory)

RewriteEngine on

# This rewrites URIs in the form /wiki/Article_Name to /w/index.php/Article_Name.
RewriteCond %{REQUEST_URI} !^/wiki/index.php.*$ [NC]
    RewriteRule ^wiki/?(.*)$ /w/index.php/$1 [L]

# This takes old style links to articles in the form /wiki/index.php/Article_Name and
# redirects them to the new-style format /wiki/Article_Name.
RewriteCond %{REQUEST_URI} ^/wiki/index.php/.*$ [NC]
    RewriteRule ^wiki/index.php/(.*)$ http://www.noah.org/wiki/$1 [R=301,NC,L]

Static HTML dump of MediaWiki

MediaWiki can be used as a CMS for generating static HTML pages. Surprisingly nobody has made it easy to dump static content from MediaWiki.

wget

This creates static dumps of a wiki using wget. This does not hide navigation links and other links that will not have any meaning outside of the wiki engine.

step 1. Authenticate and save cookies

First you must login. You can skip this step if a login is not necessary. This authenticates and saves the auth token in cookies.txt. Note the login URL and post-data because quoting and escaping can get tricky here. Also be sure to replace or set $WIKI_USERNAME and $WIKI_PASSWORD.

wget --save-cookies cookies.txt --post-data 'wpName=$WIKI_USERNAME&wpPassword=$WIKI_PASSWORD&wpRemember=1&wpLoginattempt=Log%20in' http://www.example.com/wiki/index.php\?title=Special:Userlogin\&action=submitlogin\&type=login

step 2. Download article

This grabs one article into the current directory:

wget --no-parent --page-requisites --convert-links --no-host-directories --cut-dirs=2 --load-cookies cookies.txt --directory-prefix=. http://www.example.com/wiki/index.php/My_Article

This grabs everything in the wiki into the current directory:

wget --mirror --no-parent --page-requisites --convert-links --no-host-directories --cut-dirs=2 --load-cookies cookies.txt --directory-prefix=. http://www.example.com/wiki/index.php/Main_Page

mw2html.py

This project seems sort of dead and it does not support authentication.

#!/bin/sh
/usr/bin/python /var/www/usr/local/apache2/htdocs/wiki/mw2html.py --no-made-by --left=adsense.html --force -i main_page.html http://www.noah.org/wiki/index.php/Engineering /var/www/htdocs/engineering
chown -R www:www /var/www/htdocs/engineering

Articles and Subcategories swap

I don't like having Subcategories listed before Articles. I have a long list of Subcategories, so I prefer to have Subcategories listed after all of the articles. Unfortunately, there is no easy way to adjust this in a skin. Instead, I had to modify the wiki/includes/CategoryPage.php source file. The change was simple. Here is a diff of the file that I changed. Note that the line with "$this->getSubcategorySection()" was moved down two lines.

# diff -c CategoryPage.php CategoryPage.php.noah 
*** CategoryPage.php    2007-07-18 12:09:04.000000000 -0700
--- CategoryPage.php.noah       2007-07-18 12:08:24.000000000 -0700
***************
*** 85,93 ****
                $this->finaliseCategoryState();
  
        $r = $this->getCategoryTop() .
-               $this->getSubcategorySection() .
                $this->getPagesSection() .
                $this->getImageSection() .
                $this->getCategoryBottom();
  
                wfProfileOut( __METHOD__ );
--- 85,93 ----
                $this->finaliseCategoryState();
  
        $r = $this->getCategoryTop() .
                $this->getPagesSection() .
                $this->getImageSection() .
+               $this->getSubcategorySection() .
                $this->getCategoryBottom();
  
                wfProfileOut( __METHOD__ );

Include external static content

Often it is handy to include the contents of external files directly in an article instead simply linking to the file. This describes "include" -- a MediaWiki extension that lets you include external static content from the local file system; a remote URL; or a Subversion repository.

I expanded this section into its own article. For more information see the article on MediaWiki_Include.

Article Cache Purge

Sometimes you may need to purge MediaWiki's cache of a specific article. Put the following code in an article and you will get a link that will tell MediaWiki to purge the cache for that article.

{{fullurl:{{NAMESPACE}}:{{PAGENAME}}|action=purge}}

That code will result in a link like this:

{{fullurl:{{NAMESPACE}}:{{PAGENAME}}|action=purge}}

Small words in Full Text Index

By default MySQL does not index words smaller than four characters. That makes it impossible for MediaWiki to search for words such as 'SSH' or 'FTP'. The following will correct this.

Edit /etc/mysql/my.cnf (or /etc/my.cnf) and add the following variable after [mysql] section:

ft_min_word_len = 2

Restart MySQL:

/etc/init.d/mysql restart

Rebuild full text index. You must cd into the MediaWiki install directory. For example /var/www/htdocs/wiki:

php ./maintenance/rebuildtextindex.php

Longer Maximum Article Length

http://www.mediawiki.org/wiki/Manual:$wgMaxArticleSize

debugging errors

Put this in your LocalSettings.php file:

$wgShowExceptionDetails = true;

Turn off annoying Wiki-style capitalization

This looks very interesting; however, it looks like it could have a big impact on an existing wiki.

http://www.mediawiki.org/wiki/Manual:$wgCapitalLinks

NoFollow Links

For a private Wiki you almost certainly want to set this FALSE (default is TRUE). When set to false then your links to external sites will help boost their search engine page rank. When set TRUE search engines will not count your links. This is good if you have a public site where users might try to stuff links into your pages to boost page rank.

http://www.mediawiki.org/wiki/Manual:$wgNoFollowLinks